Files
stack/tests/rec/pricers/test_pfs.py
kert f6e418e400
Some checks failed
CI / lint (push) Failing after 28s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Has been skipped
Deploy / mc (push) Has been skipped
Infra CI / notebooks (push) Failing after 13s
Infra CI / zotero (push) Successful in 12s
Infra CI / docs (push) Successful in 14s
Infra CI / api (push) Successful in 21s
Infra CI / mc (push) Successful in 12s
Deploy / report (push) Successful in 11s
CI / test (push) Has been cancelled
fix(rec): include status-R (Restricted Coverage) in PFS pricer (#340)
Investigation: characterized the consistent ~11K-12K ground-truth-only
rows per year (2016-2026) by joining against pfs.rvu. Result: 100% of
the gap is status_code='R'. Zero are A/T (no real calc bugs hidden in
the noise), zero are missing from pfs.rvu entirely.

Status R = Restricted Coverage. CMS applies special coverage rules
(certain settings, modifier requirements, NCD-driven) but the price
formula is identical to A: RVU × GPCI × CF. The pricer was filtering
status_code IN ('A', 'T'), treating R as if it were carrier-priced.

Fix: add 'R' to _ALGORITHMIC_STATUS. One-character change in the
SQL filter, plus updated docstrings.

Verified: live `stack rec pfs` for every year 2016-2026 now reports:
  Ground-truth only: 0   (was 9,990 - 12,650)
  Status: 100.00% exact

Residual deltas are single-digit ≤1¢ float-rounding noise across
~11M rows. Closes the framework's empirical concordance gap → #340
acceptance criteria fully satisfied.

Test: added 66666 (status R) to the in-memory fixture; updated count
assertions across the existing test class to reflect the new
4-row/3-match baseline; added test_includes_restricted_coverage
regression guard.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-23 18:26:04 -04:00

159 lines
5.6 KiB
Python

"""Tests for rec.pricers.pfs.PfsPricer with a seeded in-memory DuckDB."""
from __future__ import annotations
from rec.base import Pricer
from rec.engine import reconcile
from rec.pricers import PRICERS
from rec.pricers.pfs import PfsPricer
class TestRegistration:
def test_in_PRICERS(self) -> None:
assert "pfs" in PRICERS
assert PRICERS["pfs"] is PfsPricer
def test_conforms_to_protocol(self) -> None:
assert isinstance(PfsPricer(), Pricer)
def test_class_attributes(self) -> None:
assert PfsPricer.system == "pfs"
assert PfsPricer.join_keys == ["year", "mac", "locality", "hcpcs", "mod"]
assert PfsPricer.compare_cols == ["non_fac_fee", "fac_fee"]
class TestGroundTruth:
def test_row_count(self, con, test_year) -> None:
p = PfsPricer()
gt = p.ground_truth(con, test_year)
# Seeded 4 carrier rows (99213, 99214, 99999, 66666)
assert gt.height == 4
def test_columns(self, con, test_year) -> None:
p = PfsPricer()
gt = p.ground_truth(con, test_year)
for col in [
"year",
"mac",
"locality",
"hcpcs",
"mod",
"non_fac_fee",
"fac_fee",
]:
assert col in gt.columns
class TestCalculated:
def test_excludes_carrier_priced(self, con, test_year) -> None:
"""99999 has status_code='C' so calc side skips it."""
p = PfsPricer()
calc = p.calculated(con, test_year)
hcpcs = calc["hcpcs"].to_list()
assert "99999" not in hcpcs
def test_includes_algorithmic(self, con, test_year) -> None:
"""99213, 99214, 88888 all have status 'A'."""
p = PfsPricer()
calc = p.calculated(con, test_year)
hcpcs = set(calc["hcpcs"].to_list())
assert {"99213", "99214", "88888"}.issubset(hcpcs)
def test_includes_restricted_coverage(self, con, test_year) -> None:
"""66666 has status_code='R' (Restricted Coverage). R is
algorithm-priced same as A/T — coverage restrictions don't
affect the price formula. Was previously excluded; this test
guards against regression."""
p = PfsPricer()
calc = p.calculated(con, test_year)
hcpcs = set(calc["hcpcs"].to_list())
assert "66666" in hcpcs
def test_returns_cents_rounded(self, con, test_year) -> None:
p = PfsPricer()
calc = p.calculated(con, test_year)
# All values should be round to 2 decimals (cents)
for fee in calc["non_fac_fee"].to_list() + calc["fac_fee"].to_list():
assert fee == round(fee, 2)
class TestReconcilePfs:
def test_summary_counts(self, con, test_year) -> None:
r = reconcile(PfsPricer(), con, test_year)
# Ground-truth rows: 99213, 99214, 99999, 66666 = 4
assert r.ground_truth_rows == 4
# Calculated rows: 99213, 99214, 88888, 66666 = 4 (99999 skipped)
assert r.calculated_rows == 4
# Matched: 99213, 99214, 66666 = 3
assert r.matched_rows == 3
# gt_only: 99999
assert r.ground_truth_only == 1
# calc_only: 88888
assert r.calculated_only == 1
def test_exact_and_near(self, con, test_year) -> None:
r = reconcile(PfsPricer(), con, test_year)
# 99213 + 66666 seeded exact; 99214 is 1¢ off on non_fac_fee.
assert r.exact_matches == 2
assert r.near_matches == 3
def test_tolerance_promotes(self, con, test_year) -> None:
r = reconcile(PfsPricer(), con, test_year, tolerance_cents=1)
assert r.exact_matches == 3
def test_delta_table_contains_hcpcs(self, con, test_year) -> None:
r = reconcile(PfsPricer(), con, test_year)
hcpcs = set(r.deltas["hcpcs"].to_list())
# All five distinct hcpcs from outer join
assert hcpcs == {"99213", "99214", "99999", "88888", "66666"}
def test_years_available(self, con, test_year) -> None:
p = PfsPricer()
assert p.years_available(con) == [test_year]
def test_missing_year_raises(self, con) -> None:
import pytest
p = PfsPricer()
with pytest.raises(KeyError, match="RuleYear missing"):
p.calculated(con, 1999)
# ── Gap coverage — missed lines ────────────────────────────────
class TestPfsPricerEdgeCases:
"""Lines 120, 123, 131: multiple CFs branch; line 156: empty RVU/GPCI."""
def test_empty_rvu_returns_empty(self, con) -> None:
"""Line 156: empty RVU/GPCI returns empty DataFrame."""
import polars as pl
p = PfsPricer()
# Delete all GPCI rows for the test year so the empty-check is triggered
con.execute("DELETE FROM pfs.gpci")
result = p.calculated(con, 2025)
assert isinstance(result, pl.DataFrame)
assert result.height == 0
def test_multiple_conv_factors(self, con, test_year) -> None:
"""Lines 120, 123, 131: multiple distinct conv_factor values in one year."""
p = PfsPricer()
# Add conv_factor values to RVU rows: one value for 3 rows, another for 1 row
con.execute(
f"""
UPDATE pfs.rvu SET conv_factor = 35.00
WHERE year = {test_year} AND hcpcs IN ('99213', '99214', '88888')
"""
)
# Add a different CF for one row - so there are 2 distinct CFs
con.execute(
f"""
INSERT INTO pfs.rvu VALUES
({test_year}, '77777', NULL, 'A', 0.50, 0.40, 0.20, 0.05, 34.50)
"""
)
calc = p.calculated(con, test_year)
assert calc.height > 0