-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pipeline.py
More file actions
232 lines (197 loc) · 7.77 KB
/
Copy pathtest_pipeline.py
File metadata and controls
232 lines (197 loc) · 7.77 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import json
from dataclasses import replace
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
import pytest
from test_jolpica import FixtureTransport, NamedFixtureTransport
from f1sql.config import Settings
from f1sql.contracts import BuildTarget
from f1sql.pipeline import (
FixtureInput,
PipelineGateError,
normalize_fixtures,
persist_fastf1_snapshot,
run_offline_fixture_pipeline,
)
from f1sql.release import verify_release
from f1sql.sources.fastf1 import FastF1Adapter, SessionSnapshot
from f1sql.sources.jolpica import JolpicaClient
from f1sql.sources.jolpica_models import RaceSummary, Result
class FixtureSession:
def __init__(self, values: dict[str, Any]) -> None:
self.__dict__.update(values)
def load(self) -> None:
return None
class FixtureProvider:
__version__ = "3.8.1"
def __init__(self, schedule: list[dict[str, Any]], values: dict[str, Any]) -> None:
self.schedule = schedule
self.values = values
def get_event_schedule(self, season: int) -> list[dict[str, Any]]:
return self.schedule
def get_session(self, season: int, event: int, identifier: str) -> FixtureSession:
return FixtureSession(self.values)
def _inputs(
tmp_path: Path,
) -> tuple[RaceSummary, tuple[Result, ...], SessionSnapshot, bytes, bytes]:
fixture_root = Path(__file__).parent / "fixtures"
jolpica_root = fixture_root / "jolpica" / "api-v1"
settings = Settings.from_env({}, cwd=tmp_path)
race_client = JolpicaClient(settings, FixtureTransport(jolpica_root))
races, collection = race_client.discover_completed_rounds_with_snapshots(
2024, datetime(2024, 3, 11, tzinfo=UTC), 24
)
results = JolpicaClient(settings, NamedFixtureTransport(jolpica_root)).fetch_results(2024, 1)
fastf1_root = fixture_root / "fastf1" / "v1"
schedule = json.loads((fastf1_root / "schedule-2024.json").read_text(encoding="utf-8"))
values = json.loads((fastf1_root / "session-2024-1.json").read_text(encoding="utf-8"))
snapshot = FastF1Adapter(settings, FixtureProvider(schedule, values)).load_session(
2024, 1, "Race"
)
return (
races[0],
results,
snapshot,
collection.snapshots[0].body,
json.dumps(snapshot.records, sort_keys=True).encode("utf-8"),
)
def _documents() -> dict[str, bytes]:
return {
"LICENSE-DATA": b"CC BY-NC-SA 4.0",
"NOTICE": b"Fixture notice",
"ATTRIBUTION.md": b"Fixture attribution",
"release-notes.md": b"Offline fixture release",
}
def _metadata() -> dict[str, object]:
return {
"schema_version": "2.0.0",
"core_repository_sha": "a" * 40,
"database_repository_sha": "b" * 40,
"source_versions": {"jolpica": "api-v1", "fastf1": "3.8.1"},
}
def test_offline_fixture_pipeline_reaches_a_verifiable_release(tmp_path: Path) -> None:
race, results, session, jolpica_raw, fastf1_raw = _inputs(tmp_path)
result = run_offline_fixture_pipeline(
target=BuildTarget(2024, 1),
race=race,
results=results,
session=session,
output_root=tmp_path / "releases",
documents=_documents(),
metadata=_metadata(),
raw_artifacts={"jolpica-season.json": jolpica_raw, "fastf1-session.json": fastf1_raw},
)
assert result.quality.passed is True
assert result.load_plan.fingerprint()
assert (result.release.output_dir / "normalized.json").exists()
assert any(
asset.name == "load-plan.json" for asset in verify_release(result.release.output_dir)
)
def test_offline_fixture_pipeline_blocks_bad_data_before_packaging(tmp_path: Path) -> None:
race, results, session, _, _ = _inputs(tmp_path)
with pytest.raises(PipelineGateError, match="result.key_unique"):
run_offline_fixture_pipeline(
target=BuildTarget(2024, 1),
race=race,
results=results + (results[0],),
session=session,
output_root=tmp_path / "releases",
documents=_documents(),
metadata=_metadata(),
)
assert not (tmp_path / "releases" / "2024.1.0").exists()
def test_offline_fixture_pipeline_blocks_cross_source_winner_conflict(tmp_path: Path) -> None:
race, results, session, _, _ = _inputs(tmp_path)
conflicting_records = {
**session.records,
"results": ({"DriverNumber": "1", "Position": 2, "Points": 25},),
}
conflicting = replace(session, records=conflicting_records)
with pytest.raises(PipelineGateError):
run_offline_fixture_pipeline(
target=BuildTarget(2024, 1),
race=race,
results=results,
session=conflicting,
output_root=tmp_path / "releases",
documents=_documents(),
metadata=_metadata(),
)
def test_offline_pipeline_allows_missing_optional_fastf1_fields(tmp_path: Path) -> None:
race, results, session, _, _ = _inputs(tmp_path)
sparse = replace(
session,
records={"results": session.records["results"]},
missing=("laps", "stints", "pit_stops", "weather", "race_control"),
)
result = run_offline_fixture_pipeline(
target=BuildTarget(2024, 1),
race=race,
results=results,
session=sparse,
output_root=tmp_path / "releases",
documents=_documents(),
metadata=_metadata(),
)
assert result.quality.passed is True
assert {gap.domain for gap in result.quality.coverage_gaps} == {
"lap",
"stint",
"pit_stop",
"weather",
"race_control",
}
def test_offline_pipeline_builds_cumulative_rounds(tmp_path: Path) -> None:
race, results, session, _, _ = _inputs(tmp_path)
second_race = replace(
race,
round=2,
race_name="Saudi Arabian Grand Prix",
scheduled_at_utc=datetime(2024, 3, 9, 17, tzinfo=UTC),
)
second_results = tuple(replace(item, round=2) for item in results)
second_session = replace(session, round=2)
fixtures = (
FixtureInput(race, results, session),
FixtureInput(second_race, second_results, second_session),
)
bundle = normalize_fixtures(fixtures)
assert {item.round for item in bundle.meetings} == {1, 2}
assert len(bundle.results) == len(results) * 2
result = run_offline_fixture_pipeline(
target=BuildTarget(2024, 2),
race=second_race,
results=second_results,
session=second_session,
fixtures=fixtures,
output_root=tmp_path / "releases",
documents=_documents(),
metadata=_metadata(),
)
assert result.quality.passed is True
assert result.load_plan.operations[4].table == "Meeting"
assert len(result.load_plan.operations[4].rows) == 2
assert len(result.load_plan.operations[7].rows) == len(results) * 2
def test_fastf1_snapshot_persistence_is_content_addressed(tmp_path: Path) -> None:
_, _, session, _, _ = _inputs(tmp_path)
from f1sql.cache import ArtifactStore
first = persist_fastf1_snapshot(session, ArtifactStore(tmp_path / "raw"))
second = persist_fastf1_snapshot(session, ArtifactStore(tmp_path / "raw"))
assert first == second
def test_offline_pipeline_can_include_a_verified_database_backup(tmp_path: Path) -> None:
race, results, session, _, _ = _inputs(tmp_path)
backup = tmp_path / "fixture.bak"
backup.write_bytes(b"verified-sql-server-backup")
result = run_offline_fixture_pipeline(
target=BuildTarget(2024, 2),
race=race,
results=results,
session=session,
output_root=tmp_path / "releases",
documents=_documents(),
metadata=_metadata(),
database_backup=backup,
)
assets = {asset.name for asset in verify_release(result.release.output_dir)}
assert "database.bak" in assets