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

fix: dbt unit tests feat without proper context #10849

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

devmessias
Copy link
Contributor

@devmessias devmessias commented Oct 14, 2024

Resolves #10539, resolves #10353 , resolves #10410

I made a very simple change to the code just to solve a similar problem 10353, but this change is obviously not the correct solution. I’ll wait for a discussion before writing any more advanced code.

Problem

absence of env_var and var in the ctx dictionary, which must be passed to the Jinja render method.

Solution

If a model has a unit test, the get_rendered method is called twice, each time with a different context. The issue arises with build_unit_test_manifest_from_test. When this method is called (

get_rendered(unit_test_node.raw_code, ctx, unit_test_node, capture_macros=True)
), the context doesn’t contain information about the var and environment variables (env_var).

Later, when the get_rendered method from clients/jinja.py calls render_template from dbt_common/clients/jinja.py, we encounter a compilation error due to the absence of env_var and var in the ctx dictionary, which must be passed to the Jinja render method. **The error will not appear if we don't have any kind of mixing with Nones, for example select {{ var('something,1) }} as result instead of select {{ var('something,1)*2 }} as result

Some questions:

Is this the expected behavior? Shouldn't the ctx used to render the string for the unit test include these variables?

Is there a reason why the same model is rendered more than once — once to build the model and again to render the unit test? Why is this string rendered a second time if it doesn’t seem to be used for any data manipulation?

models/_unit_tests.yml

version: 2

unit_tests:
  - name: test_unit_test
    model: test
    given: []
    expect:
      format: csv
      rows: |
        result
        10

models/test.sql

select {{ var('foo', 10) * 1 }} as result

This will only work for #10539 if ctx is constructed using env_var and var, but before coding any stuff I'll wait for the dbt-core team

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.

@devmessias devmessias requested a review from a team as a code owner October 14, 2024 20:21
@cla-bot cla-bot bot added the cla:yes label Oct 14, 2024
Copy link
Contributor

Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see the contributing guide.

@github-actions github-actions bot added the community This PR is from a community member label Oct 14, 2024
Copy link

codecov bot commented Oct 14, 2024

Codecov Report

Attention: Patch coverage is 75.75758% with 8 lines in your changes missing coverage. Please review.

Project coverage is 89.13%. Comparing base (40c350f) to head (7def903).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10849      +/-   ##
==========================================
- Coverage   89.18%   89.13%   -0.06%     
==========================================
  Files         183      183              
  Lines       23429    23466      +37     
==========================================
+ Hits        20895    20916      +21     
- Misses       2534     2550      +16     
Flag Coverage Δ
integration 86.41% <75.75%> (-0.08%) ⬇️
unit 62.06% <15.15%> (-0.06%) ⬇️

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

Components Coverage Δ
Unit Tests 62.06% <15.15%> (-0.06%) ⬇️
Integration Tests 86.41% <75.75%> (-0.08%) ⬇️

@devmessias
Copy link
Contributor Author

Sorry to bother you @gshank, I looked into the git history in those methods, and it seems you're probably the best person to ask these questions.

@devmessias devmessias changed the title fix: unit tests with var/env_var context fix: unit tests withthout proper context Oct 15, 2024
@devmessias devmessias marked this pull request as ready for review October 15, 2024 19:28
@devmessias devmessias changed the title fix: unit tests withthout proper context fix: dbt unit tests feat without proper context Oct 15, 2024
@devmessias
Copy link
Contributor Author

Okay, I dove into the code and found a way to make this work for me by capturing the variable and environment variable using the same logic used for ref, source and metrics

@devmessias
Copy link
Contributor Author

Hi @MichelleArk , sorry for to bother you, but since you're keeping an eye on the issues related to unit tests, could you help me with some questions I have in this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:yes community This PR is from a community member
Projects
None yet
1 participant