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 #9593: Validation of unit test parsing for incremental models #9769

Merged
merged 4 commits into from
Mar 20, 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-20240317-005611.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: 'Validation of unit test parsing for incremental models'
time: 2024-03-17T00:56:11.855232-07:00
custom:
Author: aranke
Issue: "9593"
26 changes: 25 additions & 1 deletion core/dbt/parser/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@
def process_models_for_unit_test(
manifest: Manifest, current_project: str, unit_test_def: UnitTestDefinition, models_to_versions
):

# If the unit tests doesn't have a depends_on.nodes[0] then we weren't able to resolve
# the model, either because versions hadn't been processed yet, or it's not a valid model name
if not unit_test_def.depends_on.nodes:
Expand All @@ -438,6 +437,31 @@
target_model_id = unit_test_def.depends_on.nodes[0]
target_model = manifest.nodes[target_model_id]
assert isinstance(target_model, ModelNode)

target_model_is_incremental = "macro.dbt.is_incremental" in target_model.depends_on.macros
unit_test_def_has_incremental_override = unit_test_def.overrides and isinstance(
unit_test_def.overrides.macros.get("is_incremental"), bool
)

if target_model_is_incremental and (not unit_test_def_has_incremental_override):
raise ParsingError(

Check warning on line 447 in core/dbt/parser/unit_tests.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/unit_tests.py#L447

Added line #L447 was not covered by tests
f"Boolean override for 'is_incremental' must be provided for unit test '{unit_test_def.name}' in model '{target_model.name}'"
)

unit_test_def_incremental_override_true = (
aranke marked this conversation as resolved.
Show resolved Hide resolved
unit_test_def.overrides and unit_test_def.overrides.macros.get("is_incremental")
)
unit_test_def_has_this_input = "this" in [i.input for i in unit_test_def.given]

if (
target_model_is_incremental
and unit_test_def_incremental_override_true
and (not unit_test_def_has_this_input)
):
raise ParsingError(

Check warning on line 461 in core/dbt/parser/unit_tests.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/unit_tests.py#L461

Added line #L461 was not covered by tests
f"Unit test '{unit_test_def.name}' for incremental model '{target_model.name}' must have a 'this' input"
)

# unit_test_versions = unit_test_def.versions
# We're setting up unit tests for versioned models, so if
# the model isn't versioned, we don't need to do anything
Expand Down
50 changes: 49 additions & 1 deletion tests/functional/unit_testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
{% endif %}
"""

test_my_model_incremental_yml = """
test_my_model_incremental_yml_basic = """
unit_tests:
- name: incremental_false
model: my_incremental_model
Expand Down Expand Up @@ -300,6 +300,54 @@
- {event_time: "2020-01-03", event: 3}
"""

test_my_model_incremental_yml_no_override = """
unit_tests:
- name: incremental_false
model: my_incremental_model
given:
- input: ref('events')
rows:
- {event_time: "2020-01-01", event: 1}
expect:
rows:
- {event_time: "2020-01-01", event: 1}
"""

test_my_model_incremental_yml_wrong_override = """
unit_tests:
- name: incremental_false
model: my_incremental_model
overrides:
macros:
is_incremental: foobar
given:
- input: ref('events')
rows:
- {event_time: "2020-01-01", event: 1}
expect:
rows:
- {event_time: "2020-01-01", event: 1}
"""

test_my_model_incremental_yml_no_this_input = """
unit_tests:
- name: incremental_true
model: my_incremental_model
overrides:
macros:
is_incremental: true
given:
- input: ref('events')
rows:
- {event_time: "2020-01-01", event: 1}
- {event_time: "2020-01-02", event: 2}
- {event_time: "2020-01-03", event: 3}
expect:
rows:
- {event_time: "2020-01-02", event: 2}
- {event_time: "2020-01-03", event: 3}
"""

# -- inline csv tests

test_my_model_csv_yml = """
Expand Down
60 changes: 57 additions & 3 deletions tests/functional/unit_testing/test_unit_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
datetime_test,
my_incremental_model_sql,
event_sql,
test_my_model_incremental_yml,
test_my_model_incremental_yml_basic,
test_my_model_yml_invalid,
test_my_model_yml_invalid_ref,
valid_emails_sql,
top_level_domains_sql,
external_package__accounts_seed_csv,
external_package,
test_my_model_incremental_yml_no_override,
test_my_model_incremental_yml_wrong_override,
test_my_model_incremental_yml_no_this_input,
)


Expand Down Expand Up @@ -109,13 +112,13 @@ def test_basic(self, project):
run_dbt(["run", "--no-partial-parse", "--select", "my_model"])


class TestUnitTestIncrementalModel:
class TestUnitTestIncrementalModelBasic:
@pytest.fixture(scope="class")
def models(self):
return {
"my_incremental_model.sql": my_incremental_model_sql,
"events.sql": event_sql,
"test_my_incremental_model.yml": test_my_model_incremental_yml,
"schema.yml": test_my_model_incremental_yml_basic,
}

def test_basic(self, project):
Expand All @@ -127,6 +130,57 @@ def test_basic(self, project):
assert len(results) == 2


class TestUnitTestIncrementalModelNoOverride:
@pytest.fixture(scope="class")
def models(self):
return {
"my_incremental_model.sql": my_incremental_model_sql,
"events.sql": event_sql,
"schema.yml": test_my_model_incremental_yml_no_override,
}

def test_no_override(self, project):
with pytest.raises(
ParsingError,
match="Boolean override for 'is_incremental' must be provided for unit test 'incremental_false' in model 'my_incremental_model'",
):
run_dbt(["parse"])


class TestUnitTestIncrementalModelWrongOverride:
@pytest.fixture(scope="class")
def models(self):
return {
"my_incremental_model.sql": my_incremental_model_sql,
"events.sql": event_sql,
"schema.yml": test_my_model_incremental_yml_wrong_override,
}

def test_str_override(self, project):
with pytest.raises(
ParsingError,
match="Boolean override for 'is_incremental' must be provided for unit test 'incremental_false' in model 'my_incremental_model'",
):
run_dbt(["parse"])


class TestUnitTestIncrementalModelNoThisInput:
@pytest.fixture(scope="class")
def models(self):
return {
"my_incremental_model.sql": my_incremental_model_sql,
"events.sql": event_sql,
"schema.yml": test_my_model_incremental_yml_no_this_input,
}

def test_no_this_input(self, project):
with pytest.raises(
ParsingError,
match="Unit test 'incremental_true' for incremental model 'my_incremental_model' must have a 'this' input",
):
run_dbt(["parse"])


my_new_model = """
select
my_favorite_seed.id,
Expand Down
Loading