From 04be043442ced8db2079878735ddef84c93ac4cf Mon Sep 17 00:00:00 2001 From: kert Date: Thu, 12 Nov 2026 23:36:36 -0500 Subject: [PATCH] fix(market): link validity dates, AMA guard in entities, same-CIK ticker histories, drop word-match care-management links --- src/cli/market.py | 26 ++++++++++++--- src/market/edgar.py | 54 +++++++++++++++++++++++++------ src/market/seed.py | 16 ++++++++-- src/market/seed.yaml | 12 +++---- src/market/tables.py | 11 ++++++- tests/cli/test_market_cli.py | 39 +++++++++++++++++++++++ tests/market/test_edgar.py | 27 ++++++++++++++++ tests/market/test_seed.py | 62 +++++++++++++++++++++++++++++++++--- tests/market/test_tables.py | 33 +++++++++++++++++++ 9 files changed, 252 insertions(+), 28 deletions(-) diff --git a/src/cli/market.py b/src/cli/market.py index 943709c..7bde991 100644 --- a/src/cli/market.py +++ b/src/cli/market.py @@ -95,9 +95,10 @@ def _require_key(provider: str, gap_source: str, *, write: bool) -> str | None: # ── entities ───────────────────────────────────────────────────────── -def _vocabularies() -> tuple[set[str], set[str], list[Any]]: - """Family keys, specialty keys (CMS vocabulary plus the seat labels) and - the RUC seats, from the replica.""" +def _vocabularies() -> tuple[set[str], set[str], list[Any], edgar.AmaIndex]: + """Family keys, specialty keys (CMS vocabulary plus the seat labels), the + RUC seats and the licensed CPT descriptor runs (the D3 guard), from the + replica.""" from pfs.codetables import read_seats from pfs.commenters import specialty_vocab from pfs.families import FAMILIES @@ -106,9 +107,14 @@ def _vocabularies() -> tuple[set[str], set[str], list[Any]]: try: specialties = {*specialty_vocab(con), *SEAT_LABEL_KEYS} seats = read_seats(con, "RUC") + ama = edgar.AmaIndex.of( + d + for (d,) in con.execute("SELECT descriptor FROM pfs.cpt_code").fetchall() + if d + ) finally: con.close() - return set(FAMILIES), specialties, seats + return set(FAMILIES), specialties, seats, ama def _counts(entities: list[EntityRow]) -> str: @@ -165,7 +171,7 @@ def entities( submissions and filings; --sic-sweep adds every health-SIC filer.""" companies, orgs = load_seed() try: - families, specialties, seats = _vocabularies() + families, specialties, seats, ama = _vocabularies() except Exception as exc: if not is_missing_table_error(exc): raise @@ -239,6 +245,16 @@ def entities( typer.echo(f"quote error: {e}") raise typer.Exit(1) + ama_errors = [ + f"{who}: quote shares an 8-word run with a CPT descriptor" + for who, _c, cite in edgar.filing_cites(companies) + if edgar.shares_ama_run(cite.quote, ama) + ] + if ama_errors: + for e in ama_errors: + typer.echo(f"ama error: {e}") + raise typer.Exit(1) + org_rows = [edgar.org_entity(o) for o in orgs] all_entities = [e for e, _, _ in staged] + org_rows n_sec = sum(len(s) for _, s, _ in staged) diff --git a/src/market/edgar.py b/src/market/edgar.py index cf6c8a1..a7f9a51 100644 --- a/src/market/edgar.py +++ b/src/market/edgar.py @@ -237,7 +237,15 @@ def curated_links( item key.""" rows: list[LinkRow] = [] - def add(key: str, scope: str, target: str, c: Cite, cik: str) -> None: + def add( + key: str, + scope: str, + target: str, + c: Cite, + cik: str, + valid_from: dt.date | None = None, + valid_to: dt.date | None = None, + ) -> None: rows.append( LinkRow( key, @@ -249,12 +257,22 @@ def curated_links( c.ref, "", cite(c, cik) if cite else "", + valid_from, + valid_to, ) ) for co in companies: for link in co.links: - add(co.cik, link.scope, link.target, link.cite, co.cik) + add( + co.cik, + link.scope, + link.target, + link.cite, + co.cik, + link.valid_from, + link.valid_to, + ) for o in orgs: for link in o.links: add(o.key, link.scope, link.target, link.cite, "") @@ -487,15 +505,31 @@ def quote_errors( _WORD = re.compile(r"[a-z0-9]+") -def shares_ama_run(text: str, descriptors: Iterable[str], n: int = 8) -> bool: - """D3 guard: *text* shares a run of *n* words with any licensed CPT - descriptor (same tokenization as ``pfs.origin.shares_run``).""" +class AmaIndex(frozenset): + """The 8-word runs of licensed CPT descriptors, built once.""" + + @classmethod + def of(cls, descriptors: Iterable[str], n: int = 8) -> AmaIndex: + grams: set[tuple[str, ...]] = set() + for d in descriptors: + w = _WORD.findall(d.lower()) + grams.update(tuple(w[i : i + n]) for i in range(len(w) - n + 1)) + return cls(grams) + + +def shares_ama_run( + text: str, descriptors: Iterable[str] | AmaIndex, n: int = 8 +) -> bool: + """D3 guard: *text* shares a run of *n* words with a licensed CPT + descriptor (same tokenization as ``pfs.origin.shares_run``). Pass an + :class:`AmaIndex` to check many texts against the same descriptors.""" words = _WORD.findall(text.lower()) grams = {tuple(words[i : i + n]) for i in range(len(words) - n + 1)} if not grams: return False - for d in descriptors: - w = _WORD.findall(d.lower()) - if any(tuple(w[i : i + n]) in grams for i in range(len(w) - n + 1)): - return True - return False + index = ( + descriptors + if isinstance(descriptors, AmaIndex) + else AmaIndex.of(descriptors, n) + ) + return not grams.isdisjoint(index) diff --git a/src/market/seed.py b/src/market/seed.py index 834f0d5..e694da4 100644 --- a/src/market/seed.py +++ b/src/market/seed.py @@ -66,7 +66,8 @@ class SeedSecurity: exchange: str valid_from: date | None = None valid_to: date | None = None - #: the filing that dates ``valid_from`` (a prospectus, merger 8-K, ...) + #: evidence for the ticker, and for ``valid_from`` when that is set (a + #: prospectus, merger 8-K, a filing cover listing the symbol, ...) cite: Cite | None = None #: the filing that dates ``valid_to`` (a merger or delisting 8-K, ...) cite_to: Cite | None = None @@ -77,6 +78,9 @@ class SeedLink: scope: str target: str cite: Cite + #: the period the cited filing says the link holds for + valid_from: date | None = None + valid_to: date | None = None @dataclass(frozen=True) @@ -137,7 +141,15 @@ def _links(raw: Iterable[Mapping[str, Any]] | None, who: str) -> tuple[SeedLink, out = [] for item in raw or (): scope, target = str(item["scope"]), str(item["target"]) - out.append(SeedLink(scope, target, _cite(item.get("cite"), f"{who}/{scope}"))) + out.append( + SeedLink( + scope, + target, + _cite(item.get("cite"), f"{who}/{scope}"), + item.get("valid_from"), + item.get("valid_to"), + ) + ) return tuple(out) diff --git a/src/market/seed.yaml b/src/market/seed.yaml index 6406ada..c97a1a4 100644 --- a/src/market/seed.yaml +++ b/src/market/seed.yaml @@ -115,7 +115,7 @@ companies: - {ticker: ALHC, exchange: NASDAQ} cite: {adsh: "0001628280-26-012545", title: "Alignment Healthcare, Inc. Form 10-K filed 2026-02-27 (participation ended 2025-12-31)", quote: "We discontinued our participation in the ACO REACH model as of December 31, 2025.", accessed: "2026-11-12"} links: - - {scope: program, target: "APM", cite: {adsh: "0001628280-26-012545", title: "Alignment Healthcare, Inc. Form 10-K filed 2026-02-27 (participation ended 2025-12-31)", quote: "We discontinued our participation in the ACO REACH model as of December 31, 2025.", accessed: "2026-11-12"}} + - {scope: program, target: "APM", valid_to: 2025-12-31, cite: {adsh: "0001628280-26-012545", title: "Alignment Healthcare, Inc. Form 10-K filed 2026-02-27 (participation ended 2025-12-31)", quote: "We discontinued our participation in the ACO REACH model as of December 31, 2025.", accessed: "2026-11-12"}} - {scope: specialty, target: "PRIMARY CARE", cite: {adsh: "0001628280-26-012545", title: "Alignment Healthcare, Inc. Form 10-K filed 2026-02-27", quote: "In some situations, we have capitation contracts with individual or groups of primary care providers and specialists for an actuarially determined, fixed fee per month to provide a basket of required medical services to our members.", accessed: "2026-11-12"}} - handle: CLOV name: "CLOVER HEALTH INVESTMENTS, CORP. /DE" @@ -124,9 +124,9 @@ companies: aliases: ["CLOVER HEALTH INVESTMENTS CORP DE"] securities: - {ticker: CLOV, exchange: NASDAQ} - cite: {adsh: "0001801170-23-000015", title: "CLOVER HEALTH INVESTMENTS, CORP. /DE Form 10-K filed 2023-03-01 (ACO REACH participation through 2023)", quote: "Effective January 1, 2023, after the redesign of the DC Model to the ACO REACH Model, DCE is now known as the ACO.", accessed: "2026-11-12"} + cite: {adsh: "0001801170-24-000064", title: "CLOVER HEALTH INVESTMENTS, CORP. /DE Form 10-K filed 2024-03-14 (ACO REACH participation ended 2023-12-31)", quote: "On December 1, 2023, the Company notified CMS that it will no longer participate as a REACH ACO beginning with the 2024 performance year.", accessed: "2026-11-12"} links: - - {scope: program, target: "APM", cite: {adsh: "0001801170-23-000015", title: "CLOVER HEALTH INVESTMENTS, CORP. /DE Form 10-K filed 2023-03-01 (ACO REACH participation through 2023)", quote: "Effective January 1, 2023, after the redesign of the DC Model to the ACO REACH Model, DCE is now known as the ACO.", accessed: "2026-11-12"}} + - {scope: program, target: "APM", valid_to: 2023-12-31, cite: {adsh: "0001801170-24-000064", title: "CLOVER HEALTH INVESTMENTS, CORP. /DE Form 10-K filed 2024-03-14 (ACO REACH participation ended 2023-12-31)", quote: "On December 1, 2023, the Company notified CMS that it will no longer participate as a REACH ACO beginning with the 2024 performance year.", accessed: "2026-11-12"}} - {scope: program, target: "CARE-MANAGEMENT", cite: {adsh: "0001801170-26-000057", title: "CLOVER HEALTH INVESTMENTS, CORP. /DE Form 10-K filed 2026-02-27", quote: "This external offering aims to equip clinician users with the Company's already built, clinician-centric, and AI-powered care management platform.", accessed: "2026-11-12"}} - handle: EVH name: "Evolent Health, Inc." @@ -176,6 +176,7 @@ companies: status: listed aliases: ["PEDIATRIX MEDICAL GROUP", "MEDNAX"] securities: + - {ticker: PDX, exchange: NYSE, valid_to: 2008-12-31, cite: {adsh: "0001193125-08-040809", title: "Pediatrix Medical Group, Inc. Form 10-K filed 2008-02-28", quote: "OF EQUITY SECURITIES Price Range of Common Stock Our common stock is traded on the New York Stock Exchange (the \"NYSE\") under the symbol \"PDX.\" The high and low sales price for a share of our common stock for each quarter during our last two fiscal years is set forth below, as reported in the NYSE consolidated transaction reporting system: High Low 2007 First Quarter $ 57.41 $", accessed: "2026-11-12"}, cite_to: {adsh: "0001193125-09-000041", title: "Pediatrix Medical Group, Inc. Form 8-K12B filed 2009-01-02", quote: "The Mednax Common Stock will trade on the New York Stock Exchange under the \"MD\" ticker symbol in place of the Pediatrix Common Stock. In connection with the Merger, Mednax entered into an Assignment and Joinder Agreement, dated as of January 1, 2009", accessed: "2026-11-12"}} - {ticker: MD, exchange: NYSE, valid_from: 2009-01-01, cite: {adsh: "0001193125-09-000041", title: "Pediatrix Medical Group, Inc. Form 8-K12B filed 2009-01-02", quote: "The Mednax Common Stock will trade on the New York Stock Exchange under the \"MD\" ticker symbol in place of the Pediatrix Common Stock. In connection with the Merger, Mednax entered into an Assignment and Joinder Agreement, dated as of January 1, 2009", accessed: "2026-11-12"}} cite: {adsh: "0001193125-26-058074", title: "Pediatrix Medical Group, Inc. Form 10-K filed 2026-02-19", quote: "For instance, the 2024 Medicare Physician Fee Schedule Final Rule decreased the 2024 conversion factor (i.e., the amount Medicare pays per relative value unit (wRVU)) by nearly 3.4% from the 2023 amount.", accessed: "2026-11-12"} links: @@ -266,17 +267,16 @@ companies: - {ticker: CI, exchange: NYSE, valid_from: 2018-12-21, cite: {adsh: "0001140361-18-045479", title: "Cigna Group Form 8-K12B filed 2018-12-20", quote: "Cigna common stock will cease trading on the New York Stock Exchange (the \"NYSE\") and Express Scripts common stock will cease trading on the Nasdaq Global Select Market, in each case prior to the open of trading on December 21, 2018, and New Cigna common stock will commence trading on the NYSE on December 21, 2018 under the symbol \"CI\".", accessed: "2026-11-12"}} cite: {adsh: "0001739940-26-000006", title: "Cigna Group Form 10-K filed 2026-02-26", quote: "The Evernorth Health Services segment, through our Pharmacy Benefit Services and Specialty and Care Services operating segments, provides independent and coordinated health solutions and capabilities to enable the health care system to work better and help people live healthier lives.", accessed: "2026-11-12"} links: - - {scope: program, target: "CARE-MANAGEMENT", cite: {adsh: "0001739940-26-000006", title: "Cigna Group Form 10-K filed 2026-02-26", quote: "The Evernorth Health Services segment, through our Pharmacy Benefit Services and Specialty and Care Services operating segments, provides independent and coordinated health solutions and capabilities to enable the health care system to work better and help people live healthier lives.", accessed: "2026-11-12"}} - handle: ELV name: "Elevance Health, Inc." cik: "0001156039" status: listed aliases: ["ELEVANCE HEALTH", "ANTHEM", "CARELON"] securities: - - {ticker: ELV, exchange: NYSE, valid_from: 2022-06-28, cite: {adsh: "0001193125-22-183957", title: "Elevance Health, Inc. Form 8-K filed 2022-06-28", quote: "to Rule 13e-4(c) under the Exchange Act (17 CFR 240.13e-4(c)) Securities registered pursuant to Section 12(b) of the Act: Title of each class Trading Symbol(s) Name of each exchange on which registered Common Stock ELV NYSE Indicate by check mark whether the registrant is an emerging growth company as defined in Rule 405 of the Securities Act of 1933 (§230.405 of this chapter) or", accessed: "2026-11-12"}} + - {ticker: ANTM, exchange: NYSE, valid_to: 2022-06-27, cite: {adsh: "0001193125-22-165676", title: "Elevance Health, Inc. Form 8-K filed 2022-06-02", quote: "registered pursuant to Section 12(b) of the Act: Title of each class Trading Symbol(s) Name of each exchange on which registered Common Stock ANTM NYSE Indicate by check mark whether the registrant is an emerging growth company as defined in Rule 405 of the Securities Act of 1933 (§230.405 of this chapter) or Rule 12b-2 of the Securities Exchange Act of 1934 (§240.12b-2 of this", accessed: "2026-11-12"}, cite_to: {adsh: "0001156039-22-000081", title: "Elevance Health, Inc. Form 10-Q filed 2022-07-20", quote: "We began operating as Elevance Health, Inc. and trading under our new ticker symbol \"ELV\" on June 28, 2022.", accessed: "2026-11-12"}} + - {ticker: ELV, exchange: NYSE, valid_from: 2022-06-28, cite: {adsh: "0001156039-22-000081", title: "Elevance Health, Inc. Form 10-Q filed 2022-07-20", quote: "We began operating as Elevance Health, Inc. and trading under our new ticker symbol \"ELV\" on June 28, 2022.", accessed: "2026-11-12"}} cite: {adsh: "0001156039-26-000013", title: "Elevance Health, Inc. Form 10-K filed 2026-02-06", quote: "In addition, we provide a broad array of managed care services to fee-based customers, including claims processing, stop loss insurance, care provider network access, medical management, care management, wellness programs, actuarial services and other administrative services.", accessed: "2026-11-12"} links: - - {scope: program, target: "CARE-MANAGEMENT", cite: {adsh: "0001156039-26-000013", title: "Elevance Health, Inc. Form 10-K filed 2026-02-06", quote: "In addition, we provide a broad array of managed care services to fee-based customers, including claims processing, stop loss insurance, care provider network access, medical management, care management, wellness programs, actuarial services and other administrative services.", accessed: "2026-11-12"}} - handle: LH name: "LABCORP HOLDINGS INC." cik: "0000920148" diff --git a/src/market/tables.py b/src/market/tables.py index ed48c5c..02ced33 100644 --- a/src/market/tables.py +++ b/src/market/tables.py @@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS market.security ( CREATE TABLE IF NOT EXISTS market.entity_link ( entity_key VARCHAR, scope VARCHAR, target VARCHAR, basis VARCHAR, strength VARCHAR, cite_kind VARCHAR, cite_ref VARCHAR, - passage_id VARCHAR, item_key VARCHAR + passage_id VARCHAR, item_key VARCHAR, valid_from DATE, valid_to DATE ); CREATE TABLE IF NOT EXISTS market.filing ( cik VARCHAR, adsh VARCHAR, form VARCHAR, filed DATE, accepted VARCHAR, @@ -45,6 +45,11 @@ def ensure_tables(con: Any) -> None: con.execute(stmt) # a market.entity created before the pseudo flag existed con.execute("ALTER TABLE market.entity ADD COLUMN IF NOT EXISTS pseudo BOOLEAN") + # a market.entity_link created before links carried dates + for col in ("valid_from", "valid_to"): + con.execute( + f"ALTER TABLE market.entity_link ADD COLUMN IF NOT EXISTS {col} DATE" + ) @dataclass(frozen=True) @@ -89,6 +94,10 @@ class LinkRow: cite_ref: str passage_id: str = "" item_key: str = "" + #: the period the link holds for, when the cited filing dates it + #: (``None`` = no stated bound) + valid_from: date | None = None + valid_to: date | None = None @dataclass(frozen=True) diff --git a/tests/cli/test_market_cli.py b/tests/cli/test_market_cli.py index 236f97b..ffbb4e0 100644 --- a/tests/cli/test_market_cli.py +++ b/tests/cli/test_market_cli.py @@ -56,6 +56,12 @@ def _build_db() -> duckdb.DuckDBPyConnection: "INSERT INTO terminology.cms_acceptable_provider_specialty_codes VALUES (?)", [(s.title(),) for s in sorted(_specialties())], ) + c.execute("CREATE SCHEMA pfs") + c.execute("CREATE TABLE pfs.cpt_code (code VARCHAR, descriptor VARCHAR)") + c.execute( + "INSERT INTO pfs.cpt_code VALUES ('99999', 'a licensed descriptor that no " + "filing quote should ever copy word for word')" + ) write_seats( c, "RUC", @@ -548,3 +554,36 @@ def test_entities_retires_organizations_the_seed_dropped(env): == 0 ) assert "ASCO" not in {e.key for e in T.read_entities(c, "organization")} + + +def test_entities_aborts_when_a_quote_copies_a_cpt_descriptor(env): + c, _, published, hoard = env + _cache_offline(hoard) + victim = load_seed()[0][0].cite.quote + c.execute("DELETE FROM pfs.cpt_code") + c.execute("INSERT INTO pfs.cpt_code VALUES ('1', ?)", [victim]) + res = runner.invoke(app, ["market", "entities", "--offline", "--write"]) + assert res.exit_code == 1 and "ama error: OSH" in res.output + assert published == [] + assert c.execute( + "SELECT count(*) FROM information_schema.tables WHERE table_schema = 'market'" + ).fetchone() == (0,) + + +def test_entities_stores_ticker_history_and_link_dates(env): + c, _, _, hoard = env + _cache_offline(hoard) + assert ( + runner.invoke(app, ["market", "entities", "--offline", "--write"]).exit_code + == 0 + ) + assert [(s.ticker, s.valid_to) for s in T.read_securities(c, "0001156039")] == [ + ("ANTM", date(2022, 6, 27)), + ("ELV", None), + ] + (clov,) = [ + l + for l in T.read_links(c, basis="curated", target="APM") + if l.entity_key == "0001801170" + ] + assert clov.valid_to == date(2023, 12, 31) diff --git a/tests/market/test_edgar.py b/tests/market/test_edgar.py index 585fff2..b7d77dd 100644 --- a/tests/market/test_edgar.py +++ b/tests/market/test_edgar.py @@ -450,3 +450,30 @@ def test_shipped_quotes_share_no_run_with_ama_descriptors(): companies, _ = load_seed() for who, _c, cite in edgar.filing_cites(companies): assert not edgar.shares_ama_run(cite.quote, descriptors), (who, cite.quote) + + +def test_curated_links_carry_the_seed_link_dates(): + companies, orgs = load_seed() + rows = edgar.curated_links(companies, orgs) + clov = next(r for r in rows if r.entity_key == "0001801170" and r.target == "APM") + assert clov.valid_to == date(2023, 12, 31) and clov.valid_from is None + assert all(r.valid_to is None for r in rows if r.entity_key == "0001564406") + + +def test_same_cik_rename_gives_one_security_row_per_ticker(): + companies, _ = load_seed() + elv = next(c for c in companies if c.handle == "ELV") + sub = {"cik": elv.cik, "tickers": ["ELV"], "exchanges": ["NYSE"]} + secs = edgar.securities_for(sub, elv, ["ELV"]) + assert [(s.ticker, s.valid_from, s.valid_to, s.source) for s in secs] == [ + ("ANTM", None, date(2022, 6, 27), "seed"), + ("ELV", date(2022, 6, 28), None, "seed"), + ] + + +def test_ama_index_is_built_once_and_matches_like_the_list_form(): + run = "one two three four five six seven eight nine" + idx = edgar.AmaIndex.of([run, "short"]) + assert edgar.shares_ama_run("x " + run, idx) + assert not edgar.shares_ama_run("one two three", idx) + assert not edgar.shares_ama_run("", idx) diff --git a/tests/market/test_seed.py b/tests/market/test_seed.py index c06d9d1..8d5b0f0 100644 --- a/tests/market/test_seed.py +++ b/tests/market/test_seed.py @@ -36,11 +36,10 @@ def test_seed_loads_and_every_entry_cited(): 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 - assert entry.links or isinstance(entry, S.SeedOrg) 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 all(c.links for c in companies) + 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"} @@ -316,5 +315,60 @@ def test_shipped_ticker_windows_come_from_filing_evidence(): 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 - assert md.cite.adsh and md.valid_from is not None + md = by["MD"].securities[-1] + assert md.ticker == "MD" and md.cite.adsh and md.valid_from is not None + + +def _by(): + companies, _ = S.load_seed() + return {c.handle: c for c in companies} + + +def test_payers_without_a_cms_program_quote_have_no_care_management_link(): + by = _by() + for h in ("CI", "ELV"): + assert by[h].links == () and by[h].cite.quote + + +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", + date(2022, 6, 28), + None, + ) + assert antm.cite.adsh and antm.cite_to.adsh == elv.cite.adsh + 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", date(2009, 1, 1)) + assert pdx.cite.quote and pdx.cite_to.adsh == md.cite.adsh + + +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, + ) diff --git a/tests/market/test_tables.py b/tests/market/test_tables.py index 81254df..e54083c 100644 --- a/tests/market/test_tables.py +++ b/tests/market/test_tables.py @@ -255,3 +255,36 @@ def test_retire_organizations_removes_entities_and_links(con): assert T.retire_organizations(con, ["A"]) == 1 assert [e.key for e in T.read_entities(con)] == [ENTITY.key, "A"] assert [l.entity_key for l in T.read_links(con)] == ["A"] + + +def test_link_dates_roundtrip(con): + row = T.LinkRow( + "1", + "program", + "APM", + "curated", + "curated", + "adsh", + "a", + valid_from=date(2021, 1, 1), + valid_to=date(2023, 12, 31), + ) + T.write_links(con, [row]) + assert T.read_links(con) == [row] + + +def test_ensure_tables_adds_dates_to_an_existing_link_table(): + c = duckdb.connect(":memory:") + c.execute("CREATE SCHEMA market") + c.execute( + "CREATE TABLE market.entity_link (entity_key VARCHAR, scope VARCHAR, target VARCHAR, " + "basis VARCHAR, strength VARCHAR, cite_kind VARCHAR, cite_ref VARCHAR, " + "passage_id VARCHAR, item_key VARCHAR)" + ) + c.execute( + "INSERT INTO market.entity_link VALUES ('1','program','APM','curated','c','adsh','a','','')" + ) + T.ensure_tables(c) + (old,) = T.read_links(c) + assert old.valid_from is None and old.valid_to is None + c.close()