|
| 1 | +"""Unit tests ensuring identifier_placeholder is respected by checks.""" |
| 2 | + |
| 3 | +import datetime as dt |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from koality.checks import ( |
| 8 | + AverageCheck, |
| 9 | + CountCheck, |
| 10 | + DuplicateCheck, |
| 11 | + MatchRateCheck, |
| 12 | + MaxCheck, |
| 13 | + MinCheck, |
| 14 | + NullRatioCheck, |
| 15 | + OccurrenceCheck, |
| 16 | + RegexMatchCheck, |
| 17 | + RollingValuesInSetCheck, |
| 18 | +) |
| 19 | + |
| 20 | +pytestmark = pytest.mark.unit |
| 21 | + |
| 22 | + |
| 23 | +def test_average_check_uses_identifier_placeholder() -> None: |
| 24 | + """AverageCheck uses identifier_placeholder for naming-only identifier filters.""" |
| 25 | + chk = AverageCheck( |
| 26 | + database_accessor="", |
| 27 | + database_provider=None, |
| 28 | + table="t", |
| 29 | + check_column="col", |
| 30 | + filters={"shop_id": {"type": "identifier"}}, |
| 31 | + identifier_format="filter_name", |
| 32 | + identifier_placeholder="PH", |
| 33 | + ) |
| 34 | + assert chk.identifier == "PH" |
| 35 | + assert chk.identifier_column == "SHOP_ID" |
| 36 | + |
| 37 | + |
| 38 | +def test_max_check_uses_identifier_placeholder() -> None: |
| 39 | + """MaxCheck uses identifier_placeholder for naming-only identifier filters.""" |
| 40 | + chk = MaxCheck( |
| 41 | + database_accessor="", |
| 42 | + database_provider=None, |
| 43 | + table="t", |
| 44 | + check_column="col", |
| 45 | + filters={"shop_id": {"type": "identifier"}}, |
| 46 | + identifier_format="filter_name", |
| 47 | + identifier_placeholder="PH2", |
| 48 | + ) |
| 49 | + assert chk.identifier == "PH2" |
| 50 | + assert chk.identifier_column == "SHOP_ID" |
| 51 | + |
| 52 | + |
| 53 | +def test_min_check_uses_identifier_placeholder() -> None: |
| 54 | + """MinCheck uses identifier_placeholder for naming-only identifier filters.""" |
| 55 | + chk = MinCheck( |
| 56 | + database_accessor="", |
| 57 | + database_provider=None, |
| 58 | + table="t", |
| 59 | + check_column="col", |
| 60 | + filters={"shop_id": {"type": "identifier"}}, |
| 61 | + identifier_format="filter_name", |
| 62 | + identifier_placeholder="PH3", |
| 63 | + ) |
| 64 | + assert chk.identifier == "PH3" |
| 65 | + assert chk.identifier_column == "SHOP_ID" |
| 66 | + |
| 67 | + |
| 68 | +def test_occurrence_check_uses_identifier_placeholder() -> None: |
| 69 | + """OccurrenceCheck uses identifier_placeholder for naming-only identifier filters.""" |
| 70 | + chk = OccurrenceCheck( |
| 71 | + database_accessor="", |
| 72 | + database_provider=None, |
| 73 | + max_or_min="max", |
| 74 | + table="t", |
| 75 | + check_column="col", |
| 76 | + filters={"shop_id": {"type": "identifier"}}, |
| 77 | + identifier_format="filter_name", |
| 78 | + identifier_placeholder="PH4", |
| 79 | + ) |
| 80 | + assert chk.identifier == "PH4" |
| 81 | + assert chk.identifier_column == "SHOP_ID" |
| 82 | + |
| 83 | + |
| 84 | +def test_rolling_values_in_set_check_requires_date_and_uses_placeholder() -> None: |
| 85 | + """RollingValuesInSetCheck requires a date filter and uses identifier_placeholder.""" |
| 86 | + # Provide a date filter to satisfy constructor requirements |
| 87 | + today = dt.datetime.now(tz=dt.UTC).date().isoformat() |
| 88 | + chk = RollingValuesInSetCheck( |
| 89 | + database_accessor="", |
| 90 | + database_provider=None, |
| 91 | + table="t", |
| 92 | + check_column="col", |
| 93 | + value_set=["a"], |
| 94 | + filters={ |
| 95 | + "partition_date": {"column": "DATE", "value": today, "type": "date"}, |
| 96 | + "shop_id": {"type": "identifier"}, |
| 97 | + }, |
| 98 | + identifier_format="filter_name", |
| 99 | + identifier_placeholder="PH5", |
| 100 | + ) |
| 101 | + assert chk.identifier == "PH5" |
| 102 | + assert chk.identifier_column == "SHOP_ID" |
| 103 | + |
| 104 | + |
| 105 | +def test_values_in_set_check_uses_identifier_placeholder() -> None: |
| 106 | + """ValuesInSetCheck uses identifier_placeholder for naming-only identifier filters.""" |
| 107 | + chk = RollingValuesInSetCheck( |
| 108 | + database_accessor="", |
| 109 | + database_provider=None, |
| 110 | + table="t", |
| 111 | + check_column="col", |
| 112 | + value_set=["a"], |
| 113 | + filters={ |
| 114 | + "shop_id": {"type": "identifier"}, |
| 115 | + "partition_date": { |
| 116 | + "column": "DATE", |
| 117 | + "value": dt.datetime.now(tz=dt.UTC).date().isoformat(), |
| 118 | + "type": "date", |
| 119 | + }, |
| 120 | + }, |
| 121 | + identifier_format="filter_name", |
| 122 | + identifier_placeholder="PH6", |
| 123 | + ) |
| 124 | + assert chk.identifier == "PH6" |
| 125 | + assert chk.identifier_column == "SHOP_ID" |
| 126 | + |
| 127 | + |
| 128 | +def test_regex_check_use_identifier_placeholder() -> None: |
| 129 | + """RegexMatchCheck and MatchRateCheck use identifier_placeholder for naming-only identifier filters.""" |
| 130 | + regex_chk = RegexMatchCheck( |
| 131 | + database_accessor="", |
| 132 | + database_provider=None, |
| 133 | + table="t", |
| 134 | + check_column="col", |
| 135 | + regex_to_match="^a", |
| 136 | + filters={"shop_id": {"type": "identifier"}}, |
| 137 | + identifier_format="filter_name", |
| 138 | + identifier_placeholder="PH7", |
| 139 | + ) |
| 140 | + assert regex_chk.identifier == "PH7" |
| 141 | + assert regex_chk.identifier_column == "SHOP_ID" |
| 142 | + |
| 143 | + |
| 144 | +def test_matchrate_check_uses_identifier_placeholder() -> None: |
| 145 | + """MatchRateCheck uses identifier_placeholder for naming-only identifier filters.""" |
| 146 | + # MatchRateCheck |
| 147 | + mr_chk = MatchRateCheck( |
| 148 | + database_accessor="", |
| 149 | + database_provider=None, |
| 150 | + left_table="left", |
| 151 | + right_table="right", |
| 152 | + check_column="col", |
| 153 | + join_columns=["id"], |
| 154 | + filters={"shop_id": {"type": "identifier"}}, |
| 155 | + identifier_format="filter_name", |
| 156 | + identifier_placeholder="PH8", |
| 157 | + ) |
| 158 | + assert mr_chk.identifier == "PH8" |
| 159 | + assert mr_chk.identifier_column == "SHOP_ID" |
| 160 | + |
| 161 | + |
| 162 | +# CountCheck, DuplicateCheck, NullRatioCheck, |
| 163 | +def test_count_check_uses_identifier_placeholder() -> None: |
| 164 | + """CountCheck uses identifier_placeholder for naming-only identifier filters.""" |
| 165 | + chk = CountCheck( |
| 166 | + database_accessor="", |
| 167 | + database_provider=None, |
| 168 | + table="t", |
| 169 | + check_column="col", |
| 170 | + filters={"shop_id": {"type": "identifier"}}, |
| 171 | + identifier_format="filter_name", |
| 172 | + identifier_placeholder="PH9", |
| 173 | + ) |
| 174 | + assert chk.identifier == "PH9" |
| 175 | + assert chk.identifier_column == "SHOP_ID" |
| 176 | + |
| 177 | + |
| 178 | +def test_duplicate_check_uses_identifier_placeholder() -> None: |
| 179 | + """DuplicateCheck uses identifier_placeholder for naming-only identifier filters.""" |
| 180 | + chk = DuplicateCheck( |
| 181 | + database_accessor="", |
| 182 | + database_provider=None, |
| 183 | + table="t", |
| 184 | + check_column="col", |
| 185 | + filters={"shop_id": {"type": "identifier"}}, |
| 186 | + identifier_format="filter_name", |
| 187 | + identifier_placeholder="PH10", |
| 188 | + ) |
| 189 | + assert chk.identifier == "PH10" |
| 190 | + assert chk.identifier_column == "SHOP_ID" |
| 191 | + |
| 192 | + |
| 193 | +def test_null_ratio_check_uses_identifier_placeholder() -> None: |
| 194 | + """NullRatioCheck uses identifier_placeholder for naming-only identifier filters.""" |
| 195 | + chk = NullRatioCheck( |
| 196 | + database_accessor="", |
| 197 | + database_provider=None, |
| 198 | + table="t", |
| 199 | + check_column="col", |
| 200 | + filters={"shop_id": {"type": "identifier"}}, |
| 201 | + identifier_format="filter_name", |
| 202 | + identifier_placeholder="PH11", |
| 203 | + ) |
| 204 | + assert chk.identifier == "PH11" |
| 205 | + assert chk.identifier_column == "SHOP_ID" |
0 commit comments