|
1 | | -"""Traffic and non-traffic rows are kept apart on read. |
| 1 | +"""Traffic and non-traffic rows are kept apart on read, and merged differently on write. |
2 | 2 |
|
3 | 3 | `observed_shape.source` says which mechanism produced a row. Two consumers read the table as |
4 | 4 | traffic -- `ObservedDriftDetector` and the baseline the mock builder is handed -- and until this |
@@ -149,6 +149,98 @@ def test_no_source_is_both_traffic_and_synthetic(): |
149 | 149 | assert TRAFFIC_SOURCES & SYNTHETIC_SOURCES == frozenset() |
150 | 150 |
|
151 | 151 |
|
| 152 | +# --- what the store does with a row it already holds --------------------------------- |
| 153 | +# |
| 154 | +# The same partition, applied to the conflict clause. `sample_count` is the one column in that |
| 155 | +# clause whose merge is not idempotent, and idempotence is what `CLAUDE.md` requires of a stage |
| 156 | +# re-run on the same input. For traffic the addition is right and is the reason the column |
| 157 | +# exists: two error payloads carrying one shape are two samples. For a synthetic source the |
| 158 | +# second write is the same constructed body again, so adding would count how often Sync ran. |
| 159 | +# |
| 160 | +# Both directions are parametrised over the sets rather than over the values written here, so a |
| 161 | +# fourth source added to `sync.graph.sources` arrives with a merge assertion instead of taking |
| 162 | +# whichever branch it happens to fall into. |
| 163 | + |
| 164 | + |
| 165 | +@pytest.mark.parametrize("source", sorted(TRAFFIC_SOURCES)) |
| 166 | +def test_a_traffic_row_written_again_counts_again(store: GraphStore, source: str): |
| 167 | + """The counterpart that keeps the synthetic assertion below from reading as a frozen |
| 168 | + counter. A clause that stopped counting for every source would satisfy that test and |
| 169 | + destroy the sample floor, which is the failure mode this pair exists to separate. |
| 170 | + """ |
| 171 | + for _ in range(3): |
| 172 | + store.record_observed_shape(_shape(source=source)) |
| 173 | + |
| 174 | + rows = store.observed_shapes("stripe", "PostCharges", traffic_only=False) |
| 175 | + assert [row.sample_count for row in rows] == [3] |
| 176 | + |
| 177 | + |
| 178 | +@pytest.mark.parametrize("source", sorted(SYNTHETIC_SOURCES)) |
| 179 | +def test_a_synthetic_row_written_again_does_not_count_again(store: GraphStore, source: str): |
| 180 | + """A synthetic row is a body Sync constructed, so writing it a second time is the ingest |
| 181 | + running a second time and not the shape being seen a second time. The count is one sample |
| 182 | + however many times the write happens. |
| 183 | + """ |
| 184 | + for _ in range(3): |
| 185 | + store.record_observed_shape(_shape(source=source)) |
| 186 | + |
| 187 | + rows = store.observed_shapes("stripe", "PostCharges", traffic_only=False) |
| 188 | + assert [row.sample_count for row in rows] == [1] |
| 189 | + |
| 190 | + |
| 191 | +def test_a_synthetic_count_written_before_this_clause_is_not_rewritten(store: GraphStore): |
| 192 | + """Rows already in a database were written under a clause that added, so some hold counts |
| 193 | + above one. Taking the incoming value would rewrite that history on the next write, which is |
| 194 | + a migration performed silently by a merge rather than a merge holding a counter still. |
| 195 | + """ |
| 196 | + store.record_observed_shape(_shape(source="replay", sample_count=5)) |
| 197 | + store.record_observed_shape(_shape(source="replay", sample_count=1)) |
| 198 | + |
| 199 | + rows = store.observed_shapes("stripe", "PostCharges", traffic_only=False) |
| 200 | + assert [row.sample_count for row in rows] == [5] |
| 201 | + |
| 202 | + |
| 203 | +def test_a_synthetic_rows_count_does_not_depend_on_arrival_order(store: GraphStore): |
| 204 | + """Every other column in this clause merges the same way whichever write lands first -- |
| 205 | + `LEAST`, `GREATEST`, `OR`, a union -- because sources do not arrive in order. Keeping |
| 206 | + whatever the row already held would make the counter the one column that reads the |
| 207 | + sequence, which is the property `test_an_observation_arriving_out_of_order_does_not_rewind |
| 208 | + _the_window` already refuses for the timestamps. |
| 209 | + """ |
| 210 | + store.record_observed_shape(_shape(source="replay", field_path="/a", sample_count=5)) |
| 211 | + store.record_observed_shape(_shape(source="replay", field_path="/a", sample_count=1)) |
| 212 | + store.record_observed_shape(_shape(source="replay", field_path="/b", sample_count=1)) |
| 213 | + store.record_observed_shape(_shape(source="replay", field_path="/b", sample_count=5)) |
| 214 | + |
| 215 | + rows = store.observed_shapes("stripe", "PostCharges", traffic_only=False) |
| 216 | + assert {row.field_path: row.sample_count for row in rows} == {"/a": 5, "/b": 5} |
| 217 | + |
| 218 | + |
| 219 | +def test_a_synthetic_row_written_again_still_gains_evidence_and_widens_its_window( |
| 220 | + store: GraphStore, |
| 221 | +): |
| 222 | + """Only the counter is held. `DO NOTHING` for synthetic sources would converge too, and |
| 223 | + would throw away the rest of the merge: a later write proving the field can be null, or an |
| 224 | + enum member an earlier specification did not name, or the window this row covers. The row |
| 225 | + still records that the shape was seen and when -- it stops recording how many times the |
| 226 | + write happened. |
| 227 | + """ |
| 228 | + later = NOW + timedelta(days=1) |
| 229 | + store.record_observed_shape(_shape(source="replay", nullable_seen=False)) |
| 230 | + store.record_observed_shape( |
| 231 | + _shape( |
| 232 | + source="replay", nullable_seen=True, spec_enum_values=["succeeded"], |
| 233 | + first_seen=EARLIER, last_seen=later, |
| 234 | + ) |
| 235 | + ) |
| 236 | + |
| 237 | + row = store.observed_shapes("stripe", "PostCharges", traffic_only=False)[0] |
| 238 | + assert row.sample_count == 1 |
| 239 | + assert row.nullable_seen is True |
| 240 | + assert row.spec_enum_values == ["succeeded"] |
| 241 | + assert (row.first_seen, row.last_seen) == (EARLIER, later) |
| 242 | + |
| 243 | + |
152 | 244 | # --- the two consumers, end to end -------------------------------------------------- |
153 | 245 |
|
154 | 246 |
|
@@ -287,6 +379,24 @@ def test_rows_written_before_the_filter_existed_survive_a_second_apply_schema(st |
287 | 379 | ] |
288 | 380 |
|
289 | 381 |
|
| 382 | +def test_a_held_synthetic_count_survives_a_second_apply_schema_and_a_further_write( |
| 383 | + store: GraphStore, |
| 384 | +): |
| 385 | + """The same assertion for the clause rather than for the column list, because the clause is |
| 386 | + what this task changed. A `replay` row at 5 was written under a clause that added, and it |
| 387 | + has to come through both a re-applied schema and a further write intact -- neither reset to |
| 388 | + 1 nor advanced to 6. |
| 389 | + """ |
| 390 | + store.record_observed_shape(_shape(source="replay", sample_count=5)) |
| 391 | + |
| 392 | + store.apply_schema() |
| 393 | + store.apply_schema() |
| 394 | + store.record_observed_shape(_shape(source="replay")) |
| 395 | + |
| 396 | + rows = store.observed_shapes("stripe", "PostCharges", traffic_only=False) |
| 397 | + assert [row.sample_count for row in rows] == [5] |
| 398 | + |
| 399 | + |
290 | 400 | # --- the defect this task did not fix, fixed by M3-W122 ----------------------------- |
291 | 401 |
|
292 | 402 |
|
|
0 commit comments