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
|
# -*- coding: utf-8 -*-
"""Test for web."""
import json
import unittest
from typing import ClassVar, Dict, List
import rdflib
import rdflib.plugins.parsers.notation3
import yaml
from fastapi import FastAPI
from starlette.testclient import TestClient
from bioregistry import Resource
from bioregistry.app.api import MappingResponse, URIResponse
from bioregistry.app.impl import get_app
class TestWeb(unittest.TestCase):
"""Tests for the web application."""
fastapi: ClassVar[FastAPI]
client: ClassVar[TestClient]
@classmethod
def setUpClass(cls) -> None:
"""Set up the test case with an app."""
cls.fastapi = get_app()
cls.client = TestClient(cls.fastapi)
def test_api_registry(self):
"""Test the registry endpoint."""
fail_endpoint = "/api/registry?format=FAIL"
with self.subTest(endpoint=fail_endpoint):
res = self.client.get(fail_endpoint)
self.assertEqual(400, res.status_code)
for endpoint, parse_func in [
("/api/registry", self._parse_registry_json),
("/api/registry?format=json", self._parse_registry_json),
("/api/registry?format=yaml", self._parse_registry_yaml),
# ("/api/registry?format=turtle", partial(self._parse_registry_rdf, format="turtle")),
]:
with self.subTest(endpoint=endpoint):
self._test_registry(endpoint, parse_func)
def _test_registry(self, endpoint, parse_func):
res = self.client.get(endpoint)
self.assertEqual(200, res.status_code)
self.assertIsInstance(res.text, str)
registry = parse_func(res)
self.assertIn("chebi", registry)
self.assertEqual("CHEBI", registry["chebi"].get_preferred_prefix())
@staticmethod
def _parse_registry_json(res) -> Dict[str, Resource]:
data = res.json().items()
return {key: Resource.parse_obj(resource) for key, resource in data}
def _parse_registry_rdf(self, res, fmt: str) -> Dict[str, Resource]:
graph = rdflib.Graph()
try:
graph.parse(data=res.text, format=fmt)
except rdflib.plugins.parsers.notation3.BadSyntax:
self.fail(f"Bad syntax for format {fmt}:\n\n{res.text}")
sparql = """\
SELECT ?prefix ?description ?homepage ?uri_format ?example
WHERE {
?resource a bioregistry.schema:0000001 ;
dcterms:description ?description ;
foaf:homepage ?homepage ;
bioregistry.schema:0000029 ?prefix ;
bioregistry.schema:0000006 ?uri_format ;
bioregistry.schema:0000005 ?example ;
}
"""
rv = {}
for record in graph.query(sparql):
prefix = record["prefix"]["value"]
rv[prefix] = Resource(
prefix=prefix,
description=record["description"]["value"],
homepage=record["homepage"]["value"],
uri_format=record["uri_format"]["value"],
example=record["example"]["value"],
)
return rv
@staticmethod
def _parse_registry_yaml(res) -> Dict[str, Resource]:
data = yaml.safe_load(res.text).items()
return {key: Resource.parse_obj(resource) for key, resource in data}
def test_api_resource(self):
"""Test the resource endpoint."""
res = self.client.get("/api/registry/3dmet?format=nope")
self.assertEqual(400, res.status_code)
self.assert_endpoint(
"/api/registry/3dmet",
["yaml", "json"],
)
# test something that's wrong gives a proper error
with self.subTest(fmt=None):
res = self.client.get("/api/registry/nope")
self.assertEqual(404, res.status_code)
def test_ui_resource_rdf(self):
"""Test the UI resource with content negotiation."""
prefix = "3dmet"
for accept, fmt in [
("text/turtle", "turtle"),
("text/n3", "n3"),
("application/ld+json", "jsonld"),
]:
with self.subTest(format=fmt):
res = self.client.get(f"/registry/{prefix}", headers={"Accept": accept})
self.assertEqual(
200, res.status_code, msg=f"Failed on {prefix} to accept {accept} ({fmt})"
)
self.assertEqual(accept, res.request.headers.get("Accept", []))
if fmt == "jsonld":
continue
with self.assertRaises(ValueError, msg="result was return as JSON"):
json.loads(res.text)
g = rdflib.Graph()
g.parse(data=res.text, format=fmt)
# Check for single prefix
results = list(
g.query("SELECT ?s WHERE { ?s a <https://bioregistry.io/schema/#0000001> }")
)
self.assertEqual(1, len(results))
self.assertEqual(f"https://bioregistry.io/registry/{prefix}", str(results[0][0]))
def test_api_metaregistry(self):
"""Test the metaregistry endpoint."""
self.assert_endpoint(
"/api/metaregistry",
["json", "yaml"],
)
def test_api_metaresource(self):
"""Test the metaresource endpoint."""
self.assert_endpoint(
"/api/metaregistry/miriam",
["json", "yaml", "turtle", "jsonld"],
)
def test_api_reference(self):
"""Test the reference endpoint."""
for value in [
"/api/reference/chebi:24867",
"/api/reference/ctri:CTRI/2023/04/052053", # check paths are accepted
]:
self.assert_endpoint(
value,
["json", "yaml"],
)
def test_api_collections(self):
"""Test the collections endpoint."""
self.assert_endpoint(
"/api/collection",
["json", "yaml"],
)
def test_api_collection(self):
"""Test the collection endpoint."""
self.assert_endpoint(
"/api/collection/0000001",
["json", "yaml", "turtle", "jsonld"],
)
res = self.client.get("/api/collection/0000001?format=context").json()
self.assertIn("@context", res)
self.assertIn("biostudies", res["@context"])
def test_api_contexts(self):
"""Test the contexts endpoint."""
self.assert_endpoint(
"/api/context",
["json", "yaml"],
)
def test_api_context(self):
"""Test the context endpoint."""
self.assert_endpoint(
"/api/context/obo",
["json", "yaml"],
)
def test_api_contributors(self):
"""Test the contributors endpoint."""
self.assert_endpoint(
"/api/contributors",
["json", "yaml"],
)
def test_api_contributor(self):
"""Test the contributor endpoint."""
self.assert_endpoint(
"/api/contributor/0000-0003-4423-4370",
["json", "yaml"],
)
def assert_endpoint(self, endpoint: str, formats: List[str]) -> None:
"""Test downloading the full registry as JSON."""
self.assertTrue(endpoint.startswith("/"))
with self.subTest(fmt=None):
res = self.client.get(endpoint)
self.assertEqual(200, res.status_code)
for fmt in formats:
url = f"{endpoint}?format={fmt}"
with self.subTest(fmt=fmt, endpoint=url):
res = self.client.get(url)
self.assertEqual(200, res.status_code, msg=f"\n\n{res.text}")
def test_search(self):
"""Test search."""
res = self.client.get("/api/search?q=che")
self.assertEqual(200, res.status_code, msg=f"\n\n{res.text}")
def test_autocomplete(self):
"""Test search."""
for q in ["che", "chebi", "xxxxx", "chebi:123", "chebi:dd"]:
with self.subTest(query=q):
res = self.client.get(f"/api/autocomplete?q={q}")
self.assertEqual(200, res.status_code)
def test_external_registry_mappings(self):
"""Test external registry mappings."""
url = "/api/metaregistry/obofoundry/mapping/bioportal"
res = self.client.get(url)
res_parsed = MappingResponse.parse_obj(res.json())
self.assertEqual("obofoundry", res_parsed.meta.source)
self.assertEqual("bioportal", res_parsed.meta.target)
self.assertIn("gaz", res_parsed.mappings)
self.assertEqual("GAZ", res_parsed.mappings["gaz"])
# This is an obsolete OBO Foundry ontology so it won't get uploaded to BioPortal
self.assertIn("loggerhead", res_parsed.meta.source_only)
# This is a non-ontology so it won't get in OBO Foundry
self.assertIn("DCTERMS", res_parsed.meta.target_only)
def test_iri_mapping(self):
"""Test IRI mappings.
.. seealso:: https://github.com/biopragmatics/bioregistry/issues/1065
"""
uri = "http://id.nlm.nih.gov/mesh/C063233"
res = self.client.post("/api/uri/parse/", json={"uri": uri})
self.assertEqual(200, res.status_code)
data = URIResponse.parse_obj(res.json())
self.assertEqual(uri, data.uri)
self.assertIn("https://meshb.nlm.nih.gov/record/ui?ui=C063233", data.providers.values())
# Bad URI
uri = "xxxx"
res = self.client.post("/api/uri/parse/", json={"uri": uri})
self.assertEqual(404, res.status_code)
|