Skip to content

13. Catch Orphaned and Invalid Payments

Difficulty: Medium · Topic: Data tests

Write models/schema.yml tests for payments (payment_id, order_id, method, amount). The orders model (order_id, status) is also in the project.

Clean datasets must pass every test; broken ones must make at least one test fail. dbt_utils is installed: dbt_utils.accepted_range and dbt_utils.expression_is_true are available.

Your schema.yml tests run on 8 datasets, including hidden ones: clean datasets must pass every test, broken ones must make a test fail.

Clean data (clean)

{{ ref('payments') }}

payment_idorder_idmethodamount
1001card25
1012upi40
1022card5

{{ ref('orders') }}

order_idstatus
1paid
2paid
3pending

A payment for an unknown order (broken)

Order 9 was never loaded.

{{ ref('payments') }}

payment_idorder_idmethodamount
1001card25
1039upi12

{{ ref('orders') }}

order_idstatus
1paid
2paid

Hints

Hint 1relationships checks a foreign key: - relationships: {arguments: {to: "ref('orders')", field: order_id}}.
Hint 2Like most tests, relationships skips nulls: a payment with no order passes it. "Never missing" needs not_null as well.
Hint 3"Greater than zero" excludes zero: dbt_utils.accepted_range with min_value: 0 and inclusive: false, or dbt_utils.expression_is_true with expression: "> 0" on the column.

Learn the concepts

More dbt problems

All dbt problems