Course outline

dbt / Intermediate: history and controlled workflows / Lesson 14

Design failing-row tests for business rules

Write two singular data tests that return violations and interpret their results within a documented model contract.

About 3 min readContent reviewed
On this page

Lesson goals

  • Express a business rule as violating rows.
  • Distinguish an empty violation set from broader correctness.
  • Test null and referential policies explicitly.

Translate the rule into violations

A dbt data test is useful when its query identifies records that violate a specific assertion. Before writing SQL, define the model grain, the permitted amount range, and whether a missing customer is allowed. The examples assume stg_orders and stg_customers exist and are built in an isolated target. They use synthetic rules for illustration; agree on actual business semantics and adapter-compatible SQL before execution.

Test a value constraint

The first singular test returns orders with a negative amount. A zero-row result supports the narrow assertion that no selected row has a negative amount at that time. SQL’s null comparison is not true, so this query does not flag null amounts. If amount is required, add an explicit null rule or a separate test. Also define currency, precision, and whether refunds are represented as negative amounts before adopting the rule.

sql Find negative amounts
-- tests/order_amount_nonnegative.sql
select order_id, amount
from {{ ref("stg_orders") }}
where amount < 0

The query returns negative values; null amounts require a separate rule.

Test a relationship

The second test returns non-null customer IDs that have no matching customer row. It deliberately excludes null customer IDs, so it encodes an optional relationship; add a separate requirement if null is forbidden. Duplicate customer IDs can also violate a lookup contract without appearing here. A separate uniqueness check and a clear source grain are necessary when one customer per key is required.

sql Find unmatched customers
-- tests/order_customer_exists.sql
select o.order_id, o.customer_id
from {{ ref("stg_orders") }} as o
where o.customer_id is not null
  and not exists (
    select 1
    from {{ ref("stg_customers") }} as c
    where c.customer_id = o.customer_id
  )

The query returns unmatched non-null customer IDs and treats null as optional.

Choose scope and interpret results

The ref calls express dependencies on the tested models, but the selected test run and its target determine what was actually checked. Review compiled SQL, selected resources, and warehouse permissions before running. A passing test is evidence about its query and current relation, not proof that upstream data is fresh or that every business rule holds. A failing-row query can expose sensitive values, so restrict access to test output.

Verify with controlled fixtures

Create an isolated fixture with a valid order, a negative amount, an unknown customer, and a null customer. Confirm each test reports only the violations specified by its contract. Add a null amount and duplicate customer key to demonstrate which separate checks are needed. Verify the valid case after repairing the fixture, and record the target and selected tests rather than claiming a full-project validation.

Common errors and fixes

Null amounts pass a nonnegative test

Why it happens
The SQL predicate does not select null comparisons.
How to fix it
Add an explicit not-null requirement if the business contract requires it.
Prevent it next time
Test null and negative amounts as separate cases.

A missing customer is not reported

Why it happens
The relationship query intentionally excludes null IDs.
How to fix it
Decide whether the relationship is optional and add a null test if required.
Prevent it next time
Document and test both null and unknown IDs.

A passing test is taken as full data certification

Why it happens
A narrow assertion was generalized beyond its query and target.
How to fix it
Report the exact rule, selection, and run context.
Prevent it next time
Maintain separate freshness, uniqueness, and business checks.

2 practice exercises

1. Does the negative-amount test reject a null amount?

Show worked solution

No. The predicate amount < 0 does not select null. If null is invalid, add an explicit check and test it independently.

2. Why can duplicate customer IDs pass the relationship test?

Show worked solution

The test asks only whether at least one matching customer exists. It does not count matches; use a separate uniqueness assertion when the lookup key must be unique.

Key takeaways

  • Write tests as explicit violating-row queries.
  • Interpret each result within its selection, null policy, and business contract.

Official references