---
title: "Unit test model logic with fixed rows · Agent Wranglers"
url: https://agentwranglers.se/playbook/data-engineering/data-quality-tests/model-unit-tests
id: DEN-35
level: Recommended
updated: 2026-10-22
build: a2a6bdfed830
tokens: 791
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Unit test model logic with fixed rows

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Data quality tests](https://agentwranglers.se/playbook/data-engineering/data-quality-tests.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: DEN-35
- Level: Recommended
- Chapter: [Data quality tests](https://agentwranglers.se/playbook/data-engineering/data-quality-tests.md)
- Rule: Give every model with branching logic, date math, window functions or a past bug a unit test. It feeds fixed input rows, compares the output with expected rows and runs in CI before the model is built.
- Check: A script lists the models whose SQL holds `CASE`, a window function or date arithmetic, and fails on any without a unit test. Changing one branch of a tested `CASE` makes CI fail.
- How: [Test the keys and values of every model](https://agentwranglers.se/playbook/data-engineering/data-quality-tests/model-tests.md) checks the data after a build. It cannot show that a `CASE` puts refunds in the wrong month until that month arrives. A unit test runs the model's SQL on a few hand-written rows. Include the edge cases your data lacks today, such as a leap day or a null. dbt has unit tests from version 1.8. Run them in development and CI only, since they need no production data. Fix each logic bug with a unit test that fails before the fix.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### YAML example

```yaml
unit_tests:
  - name: refunds_count_in_month_of_refund
    model: fct_monthly_revenue
    given:
      - input: ref('stg_payments')
        rows:
          - {payment_id: 1, amount: 500, kind: charge, paid_at: '2026-01-31'}
          - {payment_id: 2, amount: -500, kind: refund, paid_at: '2026-02-01'}
    expect:
      rows:
        - {month: '2026-01-01', revenue: 500}
        - {month: '2026-02-01', revenue: -500}
```

## Sources

- [dbt unit tests](https://docs.getdbt.com/docs/build/unit-tests)

## Related rules

- DEN-14 [Test the keys and values of every model](https://agentwranglers.se/playbook/data-engineering/data-quality-tests/model-tests.md) in Data engineering

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/data-engineering/data-quality-tests/model-unit-tests">Agent Wranglers data engineering playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Alert on sudden changes in volume](https://agentwranglers.se/playbook/data-engineering/data-quality-tests/volume-anomalies.md)
- Next: [Publish a service level for every shared table](https://agentwranglers.se/playbook/data-engineering/data-quality-tests/table-service-levels.md)
- [Data quality tests](https://agentwranglers.se/playbook/data-engineering/data-quality-tests.md)
