Skip to content
mentoor

Topic 3 of 5 · 5 min read

Data tests inside the pipeline

Goal: Add the tests that would have caught a given bad load and decide for each whether a failure blocks or only warns.

A pipeline that runs without errors can still deliver wrong data. Tests inside the pipeline check the data itself, and each test needs a decision: does a failure stop the load, or only raise a warning?

Check yourself · 1 of 3

A source file was loaded twice and revenue doubled. Which test would have caught it?

Choose one answer

Why it matters

A job can succeed while a source sends empty files, duplicates a day, renames a status value or stops updating. Nobody notices until a finance report is off. An assistant can write a transformation that runs and returns rows, and the rows can still be wrong. Tests are how you prove otherwise on every run.

The tests to start with

  1. not_null on keys and required fields.
  2. unique on primary keys. Duplicate keys double-count revenue after a join.
  3. accepted_values on status and type columns. A new value such as refunded_partial should be seen, not silently dropped by a case statement.
  4. relationships (referential checks): every customer_id in orders exists in customers.
  5. freshness: the newest row in a source is no more than N hours old. This catches a stalled upstream feed.
  6. Row-count reconciliation between stages: staging should have the same count as raw, a mart should match the sum of what feeds it. A drop of more than a set percentage against yesterday also deserves a flag.

Block or warn

Decide by cost of being wrong.

  • Block (error severity, downstream models do not run): a duplicate primary key, a null in a required key, a row count that falls to zero. Bad data here corrupts everything built on it.
  • Warn: a small share of unexpected values, a slightly late source, a soft business rule. The load continues, a person is told, and the test is reviewed.

Use thresholds for warnings, such as warn above 0.1% null and error above 2%.

Before and after

An assistant writes a model for daily orders and no tests. The source sends the same file twice after a retry. The report shows double revenue.

The fix adds unique on order_id at error severity, a row-count comparison with the previous day at warn, and a freshness check on the source. The duplicate load now stops before the mart is built.

What goes wrong

  • Everything blocks. Pipelines fail daily over trivial issues, and people disable the tests.
  • Everything warns. The warnings pile up and nobody reads them.
  • Tests only on the final table. You learn something is wrong but not which stage broke.
  • Tests that check the logic you just wrote. An assistant that writes both can copy the same mistake into both.

Examples

Tests that would have caught the double load

models:
  - name: stg_orders
    columns:
      - name: order_id
        data_tests:
          - unique
          - not_null
      - name: status
        data_tests:
          - accepted_values:
              arguments:
                values: [placed, paid, shipped, refunded]
              config:
                severity: warn

A file loaded twice breaks unique and stops the run. A new status value only warns, so someone adds it on purpose.

Reconciling two stages

select (select count(*) from {{ ref('stg_orders') }}) as staged,
       (select count(*) from {{ source('shop', 'orders') }}) as raw

If the counts differ, a join or filter in staging dropped or multiplied rows. Without this check you find out from the dashboard.

Key points

  • Tests check the data on every run, because a pipeline can succeed and still deliver wrong data.
  • Start with not_null, unique, accepted_values, relationships and a source freshness check.
  • Reconcile row counts between stages so you can see which stage lost or duplicated rows.
  • Block on failures that corrupt everything downstream, such as duplicate keys; warn on soft or small deviations.
  • Set thresholds (warn at a small share, error at a larger one) instead of a single pass or fail.

Common mistakes

  • Making every test block the run until people switch the tests off.
  • Making every test a warning so none is acted on.
  • Testing only the final table, so the broken stage is unknown.
  • Accepting tests written by the same assistant that wrote the model without asking what each one would catch.

Check yourself · 2 of 3

Which failure should normally block the load rather than only warn?

Choose one answer

Check yourself · 3 of 3

What does comparing the row count of a staging model with its raw source tell you?

Choose one answer

Practice items, not counted towards your mastery.

Start here