diff options
| author | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-10-03 15:57:54 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-10-04 06:28:08 +0000 |
| commit | 0c58ad5611077502c639cb1b3406f476f930c863 (patch) | |
| tree | b7f5ecddfdab67c8cb8ac41abb48c457eee6f1a1 /test/python | |
| parent | 18f20d6df2310ef822e541791770dfe9d7b26fc2 (diff) | |
| download | genenetwork4-0c58ad5611077502c639cb1b3406f476f930c863.tar.gz | |
Add GET /groups/{group}/traits/{trait_id}; time DB tests; share pool.
* src/db.rs (TraitInfo): New struct. Joins PublishXRef, Phenotype and
Publication; LEFT JOIN on the latter two since a handful of
PublishXRef rows have no matching row. SQL column aliases are written
to match mysql_common's UpperCamelCase conversion exactly, so no field
needs a #[mysql(rename = "...")] override.
(get_group_trait_info): New function.
(pool): New function. mysql::Pool::new eagerly opens its default 10
connections before returning (about 7s against this database); every
query function here used to call it per request, paying that cost on
every single API call. Builds the Pool once behind a
OnceLock<Mutex<Option<Pool>>> and shares the cheap, Arc-backed handle
from then on (OnceLock::get_or_init can't be used alone since
Pool::new is fallible and get_or_try_init is still unstable).
(get_all_species, get_species_by_name, get_all_groups,
get_group_by_name, get_group_strains, get_group_trait_values,
get_group_trait_info): Use it instead of building their own Pool.
* src/lib.rs (GroupTraitLookup): Generalize over the found value (was
Vec<db::TraitValue>-specific), so group_trait_info can share it with
group_trait_values.
(get_group_trait_info_result): New function.
(group_trait_info): New handler for GET /groups/{group}/traits/{trait_id}.
(app): Register it.
* tests/common/mod.rs: New file. get() issues a GET against
genenetwork4::app() and warns on stderr if it takes over a second --
this is what surfaced the Pool::new cost above.
* tests/species.rs, tests/species_by_name.rs, tests/groups.rs,
tests/group_by_name.rs, tests/group_strains.rs,
tests/group_trait_values.rs: Use tests/common::get instead of building
each request by hand.
* tests/group_trait_info.rs, test/python/test_group_trait_info.py: New
files.
* tests/openapi.rs, test/python/test_openapi.py: Check the new path is
in the generated spec.
* README.md (GET /groups/{group}/traits/{trait_id}): Document it.
(Tests): List the new test files; document tests/common::get's timing
warnings and how to see them.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d
Diffstat (limited to 'test/python')
| -rw-r--r-- | test/python/test_group_trait_info.py | 83 | ||||
| -rw-r--r-- | test/python/test_openapi.py | 1 |
2 files changed, 84 insertions, 0 deletions
diff --git a/test/python/test_group_trait_info.py b/test/python/test_group_trait_info.py new file mode 100644 index 0000000..b37250e --- /dev/null +++ b/test/python/test_group_trait_info.py @@ -0,0 +1,83 @@ +"""Mirrors tests/group_trait_info.rs. Needs a live MariaDB (SQL_URI); +skips with a message rather than failing when none is reachable, same as +test_group_by_name.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 GroupTraitInfoTest(ServerTestCase): + def test_group_trait_info_bxd_returns_phenotype_and_publication(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", "/groups/BXD/traits/10001") + response = conn.getresponse() + body = response.read() + finally: + conn.close() + self.assertEqual(response.status, 200) + + info = json.loads(body) + self.assertEqual(info["id"], 10001) + self.assertEqual(info["units"], "mg") + self.assertIn("Cerebellum weight", info["post_publication_description"]) + self.assertEqual(info["pubmed_id"], 11438585) + self.assertEqual(info["authors"], "Airey DC, Lu L, Williams RW") + self.assertEqual(info["year"], "2001") + + def test_group_trait_info_unknown_group_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", "/groups/not-a-real-group/traits/10001") + response = conn.getresponse() + body = response.read() + finally: + conn.close() + self.assertEqual(response.status, 404) + + error = json.loads(body) + self.assertEqual(error["status"], 404) + self.assertEqual(error["code"], "group_not_found") + self.assertEqual(error["operation_id"], "group_trait_info") + self.assertEqual(error["next"], "/groups") + + def test_group_trait_info_unknown_trait_in_group_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", "/groups/BXD/traits/999999999") + response = conn.getresponse() + body = response.read() + finally: + conn.close() + self.assertEqual(response.status, 404) + + error = json.loads(body) + self.assertEqual(error["status"], 404) + self.assertEqual(error["code"], "trait_not_found") + self.assertEqual(error["operation_id"], "group_trait_info") + + +if __name__ == "__main__": + unittest.main() diff --git a/test/python/test_openapi.py b/test/python/test_openapi.py index 753df4e..3b4b704 100644 --- a/test/python/test_openapi.py +++ b/test/python/test_openapi.py @@ -28,6 +28,7 @@ class OpenApiTest(ServerTestCase): "/groups", "/groups/{group}", "/groups/{group}/strains", + "/groups/{group}/traits/{trait_id}", "/groups/{group}/traits/{trait_id}/values", ): self.assertIn(path, doc["paths"]) |
