Skip to content

Drop dead .polity_bridge() and mock the crosswalk the aggregator uses - #770

Open
lbm364dl wants to merge 1 commit into
mainfrom
claude/polity-bridge-dead-code-590
Open

Drop dead .polity_bridge() and mock the crosswalk the aggregator uses#770
lbm364dl wants to merge 1 commit into
mainfrom
claude/polity-bridge-dead-code-590

Conversation

@lbm364dl

Copy link
Copy Markdown
Collaborator

What was wrong (mechanism)

.polity_bridge() (R/read_raw_inputs.R) memoised an area → polity bridge that
nothing called. Confirmed against the loaded namespace rather than by grep,
so a call assembled as a string would also have shown:

ns <- asNamespace("whep")
Filter(function(nm) {
  f <- get(nm, envir = ns)
  is.function(f) && any(grepl("\\.polity_bridge", deparse(body(f))))
}, ls(ns, all.names = TRUE))
#> character(0)

.aggregate_to_polities(), the only plausible caller, resolves through
.add_polity_columns_dt().polity_crosswalk() instead, because it needs the
year-aware lookup; .polity_bridge() wrapped the year-blind
.current_area_lookup() (one row per area_code).

The cost was in the tests: two blocks in test_read_raw_inputs.R mocked
.polity_bridge() and read as though the fixture controlled what the aggregator
resolved. It did not. They passed because the shipped crosswalk resolves area
203 to ESP-1800-2025 / "Spain" / bucket 203 on its own — so they were not the
hermetic unit tests they looked like, and a reader reasoned from a fixture that
was never in play.

What changed

  • Deleted .polity_bridge().
  • Replaced the two inert local_mocked_bindings(.polity_bridge = …) blocks with
    a .local_aggregator_crosswalk() helper that mocks .polity_crosswalk()
    the binding the aggregator actually reaches — mirroring what
    test_polity_folds.R's .local_fold_crosswalk() already does. Its fixture is
    deliberately not a shipped value (Testland / TST-1900-2025).
  • Added a test asserting the aggregator carries the fixture's label and reporting
    polity code out, so an inert mock cannot pass again.

Classification: mechanical — dead-code removal plus a test-fidelity fix. No
published value changes; the deleted helper had no callers.

Verification

The new assertion fails whenever the mock is inert. Probe run on the unfixed
code (git stash of the source change), mocking .polity_bridge() with the same
Testland fixture:

✖ | 2        0 | bridge_probe
Failure ('test_bridge_probe.R:22:3'): mocking .polity_bridge controls the aggregator (whep#590 probe)
result$area (`actual`) not equal to "Testland" (`expected`).
`actual`:   "Spain"
`expected`: "Testland"

Failure ('test_bridge_probe.R:23:3'): …
result$reporting_polity_code (`actual`) not equal to "TST-1900-2025" (`expected`).
`actual`:   "ESP-1800-2025"
`expected`: "TST-1900-2025"

[ FAIL 2 | WARN 0 | SKIP 0 | PASS 0 ]

The shipped Spain values leaking through the fixture are the defect. With the
fix, the same assertions routed through the mocked .polity_crosswalk() pass:

devtools::test(filter = "read_raw_inputs")
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 26 ]

devtools::test(filter = "polity_folds")   # aggregator's other consumer
[ FAIL 0 | WARN 11 | SKIP 0 | PASS 114 ]

air format . clean; lintr clean on both changed files (no lints). No roxygen
change (the helper was undocumented), so no man/ or _pkgdown.yml update.

Closes #590.

🤖 Generated with Claude Code

…walk

`.polity_bridge()` in R/read_raw_inputs.R memoised an area -> polity bridge
that nothing called: `.aggregate_to_polities()` resolves areas through
`.add_polity_columns_dt()` -> `.polity_crosswalk()`, because it needs the
year-aware lookup, while `.polity_bridge()` wrapped the year-blind
`.current_area_lookup()`. Verified against the loaded namespace, not by
grep, so a call assembled as a string would also have shown.

Two tests in test_read_raw_inputs.R mocked it and read as though the fixture
controlled what the aggregator resolved. It did not: they passed because the
shipped crosswalk resolves area 203 to Spain / bucket 203 anyway, so the
tests silently depended on package data the fixture appeared to rule out.

Delete the helper, and route the two tests through a
`.local_aggregator_crosswalk()` helper that mocks `.polity_crosswalk()` --
the binding the aggregator actually reaches -- with a deliberately
non-shipped `Testland` / `TST-1900-2025` fixture, plus a new test asserting
the aggregator carries those values out. That assertion fails when the mock
is inert (it sees `Spain` / `ESP-1800-2025`), so the mock can no longer be
mistaken for live.

No published value changes: the deleted helper had no callers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@lbm364dl lbm364dl self-assigned this Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.polity_bridge() is dead code, and two tests mock it believing it controls the aggregator

1 participant