Implementation:173TECH Sayn TestTask
| Knowledge Sources | |
|---|---|
| Domains | Data_Quality, Testing |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete tool for executing custom SQL data quality tests that pass when the query returns zero rows, provided by the SAYN framework.
Description
The TestTask class executes user-written SQL test queries. The test follows the "failure-row pattern": the SQL query should return rows that violate the assertion. If the query returns zero rows, the test passes. If any rows are returned, the test fails with diagnostic information.
Key behavior:
- Compiles the test SQL file with
src()macro for table references - Appends
LIMIT 5to the query to bound results - Supports
config()Jinja macro for Jinja-time config overrides (e.g., changingdb) - On failure, writes the compiled query to
compile/{group}/{name}_test.sqlfor debugging
Usage
Use this task type when standard column tests (unique, not_null, allowed_values) are insufficient and you need custom validation logic such as cross-table referential integrity or business rule checks.
Code Reference
Source Location
- Repository: SAYN
- File:
sayn/tasks/test.py - Lines: L13-27 (
Tests,Columnsmodels), L30-41 (Configmodel), L44-58 (CompileConfig), L61-176 (TestTaskclass)
Signature
class Config(BaseModel):
test_folder: Path
file_name: FilePath
db: Optional[str]
class TestTask(Task):
@property
def target_db(self) -> Database
def config(self, **config) -> Result
def config_macro(self, **config) -> str
def setup(self) -> Result
def run(self) -> Result
def compile(self) -> Result
def test(self) -> Result
Import
from sayn.tasks.test import TestTask
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| file_name | FilePath | Yes Test SQL file path relative to sql/ folder
| |
| db | str | No Target connection (defaults to default_db)
|
Outputs
| Name | Type | Description |
|---|---|---|
| test result | Result Ok (pass, 0 rows) or Err (fail, rows found) | |
| compiled query | file Written to compile/{group}/{name}_test.sql
|
Usage Examples
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
Running Tests
# Run all tests
sayn test
# Run a specific test
sayn test -t test_no_orphan_orders
# Run tasks with their standard column tests
sayn run --with-tests
Test with Config Override
-- sql/test_data_freshness.sql
{{ config(db='warehouse') }}
SELECT MAX(updated_at) as max_date, 'stale_data' as test_type
FROM {{ src('analytics.users') }}
WHERE updated_at < CURRENT_DATE - INTERVAL '7 days'