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

Schema test macro referencing package macro errors in parse-time rendering #3324

Closed
jtcohen6 opened this issue May 6, 2021 · 1 comment · Fixed by #3345
Closed

Schema test macro referencing package macro errors in parse-time rendering #3324

jtcohen6 opened this issue May 6, 2021 · 1 comment · Fixed by #3345
Assignees
Labels
bug Something isn't working
Milestone

Comments

@jtcohen6
Copy link
Contributor

jtcohen6 commented May 6, 2021

Describe the bug

Related to #3272 + #3280. The schema test macro is now in the rendering context for schema tests, which is great! Now, however, if a schema test macro depends on a macro from a package, it raises an error.

Reproduce

packages.yml:

packages:
  - package: fishtown-analytics/dbt_utils
    version: 0.6.4

macros/test/my_test.sql:

{% macro test_my_test(model) %}
    select {{ dbt_utils.current_timestamp() }}
{% endmacro %}

models/my_model.sql:

select 1 as fun
models:
  - name: my_model
    tests:
      - my_test

In develop and v0.19.2-rc1, I'm seeing the following error:

Compilation Error in test my_test_my_model_ (models/schema.yml)
  'dbt_utils' is undefined

  > in macro test_my_test (macros/my_test.sql)
  > called by test my_test_my_model_ (models/schema.yml)

This stacktrace should feel very familiar:

File "/usr/local/Cellar/dbt@0.19.2-rc1/0.19.2rc1_1/libexec/lib/python3.8/site-packages/dbt/parser/schemas.py", line 555, in parse_node
    node = self._parse_generic_test(
  File "/usr/local/Cellar/dbt@0.19.2-rc1/0.19.2rc1_1/libexec/lib/python3.8/site-packages/dbt/parser/schemas.py", line 467, in _parse_generic_test
    self.render_test_update(node, config, builder)
  File "/usr/local/Cellar/dbt@0.19.2-rc1/0.19.2rc1_1/libexec/lib/python3.8/site-packages/dbt/parser/schemas.py", line 505, in render_test_update
    get_rendered(

We'll need to catch this UndefinedError, extend the parse-time schema test rendering context, or something else I haven't thought of.

@jtcohen6 jtcohen6 added the bug Something isn't working label May 6, 2021
@jtcohen6 jtcohen6 added this to the 0.19.2 milestone May 6, 2021
@jtcohen6 jtcohen6 changed the title Bug: Schema test macro referencing package macro errors in parse-time rendering May 6, 2021
@jtcohen6
Copy link
Contributor Author

Possibly related: we're still seeing error #3229 for tests defined in packages. (Thanks @MartinNowak for the report, I just confirmed locally.)

Defined in a package:

{% macro test_valid_code(model) %}
    select count(*) from (
        select * from {{ model }}
        left join {{ ref('model_b') }}
    )
{% endmacro %}

Defined in the root project: model_a, model_b, and

version: 2

models:
  - name: model_a
    tests:
      - core_pkg.valid_code

Error:

Compilation Error in test core_pkg_valid_code_model_a_ (models/schema.yml)
  dbt was unable to infer all dependencies for the model "core_pkg_valid_code_model_a_".
  This typically happens when ref() is placed within a conditional block.

  To fix this, add the following hint to the top of the model "core_pkg_valid_code_model_a_":

  -- depends_on: {{ ref('model_b') }}

  > in macro test_valid_code (macros/tests.sql)
  > called by test core_pkg_valid_code_model_a_ (models/schema.yml)
  > called by test core_pkg_valid_code_model_a_ (models/schema.yml)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants