128 lines
3.7 KiB
Python
128 lines
3.7 KiB
Python
"""Tests for the 6 new GET endpoints."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from cyclone.api import app
|
|
from cyclone.store import store as global_store
|
|
|
|
FIXTURE_837 = Path(__file__).parent / "fixtures" / "co_medicaid_837p.txt"
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clear_store():
|
|
with global_store._lock:
|
|
global_store._batches.clear()
|
|
yield
|
|
with global_store._lock:
|
|
global_store._batches.clear()
|
|
|
|
|
|
@pytest.fixture
|
|
def client() -> TestClient:
|
|
return TestClient(app)
|
|
|
|
|
|
@pytest.fixture
|
|
def seeded_store():
|
|
"""One parsed 837P fixture batch."""
|
|
text = FIXTURE_837.read_text()
|
|
client = TestClient(app)
|
|
client.post(
|
|
"/api/parse-837",
|
|
files={"file": ("x.txt", text, "text/plain")},
|
|
headers={"Accept": "application/json"},
|
|
)
|
|
return client
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# /api/batches
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
|
|
def test_batches_returns_empty_list_when_no_parses(client: TestClient):
|
|
resp = client.get("/api/batches")
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
assert body == {"items": [], "total": 0, "returned": 0, "has_more": False}
|
|
|
|
|
|
def test_batches_returns_summary_after_parse(seeded_store):
|
|
resp = seeded_store.get("/api/batches")
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
assert body["total"] == 1
|
|
assert body["returned"] == 1
|
|
assert body["items"][0]["kind"] == "837p"
|
|
assert body["items"][0]["inputFilename"] == "x.txt"
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# /api/batches/{id}
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
|
|
def test_get_batch_by_id_returns_full_result(seeded_store):
|
|
batches = seeded_store.get("/api/batches").json()["items"]
|
|
bid = batches[0]["id"]
|
|
resp = seeded_store.get(f"/api/batches/{bid}")
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
assert "envelope" in body
|
|
assert "claims" in body
|
|
assert len(body["claims"]) == 2
|
|
|
|
|
|
def test_get_batch_404_when_missing(client: TestClient):
|
|
resp = client.get("/api/batches/does-not-exist")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# /api/claims
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
|
|
def test_claims_returns_empty_list_when_no_parses(client: TestClient):
|
|
resp = client.get("/api/claims")
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
assert body["items"] == []
|
|
assert body["total"] == 0
|
|
|
|
|
|
def test_claims_filters_by_status_submitted(seeded_store):
|
|
resp = seeded_store.get("/api/claims?status=submitted")
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
assert all(c["status"] == "submitted" for c in body["items"])
|
|
assert body["total"] >= 1
|
|
|
|
|
|
def test_claims_filters_by_batch_id(seeded_store):
|
|
bid = seeded_store.get("/api/batches").json()["items"][0]["id"]
|
|
resp = seeded_store.get(f"/api/claims?batch_id={bid}")
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
assert all(c["batchId"] == bid for c in body["items"])
|
|
|
|
|
|
def test_claims_sorts_by_billed_amount_desc(seeded_store):
|
|
resp = seeded_store.get("/api/claims?sort=billedAmount&order=desc")
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
amounts = [c["billedAmount"] for c in body["items"]]
|
|
assert amounts == sorted(amounts, reverse=True)
|
|
|
|
|
|
def test_claims_pagination_limit_offset(seeded_store):
|
|
resp1 = seeded_store.get("/api/claims?limit=1&offset=0")
|
|
body1 = resp1.json()
|
|
assert body1["returned"] == 1
|
|
assert body1["has_more"] is True
|