Files
scientific-agent-skills/tests/ontology-term-resolution/test_scripts.py
T
Timothy Kassis 4fb7e0bc29 Test every script-bearing skill, and enforce that it stays that way
AGENTS.md has always said that a skill shipping scripts/ puts its tests in
tests/<name>/, but nothing checked it: 54 of the 100 such skills had no suite
at all, including docx, pptx, xlsx, pdf and scanpy. All 100 now do.

tests/_meta is the guard. It runs the shared structural contract across every
skill in a single process -- safe because it parses scripts with ast and never
imports them -- and fails when a skill ships scripts/ without a suite or
without a [skills.<name>] entry in skill-requirements.toml. It needs no
scientific packages and finishes in seconds, so skill-tests.yml blocks every
pull request on it, plus the packages=[] suites.

tests/_contract holds what the per-skill suites were each reimplementing:
frontmatter conformance, the 500-line limit, no tests or bytecode under
skills/, local links resolving, scripts parsing, no eval/exec/os.system, no
standard-library shadowing, no hardcoded local paths, valid shell scripts. Also
the --help contract, which skips when a skill's packages are absent and runs
for real under --isolated, and shared behaviour for the files docx/pptx/xlsx
and five schematic-shipping skills carry byte-identical copies of, with drift
detection so they cannot diverge silently.

67 new suites, 3325 test functions. The existing suites were retrofitted: 18
no longer pin an exact skill version, so a version bump no longer breaks a
test; 11 duplicated structural methods removed; 19 wired to the --help
contract; and 5 that failed collection without their packages now skip
cleanly. run_all.py in the bare project environment goes from 6 failures to 0.

Writing the tests surfaced 19 defects in the skills, fixed here with version
bumps. The ones that changed scientific output:

  - openpiv reported vorticity 0 for a rotating flow, from a sign error in
    openpiv's y-up coordinate relabelling; solid-body rotation now gives 2w
    exactly, on grids of either orientation
  - deepchem returned solubility predictions in z-scored space while labelling
    them log(mol/L), because it transformed a y-less dataset instead of
    untransforming the output
  - neuropixels-analysis had the Allen and IBL ISI thresholds swapped,
    contradicting its own references/QUALITY_METRICS.md and inverting the two
    standards' relative strictness
  - scanpy's summarize() raised TypeError on every AnnData under anndata 0.13,
    which reports an unnamed None key on .layers; scanpy convert was broken
  - experimental-design's Latin hypercube was never reproducible: pyDOE3 draws
    from its own default_rng and ignores numpy's global seed
  - primekg shipped a hardcoded path naming a person, which is why
    no_personal_paths is now a contract rule

The remainder is upstream API drift, each verified against the installed
package: retired symbols in bioservices 1.16, gget helpers that returned lists
where a string was written, ArviZ 1.x kwargs in pymc, a positional-only
factory in pymoo, ReduceLROnPlateau(verbose=) in torch 2.13, a removed scvelo
parameter, and PyPDF2 in scientific-slides.

Two manifest environments could not build and are pinned: gget, where an
unpinned scanpy walked back to 1.9.8 and pulled llvmlite 0.36 which does not
compile on 3.13, and pymatgen, pinned to the snapshot its own _common.py
enforces rather than loosening that check. deepchem gains torch, without which
no model class exists.

python tests/run_all.py --isolated: 101 passed, 0 failed.
2026-07-28 10:20:12 -07:00

489 lines
20 KiB
Python

"""Unit tests for the ontology-term-resolution skill scripts.
The offline tests stub every network call, so the suite runs without touching
EBI. A handful of live smoke tests are gated behind OLS_LIVE_TESTS=1; they
document the API behaviour the scripts were built against.
uv run --with pytest python -m pytest tests/ontology-term-resolution -q
OLS_LIVE_TESTS=1 uv run --with pytest python -m pytest tests/ontology-term-resolution -q
"""
from __future__ import annotations
import importlib.util
import io
import json
import os
import sys
import tempfile
import unittest
from contextlib import redirect_stdout
from pathlib import Path
from unittest.mock import patch
import skill_contract
SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "ontology-term-resolution"
SCRIPTS_DIR = SKILL_ROOT / "scripts"
LIVE = os.environ.get("OLS_LIVE_TESTS") == "1"
def _load_script(name: str):
"""Load a bundled script as a module regardless of cwd."""
if str(SCRIPTS_DIR) not in sys.path:
sys.path.insert(0, str(SCRIPTS_DIR))
spec = importlib.util.spec_from_file_location(name, SCRIPTS_DIR / f"{name}.py")
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
ols_client = _load_script("ols_client")
resolve_terms = _load_script("resolve_terms")
validate_terms = _load_script("validate_terms")
def doc(**overrides):
"""A search doc shaped like the ones OLS returns."""
base = {
"obo_id": "UBERON:0002107",
"label": "liver",
"synonym": ["iecur", "jecur"],
"ontology_name": "uberon",
"is_defining_ontology": True,
"type": "class",
}
base.update(overrides)
return base
class CurieHelperTests(unittest.TestCase):
def test_is_curie_accepts_real_prefixes(self):
for value in ("UBERON:0002107", "CL:0000182", "NCBITaxon:9606", "Orphanet:558",
"HsapDv:0000001", "APOLLO_SV:00000001"):
self.assertTrue(ols_client.is_curie(value), value)
def test_is_curie_rejects_non_curies(self):
for value in ("liver", "not a curie", "", "http://purl.obolibrary.org/obo/UBERON_0002107",
"UBERON_0002107"):
self.assertFalse(ols_client.is_curie(value), value)
def test_ontology_id_is_the_lowercased_prefix(self):
self.assertEqual(ols_client.curie_to_ontology_id("HP:0001250"), "hp")
self.assertEqual(ols_client.curie_to_ontology_id("NCBITaxon:9606"), "ncbitaxon")
self.assertEqual(ols_client.curie_to_ontology_id("HsapDv:0000001"), "hsapdv")
def test_ontology_id_override_for_orphanet(self):
"""Orphanet CURIEs are served by the ontology OLS calls `ordo`."""
self.assertEqual(ols_client.curie_to_ontology_id("Orphanet:558"), "ordo")
def test_ontology_id_of_junk_is_none(self):
self.assertIsNone(ols_client.curie_to_ontology_id("not a curie"))
class IriConversionTests(unittest.TestCase):
def test_obo_purl_to_curie(self):
self.assertEqual(
ols_client.iri_to_curie("http://purl.obolibrary.org/obo/MONDO_0005135"),
"MONDO:0005135",
)
def test_efo_namespace_to_curie(self):
"""EFO does not live under the OBO PURL namespace."""
self.assertEqual(
ols_client.iri_to_curie("http://www.ebi.ac.uk/efo/EFO_0000246"), "EFO:0000246"
)
def test_orphanet_namespace_to_curie(self):
self.assertEqual(
ols_client.iri_to_curie("http://www.orpha.net/ORDO/Orphanet_558"),
"Orphanet:558",
)
def test_multi_underscore_prefix_splits_on_the_last_underscore(self):
self.assertEqual(
ols_client.iri_to_curie("http://purl.obolibrary.org/obo/APOLLO_SV_00000001"),
"APOLLO_SV:00000001",
)
def test_unconvertible_iris_return_none(self):
self.assertIsNone(ols_client.iri_to_curie(""))
self.assertIsNone(ols_client.iri_to_curie("http://example.org/nounderscore"))
def test_candidate_iris_use_the_right_template(self):
self.assertEqual(
ols_client.candidate_iris("UBERON:0002107"),
["http://purl.obolibrary.org/obo/UBERON_0002107"],
)
self.assertEqual(
ols_client.candidate_iris("EFO:0000246"),
["http://www.ebi.ac.uk/efo/EFO_0000246"],
)
self.assertEqual(
ols_client.candidate_iris("Orphanet:558"),
["http://www.orpha.net/ORDO/Orphanet_558"],
)
class LabelMatchingTests(unittest.TestCase):
def test_normalize_folds_case_and_whitespace_only(self):
self.assertEqual(ols_client.normalize_label(" Liver\tCortex "), "liver cortex")
self.assertEqual(
ols_client.normalize_label("CD4-positive"), "cd4-positive",
"hyphens carry meaning and must survive normalisation",
)
def test_synonyms_collected_from_every_field_spelling(self):
self.assertEqual(ols_client.synonyms_of({"synonym": ["a"]}), ["a"])
self.assertEqual(ols_client.synonyms_of({"synonyms": ["b"]}), ["b"])
self.assertEqual(
sorted(ols_client.synonyms_of({"exact_synonyms": ["c"], "related_synonyms": ["d"]})),
["c", "d"],
)
def test_match_type_distinguishes_label_synonym_and_partial(self):
self.assertEqual(ols_client.match_type("liver", doc()), "exact_label")
self.assertEqual(ols_client.match_type("LIVER", doc()), "exact_label")
self.assertEqual(ols_client.match_type("iecur", doc()), "exact_synonym")
self.assertEqual(
ols_client.match_type("liver", doc(label="caudate lobe of liver", synonym=[])),
"partial",
"OLS ranks partial hits alongside exact ones; the client must tell them apart",
)
class CandidateRankingTests(unittest.TestCase):
def test_dedupe_keeps_the_defining_ontologys_copy(self):
docs = [
doc(ontology_name="cl", is_defining_ontology=False),
doc(ontology_name="uberon", is_defining_ontology=True),
]
deduped = ols_client.dedupe_candidates(docs)
self.assertEqual(len(deduped), 1)
self.assertEqual(deduped[0]["ontology_name"], "uberon")
def test_dedupe_skips_docs_without_an_identifier(self):
self.assertEqual(ols_client.dedupe_candidates([{"label": "x"}]), [])
def test_exact_matches_outrank_partial_ones(self):
docs = [
doc(obo_id="UBERON:0001117", label="caudate lobe of liver", synonym=[]),
doc(obo_id="UBERON:0002107", label="liver"),
]
ranked = ols_client.rank_candidates("liver", docs)
self.assertEqual([r["obo_id"] for r in ranked],
["UBERON:0002107", "UBERON:0001117"])
self.assertEqual(ranked[0]["match_type"], "exact_label")
self.assertEqual(ranked[1]["match_type"], "partial")
def test_label_match_outranks_synonym_match(self):
docs = [
doc(obo_id="UBERON:0000001", label="other", synonym=["liver"]),
doc(obo_id="UBERON:0002107", label="liver", synonym=[]),
]
ranked = ols_client.rank_candidates("liver", docs)
self.assertEqual(ranked[0]["obo_id"], "UBERON:0002107")
self.assertEqual(ranked[1]["match_type"], "exact_synonym")
class ResolveStrategyTests(unittest.TestCase):
def test_ladder_stops_at_the_first_strategy_that_hits(self):
calls = []
def fake_search(text, **kwargs):
calls.append(kwargs.get("query_fields"))
return [doc()]
with patch.object(resolve_terms, "search", fake_search):
result = resolve_terms.resolve_one(
"liver", ontology="uberon", subtree_iri=None, rows=5, exact_only=False
)
self.assertEqual(result["strategy"], "exact")
self.assertEqual(calls, ["label,synonym"], "later strategies must not run")
def test_ladder_escalates_when_exact_finds_nothing(self):
seen = []
def fake_search(text, **kwargs):
seen.append(kwargs)
return [doc(label="heart left ventricle", synonym=[])] if len(seen) == 3 else []
with patch.object(resolve_terms, "search", fake_search):
result = resolve_terms.resolve_one(
"left ventrical", ontology=None, subtree_iri=None, rows=5, exact_only=False
)
self.assertEqual(result["strategy"], "fulltext")
self.assertEqual(result["candidates"][0]["match_type"], "partial")
def test_exact_only_never_escalates(self):
calls = []
def fake_search(text, **kwargs):
calls.append(kwargs)
return []
with patch.object(resolve_terms, "search", fake_search):
result = resolve_terms.resolve_one(
"zzzquux", ontology=None, subtree_iri=None, rows=5, exact_only=True
)
self.assertEqual(len(calls), 1)
self.assertEqual(result["candidates"], [])
def test_unresolved_query_becomes_a_visible_row(self):
rows = resolve_terms.to_rows([{"query": "zzz", "strategy": "exact", "candidates": []}])
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]["match_type"], "unresolved")
self.assertEqual(rows[0]["curie"], "", "an unresolved term must never carry an ID")
class ValidateTermTests(unittest.TestCase):
def check(self, term, curie="UBERON:0002107", label=None, branch=None, ontologies=None,
ancestors=()):
with patch.object(validate_terms, "term_detail", return_value=term), \
patch.object(validate_terms, "ancestor_curies", return_value=set(ancestors)):
return validate_terms.check_term(
curie, label, branch=branch, expect_ontologies=ontologies
)
def live_term(self, **overrides):
base = {
"label": "liver",
"ontology_name": "uberon",
"is_defining_ontology": True,
"is_obsolete": False,
"type": "class",
"synonyms": ["iecur"],
"_home_ontology": "uberon",
"_resolved_via": "obo_id",
}
base.update(overrides)
return base
def test_valid_term_passes(self):
result = self.check(self.live_term(), label="liver")
self.assertEqual(result["status"], "ok")
self.assertEqual(result["actual_label"], "liver")
def test_malformed_curie_is_rejected_without_a_lookup(self):
with patch.object(validate_terms, "term_detail") as lookup:
result = validate_terms.check_term(
"liver", None, branch=None, expect_ontologies=None
)
lookup.assert_not_called()
self.assertEqual(result["status"], "malformed_curie")
def test_missing_term_is_not_found(self):
self.assertEqual(self.check(None)["status"], "not_found")
def test_obsolete_term_reports_its_replacement_as_a_curie(self):
term = self.live_term(
label="obsolete_parasitic infection",
ontology_name="efo",
is_obsolete=True,
term_replaced_by="http://purl.obolibrary.org/obo/MONDO_0005135",
_home_ontology="efo",
)
result = self.check(term, curie="EFO:0001067")
self.assertEqual(result["status"], "obsolete")
self.assertEqual(result["replacement"], "MONDO:0005135")
def test_obsolete_without_replacement_says_so(self):
result = self.check(self.live_term(is_obsolete=True))
self.assertEqual(result["status"], "obsolete")
self.assertEqual(result["replacement"], "")
self.assertIn("no stated replacement", result["detail"])
def test_label_mismatch_reports_both_labels(self):
result = self.check(self.live_term(), label="kidney")
self.assertEqual(result["status"], "label_mismatch")
self.assertIn("kidney", result["detail"])
self.assertIn("liver", result["detail"])
def test_label_comparison_is_case_insensitive(self):
self.assertEqual(self.check(self.live_term(), label="LIVER")["status"], "ok")
def test_synonym_label_warns_and_names_the_primary_label(self):
result = self.check(self.live_term(), label="iecur")
self.assertEqual(result["status"], "matched_synonym")
self.assertIn("primary label is 'liver'", result["detail"])
def test_wrong_ontology_is_caught(self):
term = self.live_term(label="hepatocyte", ontology_name="cl", _home_ontology="cl")
result = self.check(term, curie="CL:0000182", ontologies={"uberon"})
self.assertEqual(result["status"], "wrong_ontology")
def test_branch_membership_passes_and_fails(self):
term = self.live_term()
ok = self.check(term, branch="UBERON:0000465", ancestors={"UBERON:0000465"})
self.assertEqual(ok["status"], "ok")
bad = self.check(term, branch="CL:0000000", ancestors={"UBERON:0000465"})
self.assertEqual(bad["status"], "wrong_branch")
def test_a_term_is_in_its_own_branch(self):
result = self.check(self.live_term(), branch="UBERON:0002107", ancestors=set())
self.assertEqual(result["status"], "ok")
def test_imported_only_term_warns(self):
term = self.live_term(ontology_name="efo", is_defining_ontology=False,
_home_ontology="mondo", _resolved_via="iri")
result = self.check(term, curie="MONDO:0000001")
self.assertEqual(result["status"], "imported_only")
self.assertIn("mondo does not define", result["detail"])
def test_non_class_terms_warn(self):
result = self.check(self.live_term(type="property"))
self.assertEqual(result["status"], "not_a_class")
def test_failure_beats_warning(self):
"""A wrong branch outranks a synonym-label warning."""
result = self.check(self.live_term(), label="iecur", branch="CL:0000000",
ancestors={"UBERON:0000465"})
self.assertEqual(result["status"], "wrong_branch")
class InputParsingTests(unittest.TestCase):
def parse(self, text, terms=()):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "in.tsv"
path.write_text(text, encoding="utf-8")
args = validate_terms.build_parser().parse_args(
[*terms, "--input", str(path)]
)
return validate_terms.read_pairs(args)
def test_tsv_with_headers(self):
pairs = self.parse("id\tlabel\nUBERON:0002107\tliver\nCL:0000182\thepatocyte\n")
self.assertEqual(pairs, [("UBERON:0002107", "liver"), ("CL:0000182", "hepatocyte")])
def test_alternate_header_names(self):
pairs = self.parse("ontology_term_id\tterm_label\nUBERON:0002107\tliver\n")
self.assertEqual(pairs, [("UBERON:0002107", "liver")])
def test_csv_is_detected(self):
pairs = self.parse("id,label\nUBERON:0002107,liver\n")
self.assertEqual(pairs, [("UBERON:0002107", "liver")])
def test_headerless_two_columns(self):
pairs = self.parse("UBERON:0002107\tliver\n")
self.assertEqual(pairs, [("UBERON:0002107", "liver")])
def test_single_column_has_no_labels(self):
pairs = self.parse("UBERON:0002107\nCL:0000182\n")
self.assertEqual(pairs, [("UBERON:0002107", None), ("CL:0000182", None)])
def test_id_only_header_without_label_column(self):
pairs = self.parse("curie\nUBERON:0002107\n")
self.assertEqual(pairs, [("UBERON:0002107", None)])
def test_comment_and_blank_lines_are_skipped(self):
pairs = self.parse("id\tlabel\n# a note\n\nUBERON:0002107\tliver\n")
self.assertEqual(pairs, [("UBERON:0002107", "liver")])
def test_positional_terms_merge_with_file_terms(self):
pairs = self.parse("id\nCL:0000182\n", terms=["UBERON:0002107"])
self.assertEqual(pairs, [("UBERON:0002107", None), ("CL:0000182", None)])
def test_resolve_deduplicates_queries_preserving_order(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "q.txt"
path.write_text("liver\n# comment\nheart\nliver\n", encoding="utf-8")
args = resolve_terms.build_parser().parse_args(["--input", str(path)])
self.assertEqual(resolve_terms.read_inputs(args), ["liver", "heart"])
class ExitCodeTests(unittest.TestCase):
def run_validate(self, argv, results):
with patch.object(validate_terms, "check_term", side_effect=results), \
redirect_stdout(io.StringIO()) as out:
code = validate_terms.main(argv)
return code, out.getvalue()
def result(self, status):
return {"id": "X:1", "status": status, "actual_label": "", "ontology": "",
"replacement": "", "detail": ""}
def test_clean_run_exits_zero(self):
code, _ = self.run_validate(["UBERON:0002107"], [self.result("ok")])
self.assertEqual(code, 0)
def test_failure_exits_one(self):
code, _ = self.run_validate(["UBERON:9999999"], [self.result("not_found")])
self.assertEqual(code, 1)
def test_warning_alone_exits_zero(self):
code, _ = self.run_validate(["UBERON:0002107"], [self.result("matched_synonym")])
self.assertEqual(code, 0)
def test_strict_makes_warnings_fail(self):
code, _ = self.run_validate(
["UBERON:0002107", "--strict"], [self.result("matched_synonym")]
)
self.assertEqual(code, 1)
def test_no_input_exits_two(self):
with redirect_stdout(io.StringIO()):
self.assertEqual(validate_terms.main([]), 2)
def test_bad_branch_curie_exits_two(self):
with redirect_stdout(io.StringIO()):
code = validate_terms.main(["UBERON:0002107", "--branch", "liver"])
self.assertEqual(code, 2)
def test_tsv_output_has_the_documented_columns(self):
_, output = self.run_validate(["UBERON:0002107"], [self.result("ok")])
self.assertEqual(output.splitlines()[0].split("\t"), list(validate_terms.TSV_COLUMNS))
def test_json_output_is_a_list_of_records(self):
_, output = self.run_validate(
["UBERON:0002107", "--format", "json"], [self.result("ok")]
)
self.assertEqual(json.loads(output)[0]["status"], "ok")
@unittest.skipUnless(LIVE, "set OLS_LIVE_TESTS=1 to run tests that call EBI OLS")
class LiveApiTests(unittest.TestCase):
"""Pin the live-service behaviour the offline logic assumes."""
def test_exact_true_alone_is_not_exact_label_matching(self):
loose = ols_client.search("liver", ontology="uberon", query_fields=None, rows=5)
tight = ols_client.search("liver", ontology="uberon", query_fields="label", rows=5)
self.assertGreater(len(loose), len(tight))
self.assertEqual([d["label"] for d in tight], ["liver"])
def test_obsolete_term_carries_its_replacement(self):
term = ols_client.term_detail("EFO:0001067")
self.assertTrue(term["is_obsolete"])
self.assertEqual(
ols_client.iri_to_curie(term["term_replaced_by"]), "MONDO:0005135"
)
def test_search_cannot_report_obsolescence(self):
docs = ols_client.search(
"obsolete_parasitic infection", ontology="efo", include_obsolete=True, rows=1
)
self.assertTrue(docs)
self.assertNotIn("is_obsolete", docs[0])
self.assertNotIn("term_replaced_by", docs[0])
def test_iri_fallback_resolves_a_term_missing_from_the_obo_id_index(self):
term = ols_client.term_detail("MONDO:0000001")
self.assertIsNotNone(term, "MONDO:0000001 is live but unindexed by obo_id")
self.assertEqual(term["_resolved_via"], "iri")
def test_orphanet_override_resolves(self):
term = ols_client.term_detail("Orphanet:558")
self.assertEqual(term["label"], "Marfan syndrome")
def test_unknown_id_resolves_to_nothing(self):
self.assertIsNone(ols_client.term_detail("UBERON:9999999"))
# The shared --help contract: every argparse CLI this skill ships answers --help
# without doing any work. It skips when the skill's packages are absent and runs
# for real under `python tests/run_all.py --isolated`.
CliHelpTests = skill_contract.cli.help_test_case(SKILL_ROOT)
if __name__ == "__main__":
unittest.main()