DBT测试中可以参考另一张表吗?

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

我想知道是否可以运行包含 DBT 中另一个表的测试。 我尝试询问 chatgpt,但它的建议似乎不起作用。

类似这样的 yml:

models:
  - name: a
    description: table A
    docs:
      show: true
    config:
      tags: [test]
    tests:
      - dbt_expectations.expect_table_row_count_to_equal:
          value: 5555
    assertion:
      sql: |
        SELECT COUNT(*) FROM schema.a a join schema.b b on b.account_id = a.id
      equals: 12345
testing dbt
1个回答
0
投票

从 DBT slack 小组得到答案。 在测试/通用中创建一个测试,如下所示:

{% test multi_model_test(model, column_name, other_model, other_column, id_left, id_right) %}
SELECT *
FROM (
    SELECT SUM({{ model }}.{{ column_name }}) - SUM({{ other_model }}.{{ other_column }}) AS diff
    FROM {{ model }}
    LEFT JOIN {{ other_model }}
        ON {{ model }}.{{ id_left }} = {{ other_model }}.{{ id_right }}
) AS sums
WHERE sum_model != sum_other_model
{% endtest %}

然后将其添加到模型yml中:

      - name: total
        tests:
          - dbt_expectations.expect_column_values_to_be_of_type:
              column_type: numeric
          - multi_model_test:
              other_model: transaction
              other_column: amount
              id_left: id
              id_right: registration
© www.soinside.com 2019 - 2024. All rights reserved.