Some checks failed
CI / lint (push) Successful in 1m53s
CI / notebooks-smoke (push) Successful in 2m15s
Deploy / changes (push) Successful in 20s
CI / test (push) Failing after 6m52s
Infra CI / zotero (push) Successful in 2m26s
Infra CI / notebooks (push) Successful in 4m17s
Infra CI / api (push) Successful in 1m46s
Infra CI / docs (push) Successful in 2m22s
Infra CI / llm (push) Successful in 1m34s
Infra CI / mc (push) Successful in 1m7s
Deploy / notebooks (push) Has been skipped
Infra CI / bcda (push) Successful in 2m50s
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Successful in 2m42s
Deploy / bcda (push) Successful in 3m39s
Deploy / mc (push) Has been skipped
Deploy / llm (push) Successful in 2m3s
Deploy / report (push) Successful in 19s
393 lines
13 KiB
Python
393 lines
13 KiB
Python
"""market.seed: loading, validation, seat keys, name matching."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
|
|
import pytest
|
|
|
|
from market import seed as S
|
|
from pfs.codetables import SeatRow
|
|
|
|
|
|
def _seat(org, specialty, kind, year=2024, seat_type="permanent", body="RUC"):
|
|
return SeatRow(body, year, org, specialty, seat_type, "u", "K", "s", kind)
|
|
|
|
|
|
def _write(tmp_path, text):
|
|
p = tmp_path / "seed.yaml"
|
|
p.write_text(text)
|
|
return p
|
|
|
|
|
|
COMPANY = """
|
|
companies:
|
|
- handle: XCO
|
|
name: X Co
|
|
cik: "123"
|
|
status: listed
|
|
{extra}
|
|
"""
|
|
|
|
|
|
def test_seed_loads_and_every_entry_cited():
|
|
companies, orgs = S.load_seed()
|
|
assert len(companies) >= 30 and len(orgs) >= 40
|
|
for entry in (*companies, *orgs):
|
|
assert entry.cite.kind in ("url", "adsh", "filename", "item_key")
|
|
assert entry.cite.ref and len(entry.cite.quote) <= 400
|
|
for link in entry.links:
|
|
located = [getattr(link.cite, k) for k in S._LOCATORS]
|
|
assert sum(bool(x) for x in located) == 1
|
|
assert sum(bool(c.links) for c in companies) >= 30
|
|
assert {c.handle for c in companies} >= {"OSH", "SGFY", "AMED", "HCA", "DVA"}
|
|
assert {o.key for o in orgs} >= {"AAFP", "AMA", "AOA", "ATA", "AAHCM"}
|
|
|
|
|
|
def test_seed_has_no_excluded_names():
|
|
text = S.SEED_PATH.read_text().upper().replace(" ", "")
|
|
assert not any(w in text for w in S.EXCLUDED)
|
|
|
|
|
|
def test_seed_entry_without_cite_rejected(tmp_path):
|
|
p = _write(tmp_path, COMPANY.format(extra="aliases: [X]"))
|
|
with pytest.raises(ValueError, match="^XCO: no citation"):
|
|
S.load_seed(p)
|
|
|
|
|
|
def test_seed_link_without_cite_rejected(tmp_path):
|
|
body = COMPANY.format(
|
|
extra='cite: {url: "https://a.test"}\n links:\n - {scope: program, target: PFS}'
|
|
)
|
|
with pytest.raises(ValueError, match="^XCO/program: no citation"):
|
|
S.load_seed(_write(tmp_path, body))
|
|
|
|
|
|
def test_seed_two_locators_rejected(tmp_path):
|
|
body = COMPANY.format(extra='cite: {url: "https://a.test", adsh: "0001-23-1"}')
|
|
with pytest.raises(ValueError, match="exactly one locator"):
|
|
S.load_seed(_write(tmp_path, body))
|
|
|
|
|
|
def test_seed_long_quote_rejected(tmp_path):
|
|
body = COMPANY.format(
|
|
extra=f'cite: {{url: "https://a.test", quote: "{"q" * 401}"}}'
|
|
)
|
|
with pytest.raises(ValueError, match="quote over 400"):
|
|
S.load_seed(_write(tmp_path, body))
|
|
|
|
|
|
def test_seed_excluded_alias_rejected(tmp_path):
|
|
body = COMPANY.format(
|
|
extra='aliases: ["HARMONYCARES LLC"]\n cite: {url: "https://a.test"}'
|
|
)
|
|
with pytest.raises(ValueError, match="excluded"):
|
|
S.load_seed(_write(tmp_path, body))
|
|
|
|
|
|
def test_seed_bad_status_and_org_type(tmp_path):
|
|
bad = COMPANY.format(extra='cite: {url: "u"}').replace("listed", "zombie")
|
|
with pytest.raises(ValueError, match="status"):
|
|
S.load_seed(_write(tmp_path, bad))
|
|
org = 'orgs:\n - {key: O, name: O, org_type: club, cite: {url: "u"}}\n'
|
|
with pytest.raises(ValueError, match="org_type"):
|
|
S.load_seed(_write(tmp_path, org))
|
|
|
|
|
|
def test_empty_seed_file(tmp_path):
|
|
assert S.load_seed(_write(tmp_path, "")) == ((), ())
|
|
|
|
|
|
def test_security_windows_parse():
|
|
companies, _ = S.load_seed()
|
|
osh = next(c for c in companies if c.handle == "OSH")
|
|
(sec,) = osh.securities
|
|
assert (sec.ticker, sec.exchange) == ("OSH", "NYSE")
|
|
assert (sec.valid_from, sec.valid_to) == (date(2020, 8, 6), date(2023, 5, 2))
|
|
assert osh.status == "acquired" and osh.ended_on == date(2023, 5, 2)
|
|
assert osh.cik == "0001564406" and osh.successor_cik == "0000064803"
|
|
|
|
|
|
def _mini(links, specialty="", org_links=()):
|
|
c = S.Cite(url="u")
|
|
co = S.SeedCompany(
|
|
"CO",
|
|
"Co",
|
|
"0000000001",
|
|
"listed",
|
|
None,
|
|
"",
|
|
(),
|
|
(),
|
|
tuple(S.SeedLink(s, t, c) for s, t in links),
|
|
(),
|
|
c,
|
|
)
|
|
org = S.SeedOrg(
|
|
"O",
|
|
"O",
|
|
"specialty_society",
|
|
specialty,
|
|
(),
|
|
(),
|
|
tuple(S.SeedLink(s, t, c) for s, t in org_links),
|
|
c,
|
|
)
|
|
return [co], [org]
|
|
|
|
|
|
def test_validate_seed_targets():
|
|
kw = {
|
|
"families": {"HOME-VISITS"},
|
|
"specialties": {"FAMILY PRACTICE", "PRIMARY CARE"},
|
|
}
|
|
cos, orgs = _mini(
|
|
[
|
|
("family", "NOPE"),
|
|
("specialty", "FAMILY MEDICINE"),
|
|
("program", "ACO"),
|
|
("family", "NOPE"),
|
|
]
|
|
)
|
|
assert len(S.validate_seed(cos, orgs, **kw)) == 3
|
|
cos, orgs = _mini(
|
|
[
|
|
("family", "HOME-VISITS"),
|
|
("specialty", "FAMILY PRACTICE"),
|
|
("specialty", "PRIMARY CARE"),
|
|
("program", "PFS"),
|
|
]
|
|
)
|
|
assert S.validate_seed(cos, orgs, **kw) == []
|
|
|
|
|
|
def test_validate_seed_org_specialty_and_unknown_scope():
|
|
kw = {"families": set(), "specialties": {"UROLOGY"}}
|
|
cos, orgs = _mini([("sector", "8082")], specialty="BOGUS")
|
|
errs = S.validate_seed(cos, orgs, **kw)
|
|
assert len(errs) == 2 and any("unknown link scope" in e for e in errs)
|
|
cos, orgs = _mini([], specialty="UROLOGY")
|
|
assert S.validate_seed(cos, orgs, **kw) == []
|
|
|
|
|
|
def test_validate_seed_checks_org_links():
|
|
kw = {"families": set(), "specialties": set()}
|
|
cos, orgs = _mini([], org_links=[("program", "ACO")])
|
|
(err,) = S.validate_seed(cos, orgs, **kw)
|
|
assert err.startswith("O:") and "ACO" in err
|
|
|
|
|
|
def test_shipped_seed_targets_validate_against_vocab_shape():
|
|
companies, orgs = S.load_seed()
|
|
targets = {
|
|
(s, t)
|
|
for e in (*companies, *orgs)
|
|
for s, t in ((l.scope, l.target) for l in e.links)
|
|
}
|
|
assert {s for s, _ in targets} <= {"family", "specialty", "program"}
|
|
assert {t for s, t in targets if s == "program"} <= set(S.PROGRAMS)
|
|
|
|
|
|
def test_seat_key_and_seat_specialties():
|
|
fam = _seat("Family Medicine", "FAMILY PRACTICE", "specialty")
|
|
pc = _seat("Primary Care Rotating Seat", "", "organization", 2022, "rotating")
|
|
onc = _seat("Oncology", "", "specialty", 2026)
|
|
chair = _seat("RUC Chair", "", "category")
|
|
ama = _seat("American Medical Association", "", "organization")
|
|
assert S.seat_key(fam) == "FAMILY PRACTICE"
|
|
assert S.seat_key(pc) == "PRIMARY CARE"
|
|
assert S.seat_key(onc) == "ONCOLOGY"
|
|
assert S.seat_key(chair) is None
|
|
assert S.seat_key(ama) is None
|
|
assert S.seat_specialties([fam, pc, onc, chair, ama], years=[2024]) == {
|
|
"FAMILY PRACTICE"
|
|
}
|
|
assert S.seat_specialties([fam, pc, onc], years=[2024, 2026]) == {
|
|
"FAMILY PRACTICE",
|
|
"ONCOLOGY",
|
|
}
|
|
cpt = _seat("Family Medicine", "FAMILY PRACTICE", "specialty", body="CPT_PANEL")
|
|
assert S.seat_specialties([cpt], years=[2024]) == set()
|
|
|
|
|
|
def test_norm_org():
|
|
assert S.norm_org("Amedisys Holding, L.L.C.") == "AMEDISYS HOLDING"
|
|
assert (
|
|
S.norm_org("The American College of Physicians, Inc.")
|
|
== "AMERICAN COLLEGE OF PHYSICIANS"
|
|
)
|
|
assert S.norm_org("Johnson & Johnson") == "JOHNSON AND JOHNSON"
|
|
assert S.norm_org("America's Physician Groups") == "AMERICAS PHYSICIAN GROUPS"
|
|
|
|
|
|
def test_alias_match_exact_and_prefix():
|
|
fm = ["AMERICAN ACADEMY OF FAMILY PHYSICIANS"]
|
|
assert S.alias_match("American Academy of Family Physicians", fm, prefix=False)
|
|
assert not S.alias_match(
|
|
"AMERICAN ACADEMY OF FAMILY PHYSICIANS FOUNDATION", fm, prefix=False
|
|
)
|
|
assert S.alias_match("AMEDISYS HOLDING LLC", ["AMEDISYS"], prefix=True)
|
|
assert not S.alias_match("AMEDISYSTEMS", ["AMEDISYS"], prefix=True)
|
|
assert not S.alias_match("HCA SOMETHING", ["HCA"], prefix=True)
|
|
assert not S.alias_match("AMEDISYS HOLDING", ["AMEDISYS"], prefix=False)
|
|
|
|
|
|
# ── M12: citations must support what they cite ──────────────────────
|
|
|
|
|
|
def _filing_cites(companies):
|
|
for c in companies:
|
|
yield f"{c.handle}", c.cite
|
|
for link in c.links:
|
|
yield f"{c.handle}/{link.scope}:{link.target}", link.cite
|
|
for s in c.securities:
|
|
if s.cite:
|
|
yield f"{c.handle}/{s.ticker}", s.cite
|
|
|
|
|
|
def test_filing_cite_requires_quote(tmp_path):
|
|
body = COMPANY.format(extra='cite: {adsh: "0001-23-000001"}')
|
|
with pytest.raises(ValueError, match="^XCO: .*quote"):
|
|
S.load_seed(_write(tmp_path, body))
|
|
|
|
|
|
def test_link_filing_cite_requires_quote(tmp_path):
|
|
body = COMPANY.format(
|
|
extra='cite: {adsh: "0001-23-000001", quote: "q"}\n links:\n'
|
|
' - {scope: program, target: PFS, cite: {adsh: "0001-23-000001"}}'
|
|
)
|
|
with pytest.raises(ValueError, match="^XCO/program: .*quote"):
|
|
S.load_seed(_write(tmp_path, body))
|
|
|
|
|
|
def test_security_cite_and_org_pseudo_parse(tmp_path):
|
|
body = (
|
|
COMPANY.format(
|
|
extra='cite: {url: "u"}\n securities:\n - {ticker: XX, exchange: NYSE, '
|
|
'valid_from: 2021-06-04, cite: {adsh: "0001-21-000002", quote: "renamed"}}'
|
|
)
|
|
+ "orgs:\n - {key: P, name: P, org_type: specialty_society, pseudo: true, "
|
|
'cite: {item_key: "K"}}\n'
|
|
)
|
|
companies, orgs = S.load_seed(_write(tmp_path, body))
|
|
(sec,) = companies[0].securities
|
|
assert sec.cite.adsh == "0001-21-000002" and sec.cite.quote == "renamed"
|
|
assert orgs[0].pseudo is True
|
|
|
|
|
|
def test_shipped_seed_every_filing_cite_has_quote():
|
|
companies, _ = S.load_seed()
|
|
n = 0
|
|
for who, cite in _filing_cites(companies):
|
|
if cite.adsh:
|
|
n += 1
|
|
assert cite.quote, f"{who}: filing cite without quote"
|
|
assert n >= 60
|
|
|
|
|
|
def test_shipped_specialty_societies_cite_roster_items():
|
|
_, orgs = S.load_seed()
|
|
for o in orgs:
|
|
if o.specialty:
|
|
assert o.cite.item_key, f"{o.key}: specialty link must cite a roster item"
|
|
pseudo = {o.key for o in orgs if o.pseudo}
|
|
assert pseudo == {"PRIMARY-CARE-SEAT", "ONCOLOGY-SEAT"}
|
|
assert "ASCO" not in {o.key for o in orgs}
|
|
|
|
|
|
def test_shipped_unreachable_sites_are_gaps_and_never_accessed():
|
|
_, orgs = S.load_seed()
|
|
gaps = {g.source for g in S.load_gaps()}
|
|
assert {"web:renalmd.org", "web:asco.org", "web:ahip.org"} <= gaps
|
|
ahip = next(o for o in orgs if o.key == "AHIP")
|
|
assert ahip.cite.accessed == ""
|
|
for o in orgs:
|
|
for host in ("renalmd.org", "asco.org"):
|
|
assert host not in o.cite.url
|
|
|
|
|
|
def test_shipped_ticker_windows_come_from_filing_evidence():
|
|
companies, _ = S.load_seed()
|
|
by = {c.handle: c for c in companies}
|
|
(cano,) = by["CANO"].securities
|
|
assert cano.valid_from == date(2021, 6, 4)
|
|
assert cano.cite.adsh == "0001193125-21-186694"
|
|
assert by["CANO"].cite.adsh == "0001193125-24-023512"
|
|
(ci,) = by["CI"].securities
|
|
assert ci.valid_from == date(2018, 12, 21) and ci.cite.adsh
|
|
md = by["MD"].securities[-1]
|
|
assert md.ticker == "MD" and md.cite.adsh
|
|
|
|
|
|
def _by():
|
|
companies, _ = S.load_seed()
|
|
return {c.handle: c for c in companies}
|
|
|
|
|
|
def test_elv_links_to_the_pfs_through_its_own_10k_and_ci_is_unpriced():
|
|
by = _by()
|
|
(pfs,) = by["ELV"].links
|
|
assert (pfs.scope, pfs.target) == ("program", "PFS")
|
|
assert "RBRVS" in pfs.cite.quote and "fee schedule" in pfs.cite.quote
|
|
assert by["CI"].links == () and by["CI"].cite.quote
|
|
assert [s.priced for s in by["CI"].securities] == [False]
|
|
|
|
|
|
def test_link_dates_come_from_the_cited_filing():
|
|
by = _by()
|
|
(clov,) = [l for l in by["CLOV"].links if l.target == "APM"]
|
|
assert clov.valid_to == date(2023, 12, 31) and clov.valid_from is None
|
|
assert "no longer participate" in clov.cite.quote
|
|
(alhc,) = [l for l in by["ALHC"].links if l.target == "APM"]
|
|
assert alhc.valid_to == date(2025, 12, 31)
|
|
assert "December 31, 2025" in alhc.cite.quote
|
|
|
|
|
|
def test_same_cik_ticker_renames_are_a_history_not_a_clip():
|
|
by = _by()
|
|
antm, elv = by["ELV"].securities
|
|
assert (antm.ticker, antm.valid_from, antm.valid_to) == (
|
|
"ANTM",
|
|
None,
|
|
date(2022, 6, 27),
|
|
)
|
|
assert (elv.ticker, elv.valid_from, elv.valid_to) == ("ELV", None, None)
|
|
assert antm.cite.adsh and antm.cite_to.adsh == elv.cite.adsh
|
|
assert (antm.priced, elv.priced) == (False, True)
|
|
pdx, md = by["MD"].securities
|
|
assert (pdx.ticker, pdx.valid_from, pdx.valid_to) == (
|
|
"PDX",
|
|
None,
|
|
date(2008, 12, 31),
|
|
)
|
|
assert (md.ticker, md.valid_from, md.valid_to) == ("MD", None, None)
|
|
assert pdx.cite.quote and pdx.cite_to.adsh == md.cite.adsh
|
|
assert (pdx.priced, md.priced) == (False, True)
|
|
|
|
|
|
def test_ticker_recycled_across_ciks_stays_clipped():
|
|
ci = _by()["CI"]
|
|
(sec,) = ci.securities
|
|
assert (sec.ticker, sec.valid_from, sec.valid_to) == (
|
|
"CI",
|
|
date(2018, 12, 21),
|
|
None,
|
|
)
|
|
|
|
|
|
def test_priced_flag_parses_and_defaults_true(tmp_path):
|
|
body = COMPANY.format(
|
|
extra='cite: {url: "u"}\n securities:\n - {ticker: A, exchange: X}\n'
|
|
" - {ticker: B, exchange: X, priced: false}"
|
|
)
|
|
companies, _ = S.load_seed(_write(tmp_path, body))
|
|
assert [s.priced for s in companies[0].securities] == [True, False]
|
|
|
|
|
|
def test_only_symbol_records_are_unpriced():
|
|
companies, _ = S.load_seed()
|
|
unpriced = {
|
|
(c.handle, s.ticker) for c in companies for s in c.securities if not s.priced
|
|
}
|
|
assert unpriced == {("ELV", "ANTM"), ("MD", "PDX"), ("CI", "CI")}
|