fix(api): treat "16 plus" as secondary, the way PHASE_GROUPS already does
PR Checks / Frontend Typecheck + Tests (pull_request) Canceled after 9s
PR Checks / Backend Smoke (pull_request) Canceled after 0s
PR Checks / Build Backend (no push) (pull_request) Canceled after 0s
PR Checks / Build Frontend (no push) (pull_request) Canceled after 0s
PR Checks / Build Pipeline (no push) (pull_request) Canceled after 0s
PR Checks / AI Code Review (Claude) (pull_request) Canceled after 0s
PR Checks / Frontend Typecheck + Tests (pull_request) Canceled after 9s
PR Checks / Backend Smoke (pull_request) Canceled after 0s
PR Checks / Build Backend (no push) (pull_request) Canceled after 0s
PR Checks / Build Frontend (no push) (pull_request) Canceled after 0s
PR Checks / Build Pipeline (no push) (pull_request) Canceled after 0s
PR Checks / AI Code Review (Claude) (pull_request) Canceled after 0s
GIAS phase 6 is "16 plus", and PHASE_GROUPS deliberately files it in the secondary group. The payload helper decided the same question with `"secondary" in phase_text`, which that value does not satisfy — so a sixth-form college was handed the primary bucket and offered infant schools as its peers, with the KS2 metric key to label them. No crash; just a page confidently showing the wrong schools. The decision now lives in similar_schools.is_secondary_phase, beside the PHASE_GROUPS bucket it selects from, so the two cannot drift again. A test pins them together. The same binary assumption had a second output. computeSchoolFlags tests for the substring too, so a 16-plus school renders the primary template, and the composer was labelling the section from the template: "Other primary schools near <sixth form college>" above a row of secondaries. The section now derives its noun from the school's own phase, which also removes the duplicated wording from both composers. A 16-plus school's candidates span the whole secondary group, so no single noun fits and it gets the honest general one. Reported in review on #150. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bd2a6c385b
commit
2175dccb7c
7 files changed
+121
-14
No files matched your search
+7
-6
@@ -41,7 +41,7 @@ from .data_loader import get_data_info as get_db_info
|
||||
from . import flags
|
||||
from .places import build_place_index, build_place_registry, places_for_urn
|
||||
from .schemas import METRIC_DEFINITIONS, PHASE_GROUPS, RANKING_COLUMNS, SCHOOL_COLUMNS
|
||||
from .similar_schools import select_similar
|
||||
from .similar_schools import is_secondary_phase, select_similar
|
||||
from .utils import clean_for_json, convert_to_native
|
||||
|
||||
# Values to exclude from filter dropdowns (empty strings, non-applicable labels)
|
||||
@@ -274,11 +274,12 @@ def _similar_schools_payload(urn: int, phase: str | None) -> list[dict]:
|
||||
section simply does not render.
|
||||
"""
|
||||
try:
|
||||
phase_text = (phase or "").lower()
|
||||
# All-through schools render with the primary template, which is what
|
||||
# decides the metric, so they are not secondary here.
|
||||
is_secondary = phase_text != "all-through" and "secondary" in phase_text
|
||||
return select_similar(load_latest_school_data(), int(urn), is_secondary)
|
||||
# Decided in similar_schools, beside the PHASE_GROUPS bucket it selects
|
||||
# from, so the two cannot drift. A substring test for "secondary" here
|
||||
# would miss "16 plus" and hand a sixth-form college the primary bucket.
|
||||
return select_similar(
|
||||
load_latest_school_data(), int(urn), is_secondary_phase(phase)
|
||||
)
|
||||
except Exception:
|
||||
import logging
|
||||
|
||||
|
||||
@@ -89,6 +89,24 @@ def phase_label(phase: str | None) -> str:
|
||||
return f"{text.capitalize()} school"
|
||||
|
||||
|
||||
def is_secondary_phase(phase: str | None) -> bool:
|
||||
"""Whether this phase takes the secondary side: secondary group membership,
|
||||
minus all-through.
|
||||
|
||||
Membership is read from PHASE_GROUPS rather than tested with `"secondary" in
|
||||
phase`, because that substring misses "16 plus" — GIAS phase 6, which
|
||||
PHASE_GROUPS deliberately files as secondary. The substring version fails
|
||||
silently rather than loudly: a sixth-form college is simply handed the
|
||||
primary bucket and offered infant schools as peers.
|
||||
|
||||
All-through is the exception. PHASE_GROUPS lists it on both sides because it
|
||||
belongs on both phases' place pages, but the detail page renders it with the
|
||||
primary template, and the metric follows the template.
|
||||
"""
|
||||
text = (phase or "").strip().lower()
|
||||
return text != "all-through" and text in PHASE_GROUPS["secondary"]
|
||||
|
||||
|
||||
def _phase_group(is_secondary: bool) -> set[str]:
|
||||
return PHASE_GROUPS["secondary" if is_secondary else "primary"]
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ set, never far enough to fill the last of the six slots.
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from backend.similar_schools import select_similar
|
||||
from backend.similar_schools import is_secondary_phase, select_similar
|
||||
|
||||
# Roughly 0.7 miles apart in latitude at this longitude.
|
||||
BASE_LAT, BASE_LON = 51.5000, -0.1000
|
||||
@@ -203,6 +203,38 @@ def test_all_through_is_offered_on_both_phase_sides():
|
||||
assert 100002 in {s["urn"] for s in select_similar(secondary, 100010, is_secondary=True)}
|
||||
|
||||
|
||||
def test_sixteen_plus_is_matched_against_secondary_not_primary():
|
||||
"""GIAS phase 6 is "16 plus", and PHASE_GROUPS puts it in the secondary
|
||||
group — a sixth-form college's peers are secondaries and other colleges,
|
||||
never primary schools. A substring test for "secondary" misses it silently:
|
||||
no crash, just a page offering infant schools to a sixth form."""
|
||||
frame = _frame(
|
||||
_row(100001, "Sixth Form College", phase="16 plus", age_range="16-19"),
|
||||
_row(100002, "Nearby Secondary", phase="Secondary", latitude=_at(0.5),
|
||||
attainment_8_score=52.0),
|
||||
_row(100003, "Nearby College", phase="16 plus", latitude=_at(0.6),
|
||||
attainment_8_score=np.nan),
|
||||
_row(100004, "Nearby Primary", phase="Primary", latitude=_at(0.1)),
|
||||
)
|
||||
result = select_similar(frame, 100001, is_secondary=is_secondary_phase("16 plus"))
|
||||
urns = {s["urn"] for s in result}
|
||||
assert 100004 not in urns, "a primary school is not a peer for a sixth form"
|
||||
assert urns == {100002, 100003}
|
||||
assert all(s["metric_key"] == "attainment_8_score" for s in result)
|
||||
|
||||
|
||||
def test_is_secondary_phase_agrees_with_the_phase_groups_it_selects_from():
|
||||
"""The two must not drift: whatever this calls secondary decides which
|
||||
PHASE_GROUPS bucket the candidates come from."""
|
||||
for phase in ("Secondary", "Middle deemed secondary", "16 plus"):
|
||||
assert is_secondary_phase(phase) is True, phase
|
||||
for phase in ("Primary", "Middle deemed primary", "Nursery", "", None):
|
||||
assert is_secondary_phase(phase) is False, phase
|
||||
# In PHASE_GROUPS an all-through school is on both sides, but it renders
|
||||
# with the primary template, and the metric follows the template.
|
||||
assert is_secondary_phase("All-through") is False
|
||||
|
||||
|
||||
def test_chips_state_only_what_the_tier_earned():
|
||||
frame = _frame(
|
||||
_row(100001, "Subject", phase="Secondary", gender="Mixed",
|
||||
|
||||
Reference in new issue
Block a user