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.
171 lines
5.7 KiB
Python
171 lines
5.7 KiB
Python
"""SP14 — Payer-Rejected acknowledge endpoint tests.
|
|
|
|
The endpoint marks payer-rejected claims as acknowledged so the
|
|
working-surface lane query filters them out. The original
|
|
payer_rejected_* fields stay intact (SP11 audit).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from cyclone import db
|
|
from cyclone.db import Claim, ClaimState
|
|
from cyclone.audit_log import verify_chain
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _setup(tmp_path, monkeypatch):
|
|
monkeypatch.setenv("CYCLONE_DB_URL", f"sqlite:///{tmp_path}/test.db")
|
|
db._reset_for_tests()
|
|
db.init_db()
|
|
|
|
|
|
def _make_claim(claim_id: str, *, payer_rejected: bool = True) -> None:
|
|
"""Insert a minimal claim with optional payer_rejected markers."""
|
|
from datetime import datetime, timezone
|
|
from cyclone.db import Batch
|
|
with db.SessionLocal()() as session:
|
|
batch = Batch(
|
|
id=f"b-{claim_id}",
|
|
kind="837p",
|
|
input_filename="t.x12",
|
|
parsed_at=datetime.now(timezone.utc),
|
|
)
|
|
session.add(batch)
|
|
session.flush()
|
|
claim = Claim(
|
|
id=claim_id,
|
|
batch_id=batch.id,
|
|
patient_control_number=claim_id,
|
|
state=ClaimState.SUBMITTED,
|
|
charge_amount=100,
|
|
)
|
|
if payer_rejected:
|
|
now = datetime.now(timezone.utc)
|
|
claim.payer_rejected_at = now
|
|
claim.payer_rejected_reason = "invalid diagnosis code"
|
|
claim.payer_rejected_status_code = "A7"
|
|
session.add(claim)
|
|
session.commit()
|
|
|
|
|
|
def test_acknowledge_marks_unacknowledged_claim():
|
|
_make_claim("C1", payer_rejected=True)
|
|
from fastapi.testclient import TestClient
|
|
from cyclone.api import app
|
|
with TestClient(app) as client:
|
|
r = client.post(
|
|
"/api/inbox/payer-rejected/acknowledge",
|
|
json={"claim_ids": ["C1"], "actor": "operator"},
|
|
)
|
|
assert r.status_code == 200, r.text
|
|
body = r.json()
|
|
assert body["ok"] is True
|
|
assert body["transitioned"] == 1
|
|
assert body["already_acked"] == 0
|
|
assert body["not_found"] == 0
|
|
assert body["not_rejected"] == 0
|
|
|
|
with db.SessionLocal()() as session:
|
|
# Migration 0014: composite PK — filter-by-id returns the single
|
|
# claim with this id (no cross-batch duplicates in this fixture).
|
|
c = session.query(Claim).filter(Claim.id == "C1").first()
|
|
assert c.payer_rejected_acknowledged_at is not None
|
|
assert c.payer_rejected_acknowledged_actor == "operator"
|
|
# Original fields stay intact for audit.
|
|
assert c.payer_rejected_at is not None
|
|
assert c.payer_rejected_status_code == "A7"
|
|
assert c.payer_rejected_reason == "invalid diagnosis code"
|
|
|
|
|
|
def test_acknowledge_is_idempotent():
|
|
_make_claim("C1", payer_rejected=True)
|
|
from fastapi.testclient import TestClient
|
|
from cyclone.api import app
|
|
with TestClient(app) as client:
|
|
r1 = client.post(
|
|
"/api/inbox/payer-rejected/acknowledge",
|
|
json={"claim_ids": ["C1"]},
|
|
)
|
|
assert r1.json()["transitioned"] == 1
|
|
r2 = client.post(
|
|
"/api/inbox/payer-rejected/acknowledge",
|
|
json={"claim_ids": ["C1"]},
|
|
)
|
|
assert r2.json()["transitioned"] == 0
|
|
assert r2.json()["already_acked"] == 1
|
|
|
|
|
|
def test_acknowledge_skips_non_payer_rejected_claims():
|
|
_make_claim("C1", payer_rejected=False)
|
|
from fastapi.testclient import TestClient
|
|
from cyclone.api import app
|
|
with TestClient(app) as client:
|
|
r = client.post(
|
|
"/api/inbox/payer-rejected/acknowledge",
|
|
json={"claim_ids": ["C1"]},
|
|
)
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["transitioned"] == 0
|
|
assert body["not_rejected"] == 1
|
|
|
|
|
|
def test_acknowledge_counts_missing_ids():
|
|
from fastapi.testclient import TestClient
|
|
from cyclone.api import app
|
|
with TestClient(app) as client:
|
|
r = client.post(
|
|
"/api/inbox/payer-rejected/acknowledge",
|
|
json={"claim_ids": ["nope"]},
|
|
)
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["transitioned"] == 0
|
|
assert body["not_found"] == 1
|
|
|
|
|
|
def test_acknowledge_empty_claim_ids_400():
|
|
from fastapi.testclient import TestClient
|
|
from cyclone.api import app
|
|
with TestClient(app) as client:
|
|
r = client.post(
|
|
"/api/inbox/payer-rejected/acknowledge",
|
|
json={"claim_ids": []},
|
|
)
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_acknowledge_writes_audit_event():
|
|
_make_claim("C1", payer_rejected=True)
|
|
from fastapi.testclient import TestClient
|
|
from cyclone.api import app
|
|
with TestClient(app) as client:
|
|
r = client.post(
|
|
"/api/inbox/payer-rejected/acknowledge",
|
|
json={"claim_ids": ["C1"], "actor": "alice"},
|
|
)
|
|
assert r.status_code == 200
|
|
|
|
# SP11: the chain is intact and includes the new event.
|
|
with db.SessionLocal()() as session:
|
|
result = verify_chain(session)
|
|
assert result.ok, f"chain broken: {result.reason} at {result.first_bad_id}"
|
|
|
|
# And the event itself is queryable.
|
|
import json as _json
|
|
from cyclone.db import AuditLog
|
|
with db.SessionLocal()() as session:
|
|
events = (
|
|
session.query(AuditLog)
|
|
.filter(AuditLog.event_type == "claim.payer_rejected_acknowledged")
|
|
.all()
|
|
)
|
|
assert len(events) == 1
|
|
e = events[0]
|
|
assert e.entity_type == "claim"
|
|
assert e.entity_id == "C1"
|
|
assert e.actor == "alice"
|
|
payload = _json.loads(e.payload_json) if e.payload_json else {}
|
|
assert payload["payer_rejected_status_code"] == "A7"
|