"""The tables that say where the data came from and what may be done with it.""" from __future__ import annotations from pathlib import Path import polars as pl import yaml ROOT = Path(__file__).resolve().parent.parent METADATA = ROOT / "metadata" def test_every_source_is_official(): sources = yaml.safe_load((METADATA / "sources.yaml").read_text(encoding="utf-8"))["sources"] assert sources for source in sources: assert source["url"].startswith( ("https://www.treasurydirect.gov/", "https://home.treasury.gov/") ), source["source_id"] assert "Treasury" in source["organization"] assert sum(source["canonical"] for source in sources) >= 6 def test_the_licence_audit_records_what_was_read_and_when(): """A licence file without a date and a URL is an opinion, not an audit.""" licenses = yaml.safe_load( (METADATA / "source_licenses.yaml").read_text(encoding="utf-8") )["licenses"] assert licenses for entry in licenses: assert entry["terms_url"].startswith("https://") assert entry["inspected_on"] assert isinstance(entry["raw_redistribution_permitted"], bool) assert len(entry["determination"]) > 80 def test_official_values_are_published_only_where_redistribution_is_permitted(): licenses = yaml.safe_load( (METADATA / "source_licenses.yaml").read_text(encoding="utf-8") )["licenses"] assert all(entry["raw_redistribution_permitted"] for entry in licenses) results = ROOT / "data" / "auction_results" if results.exists(): assert pl.read_parquet(results / "*.parquet").height >= 0 def test_the_coverage_table_names_every_subset(): path = METADATA / "coverage.parquet" if not path.exists(): return coverage = pl.read_parquet(path) assert set(coverage["subset"]) >= { "auction_calendar", "auction_results", "nominal_yield_curve", "real_yield_curve", "bill_rates", } for row in coverage.iter_rows(named=True): assert row["first_date"] <= row["last_date"] assert row["row_count"] > 0 assert len(row["coverage_notes"]) > 40 def test_field_coverage_says_when_each_field_starts(): """The table that stops a reader treating a null as a zero.""" path = METADATA / "field_coverage.parquet" if not path.exists(): return fields = pl.read_parquet(path) bidders = fields.filter( (pl.col("subset") == "auction_results") & pl.col("field").is_in(["indirect_bidder_accepted", "direct_bidder_accepted"]) ) assert bidders.height == 2 # The bidder categories begin in 2008, decades after the query itself. assert all(row["first_date"].year >= 2008 for row in bidders.iter_rows(named=True)) def test_the_methodology_registry_is_published(): path = METADATA / "yield_curve_methodology.parquet" if not path.exists(): return registry = pl.read_parquet(path) assert set(registry["methodology_id"]) == {"quasi_cubic_hermite_spline", "monotone_convex"} for row in registry.iter_rows(named=True): assert row["source_url"].startswith("https://home.treasury.gov/") assert len(row["description"]) > 100 def test_the_build_record_says_how_current_the_data_is(): path = METADATA / "build.parquet" if not path.exists(): return build = pl.read_parquet(path) assert build.height == 1 row = build.row(0, named=True) assert row["recipe_hash"].startswith("sha256:") assert row["source_snapshot_at"] is not None assert row["quarantine_count"] >= 0