创建单一测试

dbt 中级

Mike Metzger

Data Engineer

什么是单一测试?

  • 自定义数据测试
  • 用 SQL 查询编写
    • 必须返回失败的行
  • 作为 tests 目录中的 .sql 文件定义

测试图片

1 照片作者:Antoine Dautry,来自 Unsplash
dbt 中级

单一测试示例

  • 创建一个测试以验证:
    • order_total >= subtotal
select *
from order
where order_total < subtotal
  • 请记住:我们希望返回的是未通过的行;有返回行则测试失败。
  • 将文件保存为 assert_order_total_gte_subtotal.sql

order_table 架构图

dbt 中级

带 Jinja 的单一测试

  • 测试中可使用 Jinja
    • ref 函数
    • 及其他合适函数
  • 运行测试时 dbt 会替换输出
    • 若 dbt 配置更改,请重新运行项目
select *
from {{ ref('order') }}
where order_total < subtotal
dbt 中级

测试调试

  • 使用 SQL 编辑器创建初始测试查询
  • 将查询放入合适的文件
  • 确保测试名称唯一
  • 使用 dbt test --select <testname> 选项
  • 检查错误并相应更新
dbt 中级

¡Vamos a practicar!

dbt 中级

Preparing Video For Download...