about summary refs log tree commit diff
path: root/test/python
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-10-04 18:19:38 +0000
committerPjotr <wrk@bosa.lan>2026-10-04 18:19:38 +0000
commitadc173c1f3c78da487827122262573e753284260 (patch)
tree9f9f5a378a3cc5ea6a0af9b02bd446eb5af39045 /test/python
parentea3633cf3e00047ca3906406c270d9f04acd2241 (diff)
downloadgenenetwork4-adc173c1f3c78da487827122262573e753284260.tar.gz
Add GET /traits/{dataset}/... spanning ProbeSet and Publish data
A new, additional endpoint family that addresses both trait categories
the same way -- (dataset, trait) -- matching GeneNetwork2's own generic
wqflask.api.router.trait_sample_data/get_trait_info, rather than the
two category-specific shapes genenetwork4 already has
(/datasets/{dataset}/probesets... and /groups/{group}/traits...), which
are unchanged and keep working exactly as before.

- db::resolve_trait_dataset(name) resolves a dataset name against
  ProbeSetFreeze first, then the new db::PublishDataset (a PublishFreeze
  row, via the new get_publish_freeze_by_name) -- whichever matches
  decides the category. This means only dataset names are ever
  "guessed" across categories, not trait names/ids, which is the much
  smaller and safer surface: GN2's own equivalent tries ProbeSet.Name
  and PublishXRef.Id against the same bare trait string and silently
  falls back, which can return the wrong trait on a same-named
  collision. get_publish_freeze_by_name accepts a PublishFreeze's own
  Name/ShortName/Id or its owning group's Name/InbredSetName/FullName/
  Id, mirroring GN2's own flexible matching (e.g. "BXDPublish" or
  "BXD").

- GET /traits/{dataset}: list every trait in a dataset, tagged by
  category.
- GET /traits/{dataset}/{trait}: one trait's metadata. A non-numeric
  trait against a Publish dataset is a 404 (can't match a
  PublishXRef.Id), not a 500.
- GET /traits/{dataset}/{trait}/values: that trait's per-strain values.
  TraitValue and ProbeSetValue are already identical in shape, so this
  unifies with no information loss, unlike the metadata endpoint above.

Metadata responses are a tagged union ({"category": "probeset"|
"publish", ...}) wrapping ProbeSetInfo/TraitInfo unchanged, rather than
a lossy common-fields schema -- the two share little beyond
mean/locus/lrs/additive. utoipa generates this as a oneOf with a
category discriminator, verified against /openapi.json.

Gated the same way as /datasets/{dataset}/probesets..., extended to
Publish data for the first time: /groups/{group}/traits itself is
still not gated, but reaching the same data through /traits/{group}
now requires a token and checks PublishFreeze.public/confidentiality.
No gn-auth-side change was needed -- POST /auth/data/authorisation
already dispatches *Publish-suffixed dataset names to its phenotype
resource resolver (gn_auth/auth/authorisation/data/phenotypes.py),
the exact mechanism this integration already relies on for mRNA data.
auth::VIEW_RESOURCE_PRIVILEGE moves from routes::datasets (private) to
routes::auth (pub) so routes::traits can reuse it instead of
duplicating the string.

tests/traits.rs and its Python mirror cover both categories (including
both ways of addressing a Publish dataset), the 401/403/404 cases, and
the non-numeric-trait-against-Publish-dataset edge case, run against a
real gn-auth instance. The Publish branch's deny path itself is only
exercised via gn-auth's "unlinked dataset" fallback (already proven for
ProbeSet) -- no non-public PublishFreeze exists in the database this
was tested against.

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_traits.py196
1 files changed, 196 insertions, 0 deletions
diff --git a/test/python/test_traits.py b/test/python/test_traits.py
new file mode 100644
index 0000000..97603ba
--- /dev/null
+++ b/test/python/test_traits.py
@@ -0,0 +1,196 @@
+"""Mirrors tests/traits.rs. Needs a live MariaDB (SQL_URI) and a live
+gn-auth (AUTH_SERVER_URL) plus GN4_TEST_AUTH_TOKEN (see
+docs/gn-auth.md); skips when any is missing, same as
+test_dataset_probeset_info.py.
+"""
+
+import http.client
+import json
+import os
+import socket
+import unittest
+from urllib.parse import urlparse
+
+from server_test_case import HOST, PORT, ServerTestCase
+
+AUTH_SERVER_URL = os.environ.get("AUTH_SERVER_URL", "http://127.0.0.1:8081/")
+
+
+def database_reachable():
+    try:
+        with socket.create_connection(("127.0.0.1", 3306), timeout=0.5):
+            return True
+    except OSError:
+        return False
+
+
+def auth_server_reachable():
+    url = urlparse(AUTH_SERVER_URL)
+    conn = http.client.HTTPConnection(url.hostname, url.port or 80, timeout=2)
+    try:
+        conn.request("GET", url.path.rstrip("/") + "/auth/public-jwks")
+        return conn.getresponse().status == 200
+    except OSError:
+        return False
+    finally:
+        conn.close()
+
+
+def auth_token():
+    return os.environ.get("GN4_TEST_AUTH_TOKEN")
+
+
+class TraitsTest(ServerTestCase):
+    def require_fixtures(self):
+        if not database_reachable():
+            self.skipTest("no database on 127.0.0.1:3306")
+        if not auth_token():
+            self.skipTest("set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)")
+        if not auth_server_reachable():
+            self.skipTest(f"no gn-auth reachable at {AUTH_SERVER_URL}")
+
+    def get(self, path, with_token=True):
+        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
+        try:
+            headers = {"Authorization": f"Bearer {auth_token()}"} if with_token else {}
+            conn.request("GET", path, headers=headers)
+            response = conn.getresponse()
+            return response, response.read()
+        finally:
+            conn.close()
+
+    def test_trait_info_probeset_category(self):
+        self.require_fixtures()
+
+        response, body = self.get("/traits/Br_U_0803_M/100001_at")
+        self.assertEqual(response.status, 200)
+
+        info = json.loads(body)
+        self.assertEqual(info["category"], "probeset")
+        self.assertEqual(info["name"], "100001_at")
+        self.assertEqual(info["symbol"], "Cd3g")
+
+    def test_trait_info_publish_category_by_group_name(self):
+        self.require_fixtures()
+
+        response, body = self.get("/traits/BXD/10001")
+        self.assertEqual(response.status, 200)
+
+        info = json.loads(body)
+        self.assertEqual(info["category"], "publish")
+        self.assertEqual(info["id"], 10001)
+
+    def test_trait_info_publish_category_by_publish_freeze_name(self):
+        self.require_fixtures()
+
+        response, body = self.get("/traits/BXDPublish/10001")
+        self.assertEqual(response.status, 200)
+
+        info = json.loads(body)
+        self.assertEqual(info["category"], "publish")
+        self.assertEqual(info["id"], 10001)
+
+    def test_trait_values_probeset_category(self):
+        self.require_fixtures()
+
+        response, body = self.get("/traits/Br_U_0803_M/100001_at/values")
+        self.assertEqual(response.status, 200)
+
+        values = json.loads(body)
+        self.assertTrue(values)
+        self.assertIn("BXD1", {v["name"] for v in values})
+
+    def test_trait_values_publish_category(self):
+        self.require_fixtures()
+
+        response, body = self.get("/traits/BXD/10001/values")
+        self.assertEqual(response.status, 200)
+
+        values = json.loads(body)
+        self.assertTrue(values)
+        self.assertIn("BXD1", {v["name"] for v in values})
+
+    def test_traits_list_probeset_category(self):
+        self.require_fixtures()
+
+        response, body = self.get("/traits/Br_U_0803_M")
+        self.assertEqual(response.status, 200)
+
+        traits = json.loads(body)
+        self.assertTrue(traits)
+        self.assertTrue(all(t["category"] == "probeset" for t in traits))
+        self.assertIn("100001_at", {t["name"] for t in traits})
+
+    def test_traits_list_publish_category(self):
+        self.require_fixtures()
+
+        response, body = self.get("/traits/BXD")
+        self.assertEqual(response.status, 200)
+
+        traits = json.loads(body)
+        self.assertTrue(traits)
+        self.assertTrue(all(t["category"] == "publish" for t in traits))
+        self.assertIn(10001, {t["id"] for t in traits})
+
+    def test_trait_info_without_a_token_returns_401(self):
+        if not database_reachable():
+            self.skipTest("no database on 127.0.0.1:3306")
+
+        response, _body = self.get("/traits/BXD/10001", with_token=False)
+        self.assertEqual(response.status, 401)
+
+    def test_trait_info_unknown_dataset_returns_404(self):
+        self.require_fixtures()
+
+        response, body = self.get("/traits/not-a-real-dataset/123")
+        self.assertEqual(response.status, 404)
+
+        error = json.loads(body)
+        self.assertEqual(error["code"], "dataset_not_found")
+        self.assertEqual(error["operation_id"], "trait_info")
+
+    def test_trait_info_unknown_trait_in_probeset_dataset_returns_404(self):
+        self.require_fixtures()
+
+        response, body = self.get("/traits/Br_U_0803_M/not-a-real-probeset")
+        self.assertEqual(response.status, 404)
+
+        error = json.loads(body)
+        self.assertEqual(error["code"], "trait_not_found")
+
+    def test_trait_info_unknown_trait_in_publish_dataset_returns_404(self):
+        self.require_fixtures()
+
+        response, body = self.get("/traits/BXD/99999999")
+        self.assertEqual(response.status, 404)
+
+        error = json.loads(body)
+        self.assertEqual(error["code"], "trait_not_found")
+
+    def test_trait_info_non_numeric_trait_in_publish_dataset_returns_404(self):
+        """A non-numeric trait segment against a Publish dataset can
+        never match a PublishXRef.Id -- same 404 as a numeric id that
+        just doesn't exist, not a 500 from a failed parse."""
+        self.require_fixtures()
+
+        response, body = self.get("/traits/BXD/not-a-number")
+        self.assertEqual(response.status, 404)
+
+        error = json.loads(body)
+        self.assertEqual(error["code"], "trait_not_found")
+
+    def test_trait_info_non_public_unlinked_probeset_dataset_returns_403(self):
+        """UTHSC_BXD_Cochlea_0225 -- same fixture as
+        test_dataset_probeset_info.py, exercised here through the
+        unified endpoint instead."""
+        self.require_fixtures()
+
+        response, body = self.get("/traits/UTHSC_BXD_Cochlea_0225/ENSMUSG00000028180")
+        self.assertEqual(response.status, 403)
+
+        error = json.loads(body)
+        self.assertEqual(error["code"], "forbidden")
+
+
+if __name__ == "__main__":
+    unittest.main()