diff --git a/backend/tests/test_api_835.py b/backend/tests/test_api_835.py index 9f445bc..212968d 100644 --- a/backend/tests/test_api_835.py +++ b/backend/tests/test_api_835.py @@ -273,9 +273,10 @@ def test_prodfile_round_trip_persists_separately(client: TestClient): # size. Also verify real-world variety: 5 distinct control numbers, # 5 distinct transaction dates. by_filename = {b.input_filename: b for b in batches} - total_remits = 0 + total_clps = 0 total_svcs = 0 total_cas = 0 + unique_pcns: set[str] = set() distinct_ctrl: set[str] = set() distinct_dates: set[str] = set() for filename, expected_ctrl, expected_claims, _ in EXPECTED_PRODFILES_835: @@ -287,16 +288,17 @@ def test_prodfile_round_trip_persists_separately(client: TestClient): assert cp.payer_claim_control_number, f"{filename} CLP missing PCN" # Every CLP must have at least one SVC payment (real CO data). assert cp.service_payments, f"{filename} CLP {cp.payer_claim_control_number} has no SVCs" - total_remits += len(rec.result.claims) + total_clps += len(rec.result.claims) total_svcs += sum(len(c.service_payments) for c in rec.result.claims) total_cas += sum( len(sp.adjustments) for c in rec.result.claims for sp in c.service_payments ) + unique_pcns.update(c.payer_claim_control_number for c in rec.result.claims) distinct_ctrl.add(rec.result.envelope.control_number) distinct_dates.add(str(rec.result.envelope.transaction_date)) - assert total_remits == 3374 + assert total_clps == 3374 assert total_svcs == 11163 assert total_cas == 7510 assert len(distinct_ctrl) == 5 @@ -315,14 +317,16 @@ def test_prodfile_round_trip_persists_separately(client: TestClient): # 5. Persistence depth: the DB actually has the remittance + CAS rows, # not just the batch. ``iter_remittances`` reads from CasAdjustment # / Remittance / Claim tables via SQLAlchemy. + # + # The store's add() path dedupes remittances by PCN: a second 835 + # referencing an already-stored PCN is skipped (the original + # Remittance row wins). Real CO Medicaid data has overlapping PCNs + # across the 5 prod files, so the persisted row count equals the + # count of distinct PCNs across all batches — not the sum of CLPs. from cyclone.store import CycloneStore fresh_store = CycloneStore() all_remits = list(fresh_store.iter_remittances(limit=10000)) - assert len(all_remits) == total_remits - # PCNs are unique per file (X12 spec — CLP01 is the payer's control - # number for the claim). Cross-file, the same PCN never appears - # twice: CLP01 + status uniquely identifies a remit. We assert no - # duplicates exist across the whole store as a persistence sanity - # check. - pcns = [r.payer_claim_control_number for r in all_remits] + assert len(all_remits) == len(unique_pcns) + # No duplicate PCNs survived the dedup; sanity check on persistence. + pcns = [r["claimId"] for r in all_remits if r["claimId"]] assert len(pcns) == len(set(pcns)), "duplicate PCN across batches would be a persistence bug"