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

[Bug] dbt build with --empty flag compiles unit tests with empty mock fixtures #10516

Closed
2 tasks done
zendesk-cisenbe opened this issue Aug 2, 2024 · 2 comments · Fixed by #10764
Closed
2 tasks done
Assignees
Labels
bug Something isn't working empty Issues related to the --empty CLI flag High Severity bug with significant impact that should be resolved in a reasonable timeframe unit tests Issues related to built-in dbt unit testing functionality

Comments

@zendesk-cisenbe
Copy link

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

When you perform dbt build -s <some_model> --empty, and there are unit tests associated with <some_model>, the fixtures for the mocks in the unit test will return 0 records, but the expectations continue to use the full fixture. This always leads to a failure of the unit test.

The compiled code for the fixture looks like this:

select * from __dbt__cte__upstream_ref where false limit 0

Expected Behavior

Unit tests should ignore the --empty flag. Fixtures for both mocks and expectations should be respected regardless of tags.

Steps To Reproduce

  1. In a dbt project containing a model with unit tests with fixtures (i. e., ref() or source())
  2. run dbt build -s <some_model> --empty
  3. The unit test will run first and fail because the fixture will return 0 rows, which will never compare to a populated expectation

Relevant log output

No response

Environment

- OS: Ubuntu 22.04.4
- Python: 3.11.9
- dbt: 1.8.3

Which database adapter are you using with dbt?

snowflake

Additional Context

No response

@zendesk-cisenbe zendesk-cisenbe added bug Something isn't working triage labels Aug 2, 2024
@dbeatty10 dbeatty10 added the unit tests Issues related to built-in dbt unit testing functionality label Aug 2, 2024
@dbeatty10 dbeatty10 changed the title [Bug] dbt build with --empty flag compiles unit tests with empty mock fixtures [Bug] dbt build with --empty flag compiles unit tests with empty mock fixtures Aug 2, 2024
@dbeatty10
Copy link
Contributor

Thanks for reporting this @zendesk-cisenbe.

I was able to reproduce this with the following files and commands:

Reprex

models/my_upstream_model.sql

select 1 as id

models/my_downstream_model.sql

select *
from {{ ref("my_upstream_model") }}

models/_unit_tests.yml

unit_tests:
  - name: test_10516

    model: my_downstream_model

    given:
      - input: ref("my_upstream_model")
        rows:
        - {id: 2}

    expect:
      rows:
        - {id: 2}

Commands:

dbt build -s my_downstream_model --empty

Output:

$ dbt build -s my_downstream_model --empty

23:59:31  Running with dbt=1.8.3
23:59:32  Found 2 models, 444 macros, 1 unit test
23:59:32  
23:59:34  Concurrency: 10 threads (target='snowflake')
23:59:34  
23:59:34  1 of 2 START unit_test my_downstream_model::test_10516 ......................... [RUN]
23:59:37  1 of 2 FAIL 1 my_downstream_model::test_10516 .................................. [FAIL 1 in 2.63s]
23:59:37  2 of 2 SKIP relation dbt_dbeatty.my_downstream_model ........................... [SKIP]
23:59:37  
23:59:37  Finished running 1 unit test, 1 view model in 0 hours 0 minutes and 4.89 seconds (4.89s).
23:59:37  
23:59:37  Completed with 1 error and 0 warnings:
23:59:37  
23:59:37  Failure in unit_test test_10516 (models/_unit_tests.yml)
23:59:37    

actual differs from expected:

@@ ,ID
---,2


23:59:37  
23:59:37    compiled code at target/compiled/my_project/models/_unit_tests.yml/models/test_10516.sql
23:59:37  
23:59:37  Done. PASS=0 WARN=0 ERROR=1 SKIP=1 TOTAL=2
image

Take a look at the unit code logic:

target/run/my_project/models/_unit_tests.yml/models/test_10516.sql

-- Build actual result given inputs
with dbt_internal_unit_test_actual as (
  select
    id, 'actual' as "actual_or_expected"
  from (
    with __dbt__cte__my_upstream_model as (

-- Fixture for my_upstream_model
select 
    
        try_cast('2' as NUMBER(1,0))
     as id
) select *
from (select * from __dbt__cte__my_upstream_model where false limit 0)
  ) _dbt_internal_unit_test_actual
),
-- Build expected result
dbt_internal_unit_test_expected as (
  select
    id, 'expected' as "actual_or_expected"
  from (
    select 
    
        try_cast('2' as NUMBER(1,0))
     as id
  ) _dbt_internal_unit_test_expected
)
-- Union actual and expected results
select * from dbt_internal_unit_test_actual
union all
select * from dbt_internal_unit_test_expected

As mentioned by @zendesk-cisenbe, note in the unit test fixture within the reprex where it is including (select * from __dbt__cte__my_upstream_model where false limit 0) instead of just __dbt__cte__my_upstream_model.

@dbeatty10 dbeatty10 removed the triage label Aug 5, 2024
@jonathanneo
Copy link

This is affecting my company from running dbt unit tests in CI, as we run our CI pipelines with the --empty flag. This is impacting ~350 dbt-core users.

@graciegoheen graciegoheen added the High Severity bug with significant impact that should be resolved in a reasonable timeframe label Sep 23, 2024
@MichelleArk MichelleArk self-assigned this Sep 23, 2024
@dbeatty10 dbeatty10 added the empty Issues related to the --empty CLI flag label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working empty Issues related to the --empty CLI flag High Severity bug with significant impact that should be resolved in a reasonable timeframe unit tests Issues related to built-in dbt unit testing functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants