SuperAPI AI 工具集

dbt-testing

测试与质量

查看项目

更新于 10-15 23:46

技能介绍

用途:帮助 AI 智能体掌握 dbt 测试策略,在数据仓库各层实施数据质量校验,包括数据库级约束、通用(可复用)测试和单次(singular)测试。

主要能力:

  1. 数据库级约束:通过 dbt_constraints 包创建真实约束,而非仅在 dbt test 运行时校验。支持主键(dbt_constraints.primary_key)、外键(dbt_constraints.foreign_key,需指定 pk_table_name 与 pk_column_name)、唯一键(dbt_constraints.unique_key),均支持复合列(column_names)。
  2. 通用测试:内置 not_null、unique、accepted_values、relationships;dbt_utils 提供 not_null_proportion、accepted_range、not_empty_string 等;也可自定义通用测试(tests/generic 下用 {% test %} 定义)。
  3. 单次测试:在 tests/singular 下编写 SQL,返回任意行即视为失败,适合复杂业务逻辑,如订单日期早于注册日期、收入对账、孤儿订单检查。
  4. 分层策略:bronze 层做基础 not_null 与主键;silver 层做业务规则与范围校验;gold 层全面启用 dbt_constraints 约束。

使用方式:在 packages.yml 中声明 Snowflake-Labs/dbt_constraints(版本 [">=0.8.0", "<1.0.0"])与 dbt-labs/dbt_utils(版本 [">=1.0.0", "<2.0.0"]),执行 dbt deps 安装。测试写在模型 YAML 的 tests 下。运行命令:dbt test、dbt build(推荐)、dbt test --select dim_customers、dbt test --select test_type:generic、dbt test --select test_type:singular、dbt test --store-failures。可在 dbt_project.yml 配置 +store_failures: true 与 +schema: dbt_test_failures,失败记录存入数据库后查询分析。severity 可设为 warn 或 error。

适用场景:维度与事实模型的数据完整性校验、主外键关系维护、业务规则验证、CI/CD 中的测试执行、测试失败排查。

限制:资料未提供具体平台支持范围、性能数据或收费信息;dbt_constraints 的数据库兼容性需参考其官方文档。内置 not_null/unique/relationships 测试仅在 dbt 运行时校验,不具备数据库级持续约束能力。