fix(backend): spec-bug fixes for T10 — Match.claim_id non-unique + reconcile test dates

Three spec-bug fixes from T10 implementation:

- Match.claim_id: drop UNIQUE constraint (T4 schema error) — reversals add a 2nd row per claim (audit trail). Add explicit non-unique index ix_matches_claim_id to preserve query performance. Mirrored in ORM and migration 0001_initial.sql.
- test_run_orphan_remit_leaves_claim_unmatched: claim PCN-A does not match remit PCN-NEW, so unmatched_claims must be 1, not 0.
- test_run_reversal_flips_paid_to_reversed: claim service_date_from was 9 days before reversal remit service_date (outside default 7-day window) so no match occurred; changed to 5 days apart so match() picks the claim.
- test_match_unique_per_claim: in test_db_models.py asserted the now-removed UNIQUE behavior; renamed and inverted to assert the audit-trail design (two Match rows per claim allowed).
This commit is contained in:
Tyler
2026-06-19 22:59:20 -06:00
parent 8c92ef43bd
commit 6a65c2b750
4 changed files with 29 additions and 12 deletions
+13 -8
View File
@@ -336,9 +336,12 @@ def test_create_match_and_activity_event():
assert events[0].payload_json == {"matched": 1}
def test_match_unique_per_claim():
"""A Claim can only have one current Match row."""
import sqlalchemy.exc
def test_match_multiple_rows_per_claim():
"""Spec-bug fix (T10): a Claim can have multiple Match rows
(original payment + reversal = audit trail). UNIQUE on claim_id
was removed; the constraint test that previously lived here
asserted the old, incorrect design.
"""
with db.SessionLocal()() as s:
s.add(Batch(
id="b1", kind="835", input_filename="x",
@@ -352,10 +355,12 @@ def test_match_unique_per_claim():
status_code="1", received_at=datetime.now(timezone.utc)))
s.add(Match(claim_id="CLM-1", remittance_id="CLP-1", strategy="auto",
matched_at=datetime.now(timezone.utc)))
s.add(Match(claim_id="CLM-1", remittance_id="CLP-2", strategy="manual",
matched_at=datetime.now(timezone.utc),
is_reversal=True, prior_claim_state=ClaimState.PAID))
# No IntegrityError — two Match rows per claim are now allowed.
s.commit()
s.add(Match(claim_id="CLM-1", remittance_id="CLP-2", strategy="manual",
matched_at=datetime.now(timezone.utc)))
with pytest.raises(sqlalchemy.exc.IntegrityError):
s.commit()
s.rollback()
rows = s.query(Match).filter(Match.claim_id == "CLM-1").all()
assert len(rows) == 2
assert {r.remittance_id for r in rows} == {"CLP-1", "CLP-2"}