Skip to content

fastflowtransform.testing.registry

Runner

Bases: Protocol

Callable signature for a generic test runner.

Returns:

Name Type Description
ok bool

Whether the test passed.

message str | None

Optional human-friendly message (usually set on failure).

example_sql str | None

Optional example SQL (shown in summary on failure).

Source code in src/fastflowtransform/testing/registry.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Runner(Protocol):
    """Callable signature for a generic test runner.

    Returns:
        ok (bool): Whether the test passed.
        message (str | None): Optional human-friendly message (usually set on failure).
        example_sql (str | None): Optional example SQL (shown in summary on failure).
    """

    __name__: str

    def __call__(
        self, con: Any, table: str, column: str | None, params: dict[str, Any]
    ) -> tuple[bool, str | None, str | None]: ...

DQParamsBase

Bases: BaseModel

Base for all dynamically created DQ params models. Forbids unknown keys by default.

Source code in src/fastflowtransform/testing/registry.py
482
483
484
485
486
487
488
class DQParamsBase(BaseModel):
    """
    Base for all dynamically created DQ params models.
    Forbids unknown keys by default.
    """

    model_config = ConfigDict(extra="forbid")

run_accepted_values

run_accepted_values(con, table, column, params)

Runner for testing.accepted_values.

Source code in src/fastflowtransform/testing/registry.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def run_accepted_values(
    con: Any, table: str, column: str | None, params: dict[str, Any]
) -> tuple[bool, str | None, str | None]:
    """Runner for testing.accepted_values."""
    values = params.get("values") or []
    where = params.get("where")

    if column is None:
        example = "-- accepted_values: column parameter is required"
        return False, "missing required parameter: column", example

    if not values:
        # No values configured -> we treat this as a no-op check.
        example = f"-- accepted_values: no values provided; check is skipped for {table}.{column}"
        return True, None, example

    in_list = testing.sql_list(values)
    example = (
        f"select distinct {column} from {table} "
        + f"where {column} is not null and {column} not in ({in_list})"
        + (f" and ({where})" if where else "")
        + " limit 5"
    )

    col = column
    try:
        testing.accepted_values(con, table, col, values=values, where=where)
        return True, None, example
    except testing.TestFailure as e:
        return False, str(e), example

run_greater_equal

run_greater_equal(con, table, column, params)

Runner for testing.greater_equal (column >= threshold).

Source code in src/fastflowtransform/testing/registry.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def run_greater_equal(
    con: Any, table: str, column: str | None, params: dict[str, Any]
) -> tuple[bool, str | None, str | None]:
    """Runner for testing.greater_equal (column >= threshold)."""
    threshold = float(params.get("threshold", 0.0))
    if column is None:
        example = f"select count(*) from {table} where <column> < {threshold}"
        return False, "missing required parameter: column", example

    example = f"select count(*) from {table} where {column} < {threshold}"
    col = column
    try:
        testing.greater_equal(con, table, col, threshold=threshold)
        return True, None, example
    except testing.TestFailure as e:
        return False, str(e), example

run_non_negative_sum

run_non_negative_sum(con, table, column, params)

Runner for testing.non_negative_sum.

Source code in src/fastflowtransform/testing/registry.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def run_non_negative_sum(
    con: Any, table: str, column: str | None, params: dict[str, Any]
) -> tuple[bool, str | None, str | None]:
    """Runner for testing.non_negative_sum."""
    if column is None:
        example = f"select coalesce(sum(<column>), 0) from {table}"
        return False, "missing required parameter: column", example

    example = f"select coalesce(sum({column}), 0) from {table}"
    col = column
    try:
        testing.non_negative_sum(con, table, col)
        return True, None, example
    except testing.TestFailure as e:
        return False, str(e), example

run_row_count_between

run_row_count_between(con, table, column, params)

Runner for testing.row_count_between.

Source code in src/fastflowtransform/testing/registry.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def run_row_count_between(
    con: Any, table: str, column: str | None, params: dict[str, Any]
) -> tuple[bool, str | None, str | None]:
    """Runner for testing.row_count_between."""
    min_rows = int(params.get("min_rows", 1))
    max_rows_param = params.get("max_rows")
    max_rows = int(max_rows_param) if max_rows_param is not None else None

    example = f"select count(*) from {table}"
    try:
        testing.row_count_between(con, table, min_rows=min_rows, max_rows=max_rows)
        return True, None, example
    except testing.TestFailure as e:
        return False, str(e), example

run_freshness

run_freshness(con, table, column, params)

Runner for testing.freshness (max timestamp delay in minutes).

Source code in src/fastflowtransform/testing/registry.py
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
def run_freshness(
    con: Any, table: str, column: str | None, params: dict[str, Any]
) -> tuple[bool, str | None, str | None]:
    """Runner for testing.freshness (max timestamp delay in minutes)."""
    if column is None:
        example = (
            f"select date_part('epoch', now() - max(<ts_column>)) / 60.0 as delay_min from {table}"
        )
        return False, "missing required parameter: column (ts_col)", example

    max_delay_raw = params.get("max_delay_minutes")
    example = f"select date_part('epoch', now() - max({column})) / 60.0 as delay_min from {table}"

    if max_delay_raw is None:
        return False, "missing required parameter: max_delay_minutes", example

    try:
        max_delay_int = int(max_delay_raw)
    except (TypeError, ValueError):
        return (
            False,
            f"invalid max_delay_minutes (expected integer minutes, got {max_delay_raw!r})",
            example,
        )

    col = column
    try:
        testing.freshness(con, table, col, max_delay_minutes=max_delay_int)
        return True, None, example
    except testing.TestFailure as e:
        return False, str(e), example

run_reconcile_equal

run_reconcile_equal(con, table, column, params)

Runner for testing.reconcile_equal (left == right within tolerances).

Source code in src/fastflowtransform/testing/registry.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
def run_reconcile_equal(
    con: Any, table: str, column: str | None, params: dict[str, Any]
) -> tuple[bool, str | None, str | None]:
    """Runner for testing.reconcile_equal (left == right within tolerances)."""
    left = params.get("left")
    right = params.get("right")
    abs_tol = params.get("abs_tolerance")
    rel_tol = params.get("rel_tolerance_pct")

    if not isinstance(left, dict) or not isinstance(right, dict):
        example = "-- reconcile_equal requires 'left' and 'right' dict parameters"
        return False, "missing or invalid 'left'/'right' parameters", example

    example = _example_scalar_side(left) + ";\n" + _example_scalar_side(right)

    try:
        testing.reconcile_equal(
            con,
            left=left,
            right=right,
            abs_tolerance=abs_tol,
            rel_tolerance_pct=rel_tol,
        )
        return True, None, example
    except testing.TestFailure as e:
        return False, str(e), example

run_reconcile_ratio_within

run_reconcile_ratio_within(con, table, column, params)

Runner for testing.reconcile_ratio_within (min_ratio <= L/R <= max_ratio).

Source code in src/fastflowtransform/testing/registry.py
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
def run_reconcile_ratio_within(
    con: Any, table: str, column: str | None, params: dict[str, Any]
) -> tuple[bool, str | None, str | None]:
    """Runner for testing.reconcile_ratio_within (min_ratio <= L/R <= max_ratio)."""
    left = params.get("left")
    right = params.get("right")
    min_ratio = params.get("min_ratio")
    max_ratio = params.get("max_ratio")

    if not isinstance(left, dict) or not isinstance(right, dict):
        example = "-- reconcile_ratio_within requires 'left' and 'right' dict parameters"
        return False, "missing or invalid 'left'/'right' parameters", example

    if min_ratio is None or max_ratio is None:
        example = _example_scalar_side(left) + ";\n" + _example_scalar_side(right)
        return False, "missing required parameters: min_ratio / max_ratio", example

    example = _example_scalar_side(left) + ";\n" + _example_scalar_side(right)

    try:
        testing.reconcile_ratio_within(
            con,
            left=left,
            right=right,
            min_ratio=float(min_ratio),
            max_ratio=float(max_ratio),
        )
        return True, None, example
    except testing.TestFailure as e:
        return False, str(e), example

run_reconcile_diff_within

run_reconcile_diff_within(con, table, column, params)

Runner for testing.reconcile_diff_within (|L - R| <= max_abs_diff).

Source code in src/fastflowtransform/testing/registry.py
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
def run_reconcile_diff_within(
    con: Any, table: str, column: str | None, params: dict[str, Any]
) -> tuple[bool, str | None, str | None]:
    """Runner for testing.reconcile_diff_within (|L - R| <= max_abs_diff)."""
    left = params.get("left")
    right = params.get("right")
    max_abs_diff = params.get("max_abs_diff")

    if not isinstance(left, dict) or not isinstance(right, dict):
        example = "-- reconcile_diff_within requires 'left' and 'right' dict parameters"
        return False, "missing or invalid 'left'/'right' parameters", example

    if max_abs_diff is None:
        example = _example_scalar_side(left) + ";\n" + _example_scalar_side(right)
        return False, "missing required parameter: max_abs_diff", example

    example = _example_scalar_side(left) + ";\n" + _example_scalar_side(right)

    try:
        testing.reconcile_diff_within(
            con,
            left=left,
            right=right,
            max_abs_diff=float(max_abs_diff),
        )
        return True, None, example
    except testing.TestFailure as e:
        return False, str(e), example

run_reconcile_coverage

run_reconcile_coverage(con, table, column, params)

Runner for testing.reconcile_coverage (anti-join count == 0).

Source code in src/fastflowtransform/testing/registry.py
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
def run_reconcile_coverage(
    con: Any, table: str, column: str | None, params: dict[str, Any]
) -> tuple[bool, str | None, str | None]:
    """Runner for testing.reconcile_coverage (anti-join count == 0)."""
    source = params.get("source")
    target = params.get("target")
    source_where = params.get("source_where")
    target_where = params.get("target_where")

    if not isinstance(source, dict) or not isinstance(target, dict):
        example = "-- reconcile_coverage requires 'source' and 'target' dict parameters"
        return False, "missing or invalid 'source'/'target' parameters", example

    example = _example_coverage_sql(source, target, source_where, target_where)

    try:
        testing.reconcile_coverage(
            con,
            source=source,
            target=target,
            source_where=source_where,
            target_where=target_where,
        )
        return True, None, example
    except testing.TestFailure as e:
        return False, str(e), example

run_relationships

run_relationships(con, table, column, params)

Runner for testing.relationships (FK-style anti join).

Source code in src/fastflowtransform/testing/registry.py
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
def run_relationships(
    con: Any, table: str, column: str | None, params: dict[str, Any]
) -> tuple[bool, str | None, str | None]:
    """Runner for testing.relationships (FK-style anti join)."""
    field = params.get("field") or column
    to_table = params.get("_to_relation") or params.get("to")
    to_field = params.get("to_field") or "id"
    where = params.get("where")
    to_where = params.get("to_where")

    example = _example_relationship_sql(
        table, field or "<field>", to_table, to_field, where, to_where
    )

    if not field:
        return False, "missing required parameter: field (or column)", example
    if not to_table:
        return False, "missing required parameter: to", example

    try:
        testing.relationships(
            con,
            table=table,
            field=field,
            to_table=to_table,
            to_field=to_field,
            where=where,
            to_where=to_where,
        )
        return True, None, example
    except testing.TestFailure as e:
        return False, str(e), example

register_python_test

register_python_test(kind, runner, *, params_model=None, origin=None, overwrite=False)

Register a custom Python test.

Parameters:

Name Type Description Default
kind str

logical test type (e.g. "no_future_orders").

required
runner Runner

callable implementing Runner.

required
params_model type[BaseModel] | None

optional Pydantic model for the params dict.

None
origin str | None

string used in error messages (e.g. module path).

None
overwrite bool

if True, replace an existing test with the same kind.

False
Source code in src/fastflowtransform/testing/registry.py
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
def register_python_test(
    kind: str,
    runner: Runner,
    *,
    params_model: type[BaseModel] | None = None,
    origin: str | None = None,
    overwrite: bool = False,
) -> None:
    """
    Register a custom Python test.

    Args:
        kind: logical test type (e.g. "no_future_orders").
        runner: callable implementing Runner.
        params_model: optional Pydantic model for the params dict.
        origin: string used in error messages (e.g. module path).
        overwrite: if True, replace an existing test with the same kind.
    """
    if kind in TESTS and not overwrite:
        raise ValueError(
            f"Test type {kind!r} is already registered "
            f"(origin={TEST_ORIGINS.get(kind, '<builtin>')!r})"
        )

    if kind in TESTS and overwrite:
        logger.warning(
            "Overwriting DQ test %r (previous origin=%r, new origin=%r)",
            kind,
            TEST_ORIGINS.get(kind),
            origin,
        )

    TESTS[kind] = runner

    if params_model is not None:
        TEST_PARAM_MODELS[kind] = params_model
    # If overwriting and no new params_model is given, keep the old one if present.
    elif overwrite and kind in TEST_PARAM_MODELS:
        pass
    else:
        # Default to a generic params model if you have one, or leave it unset
        TEST_PARAM_MODELS.pop(kind, None)

    if origin is not None:
        TEST_ORIGINS[kind] = origin
    elif overwrite:
        # if overwriting without explicit origin, don't change existing origin
        pass

register_sql_test

register_sql_test(kind, path, *, params_model=None, overwrite=False)

Register a custom SQL-based test from a *.ff.sql file.

kind: logical test type (e.g. "no_future_orders"). path: filesystem path to the template. params_model: optional Pydantic model for params. overwrite: if True, allow overriding an existing test of the same kind.

Source code in src/fastflowtransform/testing/registry.py
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
def register_sql_test(
    kind: str,
    path: Path,
    *,
    params_model: type[BaseModel] | None = None,
    overwrite: bool = False,
) -> None:
    """
    Register a custom SQL-based test from a *.ff.sql file.

    kind: logical test type (e.g. "no_future_orders").
    path: filesystem path to the template.
    params_model: optional Pydantic model for params.
    overwrite: if True, allow overriding an existing test of the same kind.
    """
    origin = str(path)
    META_KEYS = {"type", "table", "column", "severity", "tags", "name"}

    def _runner(
        con: Any, table: str, column: str | None, params: dict[str, Any]
    ) -> tuple[bool, str | None, str | None]:
        # 1) Strip generic test metadata and validate params if a schema is provided
        raw_params: dict[str, Any] = dict(params or {})
        core_params: dict[str, Any] = {k: v for k, v in raw_params.items() if k not in META_KEYS}

        if params_model is not None:
            try:
                cfg = params_model.model_validate(core_params)
            except ValidationError as exc:
                err_msg = _format_param_validation_error(kind, origin, exc)
                raise testing.TestFailure(err_msg) from exc
            # Use normalized params (e.g. converted types, defaults)
            params_validated = cfg.model_dump(exclude_none=True)
        else:
            params_validated = core_params

        # 2) Render the SQL template with a stable context
        env = REGISTRY.get_env()
        raw = path.read_text(encoding="utf-8")
        tmpl = env.from_string(raw)

        ctx: dict[str, Any] = {
            "kind": kind,
            "table": table,
            "column": column,
            "params": params_validated,
            # always present, so templates can safely do `{% if where %}`
            "where": params_validated.get("where"),
        }

        try:
            sql = tmpl.render(**ctx)
        except Exception as exc:
            raise testing.TestFailure(
                f"[{kind}] Failed to render SQL template for {origin}: {exc}"
            ) from exc

        # 3) Execute the SQL: convention here is "fail if count(*) > 0"
        n = _scalar(con, sql)
        ok = int(n or 0) == 0
        msg: str | None = None if ok else f"{kind} failed: {n} offending row(s)"
        example_sql = sql
        return ok, msg, example_sql

    register_python_test(
        kind,
        _runner,
        params_model=params_model,
        origin=origin,
        overwrite=overwrite,
    )