Skip to content

Commit

Permalink
Enable --resource-type and --exclude-resource-type CLI flags and …
Browse files Browse the repository at this point in the history
…environment variables for `dbt test` (#10706)

* Adding logic to TestSelector to remove unit tests if they are in excluded_resource_types

* Adding change log

* Respect `--resource-type` and `--exclude-resource-type` CLI flags and associated environment variables

* Test CLI flag for excluding unit tests for the `dbt test` command

* Satisy isort pre-commit hook

* Fix mypy for positional argument "resource_types" in call to "TestSelector"

* Replace `TestSelector` with `ResourceTypeSelector`

* Add co-author

* Update changelog description

* Add functional tests for new feature

* Compare the actual results, not just the count

* Remove test case covered elsewhere

* Test for `DBT_EXCLUDE_RESOURCE_TYPES` environment variable for `dbt test`

* Update per pre-commit hook

* Restore to original form (until we refactor extraneous `ResourceTypeSelector` references later)

---------

Co-authored-by: Matthew Cooper <[email protected]>
  • Loading branch information
2 people authored and peterallenwebb committed Sep 20, 2024
1 parent a854272 commit 574e2d0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
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 @@ class TestTask(RunTask):
def raise_on_first_error(self) -> bool:
return False

@property
def resource_types(self) -> List[NodeType]:
resource_types: Collection[NodeType] = resource_types_from_args(
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)

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,
)

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

0 comments on commit 574e2d0

Please sign in to comment.