Principle:173TECH Sayn Data Testing
| Knowledge Sources | |
|---|---|
| Domains | Data_Quality, Testing |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Technique for validating data quality through standard column tests (unique, not_null, allowed_values) and custom SQL tests that return failing rows, integrated into the task execution lifecycle.
Description
Data testing in SAYN operates on the principle that a test passes when its query returns zero rows — the query is written to find data that violates the assertion. Two test types are supported:
- Standard column tests: Defined in the
columnsDDL of a task. Built-in tests:unique— no duplicate values in the columnnot_null— no NULL valuesallowed_values— values must be in a specified list
Standard tests are compiled into a single UNION query that checks all assertions, returning up to 5 failing rows.
- Custom SQL tests: Separate task type (
type: test) that executes user-written SQL. The SQL should return rows that fail the test; empty result = pass.
Usage
Use this principle when you need to validate data quality after ETL processing. Standard tests are for simple column assertions; custom tests are for complex validation logic (cross-table checks, business rules).
Theoretical Basis
The test execution pattern:
- Compile test query: Build SQL that returns rows violating the assertion
- Execute: Run the query with
LIMIT 5 - Evaluate: If 0 rows returned → pass; if any rows → fail
- Report: On failure, show offending record counts and generate diagnostic queries
Standard test query structure (UNION of subqueries):
-- Abstract example (generated from template)
SELECT val, col, cnt, type FROM (
SELECT column_a AS val, 'column_a' AS col, COUNT(*) AS cnt, 'unique' AS type
FROM target_table
GROUP BY column_a HAVING COUNT(*) > 1
UNION ALL
SELECT column_b AS val, 'column_b' AS col, 1 AS cnt, 'not_null' AS type
FROM target_table WHERE column_b IS NULL
) AS t LIMIT 5;
On failure, diagnostic queries (test_problematic_values) are generated and written to the compile folder for debugging.
Practical Guide
Standard column tests in YAML:
# tasks/models.yaml
models:
user_summary:
type: sql
file_name: user_summary.sql
materialisation: table
destination: analytics.user_summary
columns:
- name: user_id
tests:
- unique
- not_null
- name: status
tests:
- name: allowed_values
allowed_values: [active, inactive, pending]
Custom SQL test:
-- sql/test_no_orphan_orders.sql
-- Returns orders without matching users (test fails if any found)
SELECT o.order_id, o.user_id
FROM {{ src('analytics.orders') }} o
LEFT JOIN {{ src('analytics.users') }} u ON o.user_id = u.user_id
WHERE u.user_id IS NULL
# tasks/tests.yaml
tests:
test_no_orphan_orders:
type: test
file_name: test_no_orphan_orders.sql
db: warehouse
Run tests:
# Run all tests
sayn test
# Run tests after task execution
sayn run --with-tests