|
| 1 | +"""The frame fingerprint behind drift refusal samples the HEAD, not the frame. |
| 2 | +
|
| 3 | +``docs/repair-plans.md`` describes ``FrameSignature`` as "row count, column |
| 4 | +names+dtypes, content sample". That wording is accurate but incomplete in a way |
| 5 | +that matters: the sample is the first ``_SIGNATURE_SAMPLE_ROWS`` (512) rows, so |
| 6 | +a change *after* row 512 that preserves the row count, the column names and the |
| 7 | +dtypes does not trip ``PlanDriftError``. |
| 8 | +
|
| 9 | +That is a deliberate design choice -- the fingerprint is documented as "cheap" |
| 10 | +and is a guard against applying a plan to the *wrong data*, not a proof the |
| 11 | +data is unchanged. These tests pin the boundary so the documented claim is |
| 12 | +backed by execution rather than prose, and so a future change to the sampling |
| 13 | +strategy has to come past a failing test. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import pandas as pd |
| 19 | +import pytest |
| 20 | + |
| 21 | +import freshdata as fd |
| 22 | +from freshdata.repairplan import _SIGNATURE_SAMPLE_ROWS, compute_frame_signature |
| 23 | + |
| 24 | + |
| 25 | +def _frame(n: int) -> pd.DataFrame: |
| 26 | + # A tie-free majority so the plan carries a stable semantic action. |
| 27 | + lower = max(1, n // 4) |
| 28 | + return pd.DataFrame({"country": ["USA"] * (n - lower) + ["usa"] * lower}) |
| 29 | + |
| 30 | + |
| 31 | +def _plan(df: pd.DataFrame): |
| 32 | + return fd.suggest_plan(df, semantic_mode="review") |
| 33 | + |
| 34 | + |
| 35 | +def test_the_documented_sample_size_is_the_one_the_code_uses(): |
| 36 | + assert _SIGNATURE_SAMPLE_ROWS == 512 |
| 37 | + |
| 38 | + |
| 39 | +def test_a_change_inside_the_head_sample_is_refused(): |
| 40 | + df = _frame(1000) |
| 41 | + plan = _plan(df) |
| 42 | + drifted = df.copy() |
| 43 | + drifted.loc[10, "country"] = "COMPLETELY-DIFFERENT" |
| 44 | + with pytest.raises(fd.PlanDriftError): |
| 45 | + fd.apply_plan(drifted, plan) |
| 46 | + |
| 47 | + |
| 48 | +def test_a_change_beyond_the_head_sample_is_not_detected(): |
| 49 | + """Documented limitation, pinned deliberately -- not an endorsement.""" |
| 50 | + df = _frame(1000) |
| 51 | + plan = _plan(df) |
| 52 | + drifted = df.copy() |
| 53 | + drifted.loc[900, "country"] = "COMPLETELY-DIFFERENT" |
| 54 | + fd.apply_plan(drifted, plan) # no PlanDriftError |
| 55 | + |
| 56 | + |
| 57 | +def test_the_boundary_sits_exactly_at_the_sample_size(): |
| 58 | + df = _frame(_SIGNATURE_SAMPLE_ROWS + 10) |
| 59 | + base = compute_frame_signature(df) |
| 60 | + |
| 61 | + last_seen = df.copy() |
| 62 | + last_seen.loc[_SIGNATURE_SAMPLE_ROWS - 1, "country"] = "CHANGED" |
| 63 | + assert compute_frame_signature(last_seen).sample_hash != base.sample_hash |
| 64 | + |
| 65 | + first_unseen = df.copy() |
| 66 | + first_unseen.loc[_SIGNATURE_SAMPLE_ROWS, "country"] = "CHANGED" |
| 67 | + assert compute_frame_signature(first_unseen).sample_hash == base.sample_hash |
| 68 | + |
| 69 | + |
| 70 | +def test_row_count_and_column_changes_are_still_caught_beyond_the_sample(): |
| 71 | + """The sample is only one of three components; the other two still apply.""" |
| 72 | + df = _frame(_SIGNATURE_SAMPLE_ROWS + 10) |
| 73 | + base = compute_frame_signature(df) |
| 74 | + |
| 75 | + assert compute_frame_signature(df.iloc[:-1]).n_rows != base.n_rows |
| 76 | + |
| 77 | + renamed = df.rename(columns={"country": "nation"}) |
| 78 | + assert compute_frame_signature(renamed).columns_hash != base.columns_hash |
0 commit comments