From a8d4ba2b4a04d76ad03e563aefd1fcf3ba86fe00 Mon Sep 17 00:00:00 2001 From: Katsuya Shimabukuro Date: Wed, 25 Sep 2024 01:57:37 +0900 Subject: [PATCH] Fix unit tests for incremental models with alias (#10755) --- .../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 79e47f65299..e90a1f13c0c 100644 --- a/core/dbt/context/providers.py +++ b/core/dbt/context/providers.py @@ -1661,7 +1661,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 83e98677f20..1da67f7e762 100644 --- a/tests/functional/unit_testing/fixtures.py +++ b/tests/functional/unit_testing/fixtures.py @@ -304,6 +304,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 53cfc84f4bf..160f528787d 100644 --- a/tests/functional/unit_testing/test_unit_testing.py +++ b/tests/functional/unit_testing/test_unit_testing.py @@ -8,6 +8,8 @@ external_package, external_package__accounts_seed_csv, my_incremental_model_sql, + my_incremental_model_versioned_yml, + my_incremental_model_with_alias_sql, my_model_a_sql, my_model_b_sql, my_model_sql, @@ -271,6 +273,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):