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

Enable --resource-type and --exclude-resource-type CLI flags and environment variables for dbt test #10706

Merged
merged 17 commits into from
Sep 19, 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/Features-20240903-132428.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Enable `--resource-type` and `--exclude-resource-type` CLI flags and environment variables for `dbt test`
time: 2024-09-03T13:24:28.592837+01:00
custom:
Author: TowardOliver dbeatty10
Issue: "10656"
2 changes: 2 additions & 0 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,8 @@ def freshness(ctx, **kwargs):
@click.pass_context
@global_flags
@p.exclude
@p.resource_type
@p.exclude_resource_type
@p.profiles_dir
@p.project_dir
@p.select
Expand Down
5 changes: 5 additions & 0 deletions core/dbt/node_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
NodeType.Snapshot,
]

TEST_NODE_TYPES: List["NodeType"] = [
NodeType.Test,
NodeType.Unit,
]

VERSIONED_NODE_TYPES: List["NodeType"] = [
NodeType.Model,
]
28 changes: 24 additions & 4 deletions core/dbt/task/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
import re
import threading
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union
from typing import (
TYPE_CHECKING,
Any,
Collection,
Dict,
List,
Optional,
Tuple,
Type,
Union,
)

import daff

Expand All @@ -25,9 +35,9 @@
from dbt.exceptions import BooleanError, DbtInternalError
from dbt.flags import get_flags
from dbt.graph import ResourceTypeSelector
from dbt.node_types import NodeType
from dbt.node_types import TEST_NODE_TYPES, NodeType
from dbt.parser.unit_tests import UnitTestManifestLoader
from dbt.task.base import BaseRunner
from dbt.task.base import BaseRunner, resource_types_from_args
from dbt.utils import _coerce_decimal, strtobool
from dbt_common.dataclass_schema import dbtClassMixin
from dbt_common.events.format import pluralize
Expand Down Expand Up @@ -387,14 +397,24 @@
def raise_on_first_error(self) -> bool:
return False

@property
def resource_types(self) -> List[NodeType]:
resource_types: Collection[NodeType] = resource_types_from_args(

Check warning on line 402 in core/dbt/task/test.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/test.py#L402

Added line #L402 was not covered by tests
self.args, set(TEST_NODE_TYPES), set(TEST_NODE_TYPES)
)

# filter out any non-test node types
resource_types = [rt for rt in resource_types if rt in TEST_NODE_TYPES]
return list(resource_types)

Check warning on line 408 in core/dbt/task/test.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/test.py#L407-L408

Added lines #L407 - L408 were not covered by tests

Comment on lines +400 to +409
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is basically the same thing as here and here.

def get_node_selector(self) -> ResourceTypeSelector:
if self.manifest is None or self.graph is None:
raise DbtInternalError("manifest and graph must be set to get perform node selection")
return ResourceTypeSelector(
graph=self.graph,
manifest=self.manifest,
previous_state=self.previous_state,
resource_types=[NodeType.Test, NodeType.Unit],
resource_types=self.resource_types,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Basically same thing as here and here.

)

def get_runner_type(self, _) -> Optional[Type[BaseRunner]]:
Expand Down
10 changes: 9 additions & 1 deletion tests/functional/unit_testing/test_unit_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,19 @@ def test_basic(self, project):
)
assert len(results) == 1

# Exclude unit tests with environment variable
# Exclude unit tests with environment variable for build command
os.environ["DBT_EXCLUDE_RESOURCE_TYPES"] = "unit_test"
results = run_dbt(["build", "--select", "my_model"], expect_pass=True)
assert len(results) == 1

# Exclude unit tests with environment variable for test command
results = run_dbt(["test", "--select", "my_model"], expect_pass=True)
assert len(results) == 0

# Exclude unit tests with environment variable for list command
results = run_dbt(["list", "--select", "my_model"], expect_pass=True)
assert len(results) == 1

del os.environ["DBT_EXCLUDE_RESOURCE_TYPES"]

# Test select by test name
Expand Down
20 changes: 19 additions & 1 deletion tests/functional/unit_testing/test_ut_resource_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,25 @@ def test_unit_test_list(self, project):
results = run_dbt(["list", "--exclude-resource-types", "model", "test"])
assert sorted(results) == EXPECTED_UNIT_TESTS

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

results = run_dbt(["test", "--exclude-resource-types", "model", "test"])
assert len(results) == len(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(["test", "--resource-type", "test"])
assert len(results) == len(EXPECTED_DATA_TESTS)

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

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

Expand All @@ -61,11 +73,17 @@ def test_unit_test_list(self, project):

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

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

results = run_dbt(["test", "--resource-type", "model"])
assert len(results) == 0

results = run_dbt(["test", "--exclude-resource-types", "unit_test", "test"])
assert len(results) == 0

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

Expand Down
Loading