Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:173TECH Sayn TestTask

From Leeroopedia
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 5 to the query to bound results
  • Supports config() Jinja macro for Jinja-time config overrides (e.g., changing db)
  • On failure, writes the compiled query to compile/{group}/{name}_test.sql for 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, Columns models), L30-41 (Config model), L44-58 (CompileConfig), L61-176 (TestTask class)

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'

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment