|
| 1 | +# Copyright (c) QuantCo 2025-2025 |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | +import textwrap |
| 4 | + |
| 5 | +import polars as pl |
| 6 | + |
| 7 | +import dataframely as dy |
| 8 | + |
| 9 | + |
| 10 | +def test_repr_no_rules() -> None: |
| 11 | + class SchemaNoRules(dy.Schema): |
| 12 | + a = dy.Integer() |
| 13 | + |
| 14 | + assert repr(SchemaNoRules) == textwrap.dedent("""\ |
| 15 | + [Schema "SchemaNoRules"] |
| 16 | + Columns: |
| 17 | + - "a": Integer(nullable=True) |
| 18 | + """) |
| 19 | + |
| 20 | + |
| 21 | +def test_repr_only_column_rules() -> None: |
| 22 | + class SchemaColumnRules(dy.Schema): |
| 23 | + a = dy.Integer(min=10) |
| 24 | + |
| 25 | + assert repr(SchemaColumnRules) == textwrap.dedent("""\ |
| 26 | + [Schema "SchemaColumnRules"] |
| 27 | + Columns: |
| 28 | + - "a": Integer(nullable=True, min=10) |
| 29 | + """) |
| 30 | + |
| 31 | + |
| 32 | +class SchemaWithRules(dy.Schema): |
| 33 | + a = dy.Integer(min=10) |
| 34 | + b = dy.String(primary_key=True, regex=r"^[A-Z]{3}$", alias="b2") |
| 35 | + |
| 36 | + @dy.rule() |
| 37 | + def my_rule() -> pl.Expr: |
| 38 | + return pl.col("a") < 100 |
| 39 | + |
| 40 | + @dy.rule(group_by=["a"]) |
| 41 | + def my_group_rule() -> pl.Expr: |
| 42 | + return pl.col("a").sum() > 50 |
| 43 | + |
| 44 | + |
| 45 | +def test_repr_with_rules() -> None: |
| 46 | + assert repr(SchemaWithRules) == textwrap.dedent("""\ |
| 47 | + [Schema "SchemaWithRules"] |
| 48 | + Columns: |
| 49 | + - "a": Integer(nullable=True, min=10) |
| 50 | + - "b2": String(nullable=False, primary_key=True, regex='^[A-Z]{3}$') |
| 51 | + Rules: |
| 52 | + - "my_rule": [(col("a")) < (dyn int: 100)] |
| 53 | + - "my_group_rule": [(col("a").sum()) > (dyn int: 50)] grouped by ['a'] |
| 54 | + """) |
0 commit comments