forked from ChelseaKR/homeroom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_context.py
More file actions
385 lines (326 loc) · 12.3 KB
/
Copy pathtest_context.py
File metadata and controls
385 lines (326 loc) · 12.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
"""District and statewide context: the ALL row, never a sum, and failing closed.
The regression this file exists for is at the top. Everything after it guards the
same promise from a different side: a context figure is a number California
published for that entity, or it is one of the three non-numbers, and it is never
assembled by Homeroom out of school rows.
"""
from pathlib import Path
import pytest
from homeroom.context import (
AbsenteeismContextDriftError,
ContextDriftError,
district_key,
load_absenteeism_context,
load_context,
)
from homeroom.enrollment import EnrollmentDriftError, parse_enrollment, school_totals
from homeroom.measures import MeasureStatus, SuppressedValueError
HEADER = (
"AcademicYear\tAggregateLevel\tCountyCode\tDistrictCode\tSchoolCode\tCountyName"
"\tDistrictName\tSchoolName\tCharter\tReportingCategory\tTOTAL_ENR\tGR_TK\tGR_KN"
"\tGR_01\tGR_02\tGR_03\tGR_04\tGR_05\tGR_06\tGR_07\tGR_08\tGR_09\tGR_10\tGR_11\tGR_12\n"
)
GRADES = "\t".join(["0"] * 14)
def row(
*,
level: str,
charter: str,
category: str,
total: str,
county: str = "57",
district: str = "72678",
school: str = "",
year: str = "2025-26",
) -> str:
return (
f"{year}\t{level}\t{county}\t{district}\t{school}\tYolo\tDavis Joint Unified"
f"\tBirch Lane\t{charter}\t{category}\t{total}\t{GRADES}\n"
)
def write(tmp_path: Path, rows: str) -> Path:
p = tmp_path / "cdenroll.txt"
p.write_text(HEADER + rows, encoding="utf-8")
return p
STATE_TA = row(
level="T", charter="ALL", category="TA", total="5731260", county="00", district=""
)
def test_district_context_is_the_all_row_not_the_first_row_that_matches(
tmp_path: Path,
) -> None:
"""The one that would have shipped a district figure fifteen times too small.
CDE publishes every district three times: charter, non-charter, and both. In
the acquired 2025-26 file Davis Joint Unified reads 561 / 7,682 / 8,243 for
the same category, and the charter row comes first. Selecting by anything
other than ``Charter == ALL`` picks a real, plausible, wrong number that no
downstream check would flag, because it is a number the state did publish.
"""
p = write(
tmp_path,
row(level="D", charter="Y", category="TA", total="561")
+ row(level="D", charter="N", category="TA", total="7682")
+ row(level="D", charter="ALL", category="TA", total="8243")
+ STATE_TA,
)
district = load_context(p).for_district("57726786056246")
assert district.total.number() == 8243
assert district.total.number() not in (561, 7682)
def test_context_never_sums_school_rows(tmp_path: Path) -> None:
"""No arithmetic over schools, even when the schools would sum to something.
Two schools totalling 300 sit under a district whose published ALL row says
450. The gap is the point: the district row counts schools this file does not
list and students whose cells are masked. Homeroom publishes 450, the state's
own figure, and never 300.
"""
p = write(
tmp_path,
row(level="S", charter="N", category="TA", total="100", school="0000001")
+ row(level="S", charter="N", category="TA", total="200", school="0000002")
+ row(level="D", charter="ALL", category="TA", total="450")
+ STATE_TA,
)
assert load_context(p).for_district("57726780000001").total.number() == 450
def test_masked_district_and_state_cells_stay_withheld(tmp_path: Path) -> None:
p = write(
tmp_path,
row(level="D", charter="ALL", category="SG_HM", total="*")
+ row(
level="T",
charter="ALL",
category="SG_HM",
total="*",
county="00",
district="",
)
+ row(level="D", charter="ALL", category="TA", total="450")
+ STATE_TA,
)
ctx = load_context(p)
for measure in (
ctx.for_district("57726786056246").subgroup("SG_HM"),
ctx.state.subgroup("SG_HM"),
):
assert measure.status is MeasureStatus.SUPPRESSED
assert not measure.is_zero
with pytest.raises(SuppressedValueError):
measure.number()
def test_absent_category_is_nothing_published_not_zero(tmp_path: Path) -> None:
p = write(
tmp_path, row(level="D", charter="ALL", category="TA", total="450") + STATE_TA
)
ctx = load_context(p)
absent = ctx.for_district("57726786056246").subgroup("RE_W")
assert absent.status is MeasureStatus.NOT_REPORTED
assert not absent.is_zero
assert ctx.state.subgroup("RE_W").status is MeasureStatus.NOT_REPORTED
def test_district_with_no_published_rows_is_absent_not_invented(
tmp_path: Path,
) -> None:
p = write(tmp_path, STATE_TA)
district = load_context(p).for_district("57726786056246")
assert district.total.status is MeasureStatus.NOT_REPORTED
assert district.cds_code == district_key("57726786056246")
def test_duplicate_all_rows_are_drift_not_last_one_wins(tmp_path: Path) -> None:
"""Two ALL rows for one entity would make file order pick the number."""
p = write(
tmp_path,
row(level="D", charter="ALL", category="TA", total="450")
+ row(level="D", charter="ALL", category="TA", total="451")
+ STATE_TA,
)
with pytest.raises(ContextDriftError, match="more than one"):
load_context(p)
def test_missing_statewide_total_is_drift(tmp_path: Path) -> None:
p = write(tmp_path, row(level="D", charter="ALL", category="TA", total="450"))
with pytest.raises(ContextDriftError, match="no statewide"):
load_context(p)
def test_aggregate_rows_spanning_two_years_are_drift(tmp_path: Path) -> None:
p = write(
tmp_path,
STATE_TA
+ row(
level="D",
charter="ALL",
category="TA",
total="450",
year="2024-25",
),
)
with pytest.raises(ContextDriftError, match="academic years"):
load_context(p)
def test_unknown_charter_value_is_drift(tmp_path: Path) -> None:
"""Charter is what tells aggregate rows apart, so an unreviewed value stops."""
p = write(tmp_path, row(level="D", charter="MAYBE", category="TA", total="450"))
with pytest.raises(EnrollmentDriftError, match="Charter"):
list(parse_enrollment(p))
def test_duplicate_school_total_rows_are_drift(tmp_path: Path) -> None:
p = write(
tmp_path,
row(level="S", charter="N", category="TA", total="100", school="0000001")
+ row(level="S", charter="Y", category="TA", total="900", school="0000001"),
)
with pytest.raises(EnrollmentDriftError, match="more than one school-level"):
school_totals(p)
def test_county_rows_are_not_mistaken_for_districts(tmp_path: Path) -> None:
"""County rows share the ALL charter value and must not become context."""
p = write(
tmp_path,
row(level="C", charter="ALL", category="TA", total="99999", district="")
+ row(level="D", charter="ALL", category="TA", total="450")
+ STATE_TA,
)
ctx = load_context(p)
assert ctx.for_district("57726786056246").total.number() == 450
assert all(f.total.number() != 99999 for f in ctx.districts.values())
# --- D3 chronic absenteeism context: two independent All/Yes/No dimensions ------
ABD_HEADER = (
"Academic Year\tAggregate Level\tCounty Code\tDistrict Code\tSchool Code"
"\tCounty Name\tDistrict Name\tSchool Name\tCharter School\tDASS"
"\tReporting Category\tChronicAbsenteeismEligibleCumulativeEnrollment"
"\tChronicAbsenteeismCount\tChronicAbsenteeismRate\n"
)
def abd_row(
*,
level: str,
charter: str,
dass: str,
category: str,
rate: str,
county: str = "01",
district: str = "10017",
school: str = "",
year: str = "2024-25",
) -> str:
return (
f"{year}\t{level}\t{county}\t{district}\t{school}\tYolo\tDavis Joint Unified"
f"\tBirch Lane\t{charter}\t{dass}\t{category}\t100\t10\t{rate}\n"
)
def write_abd(tmp_path: Path, rows: str) -> Path:
p = tmp_path / "chronicabsenteeism.txt"
p.write_text(ABD_HEADER + rows, encoding="utf-8")
return p
ABD_STATE_TA = abd_row(
level="T",
charter="All",
dass="All",
category="TA",
rate="19.0",
county="00",
district="",
)
def test_absenteeism_context_requires_both_charter_and_dass_all(
tmp_path: Path,
) -> None:
"""D3 crosses two independent dimensions; only Charter=All AND DASS=All is
the genuine district-wide rate, the same failure mode D2's own Charter=ALL
test guards from the other direction."""
p = write_abd(
tmp_path,
abd_row(level="D", charter="Yes", dass="All", category="TA", rate="5.0")
+ abd_row(level="D", charter="No", dass="All", category="TA", rate="12.0")
+ abd_row(level="D", charter="All", dass="Yes", category="TA", rate="30.0")
+ abd_row(level="D", charter="All", dass="All", category="TA", rate="11.0")
+ ABD_STATE_TA,
)
district = load_absenteeism_context(p).for_district("01100170112345")
assert district.category("TA").number() == 11.0
assert district.category("TA").number() not in (5.0, 12.0, 30.0)
def test_absenteeism_context_never_sums_school_rows(tmp_path: Path) -> None:
p = write_abd(
tmp_path,
abd_row(
level="S",
charter="No",
dass="No",
category="TA",
rate="10.0",
school="0000001",
)
+ abd_row(
level="S",
charter="No",
dass="No",
category="TA",
rate="20.0",
school="0000002",
)
+ abd_row(level="D", charter="All", dass="All", category="TA", rate="11.0")
+ ABD_STATE_TA,
)
assert (
load_absenteeism_context(p)
.for_district("01100170000001")
.category("TA")
.number()
== 11.0
)
def test_absenteeism_masked_district_and_state_rates_stay_withheld(
tmp_path: Path,
) -> None:
p = write_abd(
tmp_path,
abd_row(level="D", charter="All", dass="All", category="RB", rate="*")
+ abd_row(
level="T",
charter="All",
dass="All",
category="RB",
rate="*",
county="00",
district="",
)
+ abd_row(level="D", charter="All", dass="All", category="TA", rate="11.0")
+ ABD_STATE_TA,
)
ctx = load_absenteeism_context(p)
for measure in (
ctx.for_district("01100170112345").category("RB"),
ctx.state.category("RB"),
):
assert measure.status is MeasureStatus.SUPPRESSED
assert not measure.is_zero
with pytest.raises(SuppressedValueError):
measure.number()
def test_absenteeism_absent_category_is_nothing_published_not_zero(
tmp_path: Path,
) -> None:
p = write_abd(
tmp_path,
abd_row(level="D", charter="All", dass="All", category="TA", rate="11.0")
+ ABD_STATE_TA,
)
ctx = load_absenteeism_context(p)
absent = ctx.for_district("01100170112345").category("RW")
assert absent.status is MeasureStatus.NOT_REPORTED
assert not absent.is_zero
def test_absenteeism_duplicate_all_rows_are_drift(tmp_path: Path) -> None:
p = write_abd(
tmp_path,
abd_row(level="D", charter="All", dass="All", category="TA", rate="11.0")
+ abd_row(level="D", charter="All", dass="All", category="TA", rate="12.0")
+ ABD_STATE_TA,
)
with pytest.raises(AbsenteeismContextDriftError, match="more than one"):
load_absenteeism_context(p)
def test_absenteeism_missing_statewide_total_is_drift(tmp_path: Path) -> None:
p = write_abd(
tmp_path,
abd_row(level="D", charter="All", dass="All", category="TA", rate="11.0"),
)
with pytest.raises(AbsenteeismContextDriftError, match="no statewide"):
load_absenteeism_context(p)
def test_absenteeism_aggregate_rows_spanning_two_years_are_drift(
tmp_path: Path,
) -> None:
p = write_abd(
tmp_path,
ABD_STATE_TA
+ abd_row(
level="D",
charter="All",
dass="All",
category="TA",
rate="11.0",
year="2023-24",
),
)
with pytest.raises(AbsenteeismContextDriftError, match="academic years"):
load_absenteeism_context(p)