运行 dbt 通用测试,并将标签附加到特定模型

问题描述 投票:0回答:1

我被要求将 BigQuery 的通用 DBT 测试附加到 YAML 定义中的模型。这是因为测试执行的自动化依赖于模型的标签,并且需要将模型名称链接到测试失败。

我编写的这些测试涉及整个模型。它们不依赖于特定的列。

我看到这些测试执行错误略有不同,似乎 DBT 找不到测试:

11:31:06  1 of 1 START test foo_thing_my_model_ ............... [RUN]
11:31:06  1 of 1 ERROR foo_thing_my_model_ .................... [ERROR in 0.02s]
11:31:06  
11:31:06  Finished running 1 test in 0 hours 0 minutes and 0.67 seconds (0.67s).
11:31:07  
11:31:07  Completed with 1 error and 0 warnings:
11:31:07  
11:31:07    Compilation Error in test foo_thing_my_model_ (models/<path-to>/my_model.yml)
  'test_foo_thing' is undefined. This can happen when calling a macro that does not exist. Check for typos and/or install package dependencies with "dbt deps".

我看不出下面的设置有什么问题?

模型的 YAML 配置:

version: 2
models:
  - name: my_model
    config:
      tags:
        - aaa
        - bbb
        - foo # this filter is expected to be picked up in the test execution
    tests:
      - foo_thing

测试在

tests/
文件夹中定义,文件名
foo_thing.sql
和内容(我让模型名称作为宏的参数注入,如 https://docs.getdbt.com/best-practices 中所述/writing-custom-generic-tests 然后用作
{{  model }}
虽然我更愿意如果可能的话用
{{ ref("my_model") }}
)引用模型:

{% test foo_thing(model) %}
...
-- very complex SQL query that should return no rows to mark the test successful
...
{% endtest %}

我也尝试过将测试放在

./tests/my_model/foo_thing.sql
中,但没有成功。

我想使用以下命令调用测试:

dbt test --select tag:foo

这个设置有什么问题吗?我该如何解决它?

google-bigquery jinja2 dbt
1个回答
0
投票

测试文件需要嵌套在一个名为

generic
的目录中;所以,就你而言:

./tests/generic/test_foo_thing.sql

测试的另一个有效位置(遗留,但尚未正式弃用)位于

macros
目录中:

./macros/test_foo_thing.sql

(您的

dbt_project.yml
配置文件可能为
tests
macros
定义了不同的路径,但这不太可能。如果是这样,请更改上面的路径以匹配您的配置)

© www.soinside.com 2019 - 2024. All rights reserved.