Datasets:
Tasks:
Tabular Classification
Formats:
parquet
Languages:
English
Size:
< 1K
Tags:
economics
quantitative-finance
causal-inference
macroeconomics
housing-economics
market-microstructure
License:
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| from dataclasses import FrozenInstanceError | |
| from datetime import date | |
| from pathlib import Path | |
| import pytest | |
| from microstructure.m8_config import M8ConfigError, load_m8_config | |
| PROJECT_ROOT = Path(__file__).parents[1] | |
| CONFIG_PATH = PROJECT_ROOT / "configs" / "m8_multidate_trade_study.toml" | |
| def _mutated_config(tmp_path: Path, old: str, new: str) -> Path: | |
| source = CONFIG_PATH.read_text(encoding="utf-8") | |
| assert old in source | |
| path = tmp_path / "m8-invalid.toml" | |
| path.write_text(source.replace(old, new, 1), encoding="utf-8") | |
| return path | |
| def test_frozen_m8_config_is_typed_hashed_and_json_safe() -> None: | |
| config = load_m8_config(CONFIG_PATH) | |
| assert config.path == CONFIG_PATH.resolve() | |
| assert config.study.protocol_version == "1.0.2" | |
| assert config.study.source == "binance_spot_daily_aggtrades_archive" | |
| assert config.study.symbols == ("BTCUSDT", "ETHUSDT") | |
| assert tuple((period.date, period.role) for period in config.periods) == ( | |
| (date(2024, 1, 3), "train"), | |
| (date(2024, 1, 4), "validation"), | |
| (date(2024, 1, 5), "primary_test"), | |
| (date(2024, 1, 6), "replication_test"), | |
| ) | |
| assert config.source_sha256 == hashlib.sha256(CONFIG_PATH.read_bytes()).hexdigest() | |
| assert len(config.hash) == 64 | |
| public = config.public_dict() | |
| assert public["config_sha256"] == config.hash | |
| assert public["source_sha256"] == config.source_sha256 | |
| assert json.loads(json.dumps(public))["periods"][3]["role"] == "replication_test" | |
| def test_semantic_hash_ignores_path_comments_and_formatting(tmp_path: Path) -> None: | |
| original = load_m8_config(CONFIG_PATH) | |
| relocated_path = tmp_path / "relocated.toml" | |
| relocated_path.write_text( | |
| "# formatting-only comment\n" + CONFIG_PATH.read_text(encoding="utf-8") + "\n", | |
| encoding="utf-8", | |
| ) | |
| relocated = load_m8_config(relocated_path) | |
| assert relocated.hash == original.hash | |
| assert relocated.source_sha256 != original.source_sha256 | |
| assert relocated.path != original.path | |
| def test_config_objects_are_immutable() -> None: | |
| config = load_m8_config(CONFIG_PATH) | |
| with pytest.raises(FrozenInstanceError): | |
| config.study.seed = 1 # type: ignore[misc] | |
| def test_frozen_study_dimensions_fail_closed( | |
| tmp_path: Path, old: str, new: str, message: str | |
| ) -> None: | |
| path = _mutated_config(tmp_path, old, new) | |
| with pytest.raises(M8ConfigError, match=message): | |
| load_m8_config(path) | |
| def test_period_roles_must_have_the_exact_frozen_order(tmp_path: Path) -> None: | |
| path = _mutated_config(tmp_path, 'role = "validation"', 'role = "primary_test"') | |
| with pytest.raises(M8ConfigError, match="period date/role order is frozen"): | |
| load_m8_config(path) | |
| def test_period_dates_must_be_unique(tmp_path: Path) -> None: | |
| path = _mutated_config(tmp_path, 'date = "2024-01-04"', 'date = "2024-01-03"') | |
| with pytest.raises(M8ConfigError, match="period dates must be unique"): | |
| load_m8_config(path) | |
| def test_period_dates_cannot_be_reordered_or_replaced(tmp_path: Path) -> None: | |
| path = _mutated_config(tmp_path, 'date = "2024-01-06"', 'date = "2024-01-07"') | |
| with pytest.raises(M8ConfigError, match="period date/role order is frozen"): | |
| load_m8_config(path) | |
| def test_unknown_or_missing_fields_fail_closed(tmp_path: Path) -> None: | |
| path = _mutated_config( | |
| tmp_path, | |
| 'target = "future_trade_up"', | |
| 'target = "future_trade_up"\nunreviewed_option = true', | |
| ) | |
| with pytest.raises(M8ConfigError, match=r"study keys.*unknown=unreviewed_option"): | |
| load_m8_config(path) | |
| def test_invalid_types_do_not_coerce_bool_to_integer(tmp_path: Path) -> None: | |
| path = _mutated_config(tmp_path, "bootstrap_samples = 2000", "bootstrap_samples = true") | |
| with pytest.raises(M8ConfigError, match=r"study\.bootstrap_samples must be an integer"): | |
| load_m8_config(path) | |
| def test_invalid_toml_is_wrapped_as_m8_config_error(tmp_path: Path) -> None: | |
| path = tmp_path / "invalid.toml" | |
| path.write_text("[study\n", encoding="utf-8") | |
| with pytest.raises(M8ConfigError, match="cannot parse M8 TOML"): | |
| load_m8_config(path) | |