Cover remaining uncovered lines in bcda (client, store, log, pipe/cclf, flatten), bib (sync, spider, translate, ingest, item, store, format, ui), cms (express wrappers, log edge cases), api (base, gitea, rustfs, woodpecker, zotero), bls (table import), and pfs (pragma on race guard). 37,687 statements, 0 missed — 11,061 tests passing.
259 lines
7.7 KiB
Python
259 lines
7.7 KiB
Python
"""Tests for bib.format — citation formatting functions."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from bib.format import format_bibliography, format_citation
|
|
from bib.item import Download, Item, Manual, Regulation, Rule, Source
|
|
|
|
|
|
class TestFormatRule:
|
|
def test_apa_with_volume_and_page(self) -> None:
|
|
r = Rule(
|
|
title="CY 2026 PFS Final Rule",
|
|
date_published="2025-11-15",
|
|
fr_volume="90",
|
|
fr_page="98452",
|
|
)
|
|
cite = format_citation(r)
|
|
assert "Centers for Medicare & Medicaid Services" in cite
|
|
assert "(2025)" in cite
|
|
assert "CY 2026 PFS Final Rule" in cite
|
|
assert "*Federal Register*" in cite
|
|
assert "*90*" in cite
|
|
assert "98452" in cite
|
|
|
|
def test_apa_with_url(self) -> None:
|
|
r = Rule(
|
|
title="Test Rule",
|
|
date_published="2025-01-01",
|
|
fr_volume="90",
|
|
fr_page="1000",
|
|
url="https://example.com/rule",
|
|
)
|
|
cite = format_citation(r)
|
|
assert "https://example.com/rule" in cite
|
|
|
|
def test_apa_missing_date(self) -> None:
|
|
r = Rule(title="Test Rule")
|
|
cite = format_citation(r)
|
|
assert "(n.d.)" in cite
|
|
|
|
def test_apa_volume_only(self) -> None:
|
|
r = Rule(title="Test", fr_volume="90", date_published="2025-01-01")
|
|
cite = format_citation(r)
|
|
assert "*90*" in cite
|
|
|
|
|
|
class TestFormatManual:
|
|
def test_apa_full(self) -> None:
|
|
m = Manual(
|
|
title="Physician Fee Schedule",
|
|
manual_name="Claims Processing Manual",
|
|
pub_number="100-04",
|
|
chapter="12",
|
|
date_published="2025-01-01",
|
|
)
|
|
cite = format_citation(m)
|
|
assert "Claims Processing Manual" in cite
|
|
assert "Pub. 100-04" in cite
|
|
assert "Ch. 12" in cite
|
|
|
|
def test_apa_with_url(self) -> None:
|
|
m = Manual(
|
|
title="Test",
|
|
manual_name="CPM",
|
|
url="https://example.com/manual",
|
|
)
|
|
cite = format_citation(m)
|
|
assert "https://example.com/manual" in cite
|
|
|
|
def test_apa_uses_institution(self) -> None:
|
|
m = Manual(
|
|
title="Test",
|
|
institution="Custom Institution",
|
|
)
|
|
cite = format_citation(m)
|
|
assert "Custom Institution" in cite
|
|
|
|
|
|
class TestFormatRegulation:
|
|
def test_apa_with_section(self) -> None:
|
|
r = Regulation(
|
|
cfr_title="42",
|
|
cfr_part="414",
|
|
cfr_section="414.22",
|
|
effective_date="2025-01-01",
|
|
)
|
|
cite = format_citation(r)
|
|
assert "42 C.F.R. § 414.22" in cite
|
|
assert "(2025)" in cite
|
|
|
|
def test_apa_part_only(self) -> None:
|
|
r = Regulation(
|
|
cfr_title="42",
|
|
cfr_part="414",
|
|
effective_date="2025-01-01",
|
|
)
|
|
cite = format_citation(r)
|
|
assert "42 C.F.R. pt. 414" in cite
|
|
|
|
def test_bluebook_with_section(self) -> None:
|
|
r = Regulation(
|
|
cfr_title="42",
|
|
cfr_section="414.22",
|
|
effective_date="2025-01-01",
|
|
)
|
|
cite = format_citation(r, style="bluebook")
|
|
assert "42 C.F.R. § 414.22 (2025)." == cite
|
|
|
|
def test_bluebook_part_only(self) -> None:
|
|
r = Regulation(
|
|
cfr_title="42",
|
|
cfr_part="414",
|
|
effective_date="2025-01-01",
|
|
)
|
|
cite = format_citation(r, style="bluebook")
|
|
assert "42 C.F.R. pt. 414 (2025)." == cite
|
|
|
|
|
|
class TestFormatDownload:
|
|
def test_apa(self) -> None:
|
|
d = Download(
|
|
title="RVU26A",
|
|
date_published="2026-01-01",
|
|
url="https://example.com/rvu",
|
|
)
|
|
cite = format_citation(d)
|
|
assert "Centers for Medicare & Medicaid Services" in cite
|
|
assert "(2026)" in cite
|
|
assert "*RVU26A*" in cite
|
|
assert "https://example.com/rvu" in cite
|
|
|
|
def test_custom_website_title(self) -> None:
|
|
d = Download(
|
|
title="Data",
|
|
website_title="Custom Portal",
|
|
date_published="2026-01-01",
|
|
)
|
|
cite = format_citation(d)
|
|
assert "Custom Portal" in cite
|
|
|
|
|
|
class TestFormatSource:
|
|
def test_apa(self) -> None:
|
|
s = Source(
|
|
title="PY 2026 Financial Guarantees",
|
|
institution="CMS Innovation Center",
|
|
date_published="2025-06-01",
|
|
url="https://example.com/source",
|
|
)
|
|
cite = format_citation(s)
|
|
assert "CMS Innovation Center" in cite
|
|
assert "(2025)" in cite
|
|
assert "*PY 2026 Financial Guarantees*" in cite
|
|
|
|
def test_unknown_institution(self) -> None:
|
|
s = Source(title="Test")
|
|
cite = format_citation(s)
|
|
assert "Unknown" in cite
|
|
|
|
|
|
class TestFormatRegulationExtra:
|
|
def test_apa_custom_title(self) -> None:
|
|
"""Line 132: regulation with title != default."""
|
|
r = Regulation(
|
|
cfr_title="42",
|
|
cfr_part="414",
|
|
title="RVU Methodology", # != "42 CFR Part 414"
|
|
effective_date="2025-01-01",
|
|
)
|
|
cite = format_citation(r)
|
|
assert "*RVU Methodology*" in cite
|
|
|
|
def test_apa_default_title_not_printed(self) -> None:
|
|
"""Line 132 branch: title == default → no extra title."""
|
|
r = Regulation(
|
|
cfr_title="42",
|
|
cfr_part="414",
|
|
title="42 CFR Part 414",
|
|
effective_date="2025-01-01",
|
|
)
|
|
cite = format_citation(r)
|
|
assert "*42 CFR Part 414*" not in cite
|
|
|
|
def test_apa_with_url(self) -> None:
|
|
"""Line 134: regulation with URL."""
|
|
r = Regulation(
|
|
cfr_title="42",
|
|
cfr_part="414",
|
|
effective_date="2025-01-01",
|
|
url="https://ecfr.gov/title-42/part-414",
|
|
)
|
|
cite = format_citation(r)
|
|
assert "https://ecfr.gov/title-42/part-414" in cite
|
|
|
|
|
|
class TestFormatGeneric:
|
|
def test_unknown_item_type(self) -> None:
|
|
item = Item(
|
|
item_type="custom",
|
|
title="Custom Item",
|
|
institution="Test Org",
|
|
date_published="2025-01-01",
|
|
)
|
|
cite = format_citation(item)
|
|
assert "Test Org" in cite
|
|
assert "*Custom Item*" in cite
|
|
|
|
def test_generic_with_url(self) -> None:
|
|
"""Line 169: _format_generic with URL."""
|
|
item = Item(
|
|
item_type="custom",
|
|
title="Custom",
|
|
date_published="2025-01-01",
|
|
url="https://example.com/custom",
|
|
)
|
|
cite = format_citation(item)
|
|
assert "https://example.com/custom" in cite
|
|
|
|
def test_generic_no_title(self) -> None:
|
|
item = Item(
|
|
item_type="custom",
|
|
institution="Org",
|
|
date_published="2025-01-01",
|
|
)
|
|
cite = format_citation(item)
|
|
assert "Org. (2025)." in cite
|
|
|
|
def test_generic_no_institution(self) -> None:
|
|
item = Item(
|
|
item_type="custom",
|
|
title="Title",
|
|
date_published="2025-01-01",
|
|
)
|
|
cite = format_citation(item)
|
|
assert "Unknown" in cite
|
|
|
|
|
|
class TestFormatBibliography:
|
|
def test_numbered_entries(self) -> None:
|
|
items = [
|
|
Rule(title="Rule A", date_published="2025-01-01"),
|
|
Manual(title="Manual B"),
|
|
]
|
|
bib = format_bibliography(items)
|
|
lines = bib.strip().split("\n")
|
|
assert len(lines) == 2
|
|
assert lines[0].startswith("[1]")
|
|
assert lines[1].startswith("[2]")
|
|
|
|
def test_empty_list(self) -> None:
|
|
bib = format_bibliography([])
|
|
assert bib == ""
|
|
|
|
def test_single_item(self) -> None:
|
|
items = [Source(title="Test", date_published="2025-01-01")]
|
|
bib = format_bibliography(items)
|
|
assert bib.startswith("[1]")
|
|
assert "\n" not in bib
|