about summary refs log tree commit diff
path: root/test/python
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-10-05 15:25:22 +0000
committerPjotr Prins <pjotr.public01@thebird.nl>2026-10-07 08:26:56 +0200
commit7631cf515ffe1a81424aa53ab7052a61a3ff2b4d (patch)
tree9a02a521de26b54da79c5e7c1b86af4d343b2e66 /test/python
parent15a5c8abeb73584eeae6cae1d92176da42fa6bc3 (diff)
downloadgenenetwork4-7631cf515ffe1a81424aa53ab7052a61a3ff2b4d.tar.gz
Add GET /groups/{group}/case-attributes
Per-strain metadata beyond genotype/phenotype data (age, sex,
treatment, etc.), backed by MariaDB CaseAttribute/CaseAttributeXRefNew.
Combines GeneNetwork3's separate /case-attribute/{id}/names and
/case-attribute/{id}/values (gn3.api.case_attributes) into one
response -- neither list is large enough on its own to need splitting.
This is the only one of GeneNetwork2/3/gn-guile with the concept at
all: GeneNetwork2 delegates to GeneNetwork3 rather than storing case
attributes itself, and gn-guile doesn't have it.

db::get_group_case_attributes returns {names: [...], values: [{strain,
case_attributes: {...}}]}; values is built by folding adjacent
(strain, attribute, value) rows (ordered by strain name) rather than
grouping afterwards, since SQL already guarantees the adjacency.

Found and fixed a real bug while verifying this live: GeneNetwork3's
own query joins CaseAttribute to CaseAttributeXRefNew on
CaseAttributeId alone, but CaseAttributeId is not unique across groups
-- it's a shared vocabulary (id 37 means "Epoch" for dozens of
unrelated groups simultaneously); the actual key is
(InbredSetId, CaseAttributeId). Confirmed against this database:
without also joining on InbredSetId, BXD's result included a spurious
"Strain": "Live" attribute belonging to a different, unrelated group.
Fixed on genenetwork4's side by adding the missing join condition.

A group with no case attributes defined returns 200 with both lists
empty, not a 404 (the group itself exists) -- verified against LXS,
a real group with zero CaseAttribute rows.

tests/group_case_attributes.rs and its Python mirror cover BXD's 4
real attributes (asserting each strain has exactly those 4, not more,
to catch the join bug above), the empty-group case, missing token, and
unknown group.

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_case_attributes.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/test/python/test_group_case_attributes.py b/test/python/test_group_case_attributes.py
new file mode 100644
index 0000000..71c933f
--- /dev/null
+++ b/test/python/test_group_case_attributes.py
@@ -0,0 +1,105 @@
+"""Mirrors tests/group_case_attributes.rs. Needs a live MariaDB
+(SQL_URI, see src/db.rs) and a valid token (every endpoint needs one
+now -- see docs/gn-auth.md); skips rather than fails when either is
+missing, same as test_group_by_name.py.
+"""
+
+import http.client
+import json
+import socket
+import unittest
+
+from auth_helpers import TOKEN_ENV, auth_token
+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 GroupCaseAttributesTest(ServerTestCase):
+    EXTRA_ENV = TOKEN_ENV
+
+    def get(self, path):
+        token = auth_token()
+        if not token:
+            self.skipTest("no token (see docs/gn-auth.md)")
+        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
+        try:
+            conn.request("GET", path, headers={"Authorization": f"Bearer {token}"})
+            response = conn.getresponse()
+            return response, response.read()
+        finally:
+            conn.close()
+
+    def test_group_case_attributes_bxd_returns_names_and_values(self):
+        """BXD (InbredSetId 1) has exactly 4 case attributes -- Epoch,
+        RRID, SeqCvge, Status -- each strain in the response should have
+        exactly those 4 keys, not more (CaseAttributeId isn't unique
+        across groups -- see the comment in src/db.rs -- so getting the
+        join wrong would pull in unrelated groups' attributes too).
+        """
+        if not database_reachable():
+            self.skipTest("no database on 127.0.0.1:3306")
+
+        response, body = self.get("/groups/BXD/case-attributes")
+        self.assertEqual(response.status, 200)
+
+        case_attributes = json.loads(body)
+        self.assertEqual(
+            case_attributes["names"], ["Epoch", "RRID", "SeqCvge", "Status"]
+        )
+
+        values = case_attributes["values"]
+        self.assertTrue(values)
+        bxd1 = next(v for v in values if v["strain"] == "BXD1")
+        self.assertEqual(
+            set(bxd1["case_attributes"].keys()), set(case_attributes["names"])
+        )
+
+    def test_group_case_attributes_group_with_none_returns_empty(self):
+        """LXS (InbredSetId 9) is a real group with no case attributes
+        defined at all -- an empty result, not a 404 (the group itself
+        exists)."""
+        if not database_reachable():
+            self.skipTest("no database on 127.0.0.1:3306")
+
+        response, body = self.get("/groups/LXS/case-attributes")
+        self.assertEqual(response.status, 200)
+
+        case_attributes = json.loads(body)
+        self.assertEqual(case_attributes["names"], [])
+        self.assertEqual(case_attributes["values"], [])
+
+    def test_group_case_attributes_without_a_token_returns_401(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/case-attributes")
+            response = conn.getresponse()
+        finally:
+            conn.close()
+        self.assertEqual(response.status, 401)
+
+    def test_group_case_attributes_unknown_group_returns_404(self):
+        if not database_reachable():
+            self.skipTest("no database on 127.0.0.1:3306")
+
+        response, body = self.get("/groups/not-a-real-group/case-attributes")
+        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_case_attributes")
+        self.assertEqual(error["next"], "/groups")
+
+
+if __name__ == "__main__":
+    unittest.main()