File size: 3,021 Bytes
ebcde1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from __future__ import annotations

from datetime import UTC
from pathlib import Path

import pytest

from microstructure.config import ConfigError, datetime_to_ns, load_config

PROJECT_ROOT = Path(__file__).parents[1]


def test_smoke_config_is_typed_and_stably_hashed() -> None:
    first = load_config(PROJECT_ROOT / "configs" / "smoke.toml")
    second = load_config(PROJECT_ROOT / "configs" / "smoke.toml")

    assert first.hash == second.hash
    assert len(first.hash) == 64
    assert first.data.symbols == ("BTCUSDT", "ETHUSDT")
    assert first.data.start.tzinfo == UTC
    assert first.data.partition_root == PROJECT_ROOT / "data" / "normalized"
    assert first.evaluation.embargo_events >= first.features.label_horizon_events


def test_datetime_to_ns_rejects_naive_values() -> None:
    from datetime import datetime

    with pytest.raises(ConfigError, match="timezone aware"):
        datetime_to_ns(datetime(2024, 1, 1))


def test_config_rejects_unimplemented_schema_version(tmp_path: Path) -> None:
    source = (PROJECT_ROOT / "configs" / "smoke.toml").read_text(encoding="utf-8")
    path = tmp_path / "unsupported-schema.toml"
    path.write_text(
        source.replace('schema_version = "1.0.0"', 'schema_version = "2.0.0"'),
        encoding="utf-8",
    )

    with pytest.raises(ConfigError, match=r"unsupported data\.schema_version"):
        load_config(path)


def test_config_accepts_extensible_adapter_mode_identifier(tmp_path: Path) -> None:
    source = (PROJECT_ROOT / "configs" / "smoke.toml").read_text(encoding="utf-8")
    path = tmp_path / "third-party-mode.toml"
    path.write_text(
        source.replace('mode = "synthetic"', 'mode = "fixture_vendor.v1"'),
        encoding="utf-8",
    )

    assert load_config(path).data.mode == "fixture_vendor.v1"


def test_config_rejects_unsafe_adapter_mode_identifier(tmp_path: Path) -> None:
    source = (PROJECT_ROOT / "configs" / "smoke.toml").read_text(encoding="utf-8")
    path = tmp_path / "unsafe-mode.toml"
    path.write_text(
        source.replace('mode = "synthetic"', 'mode = "Fixture Vendor"'),
        encoding="utf-8",
    )

    with pytest.raises(ConfigError, match="lowercase adapter identifier"):
        load_config(path)


@pytest.mark.parametrize(
    ("line", "replacement", "message"),
    [
        (
            'end = "2024-01-02T00:10:00Z"',
            "",
            r"requires a bounded data\.end",
        ),
        (
            "max_events_per_symbol = 5000",
            "max_events_per_symbol = 0",
            "requires positive data.max_events_per_symbol",
        ),
    ],
)
def test_public_config_fails_fast_on_unbounded_inputs(
    tmp_path: Path, line: str, replacement: str, message: str
) -> None:
    source = (PROJECT_ROOT / "configs" / "public_sample.toml").read_text(encoding="utf-8")
    path = tmp_path / "invalid-public.toml"
    path.write_text(source.replace(line, replacement), encoding="utf-8")

    with pytest.raises(ConfigError, match=message):
        load_config(path)