534130ee2b
Migration 0014 changes the PRIMARY KEYs of claims and remittances from single-column id to composite (batch_id, id). Enables the spec'd cross-batch CLM01 / CLP01 collision workflow: same CLM01 in multiple batches is now representable (resubmits), pre-flight dedup 409 path is genuinely exercisable, force-insert can skip pre-existing duplicates. Strategy: PRAGMA defer_foreign_keys = ON + table recreation for every table whose FKs pointed at the old single-column PK. Tables recreated: remittances, claims, matches, cas_adjustments, service_line_payments, line_reconciliations. Each child table gains a batch_id (or remittance_batch_id) column for the composite FK side; INSERT INTO new SELECT FROM old JOINs populate it from the already-recreated parent. Cross-table FKs (remittances.claim_id, claims.matched_remittance_id) cannot be SQL-enforced with composite PKs (SQLite has no ALTER CONSTRAINT). Dropped at SQL level; enforced via application-layer invariants in store.manual_match / manual_unmatch / reconcile.run and the dedup.preflight_* helpers. ORM updates (db.py): - Claim / Remittance: composite PK via explicit PrimaryKeyConstraint in __table_args__ (column order matches SQL: batch_id, id). - New Claim.matched_remittance_batch_id column. - Match / CasAdjustment / ServiceLinePayment / LineReconciliation: added the batch side of their composite FK to the parent table. - SQLAlchemy before_insert events auto-populate the batch side of the composite FK from session.new, then identity_map, then SQL fallback. Production code updates: - store.manual_match / manual_unmatch: also write matched_remittance_batch_id on the claim (was missing). - api.py manual-match endpoint: same fix. - reconcile.run: same fix for auto-matched pairs. Test updates: replaced s.get(Claim, X) with the composite key (batch_id, id) where batch_id is known, or s.query().filter().first() where the test only knows the id. Tests that previously inserted a Match row pointing at a non-existent Remittance now seed the parent Remittance so the new NOT NULL composite FK is satisfied.
147 lines
5.0 KiB
Python
147 lines
5.0 KiB
Python
"""TDD: compute_lanes — four Inbox lanes from the DB on read.
|
|
|
|
Plan: docs/superpowers/plans/2026-06-20-cyclone-workflow-automation.md
|
|
Task 6 (T6).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
|
|
from cyclone import db
|
|
from cyclone.db import (
|
|
Base, Batch, Claim, ClaimState, Remittance, init_db,
|
|
)
|
|
from cyclone.inbox_lanes import compute_lanes
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _setup(tmp_path, monkeypatch):
|
|
monkeypatch.setenv("CYCLONE_DB_URL", f"sqlite:///{tmp_path}/lanes.db")
|
|
db._reset_for_tests()
|
|
db.init_db()
|
|
yield
|
|
|
|
|
|
def _add_batch() -> None:
|
|
with db.SessionLocal()() as s:
|
|
if s.get(Batch, "B-1") is not None:
|
|
return
|
|
s.add(Batch(
|
|
id="B-1", kind="837p", input_filename="x.txt",
|
|
parsed_at=datetime.now(timezone.utc),
|
|
totals_json={"total_claims": 1},
|
|
validation_json={"passed": True, "warnings": [], "errors": []},
|
|
raw_result_json={"_": "stub"},
|
|
))
|
|
s.commit()
|
|
|
|
|
|
def _add_claim(*, claim_id: str, pcn: str = "PCN",
|
|
state: ClaimState = ClaimState.SUBMITTED,
|
|
charge: Decimal = Decimal("100.00"),
|
|
provider_npi: str | None = "1234567890",
|
|
payer_id: str | None = "PAYER-A",
|
|
service_date=None) -> None:
|
|
_add_batch()
|
|
with db.SessionLocal()() as s:
|
|
s.add(Claim(
|
|
id=claim_id, batch_id="B-1",
|
|
patient_control_number=pcn, state=state,
|
|
charge_amount=charge, provider_npi=provider_npi,
|
|
payer_id=payer_id, service_date_from=service_date,
|
|
))
|
|
s.commit()
|
|
|
|
|
|
def _add_remit(*, remit_id: str, pcn: str = "PCN",
|
|
charge: Decimal = Decimal("100.00"),
|
|
claim_id: str | None = None,
|
|
payer_id: str | None = "PAYER-A",
|
|
service_date=None) -> None:
|
|
_add_batch()
|
|
with db.SessionLocal()() as s:
|
|
r = Remittance(
|
|
id=remit_id, batch_id="B-1",
|
|
payer_claim_control_number=pcn,
|
|
claim_id=claim_id,
|
|
status_code="1", status_label="Processed",
|
|
total_charge=charge,
|
|
total_paid=Decimal("0"),
|
|
received_at=datetime.now(timezone.utc),
|
|
service_date=service_date,
|
|
)
|
|
# Stash rendering_provider_npi and payer_id in raw_json for the
|
|
# 835 fields that aren't on the ORM model.
|
|
r.raw_json = {
|
|
"rendering_provider_npi": "1234567890",
|
|
"payer_id": payer_id,
|
|
"patient_control_number": pcn,
|
|
}
|
|
s.add(r)
|
|
s.commit()
|
|
|
|
|
|
def test_rejected_lane_includes_rejected_claims():
|
|
_add_claim(claim_id="C1", state=ClaimState.REJECTED)
|
|
with db.SessionLocal()() as s:
|
|
lanes = compute_lanes(s, dismissed_pairs=set())
|
|
assert "C1" in {r["id"] for r in lanes.rejected}
|
|
|
|
|
|
def test_unmatched_lane_includes_submitted_claims_with_no_remit():
|
|
_add_claim(claim_id="C1", state=ClaimState.SUBMITTED)
|
|
with db.SessionLocal()() as s:
|
|
lanes = compute_lanes(s, dismissed_pairs=set())
|
|
assert "C1" in {r["id"] for r in lanes.unmatched}
|
|
|
|
|
|
def test_unmatched_remit_appears_with_candidates():
|
|
_add_claim(claim_id="C1", pcn="PCN-1", service_date=None)
|
|
_add_remit(remit_id="R1", pcn="PCN-1") # exact match → candidate
|
|
with db.SessionLocal()() as s:
|
|
lanes = compute_lanes(s, dismissed_pairs=set())
|
|
assert any(r["id"] == "R1" for r in lanes.candidates)
|
|
|
|
|
|
def test_candidate_below_threshold_is_hidden():
|
|
_add_claim(claim_id="C1", pcn="A", charge=Decimal("100.00"))
|
|
_add_remit(remit_id="R1", pcn="Z",
|
|
charge=Decimal("999.00")) # everything mismatched
|
|
with db.SessionLocal()() as s:
|
|
lanes = compute_lanes(s, dismissed_pairs=set())
|
|
assert not any(r["id"] == "R1" for r in lanes.candidates)
|
|
|
|
|
|
def test_dismissed_pair_excluded_from_candidates():
|
|
_add_claim(claim_id="C1", pcn="PCN-1")
|
|
_add_remit(remit_id="R1", pcn="PCN-1")
|
|
with db.SessionLocal()() as s:
|
|
lanes = compute_lanes(
|
|
s, dismissed_pairs={frozenset({"C1", "R1"})},
|
|
)
|
|
assert not any(r["id"] == "R1" for r in lanes.candidates)
|
|
|
|
|
|
def test_done_today_includes_recent_terminal_states():
|
|
_add_claim(claim_id="C1", state=ClaimState.PAID)
|
|
_add_claim(claim_id="C2", state=ClaimState.PAID)
|
|
# Force C1's state_changed_at to be recent, C2 to be old.
|
|
now = datetime.now(timezone.utc)
|
|
with db.SessionLocal()() as s:
|
|
# Migration 0014: composite PK — filter-by-id returns the single
|
|
# claim with that id (no cross-batch duplicates in this test).
|
|
c1 = s.query(Claim).filter(Claim.id == "C1").first()
|
|
c1.state_changed_at = now - timedelta(hours=2)
|
|
c2 = s.query(Claim).filter(Claim.id == "C2").first()
|
|
c2.state_changed_at = now - timedelta(hours=30)
|
|
s.commit()
|
|
|
|
with db.SessionLocal()() as s:
|
|
lanes = compute_lanes(s, dismissed_pairs=set())
|
|
ids = {r["id"] for r in lanes.done_today}
|
|
assert "C1" in ids
|
|
assert "C2" not in ids
|