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 8 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: Adding logic to TestSelector to remove unit tests if they are in excluded_resource_types
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -783,6 +783,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,
]
7 changes: 0 additions & 7 deletions core/dbt/task/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from dbt.graph import Graph, GraphQueue, ResourceTypeSelector
from dbt.node_types import NodeType
from dbt.task.base import BaseRunner, resource_types_from_args
from dbt.task.test import TestSelector
from dbt_common.events.functions import fire_event

from .run import ModelRunner as run_model_runner
Expand Down Expand Up @@ -197,12 +196,6 @@ def get_node_selector(self, no_unit_tests=False) -> ResourceTypeSelector:

resource_types = self.resource_types(no_unit_tests)

if resource_types == [NodeType.Test]:
return TestSelector(
graph=self.graph,
manifest=self.manifest,
previous_state=self.previous_state,
)
return ResourceTypeSelector(
graph=self.graph,
manifest=self.manifest,
Expand Down
24 changes: 8 additions & 16 deletions core/dbt/task/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from dbt.node_types import NodeType
from dbt.task.base import resource_types_from_args
from dbt.task.runnable import GraphRunnableTask
from dbt.task.test import TestSelector
from dbt_common.events.contextvars import task_contextvars
from dbt_common.events.functions import fire_event, warn_or_error
from dbt_common.events.types import PrintEvent
Expand Down Expand Up @@ -197,23 +196,16 @@
else:
return self.args.select

def get_node_selector(self):
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")
if self.resource_types == [NodeType.Test]:
return TestSelector(
graph=self.graph,
manifest=self.manifest,
previous_state=self.previous_state,
)
else:
return ResourceTypeSelector(
graph=self.graph,
manifest=self.manifest,
previous_state=self.previous_state,
resource_types=self.resource_types,
include_empty_nodes=True,
)
return ResourceTypeSelector(

Check warning on line 202 in core/dbt/task/list.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/list.py#L202

Added line #L202 was not covered by tests
graph=self.graph,
manifest=self.manifest,
previous_state=self.previous_state,
resource_types=self.resource_types,
include_empty_nodes=True,
)

def interpret_results(self, results):
# list command should always return 0 as exit code
Expand Down
41 changes: 26 additions & 15 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 @@ -375,16 +385,6 @@
return rendered


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


class TestTask(RunTask):
"""
Testing:
Expand All @@ -397,13 +397,24 @@
def raise_on_first_error(self) -> bool:
return False

def get_node_selector(self) -> TestSelector:
@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 TestSelector(
return ResourceTypeSelector(

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

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/test.py#L413

Added line #L413 was not covered by tests
graph=self.graph,
manifest=self.manifest,
previous_state=self.previous_state,
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
13 changes: 12 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,22 @@ def test_basic(self, project):
)
assert len(results) == 1

# Exclude unit tests with environment variable
# Run test command but specify no unit tests
results = run_dbt(
["test", "--select", "my_model", "--exclude-resource-types", "unit_test"],
expect_pass=True,
)
assert len(results) == 0

# 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

del os.environ["DBT_EXCLUDE_RESOURCE_TYPES"]

# Test select by test name
Expand Down
Loading