建立單一測試

dbt 中級

Mike Metzger

Data Engineer

什麼是單一測試?

  • 自訂資料測試
  • 以 SQL 查詢撰寫
    • 必須回傳失敗的列
  • tests 目錄中以 .sql 檔定義

測試示意圖

1 Photo by Antoine Dautry on 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 中級

一起來練習吧!

dbt 中級

Preparing Video For Download...