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

Conversation

dbeatty10
Copy link
Contributor

@dbeatty10 dbeatty10 commented Sep 12, 2024

Resolves #10656

Alternative to #10657

Problem

Currently, the resource type CLI flags and environment variables apply to the dbt build, dbt list and dbt clone subcommands ONLY -- they do not extend to dbt test.

We want to extend these to apply to the dbt test subcommand also.

Solution

Simplify by standardizing upon the ResourceTypeSelector which already provides the desired resource_types parameter.

This was accomplished by:

  1. adding the resource_type and exclude_resource_type click args to the dbt test subcommand
  2. adding a TEST_NODE_TYPES list of valid test types (data tests and unit tests)
  3. 👉 adding resource_types property to the TestTask (which uses TEST_NODE_TYPES) 👈 this was the crux!

Backstory

Branched from #10657 to try an alternative approach that resolves the issue via a refactor.

Did these PRs leading up to this one:

Code examples

dbt test now enables passing values like unit_tests, tests, etc. in the --resource-type / --exclude-resource-type CLI flags (or in the environment variables DBT_RESOURCE_TYPES / DBT_EXCLUDE_RESOURCE_TYPES).

Example 1 - CLI flags

dbt test --resource-type unit_test
dbt test --exclude-resource-type unit_test

Example 2 - environment variables

DBT_RESOURCE_TYPES=unit_test dbt test
DBT_EXCLUDE_RESOURCE_TYPES=unit_test dbt test

Documentation to update

At minimum (there may be other relevant pages):

Checklist

  • I have read the contributing guide and understand what's expected of me.
  • I have run this code in development, and it appears to resolve the stated issue.
  • This PR includes tests, or tests are not required or relevant for this PR.
  • This PR has no interface changes (e.g., macros, CLI, logs, JSON artifacts, config files, adapter interface, etc.) or this PR has already received feedback and approval from Product or DX.
  • This PR includes type annotations for new and modified functions.

@cla-bot cla-bot bot added the cla:yes label Sep 12, 2024
Copy link

codecov bot commented Sep 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.96%. Comparing base (3308a43) to head (8186042).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10706      +/-   ##
==========================================
- Coverage   88.97%   88.96%   -0.01%     
==========================================
  Files         181      181              
  Lines       22971    22979       +8     
==========================================
+ Hits        20438    20443       +5     
- Misses       2533     2536       +3     
Flag Coverage Δ
integration 86.14% <100.00%> (-0.01%) ⬇️
unit 62.39% <72.72%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Unit Tests 62.39% <72.72%> (+<0.01%) ⬆️
Integration Tests 86.14% <100.00%> (-0.01%) ⬇️

@dbeatty10 dbeatty10 changed the title Adding logic to TestSelector to remove unit tests if they are in excluded_resource_types Enable --resource-type and --exclude-resource-type CLI flags and environment variables for dbt test Sep 13, 2024
@dbeatty10 dbeatty10 added the user docs [docs.getdbt.com] Needs better documentation label Sep 13, 2024
@dbeatty10 dbeatty10 marked this pull request as draft September 18, 2024 12:02
@dbeatty10 dbeatty10 marked this pull request as ready for review September 18, 2024 21:48
Comment on lines +400 to +409
@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)

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.

@dbeatty10 dbeatty10 merged commit 9ca10fb into main Sep 19, 2024
61 of 62 checks passed
@dbeatty10 dbeatty10 deleted the dbeatty/fix-10656 branch September 19, 2024 22:53
peterallenwebb pushed a commit that referenced this pull request Sep 20, 2024
…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 <asimov.1st@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:yes user docs [docs.getdbt.com] Needs better documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Exclude unit tests from running when using "dbt test" command
3 participants