mirror of
https://github.com/dbt-labs/dbt-core
synced 2025-12-17 19:31:34 +00:00
set unit test config.enabled to False if it is testing a disabled model (#12251)
This commit is contained in:
6
.changes/unreleased/Features-20251203-122926.yaml
Normal file
6
.changes/unreleased/Features-20251203-122926.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
kind: Features
|
||||
body: ':bug: :snowman: Disable unit tests whose model is disabled'
|
||||
time: 2025-12-03T12:29:26.209248-05:00
|
||||
custom:
|
||||
Author: michelleark
|
||||
Issue: "10540"
|
||||
@@ -290,8 +290,11 @@ class UnitTestParser(YamlReader):
|
||||
)
|
||||
|
||||
if tested_model_node:
|
||||
unit_test_definition.depends_on.nodes.append(tested_model_node.unique_id)
|
||||
unit_test_definition.schema = tested_model_node.schema
|
||||
if tested_model_node.config.enabled:
|
||||
unit_test_definition.depends_on.nodes.append(tested_model_node.unique_id)
|
||||
unit_test_definition.schema = tested_model_node.schema
|
||||
else:
|
||||
unit_test_definition.config.enabled = False
|
||||
|
||||
# Check that format and type of rows matches for each given input,
|
||||
# convert rows to a list of dictionaries, and add the unique_id of
|
||||
@@ -302,7 +305,7 @@ class UnitTestParser(YamlReader):
|
||||
# for calculating state:modified
|
||||
unit_test_definition.build_unit_test_checksum()
|
||||
assert isinstance(self.yaml.file, SchemaSourceFile)
|
||||
if unit_test_config.enabled:
|
||||
if unit_test_definition.config.enabled:
|
||||
self.manifest.add_unit_test(self.yaml.file, unit_test_definition)
|
||||
else:
|
||||
self.manifest.add_disabled(self.yaml.file, unit_test_definition)
|
||||
@@ -492,6 +495,13 @@ def find_tested_model_node(
|
||||
model_version = model_name_split[1] if len(model_name_split) == 2 else None
|
||||
|
||||
tested_node = manifest.ref_lookup.find(model_name, current_project, model_version, manifest)
|
||||
if not tested_node:
|
||||
disabled_node = manifest.disabled_lookup.find(
|
||||
model_name, current_project, model_version, [NodeType.Model]
|
||||
)
|
||||
if disabled_node:
|
||||
tested_node = disabled_node[0]
|
||||
|
||||
return tested_node
|
||||
|
||||
|
||||
@@ -509,22 +519,36 @@ def process_models_for_unit_test(
|
||||
f"Unable to find model '{current_project}.{unit_test_def.model}' for "
|
||||
f"unit test '{unit_test_def.name}' in {unit_test_def.original_file_path}"
|
||||
)
|
||||
unit_test_def.depends_on.nodes.append(tested_node.unique_id)
|
||||
unit_test_def.schema = tested_node.schema
|
||||
if tested_node.config.enabled:
|
||||
unit_test_def.depends_on.nodes.append(tested_node.unique_id)
|
||||
unit_test_def.schema = tested_node.schema
|
||||
else:
|
||||
# If the model is disabled, the unit test should be disabled
|
||||
unit_test_def.config.enabled = False
|
||||
|
||||
# The UnitTestDefinition should only have one "depends_on" at this point,
|
||||
# the one that's found by the "model" field.
|
||||
target_model_id = unit_test_def.depends_on.nodes[0]
|
||||
if target_model_id not in manifest.nodes:
|
||||
if target_model_id in manifest.disabled:
|
||||
# The model is disabled, so we don't need to do anything (#10540)
|
||||
return
|
||||
# If the model is disabled, the unit test should be disabled
|
||||
unit_test_def.config.enabled = False
|
||||
else:
|
||||
# If we've reached here and the model is not disabled, throw an error
|
||||
raise ParsingError(
|
||||
f"Unit test '{unit_test_def.name}' references a model that does not exist: {target_model_id}"
|
||||
)
|
||||
|
||||
if not unit_test_def.config.enabled:
|
||||
# Ensure the unit test is disabled in the manifest
|
||||
if unit_test_def.unique_id in manifest.unit_tests:
|
||||
manifest.unit_tests.pop(unit_test_def.unique_id)
|
||||
if unit_test_def.unique_id not in manifest.disabled:
|
||||
manifest.add_disabled(manifest.files[unit_test_def.file_id], unit_test_def)
|
||||
|
||||
# The unit test is disabled, so we don't need to do any further processing (#10540)
|
||||
return
|
||||
|
||||
target_model = manifest.nodes[target_model_id]
|
||||
assert isinstance(target_model, ModelNode)
|
||||
|
||||
|
||||
@@ -145,7 +145,7 @@ unit_tests:
|
||||
- {c: 3}
|
||||
"""
|
||||
|
||||
test_disabled_my_model_yml = """
|
||||
test_disabled_unit_test_my_model_yml = """
|
||||
unit_tests:
|
||||
- name: test_disabled_my_model
|
||||
model: my_model
|
||||
@@ -164,6 +164,32 @@ unit_tests:
|
||||
- {c: 3}
|
||||
"""
|
||||
|
||||
my_model_disabled_sql = """
|
||||
{{ config(enabled=false) }}
|
||||
select 1 as id
|
||||
"""
|
||||
|
||||
my_model_disabled_yml = """
|
||||
models:
|
||||
- name: my_model_disabled_yml
|
||||
config:
|
||||
enabled: false
|
||||
"""
|
||||
|
||||
disabled_my_model_tests_yml = """
|
||||
unit_tests:
|
||||
- name: test_disabled_my_model_sql
|
||||
model: my_model_disabled_sql
|
||||
given: []
|
||||
expect:
|
||||
rows: [{"id": 1}]
|
||||
- name: test_disabled_my_model_yml
|
||||
model: my_model_disabled_yml
|
||||
given: []
|
||||
expect:
|
||||
rows: [{"id": 1}]
|
||||
"""
|
||||
|
||||
test_my_model_simple_fixture_yml = """
|
||||
unit_tests:
|
||||
- name: test_my_model
|
||||
|
||||
@@ -4,10 +4,13 @@ import os
|
||||
import pytest
|
||||
from fixtures import ( # noqa: F401
|
||||
datetime_test,
|
||||
disabled_my_model_tests_yml,
|
||||
my_model_a_sql,
|
||||
my_model_b_sql,
|
||||
my_model_disabled_sql,
|
||||
my_model_disabled_yml,
|
||||
my_model_vars_sql,
|
||||
test_disabled_my_model_yml,
|
||||
test_disabled_unit_test_my_model_yml,
|
||||
test_my_model_yml,
|
||||
)
|
||||
|
||||
@@ -22,7 +25,7 @@ class TestUnitTestList:
|
||||
"my_model_a.sql": my_model_a_sql,
|
||||
"my_model_b.sql": my_model_b_sql,
|
||||
"test_my_model.yml": test_my_model_yml + datetime_test,
|
||||
"test_disabled_my_model.yml": test_disabled_my_model_yml,
|
||||
"test_disabled_my_model.yml": test_disabled_unit_test_my_model_yml,
|
||||
}
|
||||
|
||||
@pytest.fixture(scope="class")
|
||||
@@ -83,3 +86,18 @@ class TestUnitTestList:
|
||||
for result in results:
|
||||
json_result = json.loads(result)
|
||||
assert json_result["model"] == "my_model"
|
||||
|
||||
|
||||
class TestUnitTestListDisabled:
|
||||
@pytest.fixture(scope="class")
|
||||
def models(self):
|
||||
return {
|
||||
"my_model_disabled_yml.sql": "select 1 as id",
|
||||
"my_model_disabled_sql.sql": my_model_disabled_sql,
|
||||
"my_model_disabled_yml.yml": my_model_disabled_yml,
|
||||
"disabled_my_model_tests.yml": disabled_my_model_tests_yml,
|
||||
}
|
||||
|
||||
def test_disabled_unit_tests(self, project):
|
||||
results = run_dbt(["test", "--select", "test_type:unit"])
|
||||
assert len(results) == 0
|
||||
|
||||
Reference in New Issue
Block a user