Commit 8766968
authored
Assertions framework for BabelTest expectations (#102)
Adds `src/babel_validation/assertions`: the engine that turns a named
BabelTest assertion plus its parameters into a check evaluated against
NodeNorm/NameRes. Nothing consumes it yet — the GitHub issue parser that
produces the parameters arrives later in the stack — so this PR is the
engine, its generated documentation, and its offline tests.
**Stack 2 of 4** splitting #67. Base: `main` (#101 is merged).
### The assertion types
`AssertionHandler` is the base class; `NodeNormTest` and `NameResTest`
specialize it per service. Each concrete handler declares its parameters
and yields `TestResult`s. Registered: `Resolves`, `DoesNotResolve`,
`ResolvesWith`, `DoesNotResolveWith`, `HasLabel`, `ResolvesWithType`,
`SearchByName`, and `Needed` (a placeholder that always fails, marking
an issue as still needing a real test).
Both wiki (`{{BabelTest|Resolves|CHEBI:15365}}`) and YAML syntax are
supported, and an assertion can carry several independent **params
lists**, each evaluated separately so one bad one doesn't sink the rest.
### Shared parameter handling
`AssertionHandler.prepare_params_lists()` strips whitespace from every
param, rejects params lists whose CURIEs are malformed, and warms the
NodeNorm cache for the survivors in a single batched request. Both
`test_with_nodenorm()` and `test_with_nameres()` route through it, so
the NameRes path gets the same validation and the same one-request
warming rather than a NodeNorm round-trip per params list.
Two escape hatches keep that uniform treatment from being wrong for
particular assertions:
- `curie_params()` narrows which params are CURIEs — `HasLabel` to the
first, `ResolvesWithType` to everything after the Biolink type,
`SearchByName` to the expected CURIE only (its first param is a
free-text query).
- `VALIDATE_CURIES = False` opts an assertion out of format validation
entirely. `DoesNotResolve` sets it: an identifier that isn't even a
well-formed CURIE trivially doesn't resolve, which is exactly what that
assertion exists to state, so rejecting it up front would leave the
assertion unable to express its own purpose.
### A NodeNorm bulk-normalization fix
`CachedNodeNorm.normalize_curies()` built its return value from
`response.json()`, so a CURIE that NodeNorm silently omitted from its
response was absent from the returned dict rather than present with a
`None` value. A caller iterating the results would never see it and
would report success for a CURIE it never tested. The warm-cache path
happened to re-add the missing key, so the hole only opened on a cold
lookup.
It now builds the result from the requested CURIEs: exactly one entry
per request, in request order. The ordering guarantee matters
independently — "first CURIE that resolved" logic previously depended on
the server's JSON ordering when cold and on set iteration order
(per-process string hash randomization) when warm, so *which* CURIE got
blamed in a failure message could vary between runs of the same test.
### Types and naming
Parameters are named rather than left as nested `list[str]`:
- `ParamsList` — one assertion invocation's parameters. Position is
significant (`ResolvesWithType` takes its Biolink type first, `HasLabel`
is `[curie, label]`), which is why this is a list.
- `PreparedParamsList` — a frozen `(params, failure)` record.
`prepare_params_lists()` returns a list of these rather than a
`(stripped, failures_by_index)` tuple, so callers don't re-zip two
structures by hand.
Service parameters are annotated with the `NodeNormService` /
`NameResService` Protocols that `services/` already defines for the
purpose, rather than the concrete `CachedNodeNorm` / `CachedNameRes`, so
a future drop-in replacement needs no changes here.
### Guardrails
- **Registration.** `_register()` replaces the `{h.NAME: h for h in
[...]}` comprehension and raises on a `NAME` that isn't lowercase or one
that's already taken. Lowercase is load-bearing — the README promises
users that assertion names are matched case-insensitively, which only
holds if every registry key is lowercase — and a duplicate `NAME` would
otherwise silently drop a handler. Both are mistakes only made while
adding an assertion, so they fail loudly at import.
- **Missing Biolink types.** When NodeNorm returns a node with no type,
messages show `NO TYPE RETURNED`. The earlier placeholder, `unknown
type`, had the shape of a real type — the older Biolink vocabulary was
lowercase prose like `chemical entity` — so it could be misread as
something Babel actually returned.
### Documentation
`gen_docs.py` renders `assertions/README.md` from the handler class
attributes, grouping handlers by the service they test rather than by
their order in `ASSERTION_HANDLERS` — otherwise a handler registered in
the wrong place lands under the wrong heading, and the sync test can't
catch it because it regenerates the same wrong output.
`tests/test_environment/test_assertions_docs.py` asserts the checked-in
README stays in sync.
The "Adding a New Assertion Type" instructions live in that generated
README and nowhere else. There had been a second copy in the package
docstring and the two had already drifted; the docstring now points at
the README and describes the module layout instead.
### Tests
`tests/test_environment/test_assertions.py` stubs `requests.post` rather
than the service, so handlers run against the real `CachedNodeNorm` and
exercise its contract instead of a fake restating it. The fixture DB
drops one CURIE from the response entirely, reproducing what NodeNorm
does for some unknown identifiers. 13 unit tests, all offline.
Also registers the `unit` pytest marker, first used by these tests.
### Outcomes
- The assertion vocabulary is fixed, documented, and enforced, so the
issue parser in the next part of the stack has a stable target.
- Every check here runs offline — no network, no GitHub API, no Google
Sheet.
- Deliberately not here: parsing assertions out of issue bodies, and any
wiring into the existing pytest suites.
### Notes for review
`gh pr diff` shows hunks in `.gitignore`, `services/nameres.py`,
`sources/google_sheets/`, and `tests/conftest.py` that came from #101
and are already on `main` — they merge as no-ops. The net change against
`main` is the `assertions/` package, the `normalize_curies()` fix, the
two test files, and the pytest marker.
`result: dict` is left untyped for NodeNorm response entries. A
`TypedDict` would be more precise, but the response shape is Babel's to
change and a wrong one is worse than an honest `dict`, so the docstrings
say what the dict is instead.
### Verify
- `uv run pytest -m unit -q` → 13 assertion tests pass offline.
- `uv run python -m src.babel_validation.assertions.gen_docs` reproduces
the committed README.
🤖 Generated with [Claude Code](https://claude.com/claude-code)10 files changed
Lines changed: 1330 additions & 10 deletions
File tree
- src/babel_validation
- assertions
- services
- tests/test_environment
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments