"""Reading the auction query, and the artifact that says when a result was out.""" from __future__ import annotations from datetime import UTC, date, datetime import pytest from recipe.pipeline import announcement_row, calendar_row, result_row, share from recipe.sources.auctions import ( Auction, auction_id, parse_artifact, parse_auctions, security_type, term_years, to_bool, to_date, to_float, ) def test_an_absent_field_is_absent_and_not_zero(): """Bidder categories begin in 2008; before that the Treasury published none.""" assert to_float("") is None assert to_float(None) is None assert to_float("not a number") is None assert to_float("0") == 0.0 assert to_float("21957470500") == 21957470500.0 def test_dates_and_flags(): assert to_date("2026-09-10T00:00:00") == date(2026, 9, 10) assert to_date("") is None assert to_bool("Yes") is True assert to_bool("No") is False assert to_bool("") is None @pytest.mark.parametrize( ("label", "expected"), [("10-Year", 10.0), ("4-Week", 0.076712), ("29-Year 10-Month", 29.833333), (None, None)], ) def test_term_years(label, expected): assert term_years(label) == expected def test_the_query_reads_every_security_type(auction_rows): auctions = parse_auctions(auction_rows) assert len(auctions) == len(auction_rows) kinds = {security_type(auction.row) for auction in auctions} assert {"bill", "bond", "tips", "frn", "cmb"} <= kinds def test_an_identifier_survives_a_reopening(auction_rows): """A CUSIP alone is not unique: a reopening auctions the same one again.""" row = dict(auction_rows[0]) first = auction_id(row) row["auctionDate"] = "2027-01-05T00:00:00" assert auction_id(row) != first assert first.endswith(row["cusip"]) def test_the_artifact_states_the_release_minute(load_fixture): stated = parse_artifact(load_fixture("auctions/R_20260910_3.xml")) # The 29-year 11-month bond closed at 13:00 and its result went out at # 13:02. Two minutes, stated by the Treasury, not assumed by this recipe. assert stated.release_time == "13:02" assert stated.competitive_close == "13:00" assert stated.noncompetitive_close == "12:00" def test_an_announcement_artifact_states_no_release_time(load_fixture): stated = parse_artifact(load_fixture("auctions/A_20260903_5.xml")) assert stated.release_time is None assert stated.competitive_close def test_a_result_is_dated_to_the_minute_the_treasury_stated(auction_rows, load_fixture): auction = next(a for a in parse_auctions(auction_rows) if a.result_artifact == "R_20260910_3.xml") stated = parse_artifact(load_fixture("auctions/R_20260910_3.xml")) row = result_row( auction, artifact=None, times={"release_time": stated.release_time, "competitive_close": stated.competitive_close, "noncompetitive_close": stated.noncompetitive_close}, ) assert row["knowledge_time_precision"] == "minute" # 13:02 Eastern in September is 17:02 UTC. assert row["knowledge_at"] == datetime(2026, 9, 10, 17, 2, tzinfo=UTC) assert row["result_release_at"] == row["knowledge_at"] def test_a_result_with_no_artifact_is_dated_to_the_end_of_its_day(auction_rows): """Before March 2008 the Treasury published no result artifact. The auction closed at 1:00 p.m. and the result followed within minutes, but the Treasury did not write down when. Assuming the close would hand a backtest the number before anyone had it. """ auction = next( a for a in parse_auctions(auction_rows) if a.auction_date == date(1998, 7, 27) ) row = result_row(auction, artifact=None, times={}) assert row["knowledge_time_precision"] == "date" assert row["result_release_at"] is None assert row["knowledge_at"] == datetime(1998, 7, 28, 3, 59, 59, tzinfo=UTC) def test_a_calendar_entry_is_knowable_when_it_was_announced(auction_rows): auction = next( a for a in parse_auctions(auction_rows) if a.auction_date == date(2026, 9, 10) ) row = calendar_row(auction, artifact=None, times={}) assert row["announcement_date"] == date(2026, 9, 3) assert row["knowledge_at"].date() in {date(2026, 9, 3), date(2026, 9, 4)} assert row["knowledge_at"] < row["auction_at"] def test_an_announced_auction_has_a_calendar_row_and_no_result(auction_rows): auction = next(a for a in parse_auctions(auction_rows) if not a.has_result) assert calendar_row(auction, artifact=None, times={})["status"] == "scheduled" assert result_row(auction, artifact=None, times={}) is None assert announcement_row(auction, artifact=None, times={}) is not None def test_shares_are_null_rather_than_zero_when_a_leg_is_missing(): assert share(None, 100.0) is None assert share(50.0, None) is None assert share(50.0, 0.0) is None assert share(25.0, 100.0) == 25.0 def test_the_tail_is_never_fabricated(auction_rows): """No official point-in-time when-issued series exists, so neither column can.""" for auction in parse_auctions(auction_rows): row = result_row(auction, artifact=None, times={}) if row is None: continue assert row["wi_yield"] is None assert row["auction_tail"] is None def test_derived_shares_use_only_numbers_from_the_same_result(auction_rows): auction = next( a for a in parse_auctions(auction_rows) if a.auction_date == date(2026, 9, 10) ) row = result_row(auction, artifact=None, times={}) accepted = row["total_accepted"] assert row["indirect_accepted_pct"] == pytest.approx( 100 * row["indirect_bidder_accepted"] / accepted, abs=1e-6 ) def test_an_auction_with_no_date_is_not_an_auction(): assert parse_auctions([{"cusip": "X", "auctionDate": ""}]) == [] assert isinstance(Auction(auction_id="x", row={}), Auction) def test_a_row_is_reused_when_its_artifact_was_read(tmp_path): """The digest decides, not the precision. 1 357 artifacts exist and state no release time. Their rows are dated to the end of the auction day, and an earlier version of this rule refetched every one of them on every run, looking for a line that was never going to appear. A row with no digest is one whose artifact was never found, and that one is worth looking for again. """ import polars as pl from recipe.pipeline import held_results from recipe.schema import RESULT_SCHEMA, align from recipe.store import write_table rows = [ # Artifact read, stated a release minute. {"result_id": "A", "auction_id": "UST_2020-01-02_X", "knowledge_time_precision": "minute", "raw_sha256": "aaa"}, # Artifact read, stated no release time. Settled all the same. {"result_id": "B", "auction_id": "UST_2009-03-05_Y", "knowledge_time_precision": "date", "raw_sha256": "bbb"}, # No artifact was ever found. Look again. {"result_id": "C", "auction_id": "UST_1998-07-27_Z", "knowledge_time_precision": "date", "raw_sha256": None}, ] data_dir = tmp_path / "data" write_table(data_dir, "auction_results", align(pl.DataFrame(rows, infer_schema_length=None), RESULT_SCHEMA)) held = held_results(data_dir) assert set(held) == {"UST_2020-01-02_X", "UST_2009-03-05_Y"} def test_nothing_is_held_when_there_is_no_published_tree(tmp_path): from recipe.pipeline import held_results assert held_results(tmp_path / "nothing") == {} def test_closing_times_that_only_the_artifact_had_survive_a_reuse(tmp_path): """Five auctions of 11 114 state a noncompetitive close only in the artifact. Rebuilding their announcement from the query alone would silently drop a field the dataset already had, and the incremental build would stop matching the full one. """ import polars as pl from recipe.pipeline import held_closing_times from recipe.schema import ANNOUNCEMENT_SCHEMA, align from recipe.store import write_table rows = [ { "announcement_id": "UST_2020-07-13_X_ANN", "auction_id": "UST_2020-07-13_X", "competitive_close_at": datetime(2020, 7, 13, 15, 30, tzinfo=UTC), "noncompetitive_close_at": datetime(2020, 7, 10, 15, 0, tzinfo=UTC), } ] data_dir = tmp_path / "data" write_table(data_dir, "auction_announcements", align(pl.DataFrame(rows, infer_schema_length=None), ANNOUNCEMENT_SCHEMA)) held = held_closing_times(data_dir) assert held["UST_2020-07-13_X"]["noncompetitive_close_at"] == datetime( 2020, 7, 10, 15, 0, tzinfo=UTC )