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

Move unit testing to test and build commands #9108

Merged

Conversation

gshank
Copy link
Contributor

@gshank gshank commented Nov 16, 2023

resolves #8979

Problem

We've decided to move the unit testing functionality to the 'test' command from the 'unit-test' command. In addition this pull request will support running unit tests in the 'build' command.

Solution

Move code from task/unit_test.py to task/test.py. Construct a mini-manifest from just the executing test node, instead of constructing it all at once.

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/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

@gshank gshank requested review from a team as code owners November 16, 2023 19:43
@gshank gshank requested review from wpowers-dbt, VersusFacit and emmyoop and removed request for a team November 16, 2023 19:43
@cla-bot cla-bot bot added the cla:yes label Nov 16, 2023
Copy link

codecov bot commented Nov 16, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (e001991) 86.85% compared to head (4b24a66) 86.81%.

Files Patch % Lines
core/dbt/task/test.py 96.62% 3 Missing ⚠️
core/dbt/task/runnable.py 60.00% 2 Missing ⚠️
Additional details and impacted files
@@                       Coverage Diff                       @@
##           unit_testing_feature_branch    #9108      +/-   ##
===============================================================
- Coverage                        86.85%   86.81%   -0.04%     
===============================================================
  Files                              181      180       -1     
  Lines                            27156    27083      -73     
===============================================================
- Hits                             23586    23512      -74     
- Misses                            3570     3571       +1     
Flag Coverage Δ
integration 83.73% <95.55%> (-0.06%) ⬇️
unit 64.74% <43.70%> (+0.02%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gshank gshank changed the base branch from main to unit_testing_feature_branch November 16, 2023 19:46
@gshank gshank marked this pull request as draft November 16, 2023 19:46
@gshank gshank requested review from MichelleArk and graciegoheen and removed request for wpowers-dbt, VersusFacit and emmyoop November 16, 2023 19:47
class TestRunner(CompileRunner):
_ANSI_ESCAPE = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")

Check warning

Code scanning / CodeQL

Overly permissive regular expression range Medium test

Suspicious character range that is equivalent to [@A-Z].
class TestRunner(CompileRunner):
_ANSI_ESCAPE = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")

Check warning

Code scanning / CodeQL

Overly permissive regular expression range Medium test

Suspicious character range that is equivalent to \[0-9:;<=>?\].
@graciegoheen
Copy link
Contributor

I believe this also resolves #9052

core/dbt/cli/main.py Outdated Show resolved Hide resolved
@gshank gshank marked this pull request as ready for review November 22, 2023 15:53
@@ -496,7 +493,7 @@ def run(self):

if self.args.write_json:
# args.which used to determine file name for unit test manifest
Copy link
Contributor

Choose a reason for hiding this comment

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

small nit: comment can be deleted now too

actual = [str(r.status) for r in results]
expected = ["error"] * 1 + ["skipped"] * 5 + ["pass"] * 2 + ["success"] * 5
expected = ["error"] * 1 + ["skipped"] * 6 + ["pass"] * 2 + ["success"] * 5
Copy link
Contributor

Choose a reason for hiding this comment

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

this will need to be updated once we fix the order of unit tests -> model -> data tests, as expected 👍

self.yaml.path.original_file_path,
unit_test.model,
unit_test.name,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I took this for a spin locally and it worked as expected! 🎩

--- dbt_project.yml
...
unit-tests:
  jaffle_shop:
    unit_testing:
      my_model:
        +tags: project

schema.yml in models/unit_testing/schema.yml

dbt build -s +my_model,test_type:unit --exclude tag:project

=> finds no unit tests. moving the config around at various levels also worked as expected.

Would be good to add some functional testing around this either in this PR or in a follow-on issue if it balloons the scope too much. We do have tests on the fqn in tests/unit/test_unit_test_parser.py so for the scope of this refactor those are good!

Copy link
Contributor

@MichelleArk MichelleArk left a comment

Choose a reason for hiding this comment

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

💪 big lift!

@gshank gshank merged commit 3f1ed23 into unit_testing_feature_branch Nov 27, 2023
49 checks passed
@gshank gshank deleted the 8979-unit_testing_in_test_and_build branch November 27, 2023 14:50
gshank added a commit that referenced this pull request Jan 16, 2024
* Initial implementation of unit testing (from pr #2911)

Co-authored-by: Michelle Ark <[email protected]>

* 8295 unit testing artifacts (#8477)

* unit test config: tags & meta (#8565)

* Add additional functional test for unit testing selection, artifacts, etc (#8639)

* Enable inline csv format in unit testing (#8743)

* Support unit testing incremental models (#8891)

* update unit test key: unit -> unit-tests (#8988)


* convert to use unit test name at top level key (#8966)

* csv file fixtures (#9044)

* Unit test support for `state:modified` and `--defer` (#9032)

Co-authored-by: Michelle Ark <[email protected]>

* Allow use of sources as unit testing inputs (#9059)

* Use daff for diff formatting in unit testing (#8984)

* Fix #8652: Use seed file from disk for unit testing if rows not specified in YAML config (#9064)

Co-authored-by: Michelle Ark <[email protected]>
Fix #8652: Use seed value if rows not specified

* Move unit testing to test and build commands (#9108)

* Enable unit testing in non-root packages (#9184)

* convert test to data_test (#9201)

* Make fixtures files full-fledged members of manifest and enable partial parsing (#9225)

* In build command run unit tests before models (#9273)

---------

Co-authored-by: Michelle Ark <[email protected]>
Co-authored-by: Michelle Ark <[email protected]>
Co-authored-by: Emily Rockman <[email protected]>
Co-authored-by: Jeremy Cohen <[email protected]>
Co-authored-by: Kshitij Aranke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CT-3308] adjust dbt build to run in dag order -> unit tests, model materialization, data tests
3 participants