From 924ce2d36a9083582b64a3fc0b24e6ac45aef6eb Mon Sep 17 00:00:00 2001
From: Doug Beatty <44704949+dbeatty10@users.noreply.github.com>
Date: Fri, 27 Sep 2024 09:27:44 -0600
Subject: [PATCH] Fix unit tests for incremental models with alias (#10755)
 (#10769)

(cherry picked from commit a8d4ba2b4a04d76ad03e563aefd1fcf3ba86fe00)

Co-authored-by: Katsuya Shimabukuro <katsu.generation.888@gmail.com>
Co-authored-by: Gerda Shank <gerda@dbtlabs.com>
---
 .../unreleased/Fixes-20240922-133527.yaml     |  6 +++
 core/dbt/context/providers.py                 |  2 +-
 tests/functional/unit_testing/fixtures.py     | 22 +++++++++++
 .../unit_testing/test_unit_testing.py         | 38 +++++++++++++++++++
 4 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 .changes/unreleased/Fixes-20240922-133527.yaml

diff --git a/.changes/unreleased/Fixes-20240922-133527.yaml b/.changes/unreleased/Fixes-20240922-133527.yaml
new file mode 100644
index 00000000000..f31fe8c3365
--- /dev/null
+++ b/.changes/unreleased/Fixes-20240922-133527.yaml
@@ -0,0 +1,6 @@
+kind: Fixes
+body: Fix unit tests for incremental model with alias
+time: 2024-09-22T13:35:27.991398741Z
+custom:
+    Author: katsugeneration
+    Issue: "10754"
diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py
index b0b5ad6b3e6..56cd873ad0d 100644
--- a/core/dbt/context/providers.py
+++ b/core/dbt/context/providers.py
@@ -1593,7 +1593,7 @@ def this(self) -> Optional[str]:
         if self.model.this_input_node_unique_id:
             this_node = self.manifest.expect(self.model.this_input_node_unique_id)
             self.model.set_cte(this_node.unique_id, None)  # type: ignore
-            return self.adapter.Relation.add_ephemeral_prefix(this_node.name)
+            return self.adapter.Relation.add_ephemeral_prefix(this_node.identifier)  # type: ignore
         return None
 
 
diff --git a/tests/functional/unit_testing/fixtures.py b/tests/functional/unit_testing/fixtures.py
index 3028e0bc1e6..c908b762ba9 100644
--- a/tests/functional/unit_testing/fixtures.py
+++ b/tests/functional/unit_testing/fixtures.py
@@ -275,6 +275,28 @@
 {% endif %}
 """
 
+my_incremental_model_with_alias_sql = """
+{{
+    config(
+        materialized='incremental',
+        alias='alias_name'
+    )
+}}
+
+select * from {{ ref('events') }}
+{% if is_incremental() %}
+where event_time > (select max(event_time) from {{ this }})
+{% endif %}
+"""
+
+my_incremental_model_versioned_yml = """
+models:
+  - name: my_incremental_model
+    latest_version: 1
+    versions:
+      - v: 1
+"""
+
 test_my_model_incremental_yml_basic = """
 unit_tests:
   - name: incremental_false
diff --git a/tests/functional/unit_testing/test_unit_testing.py b/tests/functional/unit_testing/test_unit_testing.py
index 887c1907e76..877fa78c538 100644
--- a/tests/functional/unit_testing/test_unit_testing.py
+++ b/tests/functional/unit_testing/test_unit_testing.py
@@ -21,6 +21,8 @@
     test_my_model_yml,
     datetime_test,
     my_incremental_model_sql,
+    my_incremental_model_versioned_yml,
+    my_incremental_model_with_alias_sql,
     event_sql,
     test_my_model_incremental_yml_basic,
     test_my_model_yml_invalid,
@@ -261,6 +263,42 @@ def test_no_this_input(self, project):
 """
 
 
+class TestUnitTestIncrementalModelWithAlias:
+    @pytest.fixture(scope="class")
+    def models(self):
+        return {
+            "my_incremental_model.sql": my_incremental_model_with_alias_sql,
+            "events.sql": event_sql,
+            "schema.yml": test_my_model_incremental_yml_basic,
+        }
+
+    def test_basic(self, project):
+        results = run_dbt(["run"])
+        assert len(results) == 2
+
+        # Select by model name
+        results = run_dbt(["test", "--select", "my_incremental_model"], expect_pass=True)
+        assert len(results) == 2
+
+
+class TestUnitTestIncrementalModelWithVersion:
+    @pytest.fixture(scope="class")
+    def models(self):
+        return {
+            "my_incremental_model.sql": my_incremental_model_sql,
+            "events.sql": event_sql,
+            "schema.yml": my_incremental_model_versioned_yml + test_my_model_incremental_yml_basic,
+        }
+
+    def test_basic(self, project):
+        results = run_dbt(["run"])
+        assert len(results) == 2
+
+        # Select by model name
+        results = run_dbt(["test", "--select", "my_incremental_model"], expect_pass=True)
+        assert len(results) == 2
+
+
 class TestUnitTestExplicitSeed:
     @pytest.fixture(scope="class")
     def seeds(self):