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

[Feature] Disable unit tests whose model is disabled #10540

Closed
2 tasks done
SimonCarryer opened this issue Aug 7, 2024 · 4 comments
Closed
2 tasks done

[Feature] Disable unit tests whose model is disabled #10540

SimonCarryer opened this issue Aug 7, 2024 · 4 comments
Labels
enhancement New feature or request unit tests Issues related to built-in dbt unit testing functionality

Comments

@SimonCarryer
Copy link

SimonCarryer commented Aug 7, 2024

Is this a new bug in dbt-core?

  • I believe this is a new bug in dbt-core
  • I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

If a model is set to disabled by a macro (for example the standard config macro), then the enabled status is not known at compile time, and unit tests for the model are still compiled. This causes a compilation error:

Compilation Error
  'unit_test.data_staging.my_cool_report.my_cool_unit_test' depends on 'model.data_staging.my_cool_report' which is not in the graph!

Expected Behavior

Usually, if a model is disabled (i.e. enabled: false), then unit tests which test that model are not compiled.

Steps To Reproduce

  1. Create a model which is disabled by a macro (for example {{config(enabled=false)}})
  2. Create a unit test for that model
  3. Run the project

Relevant log output

No response

Environment

- OS: Windows
- Python: 3.12.4
- dbt: 1.8.2

Which database adapter are you using with dbt?

postgres

Additional Context

No response

@SimonCarryer SimonCarryer added bug Something isn't working triage labels Aug 7, 2024
@dbeatty10 dbeatty10 added the unit tests Issues related to built-in dbt unit testing functionality label Aug 9, 2024
@dbeatty10
Copy link
Contributor

I see what you are saying @SimonCarryer 👍

Specifically, both generic and singular data tests are disabled when they depend on a model that is disabled, and you'd like unit tests to behave the same way. This looks like the expected behavior to me though, so I'm going to update this to a feature request for further consideration.

We should also consider unit tests through the lens of indirect selection for data tests.

This is similar to #9109, but distinct.

Reprex

models/my_cool_report.sql

select 1 as id

models/_models.yml

models:
  - name: my_cool_report
    config:
      enabled: false
    columns:
      - name: id
        data_tests:
          - not_null

tests/assert_coolness.sql

select *
from {{ ref('my_cool_report')}}
where id is null

models/_unit_tests.yml

unit_tests:
  - name: my_cool_unit_test
    model: my_cool_report
    given: []
    expect:
      rows:
        - {id: 1}

Commands:

dbt build
Compilation Error
  'test.my_project.assert_coolness' depends on 'model.my_project.my_cool_report' which is not in the graph!

Output:

dbt build --no-partial-parse

Output:

15:25:52  Encountered an error:
'model.my_project.my_cool_report'
15:25:52  Traceback (most recent call last):
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/cli/requires.py", line 138, in wrapper
    result, success = func(*args, **kwargs)
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/cli/requires.py", line 101, in wrapper
    return func(*args, **kwargs)
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/cli/requires.py", line 218, in wrapper
    return func(*args, **kwargs)
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/cli/requires.py", line 247, in wrapper
    return func(*args, **kwargs)
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/cli/requires.py", line 294, in wrapper
    return func(*args, **kwargs)
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/cli/requires.py", line 320, in wrapper
    ctx.obj["manifest"] = parse_manifest(
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/parser/manifest.py", line 1898, in parse_manifest
    manifest = ManifestLoader.get_full_manifest(
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/parser/manifest.py", line 330, in get_full_manifest
    manifest = loader.load()
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/parser/manifest.py", line 487, in load
    self.process_unit_tests(self.root_project.project_name)
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/parser/manifest.py", line 1294, in process_unit_tests
    process_models_for_unit_test(
  File "/Users/dbeatty/projects/environments/dbt_1.8/lib/python3.10/site-packages/dbt/parser/unit_tests.py", line 468, in process_models_for_unit_test
    target_model = manifest.nodes[target_model_id]
KeyError: 'model.my_project.my_cool_report'

@dbeatty10 dbeatty10 added enhancement New feature or request and removed bug Something isn't working labels Aug 9, 2024
@dbeatty10 dbeatty10 changed the title [Bug] Unit tests are compiled for disabled models if the model's enabled status is set by a macro [Feature] Disable unit tests if its model is disabled Aug 9, 2024
@dbeatty10 dbeatty10 changed the title [Feature] Disable unit tests if its model is disabled [Feature] Disable unit test if its model is disabled Aug 9, 2024
@dbeatty10 dbeatty10 changed the title [Feature] Disable unit test if its model is disabled [Feature] Disable unit tests whose model is disabled Aug 9, 2024
@SimonCarryer
Copy link
Author

SimonCarryer commented Aug 9, 2024

Oh no! As best I can tell, unit tests on disabled models are already disabled. The issue is that if the model is disabled by a macro, rather than in plain text, then the unit test is still compiled (and causes a compilation error).

@dbeatty10
Copy link
Contributor

The example in #10540 (comment) just disables the model in YAML like this (which uses plain text rather than a macro):

models:
  - name: my_cool_report
    config:
      enabled: false

And it leaves all the unit tests related to that model as enabled and gives the compilation error you mentioned. Are you seeing something different? i.e., do you have an example where the unit test is disabled?

As an aside, when I disable a model using {{ config(enabled=false) }} within the model definition rather than YAML, I get a Parsing Error rather than a Compilation Error:

Parsing Error
  Unable to find model 'my_project.my_cool_report' for unit test 'my_cool_unit_test' in models/_unit_tests.yml

tsturge added a commit that referenced this issue Oct 7, 2024
Also, when a model is disabled, disable the corresponding unit tests automatically. (#10540)
tsturge added a commit that referenced this issue Oct 30, 2024
Also, when a model is disabled, disable the corresponding unit tests automatically. (#10540)
@graciegoheen
Copy link
Contributor

Closed by #10831

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request unit tests Issues related to built-in dbt unit testing functionality
Projects
None yet
Development

No branches or pull requests

3 participants