forked from ChelseaKR/gtfs-scorecard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dataset.py
More file actions
313 lines (275 loc) · 10.3 KB
/
Copy pathtest_dataset.py
File metadata and controls
313 lines (275 loc) · 10.3 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""Tests for the open national quality dataset builder."""
from __future__ import annotations
import csv
import io
from typing import Any
from scorecard_pipeline import RUBRIC_VERSION, SCORING_PROFILE_ID
from scorecard_pipeline.dataset import (
COLUMNS,
build_quality_dataset,
national_summary,
to_csv,
)
from scorecard_pipeline.fetch import RAW_READER_ARCHIVE_PROFILE
from scorecard_pipeline.validate import VALIDATOR_VERSION
def _history_point(
date: str,
grade: str,
score: float,
*,
correctness: float,
freshness: float,
completeness: float,
realtime: float | None = None,
days_until_expiry: int | None = None,
service_horizon_status: str = "within_review_threshold",
) -> dict[str, Any]:
categories: dict[str, float] = {
"correctness": correctness,
"freshness": freshness,
"completeness": completeness,
}
if realtime is not None:
categories["realtime"] = realtime
return {
"date": date,
"grade": grade,
"score": score,
"rubric_version": RUBRIC_VERSION,
"scoring_profile_id": SCORING_PROFILE_ID,
"scoring_profile_rubric_version": RUBRIC_VERSION,
"validator_version": VALIDATOR_VERSION,
"feed_sha256": f"sha-{date}-{score}-{grade}",
"categories": categories,
"days_until_expiry": days_until_expiry,
"service_horizon_status": service_horizon_status,
}
def _sample_index() -> dict[str, Any]:
return {
"agencies": {
"yolobus": {
"name": "Yolobus",
"history": [
_history_point(
"2026-05-01",
"C",
72.0,
correctness=70.0,
freshness=60.0,
completeness=80.0,
days_until_expiry=10,
),
# Latest point: this is the one that should land in the row.
_history_point(
"2026-06-01",
"B",
85.0,
correctness=88.0,
freshness=90.0,
completeness=80.0,
realtime=82.0,
days_until_expiry=120,
),
],
},
"unitrans": {
"name": "Unitrans",
"history": [
# No realtime feed -> realtime score absent.
_history_point(
"2026-06-02",
"A",
93.0,
correctness=95.0,
freshness=90.0,
completeness=94.0,
days_until_expiry=-5,
),
],
},
}
}
def test_rows_use_latest_history_point_only() -> None:
dataset = build_quality_dataset(_sample_index())
assert dataset["schema_version"] == "1.3"
assert dataset["generated_fields"] == list(COLUMNS)
rows = dataset["rows"]
# Sorted by id: unitrans before yolobus.
assert [r["id"] for r in rows] == ["unitrans", "yolobus"]
yolo = rows[1]
assert yolo == {
"id": "yolobus",
"name": "Yolobus",
"date": "2026-06-01",
"grade": "B",
"score": 85.0,
"rubric_version": RUBRIC_VERSION,
"scoring_profile_id": SCORING_PROFILE_ID,
"scoring_profile_rubric_version": RUBRIC_VERSION,
"validator_version": VALIDATOR_VERSION,
"reader_archive_profile": RAW_READER_ARCHIVE_PROFILE,
"feed_sha256": "sha-2026-06-01-85.0-B",
"comparison_eligible": True,
"correctness": 88.0,
"freshness": 90.0,
"completeness": 80.0,
"realtime": 82.0,
"days_until_expiry": 120,
"service_horizon_status": "within_review_threshold",
}
assert dataset["comparison"]["eligible_count"] == 1
assert dataset["comparison"]["required_measured_categories"] == [
"correctness",
"freshness",
"completeness",
"realtime",
]
# Unitrans publishes no realtime feed: realtime is None, not zero. This
# synthetic tie selects the larger measured-category signature, so Unitrans
# remains public but is marked outside that one homogeneous cohort.
assert rows[0]["realtime"] is None
assert rows[0]["comparison_eligible"] is False
def test_schema_version_override() -> None:
dataset = build_quality_dataset(_sample_index(), schema_version="2.1")
assert dataset["schema_version"] == "2.1"
def test_agencies_without_history_are_skipped() -> None:
index = {
"agencies": {
"new": {"name": "Brand New Transit", "history": []},
"real": {
"name": "Real Transit",
"history": [
_history_point(
"2026-06-01",
"A",
91.0,
correctness=90.0,
freshness=92.0,
completeness=91.0,
days_until_expiry=30,
)
],
},
}
}
rows = build_quality_dataset(index)["rows"]
assert [r["id"] for r in rows] == ["real"]
def test_dataset_prefers_curated_registry_name_without_rewriting_history() -> None:
from scorecard_pipeline.config import Agency
index = _sample_index()
index["agencies"]["unitrans"]["name"] = "Stale Export Name"
agency = Agency(
id="unitrans",
name="Unitrans",
static_gtfs_url="https://example.com/unitrans.zip",
)
dataset = build_quality_dataset(index, agencies=[agency])
row = next(item for item in dataset["rows"] if item["id"] == "unitrans")
assert row["name"] == "Unitrans"
assert index["agencies"]["unitrans"]["name"] == "Stale Export Name"
def test_csv_round_trips_header_and_values() -> None:
dataset = build_quality_dataset(_sample_index())
text = to_csv(dataset)
reader = list(csv.reader(io.StringIO(text)))
assert reader[0] == list(COLUMNS)
assert len(reader) == 1 + len(dataset["rows"])
# Parse the data rows back into dicts and compare against the dataset rows.
parsed = [dict(zip(COLUMNS, line, strict=True)) for line in reader[1:]]
yolo = next(p for p in parsed if p["id"] == "yolobus")
assert yolo["name"] == "Yolobus"
assert yolo["grade"] == "B"
assert yolo["score"] == "85.0"
assert yolo["rubric_version"] == RUBRIC_VERSION
assert yolo["scoring_profile_id"] == SCORING_PROFILE_ID
assert yolo["scoring_profile_rubric_version"] == RUBRIC_VERSION
assert yolo["validator_version"] == VALIDATOR_VERSION
assert yolo["feed_sha256"] == "sha-2026-06-01-85.0-B"
assert yolo["comparison_eligible"] == "True"
assert yolo["realtime"] == "82.0"
assert yolo["days_until_expiry"] == "120"
assert yolo["service_horizon_status"] == "within_review_threshold"
# Missing realtime renders as an empty cell, not "None".
unitrans = next(p for p in parsed if p["id"] == "unitrans")
assert unitrans["realtime"] == ""
def test_csv_escapes_commas_in_names() -> None:
index = {
"agencies": {
"x": {
"name": "Davis, CA Transit",
"history": [
_history_point(
"2026-06-01",
"B",
80.0,
correctness=80.0,
freshness=80.0,
completeness=80.0,
days_until_expiry=5,
)
],
}
}
}
text = to_csv(build_quality_dataset(index))
(parsed_row,) = list(csv.reader(io.StringIO(text)))[1:]
assert parsed_row[COLUMNS.index("name")] == "Davis, CA Transit"
def test_national_summary_aggregates() -> None:
summary = national_summary(build_quality_dataset(_sample_index()))
assert summary["agency_count"] == 2
# (85.0 + 93.0) / 2 = 89.0
assert summary["average_score"] == 89.0
assert summary["grade_distribution"] == {"A": 1, "B": 1, "C": 0, "D": 0, "F": 0}
# yolobus (120) current, unitrans (-5) expired -> 1 of 2.
assert summary["pct_current"] == 50.0
def test_national_summary_unknown_expiry_is_not_current() -> None:
index = {
"agencies": {
"a": {
"name": "A",
"history": [
_history_point(
"2026-06-01",
"A",
90.0,
correctness=90.0,
freshness=90.0,
completeness=90.0,
days_until_expiry=None,
)
],
}
}
}
summary = national_summary(build_quality_dataset(index))
assert summary["pct_current"] == 0.0
def test_empty_index_yields_empty_rows_and_zeroed_summary() -> None:
dataset = build_quality_dataset({})
assert dataset["rows"] == []
assert dataset["generated_fields"] == list(COLUMNS)
assert dataset["comparison"]["eligible_count"] == 0
# CSV is just the header row.
assert to_csv(dataset).strip() == ",".join(COLUMNS)
summary = national_summary(dataset)
assert summary == {
"agency_count": 0,
"average_score": None,
"grade_distribution": {"A": 0, "B": 0, "C": 0, "D": 0, "F": 0},
"pct_current": 0.0,
}
def test_legacy_history_derives_distant_horizon_for_first_api_publish() -> None:
index = _sample_index()
latest = index["agencies"]["unitrans"]["history"][-1]
latest["date"] = "2026-07-13"
latest["days_until_expiry"] = 26_834
del latest["service_horizon_status"]
row = build_quality_dataset(index)["rows"][0]
assert row["days_until_expiry"] == 26_834
assert row["service_horizon_status"] == "unusually_distant"
def test_legacy_history_without_date_or_expiry_stays_unknown() -> None:
index = _sample_index()
latest = index["agencies"]["unitrans"]["history"][-1]
latest["date"] = None
latest["days_until_expiry"] = None
del latest["service_horizon_status"]
row = build_quality_dataset(index)["rows"][0]
assert row["service_horizon_status"] == "unknown"