Download tests/test_quality.py from ZipLime/commitments-of-traders: direct link, hf CLI and curl.
- Browser
- Download file 5.6 kB
-
https://huggingface.co/datasets/ZipLime/commitments-of-traders/resolve/main/tests/test_quality.py
- Command line
-
hf download hf://datasets/ZipLime/commitments-of-traders/tests/test_quality.py
-
curl -L -o test_quality.py https://huggingface.co/datasets/ZipLime/commitments-of-traders/resolve/main/tests/test_quality.py
5.6 kB
| """The gate, checked against the failures it exists to catch.""" | |
| from __future__ import annotations | |
| from datetime import UTC, date, datetime, timedelta | |
| import polars as pl | |
| from recipe.archive import Week | |
| from recipe.build import build_all | |
| from recipe.quality import validate | |
| from recipe.schedule import ReleaseCalendar | |
| from recipe.schema import CONFIG_SCHEMAS, align | |
| from recipe.store import write_table | |
| RUN_AT = datetime(2026, 9, 13, tzinfo=UTC) | |
| PLAIN = ReleaseCalendar(announced={}, scheduled={}) | |
| BUILT_REPORTS = {"legacy"} | |
| def corpus(markets: int = 300, weeks: int = 760) -> list[Week]: | |
| """Enough of a corpus to clear the floors, so a test can fail one check alone.""" | |
| rows = [] | |
| start = date(2011, 1, 4) | |
| for offset in range(weeks): | |
| day = start + timedelta(days=7 * offset) | |
| for index in range(markets // 60 + 1): | |
| code = f"{index:06d}" | |
| rows.append( | |
| Week( | |
| report_type="legacy", market_code=code, | |
| market_and_exchange_name=f"MARKET {code} - AN EXCHANGE", | |
| exchange_code="CBT", commodity_code="001", report_date=day, | |
| values={ | |
| "open_interest": 100.0, "traders_total": 10.0, | |
| "noncommercial_long": 40.0, "noncommercial_short": 25.0, | |
| "noncommercial_spreading": 10.0, | |
| "commercial_long": 30.0, "commercial_short": 50.0, | |
| "nonreportable_long": 20.0, "nonreportable_short": 15.0, | |
| "change_in_open_interest": 0.0, | |
| }, | |
| ) | |
| ) | |
| # Enough distinct markets to clear the market floor. | |
| extra = [] | |
| for index in range(markets): | |
| code = f"9{index:05d}" | |
| extra.append( | |
| Week( | |
| report_type="legacy", market_code=code, | |
| market_and_exchange_name=f"MARKET {code} - AN EXCHANGE", | |
| exchange_code="CBT", commodity_code="001", report_date=start, | |
| values={ | |
| "open_interest": 100.0, "traders_total": 10.0, | |
| "noncommercial_long": 40.0, "noncommercial_short": 25.0, | |
| "noncommercial_spreading": 10.0, | |
| "commercial_long": 30.0, "commercial_short": 50.0, | |
| "nonreportable_long": 20.0, "nonreportable_short": 15.0, | |
| "change_in_open_interest": None, | |
| }, | |
| ) | |
| ) | |
| return rows + extra | |
| def write(tmp_path, frames) -> None: | |
| for name in CONFIG_SCHEMAS: | |
| write_table(tmp_path / "data", name, frames.get(name, pl.DataFrame())) | |
| # This file builds a few thousand rows, not the four hundred thousand a real | |
| # build has. The floors are declared rather than disabled. | |
| FLOORS = (4_000, 250, 700) | |
| def check(tmp_path): | |
| return validate(tmp_path / "data", expected_reports=BUILT_REPORTS, floors=FLOORS) | |
| def test_a_clean_build_passes(tmp_path): | |
| frames = build_all(corpus(), PLAIN, run_at=RUN_AT) | |
| frames["calendar"] = pl.DataFrame() | |
| write(tmp_path, frames) | |
| report = check(tmp_path) | |
| assert report.ok, report.errors | |
| def test_a_timestamp_taken_from_the_report_date_is_refused(tmp_path): | |
| """The mistake the whole dataset exists to prevent, made deliberately.""" | |
| frames = build_all(corpus(), PLAIN, run_at=RUN_AT) | |
| leaked = frames["positions"].with_columns( | |
| pl.col("report_date").alias("release_date_et"), | |
| pl.lit(0).cast(pl.Int32).alias("knowledge_lag_days"), | |
| ) | |
| frames["positions"] = align(leaked, CONFIG_SCHEMAS["positions"]) | |
| frames["calendar"] = pl.DataFrame() | |
| write(tmp_path, frames) | |
| report = check(tmp_path) | |
| assert not report.ok | |
| assert any("taken from the data date" in error for error in report.errors) | |
| def test_a_column_mapped_to_the_wrong_field_is_refused(tmp_path): | |
| """Open interest is the sum of the long side, by construction. | |
| Point a column at the wrong field and every number stays plausible; only | |
| the arithmetic stops working, which is the only way to notice. | |
| """ | |
| frames = build_all(corpus(), PLAIN, run_at=RUN_AT) | |
| broken = frames["positions"].with_columns( | |
| (pl.col("commercial_long") * 3).alias("commercial_long") | |
| ) | |
| frames["positions"] = align(broken, CONFIG_SCHEMAS["positions"]) | |
| frames["calendar"] = pl.DataFrame() | |
| write(tmp_path, frames) | |
| report = check(tmp_path) | |
| assert not report.ok | |
| assert any("do not add up to their own open interest" in error for error in report.errors) | |
| def test_a_release_at_an_invented_hour_is_refused(tmp_path): | |
| frames = build_all(corpus(), PLAIN, run_at=RUN_AT) | |
| shifted = frames["releases"].with_columns( | |
| (pl.col("release_at") + timedelta(hours=2)).alias("release_at") | |
| ) | |
| frames["releases"] = align(shifted, CONFIG_SCHEMAS["releases"]) | |
| frames["calendar"] = pl.DataFrame() | |
| write(tmp_path, frames) | |
| report = check(tmp_path) | |
| assert not report.ok | |
| assert any("not only 15:30 Eastern" in error for error in report.errors) | |
| def test_a_report_that_disappears_entirely_is_refused(tmp_path): | |
| """Every other check would pass on what remains.""" | |
| frames = build_all(corpus(), PLAIN, run_at=RUN_AT) | |
| frames["calendar"] = pl.DataFrame() | |
| write(tmp_path, frames) | |
| report = validate( | |
| tmp_path / "data", expected_reports={"legacy", "disaggregated"}, floors=FLOORS | |
| ) | |
| assert not report.ok | |
| assert any("no rows at all for" in error and "disaggregated" in error | |
| for error in report.errors) | |