10. Write the Tests for an Orders Model
Difficulty: Easy · Topic: Data tests
The orders model (order_id, customer_id, status, amount) feeds the revenue dashboard. Write models/schema.yml with data tests that enforce what the business promises:
- Each order appears once, and
order_idis never missing. - Every order has a
customer_id. statusis always one ofplaced,shipped,completed,returned, and never missing.
Your tests are run on several datasets. On a clean dataset every test must pass (a test that fails on good data stops the pipeline for nothing). On a broken dataset at least one test must fail. You see two datasets; the hidden ones break the rules in other ways.
Your schema.yml tests run on 7 datasets, including hidden ones: clean datasets must pass every test, broken ones must make a test fail.
Clean data (clean)
{{ ref('orders') }}
| order_id | customer_id | status | amount |
|---|---|---|---|
| 1 | 10 | placed | 25 |
| 2 | 11 | shipped | 40.5 |
| 3 | 10 | completed | 12 |
| 4 | 12 | placed | 99.99 |
A status typo (broken)
A new app version writes "shiped".
{{ ref('orders') }}
| order_id | customer_id | status | amount |
|---|---|---|---|
| 1 | 10 | placed | 25 |
| 2 | 11 | shiped | 40.5 |
| 3 | 10 | completed | 12 |
Hints
Hint 1
Column tests go undercolumns:, each with a name and a list of data_tests (or tests): - unique, - not_null.Hint 2
accepted_values takes the list of values: - accepted_values: {arguments: {values: [placed, shipped, completed, returned]}} (older projects put values directly under the test name; both work).Hint 3
Check whataccepted_values does with a null status: null not in (...) is not true, so the row is not reported.Learn the concepts
- Data tests · 5 min read. unique, not_null, accepted_values and relationships, singular tests, and how a test fails: it returns rows.