209 lines
7.6 KiB
Python
209 lines
7.6 KiB
Python
"""Tests for pfs.nprm — NPRM Addendum B loader → pfs.rvu_proposed (#592, #612)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import duckdb
|
|
import pytest
|
|
|
|
from pfs import nprm
|
|
|
|
|
|
def _fixture(tmp_path, name="CY2026_NPRM_Addendum_B.csv"):
|
|
p = tmp_path / name
|
|
p.write_text(
|
|
"HCPCS,MOD,DESCRIPTION,STATUS CODE,WORK RVU,"
|
|
"NON-FAC PE RVU,FAC PE RVU,MP RVU\n"
|
|
"99213,,Office visit est,A,1.3,1.5,0.55,0.1\n"
|
|
"0001A,,Admin covid,X,0.0,0.0,0.0,0.0\n"
|
|
)
|
|
return p
|
|
|
|
|
|
def _fixture_2027(tmp_path):
|
|
"""CY2027 partition with a distinct HCPCS set so the two are distinguishable."""
|
|
p = tmp_path / "CY2027_NPRM_Addendum_B.csv"
|
|
p.write_text(
|
|
"HCPCS,MOD,DESCRIPTION,STATUS CODE,WORK RVU,"
|
|
"NON-FAC PE RVU,FAC PE RVU,MP RVU\n"
|
|
"99214,,Office visit est L4,A,1.9,2.1,0.75,0.15\n"
|
|
"0002A,,Admin covid dose 2,X,0.0,0.0,0.0,0.0\n"
|
|
)
|
|
return p
|
|
|
|
|
|
def _patch_discovery(monkeypatch, mapping):
|
|
"""monkeypatch._discover_addendum_b(tag) -> path, from a {tag: path} dict."""
|
|
|
|
def _fake(tag):
|
|
try:
|
|
return mapping[tag]
|
|
except KeyError:
|
|
raise FileNotFoundError(
|
|
f"No Addendum B attachment found among bib items tagged {tag!r}"
|
|
) from None
|
|
|
|
monkeypatch.setattr(nprm, "_discover_addendum_b", _fake)
|
|
|
|
|
|
def test_load_rvu_proposed_loads_and_reloads(tmp_path, monkeypatch):
|
|
path = _fixture(tmp_path)
|
|
_patch_discovery(monkeypatch, {"sup:2026_PFS_NPRM": path})
|
|
con = duckdb.connect()
|
|
out = nprm.load_rvu_proposed(con, year=2026)
|
|
assert out["rows"] == 2
|
|
assert out["cms_rule_id"] == "CMS-1832-P"
|
|
row = con.execute(
|
|
"SELECT hcpcs, work_rvu, cms_rule_id FROM pfs.rvu_proposed WHERE hcpcs='99213'"
|
|
).fetchone()
|
|
assert row == ("99213", 1.3, "CMS-1832-P")
|
|
nprm.load_rvu_proposed(con, year=2026) # idempotent delete-and-reload
|
|
assert con.execute("SELECT count(*) FROM pfs.rvu_proposed").fetchone()[0] == 2
|
|
|
|
|
|
def test_nprm_sources_covers_both_years():
|
|
years = {source[0] for source in nprm.NPRM_SOURCES}
|
|
assert years == {2026, 2027}
|
|
|
|
|
|
def test_load_rvu_proposed_two_partitions_side_by_side(tmp_path, monkeypatch):
|
|
"""Loading both years leaves both rule partitions intact side by side."""
|
|
path_2026 = _fixture(tmp_path)
|
|
path_2027 = _fixture_2027(tmp_path)
|
|
_patch_discovery(
|
|
monkeypatch,
|
|
{"sup:2026_PFS_NPRM": path_2026, "sup:2027_PFS_NPRM": path_2027},
|
|
)
|
|
con = duckdb.connect()
|
|
nprm.load_rvu_proposed(con, year=2026)
|
|
nprm.load_rvu_proposed(con, year=2027)
|
|
|
|
counts = dict(
|
|
con.execute(
|
|
"SELECT cms_rule_id, count(*) FROM pfs.rvu_proposed GROUP BY 1"
|
|
).fetchall()
|
|
)
|
|
assert counts == {"CMS-1832-P": 2, "CMS-1848-P": 2}
|
|
|
|
|
|
def test_load_rvu_proposed_reload_one_leaves_other_intact(tmp_path, monkeypatch):
|
|
"""Reloading the 2026 partition doesn't touch the 2027 partition's rows."""
|
|
path_2026 = _fixture(tmp_path)
|
|
path_2027 = _fixture_2027(tmp_path)
|
|
_patch_discovery(
|
|
monkeypatch,
|
|
{"sup:2026_PFS_NPRM": path_2026, "sup:2027_PFS_NPRM": path_2027},
|
|
)
|
|
con = duckdb.connect()
|
|
nprm.load_rvu_proposed(con, year=2026)
|
|
nprm.load_rvu_proposed(con, year=2027)
|
|
nprm.load_rvu_proposed(con, year=2026) # reload just 2026
|
|
|
|
counts = dict(
|
|
con.execute(
|
|
"SELECT cms_rule_id, count(*) FROM pfs.rvu_proposed GROUP BY 1"
|
|
).fetchall()
|
|
)
|
|
assert counts == {"CMS-1832-P": 2, "CMS-1848-P": 2}
|
|
row_2027 = con.execute(
|
|
"SELECT hcpcs, work_rvu FROM pfs.rvu_proposed WHERE cms_rule_id='CMS-1848-P' "
|
|
"AND hcpcs='99214'"
|
|
).fetchone()
|
|
assert row_2027 == ("99214", 1.9)
|
|
|
|
|
|
def test_load_rvu_proposed_missing_tag_raises_naming_it(tmp_path, monkeypatch):
|
|
"""Discovery failure names the missing tag in the error."""
|
|
_patch_discovery(monkeypatch, {}) # no tags registered at all
|
|
con = duckdb.connect()
|
|
with pytest.raises(FileNotFoundError, match="sup:2026_PFS_NPRM"):
|
|
nprm.load_rvu_proposed(con, year=2026)
|
|
|
|
|
|
def test_load_rvu_proposed_unknown_year_raises():
|
|
con = duckdb.connect()
|
|
with pytest.raises(ValueError, match="2099"):
|
|
nprm.load_rvu_proposed(con, year=2099)
|
|
|
|
|
|
def test_load_rvu_proposed_drops_and_recreates_stale_schema(tmp_path, monkeypatch):
|
|
"""A pre-existing table with old column names is dropped and rebuilt.
|
|
|
|
Regression guard for the pre-#612 table (columns ``modifier`` /
|
|
``nonfac_pe_rvu`` instead of ``mod`` / ``non_fac_pe_rvu``) — CREATE
|
|
TABLE IF NOT EXISTS alone won't fix a live mismatched table.
|
|
"""
|
|
path = _fixture(tmp_path)
|
|
_patch_discovery(monkeypatch, {"sup:2026_PFS_NPRM": path})
|
|
con = duckdb.connect()
|
|
con.execute("CREATE SCHEMA IF NOT EXISTS pfs")
|
|
con.execute(
|
|
"CREATE TABLE pfs.rvu_proposed ("
|
|
"hcpcs VARCHAR, modifier VARCHAR, description VARCHAR, "
|
|
"status_code VARCHAR, work_rvu DOUBLE, nonfac_pe_rvu DOUBLE, "
|
|
"fac_pe_rvu DOUBLE, mp_rvu DOUBLE, cms_rule_id VARCHAR)"
|
|
)
|
|
out = nprm.load_rvu_proposed(con, year=2026)
|
|
assert out["rows"] == 2
|
|
cols = [r[0] for r in con.execute("DESCRIBE pfs.rvu_proposed").fetchall()]
|
|
assert cols == nprm._TARGET_COLUMNS
|
|
|
|
|
|
def _dirty_fixture(tmp_path):
|
|
"""Real-file quirks: padded cells, "NA" markers, a footnote row.
|
|
|
|
Mirrors what the actual CY2026 Addendum B workbook contains — this
|
|
combination broke silently on the real file (see task-5-report.md)
|
|
and was fixed via manual smoke-testing rather than a fixture test,
|
|
so these rows lock the fix in.
|
|
"""
|
|
p = tmp_path / "CY2026_NPRM_Addendum_B_dirty.csv"
|
|
p.write_text(
|
|
"HCPCS,MOD,DESCRIPTION,STATUS CODE,WORK RVU,"
|
|
"NON-FAC PE RVU,FAC PE RVU,MP RVU\n"
|
|
" 99213 , ,Office visit est,A, 1.30 , 1.50 , 0.55 , 0.10 \n"
|
|
"99214,,Office visit NA test,A,NA,NA,NA,NA\n"
|
|
"1 CPT codes and descriptors only are copyright 2025 American "
|
|
"Medical Association. All Rights Reserved.,,,,,,,\n"
|
|
)
|
|
return p
|
|
|
|
|
|
def test_load_rvu_proposed_strips_padding(tmp_path, monkeypatch):
|
|
"""Whitespace-padded HCPCS/numeric cells load clean, not null."""
|
|
path = _dirty_fixture(tmp_path)
|
|
_patch_discovery(monkeypatch, {"sup:2026_PFS_NPRM": path})
|
|
con = duckdb.connect()
|
|
nprm.load_rvu_proposed(con, year=2026)
|
|
row = con.execute(
|
|
"SELECT hcpcs, description, work_rvu, non_fac_pe_rvu, fac_pe_rvu, mp_rvu "
|
|
"FROM pfs.rvu_proposed WHERE hcpcs='99213'"
|
|
).fetchone()
|
|
assert row == ("99213", "Office visit est", 1.3, 1.5, 0.55, 0.1)
|
|
|
|
|
|
def test_load_rvu_proposed_na_marker_becomes_null(tmp_path, monkeypatch):
|
|
""" "NA" in a numeric cell loads as SQL NULL, not a cast error."""
|
|
path = _dirty_fixture(tmp_path)
|
|
_patch_discovery(monkeypatch, {"sup:2026_PFS_NPRM": path})
|
|
con = duckdb.connect()
|
|
nprm.load_rvu_proposed(con, year=2026)
|
|
row = con.execute(
|
|
"SELECT work_rvu, non_fac_pe_rvu, fac_pe_rvu, mp_rvu "
|
|
"FROM pfs.rvu_proposed WHERE hcpcs='99214'"
|
|
).fetchone()
|
|
assert row == (None, None, None, None)
|
|
|
|
|
|
def test_load_rvu_proposed_drops_footnote_rows(tmp_path, monkeypatch):
|
|
"""A footnote row (first cell = long sentence) is not a HCPCS code."""
|
|
path = _dirty_fixture(tmp_path)
|
|
_patch_discovery(monkeypatch, {"sup:2026_PFS_NPRM": path})
|
|
con = duckdb.connect()
|
|
out = nprm.load_rvu_proposed(con, year=2026)
|
|
assert out["rows"] == 2 # only 99213 and 99214 — footnote row excluded
|
|
assert con.execute("SELECT count(*) FROM pfs.rvu_proposed").fetchone()[0] == 2
|
|
footnote = con.execute(
|
|
"SELECT count(*) FROM pfs.rvu_proposed WHERE hcpcs LIKE '1 CPT%'"
|
|
).fetchone()[0]
|
|
assert footnote == 0
|