feat(backend): add Match + ActivityEvent ORM models
This commit is contained in:
@@ -10,7 +10,7 @@ import pytest
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.db import Base, Batch, CasAdjustment, Claim, ClaimState, Remittance
|
||||
from cyclone.db import ActivityEvent, Base, Batch, CasAdjustment, Claim, ClaimState, Match, Remittance
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -293,3 +293,69 @@ def test_remittance_cascade_delete_drops_cas_adjustments():
|
||||
select(CasAdjustment).where(CasAdjustment.remittance_id == "CLP-1")
|
||||
).all()
|
||||
assert len(cas) == 0
|
||||
|
||||
|
||||
def test_create_match_and_activity_event():
|
||||
with db.SessionLocal()() as s:
|
||||
s.add(Batch(
|
||||
id="b1", kind="835", input_filename="x",
|
||||
parsed_at=datetime(2026, 6, 19, tzinfo=timezone.utc),
|
||||
))
|
||||
s.add(Claim(
|
||||
id="CLM-1", batch_id="b1", patient_control_number="CLM-1",
|
||||
charge_amount=Decimal("124.00"), state=ClaimState.SUBMITTED,
|
||||
))
|
||||
s.add(Remittance(
|
||||
id="CLP-1", batch_id="b1", payer_claim_control_number="CLM-1",
|
||||
status_code="1", total_charge=Decimal("124.00"), total_paid=Decimal("124.00"),
|
||||
received_at=datetime(2026, 6, 19, tzinfo=timezone.utc),
|
||||
))
|
||||
m = Match(
|
||||
claim_id="CLM-1", remittance_id="CLP-1",
|
||||
strategy="auto", is_reversal=False,
|
||||
matched_at=datetime(2026, 6, 19, 13, 0, tzinfo=timezone.utc),
|
||||
)
|
||||
s.add(m)
|
||||
s.flush()
|
||||
s.add(ActivityEvent(
|
||||
ts=datetime(2026, 6, 19, 13, 0, tzinfo=timezone.utc),
|
||||
kind="reconcile", batch_id="b1", claim_id="CLM-1",
|
||||
payload_json={"matched": 1},
|
||||
))
|
||||
s.commit()
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
loaded = s.get(Match, 1) # autoincrement id
|
||||
assert loaded is not None
|
||||
assert loaded.strategy == "auto"
|
||||
assert loaded.is_reversal is False
|
||||
|
||||
events = s.query(ActivityEvent).all()
|
||||
assert len(events) == 1
|
||||
assert events[0].kind == "reconcile"
|
||||
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
|
||||
with db.SessionLocal()() as s:
|
||||
s.add(Batch(
|
||||
id="b1", kind="835", input_filename="x",
|
||||
parsed_at=datetime(2026, 6, 19, tzinfo=timezone.utc),
|
||||
))
|
||||
s.add(Claim(id="CLM-1", batch_id="b1", patient_control_number="CLM-1",
|
||||
charge_amount=Decimal("0"), state=ClaimState.SUBMITTED))
|
||||
s.add(Remittance(id="CLP-1", batch_id="b1", payer_claim_control_number="x",
|
||||
status_code="1", received_at=datetime.now(timezone.utc)))
|
||||
s.add(Remittance(id="CLP-2", batch_id="b1", payer_claim_control_number="y",
|
||||
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.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()
|
||||
|
||||
Reference in New Issue
Block a user