Files
stack/tests/opps/test_rules.py
kert 2e40801daf
Some checks failed
CI / lint (push) Failing after 42s
CI / notebooks-smoke (push) Successful in 1m57s
Deploy / changes (push) Successful in 17s
Infra CI / notebooks (push) Successful in 58s
CI / test (push) Successful in 4m7s
Infra CI / zotero (push) Successful in 22s
Infra CI / docs (push) Successful in 24s
Infra CI / api (push) Successful in 1m12s
Infra CI / bcda (push) Successful in 1m54s
Infra CI / llm (push) Successful in 1m2s
Infra CI / mc (push) Successful in 19s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Lean Pay / pay (push) Successful in 13s
Deploy / api (push) Successful in 1m25s
Deploy / bcda (push) Successful in 1m47s
Deploy / mc (push) Has been skipped
Deploy / llm (push) Successful in 1m14s
Deploy / report (push) Successful in 14s
Comment Pull / comment-pull (push) Failing after 52s
Notebooks Integration / notebooks-integration (push) Failing after 11m46s
Renovate / renovate (push) Successful in 1m58s
LLM Golden / llm-golden (push) Successful in 23m29s
Zotero Sync / zotero-sync (push) Successful in 3m11s
Package Supply Chain / pkg-supply-chain (push) Successful in 1m17s
feat(pay): transcribe the OPPS drug packaging thresholds
Each OPPS rule year now stores the per-day drug packaging threshold
its own final rule adopts, from $90 in 2014 to $140 in 2025 and 2026.
CY2024 stays at the finalized $135, not the proposed $140. The $55
bypass threshold, the outlier amount, and the diagnostic
radiopharmaceutical thresholds are not this field.
2026-11-17 17:58:28 -05:00

195 lines
7.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Golden + self-consistency guards for opps.rules conversion factors.
The OPPS conversion factor per year is correctness-critical reference
data: OppsPricer computes national APC rates as ``weight × CF`` and
reconciles them against CMS's published Addendum B. Three guards keep
it bulletproof:
1. ``TestGoldenConversionFactors`` pins every year's CF to the value
taken from that year's Federal Register OPPS Final Rule (CY2014-
CY2020) and cross-checked against the empirical
``payment_rate / relative_weight`` ratio in Addendum B (CY2021-
CY2026). Runs everywhere (no data needed), so any accidental edit
fails CI. Changing a value requires updating the citation below too.
2. ``TestAddendumBFixtureConsistency`` reconciles OppsPricer against a
committed slice of *real* Addendum B rows (fixtures/addendum_b_
sample.csv — real published payment_rate, not computed). It runs in
CI with no external data, and because the rows are real a wrong CF
fails here — the independent correctness proof, enforced in CI.
3. ``TestAddendumBSelfConsistency`` (skipped when the aco DuckDB is
absent, e.g. headless CI) does the same against the full loaded
Addendum B — broader coverage where the data is present.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from conf import path
from opps.rules import RULES
_FIXTURE = Path(__file__).parent / "fixtures" / "addendum_b_sample.csv"
# Full OPPS conversion factor — the rate for hospitals that MEET quality
# reporting (not the reduced/OQR-penalty CF).
#
# Sources (every year cross-checked BOTH ways):
# Federal Register — each year's OPPS/ASC Final Rule preamble ("we are
# using a conversion factor of $X"), primary for CY2014-2020.
# Empirical — payment_rate / relative_weight in opps.addendum_b (all 13
# years now ingested, 5,000+ APC-priced rows/year), which
# reproduces the published national rate to the cent.
# CY2020 = 80.784, the as-published NFRM value that reproduces the CY2020
# Addendum B we ingest (the archived NFRM addenda are our source).
# CMS later cited a corrected 80.793; we track the value that
# matches our data.
# CY2016 is genuinely below CY2015 — a one-time -2.0pp packaged-lab
# ("two-times") adjustment more than offset the market basket.
_EXPECTED_CF: dict[int, float] = {
2014: 72.672,
2015: 74.144,
2016: 73.725,
2017: 75.001,
2018: 78.636,
2019: 79.490,
2020: 80.784,
2021: 82.797,
2022: 84.177,
2023: 85.585,
2024: 87.382,
2025: 89.169,
2026: 91.415,
}
_HAS_ACO_DB = path("db.aco").exists()
class TestGoldenConversionFactors:
"""CI-runnable pin — guards against accidental CF edits."""
def test_all_years_present(self) -> None:
assert set(RULES) == set(_EXPECTED_CF)
@pytest.mark.parametrize("year", sorted(_EXPECTED_CF))
def test_cf_matches_published_value(self, year: int) -> None:
assert RULES[year].conversion_factor == _EXPECTED_CF[year], (
f"CY{year} OPPS conversion factor changed. If this is a real "
f"correction, update _EXPECTED_CF with a Federal Register / "
f"Addendum B citation — do not silently override it."
)
def test_monotonic_except_2016(self) -> None:
"""CFs rise year over year, except the deliberate CY2016 dip."""
years = sorted(_EXPECTED_CF)
for prev, cur in zip(years, years[1:]):
prev_cf = RULES[prev].conversion_factor
cur_cf = RULES[cur].conversion_factor
if cur == 2016:
assert cur_cf < prev_cf
else:
assert cur_cf > prev_cf
# Per-day drug packaging threshold the year's own OPPS/ASC final rule
# adopts. Not the $55 bypass-list threshold, the outlier fixed-dollar
# threshold, or the diagnostic-radiopharmaceutical threshold ($630 in
# CY2025, $655 in CY2026). CY2024's proposal was $140; the final
# recalculation rounds to $135.
_DRUG_PACKAGING_THRESHOLD: dict[int, float] = {
2014: 90, # 78 FR 75022
2015: 95, # 79 FR 66882
2016: 100, # 80 FR 70433
2017: 110, # 81 FR 79666
2018: 120, # 82 FR 52483
2019: 125, # 83 FR 58964
2020: 130, # 84 FR 61312
2021: 130, # 85 FR 86032
2022: 130, # 86 FR 63636
2023: 135, # 87 FR 71961
2024: 135, # 88 FR 81780
2025: 140, # 89 FR 94237
2026: 140, # 90 FR 53701
}
class TestDrugPackagingThreshold:
"""The adopted drug packaging threshold, one dollar per final rule."""
def test_every_rule_year_has_one(self) -> None:
assert set(RULES) == set(_DRUG_PACKAGING_THRESHOLD)
@pytest.mark.parametrize("year", sorted(_DRUG_PACKAGING_THRESHOLD))
def test_matches_the_adopted_final_rule_dollar(self, year: int) -> None:
assert RULES[year].packaging_threshold == _DRUG_PACKAGING_THRESHOLD[year]
class TestAddendumBFixtureConsistency:
"""CI-runnable correctness proof against a committed slice of real
Addendum B rows.
The fixture holds real published ``payment_rate`` values (never
computed), spanning APC weights from ~0.05 to ~700 across every year
CY2014-CY2026. If any of those years' conversion factors is wrong, the
calculated ``weight × CF`` diverges from the published rate and this
fails — no external dataset required, so the correctness check runs
in headless CI.
"""
def test_fixture_reconciles_to_the_cent(self) -> None:
import duckdb
from rec.engine import reconcile
from rec.pricers.opps import OppsPricer
con = duckdb.connect(":memory:")
con.execute("CREATE SCHEMA opps")
con.execute(
"CREATE TABLE opps.addendum_b AS SELECT * FROM read_csv_auto(?)",
[str(_FIXTURE)],
)
pricer = OppsPricer()
years = pricer.years_available(con)
assert years == list(range(2014, 2027))
for year in years:
r = reconcile(pricer, con, year)
assert r.matched_rows > 0, f"CY{year}: no fixture rows"
assert r.exact_matches == r.matched_rows, (
f"CY{year}: {r.matched_rows - r.exact_matches} of "
f"{r.matched_rows} real Addendum B rows do not match "
f"weight × CF to the cent — the CY{year} conversion "
f"factor is wrong."
)
@pytest.mark.skipif(not _HAS_ACO_DB, reason="data/aco.duckdb not present")
class TestAddendumBSelfConsistency:
"""Prove weight × CF reproduces CMS's published Addendum B rate.
Runs only where the aco DuckDB (with ``opps.addendum_b``) is present.
This is the independent check that the golden CFs are *correct*: if a
CF is wrong, the calculated national rate diverges from the published
``payment_rate`` and this fails.
"""
def test_reconciles_to_the_cent_for_all_loaded_years(self) -> None:
from conf import connect
from rec.engine import reconcile
from rec.pricers.opps import OppsPricer
con = connect.duckdb()
pricer = OppsPricer()
years = pricer.years_available(con)
assert years, "opps.addendum_b has no years overlapping RULES"
for year in years:
r = reconcile(pricer, con, year)
assert r.matched_rows > 0, f"CY{year}: no APC rows to reconcile"
assert r.exact_matches == r.matched_rows, (
f"CY{year}: {r.matched_rows - r.exact_matches} of "
f"{r.matched_rows} APC rows do not match weight × CF to the "
f"cent — the CY{year} conversion factor is wrong."
)