Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit tests for incremental models with alias #10755

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20240922-133527.yaml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root causes of the regressions reported in #10754 and #10763 was that:

  1. the update from this_node.name to this_node.identifier was accidentally overlooked during the implementation and code review of https://github.com/dbt-labs/dbt-core/pull/10290/files, and
  2. there were no functional tests to catch those particular cases

This PR addresses both of those root causes ✅ ✅

return None


Expand Down
22 changes: 22 additions & 0 deletions tests/functional/unit_testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions tests/functional/unit_testing/test_unit_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Comment on lines +276 to +291
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests the regression reported in #10754.

I confirmed this test fails without the fix but succeeds with the fix in place:

python -m pytest tests/functional/unit_testing/test_unit_testing.py::TestUnitTestIncrementalModelWithAlias



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
Comment on lines +294 to +309
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests the regression reported in #10763.

I confirmed this test fails without the fix but succeeds with the fix in place:

python -m pytest tests/functional/unit_testing/test_unit_testing.py::TestUnitTestIncrementalModelWithVersion



class TestUnitTestExplicitSeed:
@pytest.fixture(scope="class")
def seeds(self):
Expand Down
Loading