diff options
| author | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-10-03 12:39:04 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-10-03 12:39:04 +0000 |
| commit | 6d2254a53e298ad3f2411252f9ffb22f38ab4676 (patch) | |
| tree | 1bd69545372ffb63ce43d00a571fd1d3fe589dc9 /test/python | |
| parent | bb1a44b0c75471b5c004170552540b9632f89810 (diff) | |
| download | genenetwork4-6d2254a53e298ad3f2411252f9ffb22f38ab4676.tar.gz | |
Add /species/{species} for a single species' full row.
Checked for an existing implementation first: GeneNetwork2's
wqflask/api/router.py has /api/v_pre1/species/<species_name>, matching
Name, FullName or SpeciesName, but returning only a 4-column subset
(Id, Name, FullName, TaxonomyId). GeneNetwork3's gn3/db/wiki.py has
only internal name-to-id lookups for the wiki feature; its /species is
SPARQL-based metadata, not a Species table row. gn-guile's species
code (gn/data/species.scm) is also SPARQL/RDF metadata, not backed by
db_webqtl.
* src/db.rs (params): Import.
(SpeciesDetail): New struct, one full row of the Species table.
(get_species_by_name): New function. Same match logic as
GeneNetwork2's get_species_info (Name, FullName or SpeciesName), but
selects every column instead of a fixed subset.
* src/lib.rs (app): Add /species/{species} route.
(species_by_name): New handler. 404 if no species matches.
* tests/species_by_name.rs: New file. Tests mouse specifically
(Name=mouse is always present) and an unknown-species 404 case; skips
rather than fails when no database is reachable, same as species.rs.
* test/python/test_species_by_name.py: New file. Likewise.
* tests/openapi.rs (openapi_json_describes_all_endpoints): Check for
/species/{species} among the documented paths.
* test/python/test_openapi.py (test_openapi_json_describes_all_endpoints):
Likewise.
* README.md: Document /species/{species}.
Diffstat (limited to 'test/python')
| -rw-r--r-- | test/python/test_openapi.py | 2 | ||||
| -rw-r--r-- | test/python/test_species_by_name.py | 56 |
2 files changed, 57 insertions, 1 deletions
diff --git a/test/python/test_openapi.py b/test/python/test_openapi.py index 03100b9..0815d34 100644 --- a/test/python/test_openapi.py +++ b/test/python/test_openapi.py @@ -20,7 +20,7 @@ class OpenApiTest(ServerTestCase): doc = json.loads(body) self.assertEqual(doc["info"]["title"], "GeneNetwork4 API") - for path in ("/", "/version", "/species"): + for path in ("/", "/version", "/species", "/species/{species}"): self.assertIn(path, doc["paths"]) diff --git a/test/python/test_species_by_name.py b/test/python/test_species_by_name.py new file mode 100644 index 0000000..86f80a3 --- /dev/null +++ b/test/python/test_species_by_name.py @@ -0,0 +1,56 @@ +"""Mirrors tests/species_by_name.rs. Needs a live MariaDB (SQL_URI); skips +with a message rather than failing when none is reachable, same as +test_species.py. +""" + +import http.client +import json +import socket +import unittest + +from server_test_case import HOST, PORT, ServerTestCase + + +def database_reachable(): + try: + with socket.create_connection(("127.0.0.1", 3306), timeout=0.5): + return True + except OSError: + return False + + +class SpeciesByNameTest(ServerTestCase): + def test_species_by_name_mouse_returns_species_row(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) + try: + conn.request("GET", "/species/mouse") + response = conn.getresponse() + body = response.read() + finally: + conn.close() + self.assertEqual(response.status, 200) + + species = json.loads(body) + self.assertEqual(species["name"], "mouse") + self.assertEqual(species["full_name"], "Mus musculus") + self.assertEqual(species["taxonomy_id"], 10090) + + def test_species_by_name_unknown_species_returns_404(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) + try: + conn.request("GET", "/species/not-a-real-species") + response = conn.getresponse() + response.read() + finally: + conn.close() + self.assertEqual(response.status, 404) + + +if __name__ == "__main__": + unittest.main() |
