Download tests/test_quality.py from ZipLime/us-treasury-events: direct link, hf CLI and curl.
- Browser
- Download file 13.3 kB
-
https://huggingface.co/datasets/ZipLime/us-treasury-events/resolve/main/tests/test_quality.py
- Command line
-
hf download hf://datasets/ZipLime/us-treasury-events/tests/test_quality.py
-
curl -L -o test_quality.py https://huggingface.co/datasets/ZipLime/us-treasury-events/resolve/main/tests/test_quality.py
13.3 kB
| """The gate, and the look-ahead it exists to catch. | |
| Each test builds a small, valid dataset and breaks exactly one thing in the way | |
| a real bug would break it. A check that cannot be made to fail is not a check. | |
| """ | |
| from __future__ import annotations | |
| from datetime import UTC, date, datetime | |
| import polars as pl | |
| import pytest | |
| from recipe.quality import validate | |
| from recipe.schema import CONFIG_SCHEMAS, align | |
| from recipe.store import write_table | |
| AUCTION_AT = datetime(2026, 9, 10, 17, 2, tzinfo=UTC) # 13:02 Eastern | |
| CURVE_AT = datetime(2026, 9, 10, 22, 0, tzinfo=UTC) # 18:00 Eastern | |
| OBSERVED_AT = datetime(2026, 9, 10, 19, 30, tzinfo=UTC) # 15:30 Eastern | |
| ANNOUNCED_AT = datetime(2026, 9, 4, 3, 59, 59, tzinfo=UTC) # end of 3 September | |
| def _rows() -> dict[str, list[dict]]: | |
| provenance = { | |
| "source": "treasurydirect.gov", | |
| "source_url": "https://example.invalid/auction", | |
| "raw_sha256": "abc", | |
| "parser_version": "1.0.0", | |
| "first_seen_at": AUCTION_AT, | |
| "ingested_at": datetime(2026, 9, 11, tzinfo=UTC), | |
| } | |
| rates = { | |
| "source": "home.treasury.gov", | |
| "source_url": "https://example.invalid/rates", | |
| "raw_sha256": "def", | |
| "parser_version": "1.0.0", | |
| "first_seen_at": CURVE_AT, | |
| "ingested_at": datetime(2026, 9, 11, tzinfo=UTC), | |
| } | |
| curve = { | |
| "curve_date": date(2026, 9, 10), | |
| "market_observation_at": OBSERVED_AT, | |
| "publication_at": None, | |
| "knowledge_at": CURVE_AT, | |
| "knowledge_date": CURVE_AT, | |
| "knowledge_time_precision": "modeled_conservative", | |
| "availability_time_basis": "treasury_usually_available_by_1800_et", | |
| **rates, | |
| } | |
| return { | |
| "auction_calendar": [ | |
| { | |
| "auction_id": "UST_2026-09-10_912810UW6", | |
| "cusip": "912810UW6", | |
| "security_type": "bond", | |
| "security_term": "29-Year 11-Month", | |
| "reopening": True, | |
| "announcement_date": date(2026, 9, 3), | |
| "announcement_at": ANNOUNCED_AT, | |
| "auction_date": date(2026, 9, 10), | |
| "auction_at": AUCTION_AT, | |
| "issue_date": date(2026, 9, 15), | |
| "maturity_date": date(2056, 8, 15), | |
| "offering_amount": 22_000_000_000.0, | |
| "status": "published", | |
| "knowledge_at": ANNOUNCED_AT, | |
| "knowledge_date": ANNOUNCED_AT, | |
| "knowledge_time_precision": "date", | |
| **provenance, | |
| } | |
| ], | |
| "auction_announcements": [ | |
| { | |
| "announcement_id": "UST_2026-09-10_912810UW6_ANN", | |
| "auction_id": "UST_2026-09-10_912810UW6", | |
| "release_at": None, | |
| "knowledge_at": ANNOUNCED_AT, | |
| "knowledge_date": ANNOUNCED_AT, | |
| "knowledge_time_precision": "date", | |
| "offering_amount": 22_000_000_000.0, | |
| "security_type": "bond", | |
| "security_term": "29-Year 11-Month", | |
| "cusip": "912810UW6", | |
| "announcement_date": date(2026, 9, 3), | |
| "auction_date": date(2026, 9, 10), | |
| "issue_date": date(2026, 9, 15), | |
| "maturity_date": date(2056, 8, 15), | |
| "reopening": True, | |
| **provenance, | |
| } | |
| ], | |
| "auction_results": [ | |
| { | |
| "result_id": "UST_2026-09-10_912810UW6_RES", | |
| "auction_id": "UST_2026-09-10_912810UW6", | |
| "cusip": "912810UW6", | |
| "security_type": "bond", | |
| "security_term": "29-Year 11-Month", | |
| "auction_date": date(2026, 9, 10), | |
| "issue_date": date(2026, 9, 15), | |
| "maturity_date": date(2056, 8, 15), | |
| "result_release_at": AUCTION_AT, | |
| "knowledge_at": AUCTION_AT, | |
| "knowledge_date": AUCTION_AT, | |
| "knowledge_time_precision": "minute", | |
| "auction_format": "Single-Price", | |
| "offering_amount": 22_000_000_000.0, | |
| "total_tendered": 57_466_872_500.0, | |
| "total_accepted": 22_000_024_000.0, | |
| "competitive_tendered": 57_424_319_000.0, | |
| "competitive_accepted": 21_957_470_500.0, | |
| "noncompetitive_accepted": 42_553_500.0, | |
| "high_yield": 5.308, | |
| "bid_to_cover": 2.61, | |
| "allocation_percentage": 64.29, | |
| "primary_dealer_accepted": 484_800_000.0, | |
| "direct_bidder_accepted": 4_019_900_000.0, | |
| "indirect_bidder_accepted": 17_452_770_500.0, | |
| "dealer_accepted_pct": 2.203996, | |
| "direct_accepted_pct": 18.272252, | |
| "indirect_accepted_pct": 79.330688, | |
| "noncompetitive_accepted_pct": 0.193425, | |
| "tendered_to_offering": 2.612131, | |
| "wi_yield": None, | |
| "auction_tail": None, | |
| "status": "published", | |
| **provenance, | |
| } | |
| ], | |
| "nominal_yield_curve": [ | |
| {**curve, "curve_id": f"nominal_2026-09-10_{maturity}", "curve_type": "nominal", | |
| "maturity": maturity, "source_label": maturity, "maturity_years": years, | |
| "yield_pct": value, "methodology_id": "monotone_convex"} | |
| for maturity, years, value in ( | |
| ("3M", 0.25, 4.0), ("2Y", 2.0, 4.56), ("5Y", 5.0, 4.75), | |
| ("10Y", 10.0, 4.95), ("30Y", 30.0, 5.37), | |
| ) | |
| ], | |
| "real_yield_curve": [ | |
| {**curve, "curve_id": f"real_2026-09-10_{maturity}", "curve_type": "real", | |
| "maturity": maturity, "source_label": maturity, "maturity_years": years, | |
| "yield_pct": value, "methodology_id": None} | |
| for maturity, years, value in (("5Y", 5.0, 2.29), ("10Y", 10.0, 2.55)) | |
| ], | |
| "bill_rates": [ | |
| { | |
| "bill_rate_id": "bill_2026-09-10_13W", | |
| "rate_date": date(2026, 9, 10), | |
| "maturity": "13W", | |
| "source_label": "13 WEEKS", | |
| "maturity_weeks": 13.0, | |
| "bank_discount_rate": 3.93, | |
| "coupon_equivalent_rate": 4.03, | |
| "market_observation_at": OBSERVED_AT, | |
| "knowledge_at": CURVE_AT, | |
| "knowledge_date": CURVE_AT, | |
| "knowledge_time_precision": "modeled_conservative", | |
| "availability_time_basis": "treasury_usually_available_by_1800_et", | |
| **rates, | |
| } | |
| ], | |
| "curve_features": [ | |
| { | |
| "feature_id": "curve_2026-09-10", | |
| "curve_date": date(2026, 9, 10), | |
| "knowledge_at": CURVE_AT, | |
| "knowledge_date": CURVE_AT, | |
| "knowledge_time_precision": "modeled_conservative", | |
| "slope_3m10y_bp": 95.0, | |
| "slope_2y10y_bp": 39.0, | |
| "slope_5y30y_bp": 62.0, | |
| "real_5y10y_slope_bp": 26.0, | |
| "nominal_10y_minus_real_10y_bp": 240.0, | |
| "derived_from": "nominal_yield_curve, real_yield_curve", | |
| "parser_version": "1.0.0", | |
| "ingested_at": datetime(2026, 9, 11, tzinfo=UTC), | |
| } | |
| ], | |
| "pit": [ | |
| { | |
| "entity_id": "UST_CMT_10Y", | |
| "event_date": date(2026, 9, 10), | |
| "knowledge_date": CURVE_AT, | |
| "subset": "nominal_yield_curve", | |
| "record_id": "nominal_2026-09-10_10Y", | |
| "actual": 4.95, | |
| "previous": None, | |
| "change": None, | |
| "forecast": None, | |
| "unit": "percent", | |
| "value_method": "reported", | |
| "knowledge_time_precision": "modeled_conservative", | |
| "source_url": "https://example.invalid/rates", | |
| "parser_version": "1.0.0", | |
| "ingested_at": datetime(2026, 9, 11, tzinfo=UTC), | |
| } | |
| ], | |
| } | |
| def _write(tmp_path, rows: dict[str, list[dict]]): | |
| data_dir = tmp_path / "data" | |
| for name, table in rows.items(): | |
| frame = align(pl.DataFrame(table, infer_schema_length=None), CONFIG_SCHEMAS[name]) | |
| write_table(data_dir, name, frame) | |
| return data_dir | |
| def good(tmp_path): | |
| return _write(tmp_path, _rows()) | |
| def test_a_correct_dataset_passes(good): | |
| report = validate(good) | |
| assert report.ok, report.errors | |
| def test_a_curve_dated_to_half_past_three_is_caught(tmp_path): | |
| """The check this dataset exists for. | |
| The quotations behind the curve are struck at 15:30 Eastern and the | |
| Treasury says the rates are usually on its site by 18:00. Dating the curve | |
| to 15:30 hands a backtest two and a half hours it did not have, every | |
| trading day since 1990. | |
| """ | |
| rows = _rows() | |
| for row in rows["nominal_yield_curve"]: | |
| row["knowledge_at"] = row["knowledge_date"] = OBSERVED_AT | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("before 18:00 Eastern" in message for message in report.errors) | |
| def test_a_curve_claiming_an_exact_time_is_caught(tmp_path): | |
| rows = _rows() | |
| for row in rows["nominal_yield_curve"]: | |
| row["knowledge_time_precision"] = "exact" | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("precision the Treasury never published" in message for message in report.errors) | |
| def test_a_result_knowable_before_its_auction_is_caught(tmp_path): | |
| rows = _rows() | |
| earlier = datetime(2026, 9, 9, 17, 2, tzinfo=UTC) | |
| rows["auction_results"][0]["knowledge_at"] = earlier | |
| rows["auction_results"][0]["knowledge_date"] = earlier | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("before their auction" in message for message in report.errors) | |
| def test_a_calendar_entry_knowable_before_its_announcement_is_caught(tmp_path): | |
| rows = _rows() | |
| earlier = datetime(2026, 9, 1, 4, 0, tzinfo=UTC) | |
| rows["auction_calendar"][0]["knowledge_at"] = earlier | |
| rows["auction_calendar"][0]["knowledge_date"] = earlier | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("before they were announced" in message for message in report.errors) | |
| def test_accepting_more_than_was_tendered_is_caught(tmp_path): | |
| rows = _rows() | |
| rows["auction_results"][0]["total_accepted"] = 99_000_000_000.0 | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("more than was tendered" in message for message in report.errors) | |
| def test_bidder_categories_that_do_not_add_up_are_caught(tmp_path): | |
| """Reading the wrong column would break the sum; rounding would not.""" | |
| rows = _rows() | |
| rows["auction_results"][0]["indirect_bidder_accepted"] = 1_000_000.0 | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("do not sum" in message for message in report.errors) | |
| def test_a_security_maturing_before_it_is_issued_is_caught(tmp_path): | |
| rows = _rows() | |
| rows["auction_results"][0]["maturity_date"] = date(2026, 9, 1) | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("mature on or before" in message for message in report.errors) | |
| def test_a_fabricated_tail_is_caught(tmp_path): | |
| """No official point-in-time when-issued series exists, so neither column may.""" | |
| rows = _rows() | |
| rows["auction_results"][0]["auction_tail"] = 1.5 | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("does not invent one" in message for message in report.errors) | |
| def test_two_yields_for_one_maturity_on_one_day_are_caught(tmp_path): | |
| rows = _rows() | |
| duplicate = dict(rows["nominal_yield_curve"][0]) | |
| duplicate["curve_id"] = "nominal_2026-09-10_3M_again" | |
| rows["nominal_yield_curve"].append(duplicate) | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("sharing a date, type and maturity" in message for message in report.errors) | |
| def test_a_null_yield_is_caught(tmp_path): | |
| """A maturity the Treasury did not publish should have no row at all.""" | |
| rows = _rows() | |
| rows["nominal_yield_curve"][0]["yield_pct"] = None | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("null yields" in message for message in report.errors) | |
| def test_a_curve_dated_in_the_future_is_caught(tmp_path): | |
| rows = _rows() | |
| ahead = date(datetime.now(UTC).year + 2, 1, 5) | |
| for row in rows["nominal_yield_curve"]: | |
| row["curve_date"] = ahead | |
| row["knowledge_at"] = row["knowledge_date"] = datetime(ahead.year, 1, 5, 23, 0, tzinfo=UTC) | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("dated in the future" in message for message in report.errors) | |
| def test_an_orphan_result_is_caught(tmp_path): | |
| rows = _rows() | |
| rows["auction_results"][0]["auction_id"] = "UST_1999-01-01_NOPE" | |
| report = validate(_write(tmp_path, rows)) | |
| assert not report.ok | |
| assert any("not in the calendar" in message for message in report.errors) | |