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 --resource-type test for dbt list and dbt build #10730

Merged
merged 5 commits into from
Sep 18, 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-20240917-174446.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix `--resource-type test` for `dbt list` and `dbt build`
time: 2024-09-17T17:44:46.121032-06:00
custom:
Author: dbeatty10
Issue: "10730"
1 change: 1 addition & 0 deletions core/dbt/task/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def get_node_selector(self, no_unit_tests=False) -> ResourceTypeSelector:
graph=self.graph,
manifest=self.manifest,
previous_state=self.previous_state,
resource_types=resource_types,
)
return ResourceTypeSelector(
graph=self.graph,
Expand Down
1 change: 1 addition & 0 deletions core/dbt/task/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def get_node_selector(self):
graph=self.graph,
manifest=self.manifest,
previous_state=self.previous_state,
resource_types=self.resource_types,
)
else:
return ResourceTypeSelector(
Expand Down
6 changes: 4 additions & 2 deletions core/dbt/task/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,14 @@ def _render_daff_diff(self, daff_diff: daff.TableDiff) -> str:


class TestSelector(ResourceTypeSelector):
def __init__(self, graph, manifest, previous_state) -> None:
def __init__(
self, graph, manifest, previous_state, resource_types=[NodeType.Test, NodeType.Unit]
) -> None:
super().__init__(
graph=graph,
manifest=manifest,
previous_state=previous_state,
resource_types=[NodeType.Test, NodeType.Unit],
resource_types=resource_types,
)


Expand Down
12 changes: 12 additions & 0 deletions tests/functional/unit_testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@
FROM {{ ref('my_model_a') }}
"""

test_my_model_a_yml = """
models:
- name: my_model_a
columns:
- name: a
tests:
- not_null
- name: id
tests:
- not_null
"""

test_my_model_yml = """
unit_tests:
- name: test_my_model
Expand Down
73 changes: 73 additions & 0 deletions tests/functional/unit_testing/test_ut_resource_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pytest
from fixtures import ( # noqa: F401
my_model_a_sql,
my_model_b_sql,
my_model_sql,
test_my_model_a_yml,
test_my_model_pass_yml,
)

from dbt.tests.util import run_dbt

EXPECTED_MODELS = [
"test.my_model",
"test.my_model_a",
"test.my_model_b",
]

EXPECTED_DATA_TESTS = [
"test.not_null_my_model_a_a",
"test.not_null_my_model_a_id",
]

EXPECTED_UNIT_TESTS = [
"unit_test:test.test_my_model",
]


class TestUnitTestResourceTypes:
@pytest.fixture(scope="class")
def models(self):
return {
"my_model.sql": my_model_sql,
"my_model_a.sql": my_model_a_sql,
"my_model_b.sql": my_model_b_sql,
"test_my_model.yml": test_my_model_pass_yml,
"test_my_model_a.yml": test_my_model_a_yml,
}

def test_unit_test_list(self, project):
results = run_dbt(["run"])

# unit tests
results = run_dbt(["list", "--resource-type", "unit_test"])
assert sorted(results) == EXPECTED_UNIT_TESTS

results = run_dbt(["list", "--exclude-resource-types", "model", "test"])
assert sorted(results) == EXPECTED_UNIT_TESTS

# data tests
results = run_dbt(["list", "--resource-type", "test"])
assert sorted(results) == EXPECTED_DATA_TESTS

results = run_dbt(["list", "--exclude-resource-types", "unit_test", "model"])
assert sorted(results) == EXPECTED_DATA_TESTS

results = run_dbt(["build", "--resource-type", "test"])
assert len(results) == len(EXPECTED_DATA_TESTS)

results = run_dbt(["build", "--exclude-resource-types", "unit_test", "model"])
assert len(results) == len(EXPECTED_DATA_TESTS)

# models
results = run_dbt(["list", "--resource-type", "model"])
assert len(results) == len(EXPECTED_MODELS)

results = run_dbt(["list", "--exclude-resource-type", "unit_test", "test"])
assert sorted(results) == EXPECTED_MODELS

results = run_dbt(["build", "--resource-type", "model"])
assert len(results) == len(EXPECTED_MODELS)

results = run_dbt(["build", "--exclude-resource-type", "unit_test", "test"])
assert len(results) == len(EXPECTED_MODELS)