|
| 1 | +# Copyright (c) QuantCo 2025-2025 |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +import polars as pl |
| 5 | + |
| 6 | +import dataframely as dy |
| 7 | + |
| 8 | + |
| 9 | +def test_collection_matches_itself() -> None: |
| 10 | + """Collections should match themselves.""" |
| 11 | + |
| 12 | + class MySchema(dy.Schema): |
| 13 | + foo = dy.Integer() |
| 14 | + |
| 15 | + # First collection has one member |
| 16 | + class MyCollection1(dy.Collection): |
| 17 | + x: dy.LazyFrame[MySchema] |
| 18 | + |
| 19 | + assert MyCollection1.matches(MyCollection1) |
| 20 | + |
| 21 | + |
| 22 | +def test_collection_matches_different_members() -> None: |
| 23 | + """Collections should count as different if they have members with different |
| 24 | + names.""" |
| 25 | + |
| 26 | + class MySchema(dy.Schema): |
| 27 | + foo = dy.Integer() |
| 28 | + |
| 29 | + class MyCollection1(dy.Collection): |
| 30 | + x: dy.LazyFrame[MySchema] |
| 31 | + |
| 32 | + class MyCollection2(dy.Collection): |
| 33 | + y: dy.LazyFrame[MySchema] |
| 34 | + |
| 35 | + # Should not match |
| 36 | + assert not MyCollection1.matches(MyCollection2) |
| 37 | + |
| 38 | + |
| 39 | +def test_collection_matches_different_schemas() -> None: |
| 40 | + """Collections should count as different if their members have different schemas.""" |
| 41 | + |
| 42 | + class MyIntSchema(dy.Schema): |
| 43 | + foo = dy.Integer() |
| 44 | + |
| 45 | + class MyStringSchema(dy.Schema): |
| 46 | + foo = dy.String() |
| 47 | + |
| 48 | + assert not MyIntSchema.matches(MyStringSchema), ( |
| 49 | + "Test schemas must not match for test setup to make sense" |
| 50 | + ) |
| 51 | + |
| 52 | + # Collections have the same member name |
| 53 | + # but mismatching schemas |
| 54 | + class MyCollection1(dy.Collection): |
| 55 | + x: dy.LazyFrame[MyIntSchema] |
| 56 | + |
| 57 | + class MyCollection2(dy.Collection): |
| 58 | + x: dy.LazyFrame[MyStringSchema] |
| 59 | + |
| 60 | + # Should not match |
| 61 | + assert not MyCollection1.matches(MyCollection2) |
| 62 | + |
| 63 | + |
| 64 | +def test_collection_matches_different_filter_names() -> None: |
| 65 | + """Collections should count as different if they have the same members but different |
| 66 | + names.""" |
| 67 | + |
| 68 | + class MyIntSchema(dy.Schema): |
| 69 | + foo = dy.Integer(primary_key=True) |
| 70 | + |
| 71 | + class MyCollection1(dy.Collection): |
| 72 | + x: dy.LazyFrame[MyIntSchema] |
| 73 | + |
| 74 | + class MyCollection2(MyCollection1): |
| 75 | + @dy.filter() |
| 76 | + def test_filter(self) -> pl.LazyFrame: |
| 77 | + return dy.filter_relationship_one_to_one(self.x, self.x, ["foo"]) |
| 78 | + |
| 79 | + # Should not match |
| 80 | + assert not MyCollection1.matches(MyCollection2) |
| 81 | + |
| 82 | + |
| 83 | +def test_collection_matches_different_filter_logc() -> None: |
| 84 | + """Collections should count as different if they have the same members but different |
| 85 | + filter logic.""" |
| 86 | + |
| 87 | + class MyIntSchema(dy.Schema): |
| 88 | + foo = dy.Integer(primary_key=True) |
| 89 | + |
| 90 | + class BaseCollection(dy.Collection): |
| 91 | + x: dy.LazyFrame[MyIntSchema] |
| 92 | + |
| 93 | + class MyCollection1(BaseCollection): |
| 94 | + @dy.filter() |
| 95 | + def test_filter(self) -> pl.LazyFrame: |
| 96 | + return dy.filter_relationship_one_to_one(self.x, self.x, ["foo"]) |
| 97 | + |
| 98 | + class MyCollection2(BaseCollection): |
| 99 | + @dy.filter() |
| 100 | + def test_filter(self) -> pl.LazyFrame: |
| 101 | + return dy.filter_relationship_one_to_at_least_one(self.x, self.x, ["foo"]) |
| 102 | + |
| 103 | + assert not MyCollection1.matches(MyCollection2) |
| 104 | + |
| 105 | + |
| 106 | +def test_collection_matches_different_optional() -> None: |
| 107 | + """Collections should count as different if their members differ in whether they are |
| 108 | + optional or not.""" |
| 109 | + |
| 110 | + class FooSchema(dy.Schema): |
| 111 | + x = dy.Integer() |
| 112 | + |
| 113 | + class MandatoryFooCollection(dy.Collection): |
| 114 | + foo: dy.LazyFrame[FooSchema] |
| 115 | + |
| 116 | + class OptionalFooCollection(dy.Collection): |
| 117 | + foo: dy.LazyFrame[FooSchema] | None |
| 118 | + |
| 119 | + assert not MandatoryFooCollection.matches(OptionalFooCollection) |
0 commit comments