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] Unit tests should support being set as enabled or not #10097

Closed
3 tasks done
joellabes opened this issue May 7, 2024 · 4 comments
Closed
3 tasks done

[Feature] Unit tests should support being set as enabled or not #10097

joellabes opened this issue May 7, 2024 · 4 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request unit tests Issues related to built-in dbt unit testing functionality

Comments

@joellabes
Copy link
Contributor

joellabes commented May 7, 2024

Is this your first time submitting a feature request?

  • I have read the expectations for open source contributors
  • I have searched the existing issues, and I could not find an existing issue for this feature
  • I am requesting a straightforward extension of existing dbt functionality, rather than a Big Idea better suited to a discussion

Describe the feature

Data tests can be disabled:

models: 
  - name: my_model
    columns: 
      - name: my_column
        tests: 
          - unique:
              config:
                enabled: false

But unit tests can't:

unit_tests: 
  - name: my_unit_test
    ...
    config:
      enabled: {{ target.type == 'bigquery' }} #doesn't work

I would like to be able to disable unit tests like every other node type in my project.

Describe alternatives you've considered

Michelle suggested using tags:

  - name: my_unit_test
    model: my_model
    ...
    config:
      tags: [bq_only]

and a command like dbt --target bigquery test -s test_type:unit --exclude tag:bq_only

which is, like, fine, but not as elegant.

Who will this benefit?

My specific use case is having unit tests for a dbt package to run in CI, but having some tests which should only run against a specific platform (e.g. unit testing BQ struct behaviour).

Are you interested in contributing this feature?

No response

Anything else?

No response

@joellabes joellabes added enhancement New feature or request triage labels May 7, 2024
@dbeatty10 dbeatty10 added the unit tests Issues related to built-in dbt unit testing functionality label May 7, 2024
@dbeatty10
Copy link
Contributor

What do you think @graciegoheen ?

enabled can be configured for all the other resource types that I tried out:

  • models
  • seeds
  • snapshots
  • sources
  • exposures
  • semantic models
  • generic data tests
  • singular data tests
  • analyses
resource type dbt_project.yml properties YAML config() macro
models
snapshots
seeds
sources
exposures
semantic models
analyses
singular data tests
generic data tests
unit tests

Example files

models/metricflow_time_spine.sql

{{ config(materialized='table') }}

select cast({{ dbt.string_literal("2000-01-01") }} as date) as date_day

Note: singular data tests can't be disabled within a YAML file (like models/_properties.yml, but either dbt_project.yml or config() within SQL file are options.

tests/singular_data_test.sql

-- { { config(enabled=false) }}

select *
from {{ ref('metricflow_time_spine' )}}
where 0=1

snapshots/my_snapshot.sql

{% snapshot my_snapshot %}

{{
    config(
      target_database=target.database,
      target_schema=target.schema,
      unique_key='id',
      strategy='check',
      check_cols='all',
    )
}}

select 1 as id

{% endsnapshot %}

seeds/my_seed.csv

id
1

Note: analyses can't be selectively disabled within dbt_project.yml -- they inherit the config from models within dbt_project.yml.

analyses/my_analysis.sql

-- { { config(enabled=false) }}

select 1 as id

models/_properties.yml

models:
  - name: metricflow_time_spine
    config:
      enabled: true
    columns:
      - name: date_day
        data_tests:
          - not_null:
              name: generic_data_test
              config:
                enabled: true

sources:
  - name: my_source
    tables:
      - name: my_source_table
    config:
      enabled: true

seeds:
  - name: my_seed
    config:
      enabled: true

snapshots:
  - name: my_snapshot
    config:
      enabled: true

analyses:
  - name: my_analysis
    config:
      enabled: true

exposures:
  - name: my_exposure
    type: dashboard
    owner:
      email: exposure_owner@my_company.com
    config:
      enabled: true

metrics:
  - name: my_metric
    label: my_metric
    type: derived
    type_params:
      expr: revenue - cost
    config:
      enabled: true

semantic_models:
  - name: my_semantic_model
    model: ref('metricflow_time_spine')
    config:
      enabled: true

saved_queries:
  - name: my_saved_query
    query_params:
      metrics:
        - my_metric
    config:
      enabled: true

unit_tests:
  - name: my_unit_test
    given: []
    model: metricflow_time_spine
    expect:
      rows:
        - {date_day: 2000-01-01}
    # Note: this does NOT have any effect
    # So need to comment out or delete this unit test if its associated model(s) are disabled
    config:
      enabled: true

dbt_project.yml

name: "my_project"

# Note: also disables analyses!
models:
  my_project:
    +enabled: true

seeds:
  my_project:
    +enabled: true

snapshots:
  my_project:
    +enabled: true

sources:
  my_project:
    +enabled: true

exposures:
  my_project:
    +enabled: true

metrics:
  my_project:
    +enabled: true

# Note: inconsistent to have "semantic-models" with a dash, but "data_tests" and "unit_tests" with underscores
semantic-models:
  my_project:
    +enabled: true

# Note: this has no effect -- must configure within properties.yml or config() within the analysis file
analyses:
  my_project:
    +enabled: true

data_tests:
  my_project:
    +enabled: true

# This has no effect -- there is no way to disable a unit test outside of commenting it out or excluding it from the selection.
# It will give the following warning:
#   [WARNING]: Configuration paths exist in your dbt_project.yml file which do not apply to any resources.
#   There are 1 unused configuration paths:
#   - unit_tests.my_project
unit_tests:
  my_project:
    +enabled: true

Run this command to see which resources are enabled:

dbt list --resource-type all

Then change the enabled config for one or more resources and re-run the dbt list command above to see if it disappears or not.

@graciegoheen
Copy link
Contributor

We actually already have an issue for this -> #9109

@dbeatty10
Copy link
Contributor

Closing as a duplicate of #9109

@dbeatty10 dbeatty10 closed this as not planned Won't fix, can't repro, duplicate, stale May 7, 2024
@dbeatty10 dbeatty10 added duplicate This issue or pull request already exists and removed triage labels May 7, 2024
@joellabes
Copy link
Contributor Author

oops my bad 😬 ty both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists 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