From 67726a7f7c4e1e28fa3e45ef2a9c070c08b31a4b Mon Sep 17 00:00:00 2001 From: Katsuya Shimabukuro Date: Sun, 22 Sep 2024 13:24:42 +0000 Subject: [PATCH 1/4] Fix unittest incremental model with alias --- core/dbt/context/providers.py | 2 +- tests/functional/unit_testing/fixtures.py | 14 ++++++++++++++ .../unit_testing/test_unit_testing.py | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py index 898437bf4da..37c1a1887ac 100644 --- a/core/dbt/context/providers.py +++ b/core/dbt/context/providers.py @@ -1636,7 +1636,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..c186a8bbc2f 100644 --- a/tests/functional/unit_testing/fixtures.py +++ b/tests/functional/unit_testing/fixtures.py @@ -304,6 +304,20 @@ {% 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 %} +""" + 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..17fd2f5334a 100644 --- a/tests/functional/unit_testing/test_unit_testing.py +++ b/tests/functional/unit_testing/test_unit_testing.py @@ -8,6 +8,7 @@ external_package, external_package__accounts_seed_csv, my_incremental_model_sql, + my_incremental_model_with_alias_sql, my_model_a_sql, my_model_b_sql, my_model_sql, @@ -271,6 +272,24 @@ 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 TestUnitTestExplicitSeed: @pytest.fixture(scope="class") def seeds(self): From 4ce3aa794ed0e62de4e7585384a3d67f0ecfd438 Mon Sep 17 00:00:00 2001 From: Katsuya Shimabukuro Date: Sun, 22 Sep 2024 13:35:56 +0000 Subject: [PATCH 2/4] Add changie changelog --- .changes/unreleased/Fixes-20240922-133527.yaml | 6 ++++++ 1 file changed, 6 insertions(+) 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..1f86f2519bd --- /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: "10605" From f4e46a74907f41cb5d29fc02acca188e0fb24137 Mon Sep 17 00:00:00 2001 From: Katsuya Shimabukuro Date: Sun, 22 Sep 2024 15:13:17 +0000 Subject: [PATCH 3/4] Change issue referrence --- .changes/unreleased/Fixes-20240922-133527.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/unreleased/Fixes-20240922-133527.yaml b/.changes/unreleased/Fixes-20240922-133527.yaml index 1f86f2519bd..f31fe8c3365 100644 --- a/.changes/unreleased/Fixes-20240922-133527.yaml +++ b/.changes/unreleased/Fixes-20240922-133527.yaml @@ -3,4 +3,4 @@ body: Fix unit tests for incremental model with alias time: 2024-09-22T13:35:27.991398741Z custom: Author: katsugeneration - Issue: "10605" + Issue: "10754" From 79d650b6c222cec47b556dcfa641c0cc7392cf25 Mon Sep 17 00:00:00 2001 From: Doug Beatty Date: Mon, 23 Sep 2024 14:53:43 -0600 Subject: [PATCH 4/4] Add functional tests for unit testing an incremental model with a version --- tests/functional/unit_testing/fixtures.py | 8 ++++++++ .../unit_testing/test_unit_testing.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tests/functional/unit_testing/fixtures.py b/tests/functional/unit_testing/fixtures.py index c186a8bbc2f..1da67f7e762 100644 --- a/tests/functional/unit_testing/fixtures.py +++ b/tests/functional/unit_testing/fixtures.py @@ -318,6 +318,14 @@ {% 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 17fd2f5334a..160f528787d 100644 --- a/tests/functional/unit_testing/test_unit_testing.py +++ b/tests/functional/unit_testing/test_unit_testing.py @@ -8,6 +8,7 @@ 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, @@ -290,6 +291,24 @@ def test_basic(self, project): 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):