fix: dbt unit tests feat without proper context #10849
Open
+62
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
andvar
in thectx
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 withbuild_unit_test_manifest_from_test
. When this method is called (dbt-core/core/dbt/parser/unit_tests.py
Line 116 in f6088db
var
and environment variables (env_var
).Later, when the
get_rendered
method fromclients/jinja.py
callsrender_template
fromdbt_common/clients/jinja.py
, we encounter a compilation error due to the absence ofenv_var
andvar
in thectx
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 exampleselect {{ var('something,1) }} as result
instead ofselect {{ 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
models/test.sql
This will only work for #10539 if ctx is constructed using
env_var
andvar
, but before coding any stuff I'll wait for the dbt-core teamChecklist