diff options
Diffstat (limited to 'test/python')
| -rw-r--r-- | test/python/server_test_case.py | 2 | ||||
| -rw-r--r-- | test/python/test_dataset_probeset_info.py | 22 | ||||
| -rw-r--r-- | test/python/test_dataset_probeset_values.py | 21 | ||||
| -rw-r--r-- | test/python/test_dataset_probesets.py | 19 | ||||
| -rw-r--r-- | test/python/test_group_case_attributes.py | 105 | ||||
| -rw-r--r-- | test/python/test_group_trait_info.py | 15 | ||||
| -rw-r--r-- | test/python/test_search.py | 93 | ||||
| -rw-r--r-- | test/python/test_traits.py | 19 |
8 files changed, 293 insertions, 3 deletions
diff --git a/test/python/server_test_case.py b/test/python/server_test_case.py index d4bb330..be2dcf9 100644 --- a/test/python/server_test_case.py +++ b/test/python/server_test_case.py @@ -73,7 +73,7 @@ def _read_exact(sock, count): def _read_scramble(handshake): """Extract the 20-byte auth plugin data from the server handshake.""" - pos = 1 # protocol version + pos = 5 # 4-byte packet header, then protocol version pos = handshake.index(b"\0", pos) + 1 # server version string pos += 4 # connection id scramble = handshake[pos : pos + 8] diff --git a/test/python/test_dataset_probeset_info.py b/test/python/test_dataset_probeset_info.py index 32d25a6..04241ba 100644 --- a/test/python/test_dataset_probeset_info.py +++ b/test/python/test_dataset_probeset_info.py @@ -56,6 +56,7 @@ class DatasetProbesetInfoTest(ServerTestCase): self.assertEqual(info["name"], "100001_at") self.assertEqual(info["symbol"], "Cd3g") self.assertIsInstance(info["mean"], float) + self.assertAlmostEqual(info["lod"], info["lrs"] / 4.61) def test_dataset_probeset_info_without_a_token_returns_401(self): if not database_reachable(): @@ -105,6 +106,27 @@ class DatasetProbesetInfoTest(ServerTestCase): error = json.loads(body) self.assertEqual(error["code"], "forbidden") + def test_dataset_probeset_info_recent_non_public_dataset_returns_403(self): + """Same as above, but with a dataset genuinely less than a year + old (UTHSC_BXD_Cochlea_0225 above is real but was already ~20 + months old by the time this test was written) -- the age-embargo + rule and the "not yet linked to any gn-auth resource" deny path + both need to hold for freshly-added data, not just for something + that merely hasn't aged into public yet. + UTHSC_BXD_2wk_Cochlea_0926 (public=1, confidentiality=1, created + 2026-09-01) is the most recently added ProbeSetFreeze in the + database as of this writing. + """ + self.require_fixtures() + + response, body = self.get( + "/datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets/ENSMUSG00000028180" + ) + self.assertEqual(response.status, 403) + + error = json.loads(body) + self.assertEqual(error["code"], "forbidden") + if __name__ == "__main__": unittest.main() diff --git a/test/python/test_dataset_probeset_values.py b/test/python/test_dataset_probeset_values.py index 7a934d0..b11b5d6 100644 --- a/test/python/test_dataset_probeset_values.py +++ b/test/python/test_dataset_probeset_values.py @@ -109,6 +109,27 @@ class DatasetProbesetValuesTest(ServerTestCase): error = json.loads(body) self.assertEqual(error["code"], "forbidden") + def test_dataset_probeset_values_recent_non_public_dataset_returns_403(self): + """Same as above, but with a dataset genuinely less than a year + old (UTHSC_BXD_Cochlea_0225 above is real but was already ~20 + months old by the time this test was written) -- the age-embargo + rule and the "not yet linked to any gn-auth resource" deny path + both need to hold for freshly-added data, not just for something + that merely hasn't aged into public yet. + UTHSC_BXD_2wk_Cochlea_0926 (public=1, confidentiality=1, created + 2026-09-01) is the most recently added ProbeSetFreeze in the + database as of this writing. + """ + self.require_fixtures() + + response, body = self.get( + "/datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets/ENSMUSG00000028180/values" + ) + self.assertEqual(response.status, 403) + + error = json.loads(body) + self.assertEqual(error["code"], "forbidden") + if __name__ == "__main__": unittest.main() diff --git a/test/python/test_dataset_probesets.py b/test/python/test_dataset_probesets.py index dcfcd99..c12b42a 100644 --- a/test/python/test_dataset_probesets.py +++ b/test/python/test_dataset_probesets.py @@ -89,6 +89,25 @@ class DatasetProbesetsTest(ServerTestCase): error = json.loads(body) self.assertEqual(error["code"], "forbidden") + def test_dataset_probesets_recent_non_public_dataset_returns_403(self): + """Same as above, but with a dataset genuinely less than a year + old (UTHSC_BXD_Cochlea_0225 above is real but was already ~20 + months old by the time this test was written) -- the age-embargo + rule and the "not yet linked to any gn-auth resource" deny path + both need to hold for freshly-added data, not just for something + that merely hasn't aged into public yet. + UTHSC_BXD_2wk_Cochlea_0926 (public=1, confidentiality=1, created + 2026-09-01) is the most recently added ProbeSetFreeze in the + database as of this writing. + """ + self.require_fixtures() + + response, body = self.get("/datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets") + self.assertEqual(response.status, 403) + + error = json.loads(body) + self.assertEqual(error["code"], "forbidden") + if __name__ == "__main__": unittest.main() 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() diff --git a/test/python/test_group_trait_info.py b/test/python/test_group_trait_info.py index d5a0761..1e4fab8 100644 --- a/test/python/test_group_trait_info.py +++ b/test/python/test_group_trait_info.py @@ -36,6 +36,21 @@ class GroupTraitInfoTest(ServerTestCase): finally: conn.close() + def test_group_trait_info_reports_lod_and_locus(self): + # The "highest LOD ... serotonin in BXD male midbrain" trait: see + # test_search.py. `lod` is `lrs` / 4.61. + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + response, body = self.get("/groups/BXD/traits/17985") + self.assertEqual(response.status, 200) + + info = json.loads(body) + self.assertEqual(info["locus"], "rsm10000012461") + self.assertAlmostEqual(info["lod"], info["lrs"] / 4.61) + self.assertAlmostEqual(info["lod"], 2.603, places=3) + self.assertAlmostEqual(info["additive"], -1.3041, places=4) + 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") diff --git a/test/python/test_search.py b/test/python/test_search.py index 18cc54c..bfd1d38 100644 --- a/test/python/test_search.py +++ b/test/python/test_search.py @@ -18,11 +18,20 @@ import unittest import urllib.parse from auth_helpers import TOKEN_ENV, auth_server_reachable, auth_token -from server_test_case import HOST, PORT, ServerTestCase, wait_for_port +from server_test_case import ( + HOST, + PORT, + ServerTestCase, + database_reachable, + wait_for_port, +) FIXTURE = os.path.join( os.path.dirname(__file__), "..", "data", "search_alcohol_phenotype.json" ) +SEROTONIN_FIXTURE = os.path.join( + os.path.dirname(__file__), "..", "data", "search_serotonin_midbrain_bxd.json" +) class Gn3StubHandler(http.server.BaseHTTPRequestHandler): @@ -30,7 +39,17 @@ class Gn3StubHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): query = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query) - if "boom" in query.get("query", [""])[0]: + if "serotonin" in query.get("query", [""])[0]: + # gn3 has no sort of its own: a sorted /search must ask for a + # wide page (1000), not just `limit`, to rank across it. + if query.get("per_page") != ["1000"]: + body = b"per_page must be 1000" + self.send_response(http.client.INTERNAL_SERVER_ERROR) + else: + with open(SEROTONIN_FIXTURE, "rb") as fixture: + body = fixture.read() + self.send_response(http.client.OK) + elif "boom" in query.get("query", [""])[0]: body = b'{"error": "gn3 exploded"}' self.send_response(http.client.INTERNAL_SERVER_ERROR) elif "&&" in query.get("query", [""])[0]: @@ -129,6 +148,76 @@ class SearchTest(ServerTestCase): self.assertEqual(status, http.client.BAD_REQUEST) self.assertIn("phenotype", body["detail"]) + def test_search_validates_sort_and_order(self): + self.require_auth() + + status, body = self.get("/search?q=alcohol&sort=mean") + self.assertEqual(status, http.client.BAD_REQUEST) + self.assertIn("lrs", body["detail"]) + + status, body = self.get("/search?q=alcohol&sort=lod&order=sideways") + self.assertEqual(status, http.client.BAD_REQUEST) + self.assertIn("asc", body["detail"]) + + def test_highest_lod_for_serotonin_in_bxd_male_midbrain(self): + # "What is the highest LOD score, locus marker, and additive + # effect for serotonin in BXD male midbrain?" -- mirrors scenario 7 + # of tests/search.rs. Males only: " males [" matches "adult males [" + # but not "adult females [" or "adult males and females [". + self.require_auth() + + status, body = self.get( + "/search?q=serotonin%20AND%20midbrain%20AND%20group:BXD" + "&sort=lod&limit=100" + ) + self.assertEqual(status, http.client.OK) + hits = body["data"] + self.assertEqual(len(hits), 100) + lods = [hit["lod"] for hit in hits] + self.assertEqual(lods, sorted(lods, reverse=True)) + for hit in hits: + self.assertAlmostEqual(hit["lod"], hit["lrs"] / 4.61) + + male_midbrain = [ + hit + for hit in hits + if "Midbrain" in hit["description"] and " males [" in hit["description"] + ] + serotonin = next( + hit + for hit in male_midbrain + if "5-hydroxytryptamine (5-HT, serotonin) level" in hit["description"] + ) + self.assertEqual(serotonin["name"], "17985") + self.assertAlmostEqual(serotonin["lod"], 2.603, places=3) + self.assertAlmostEqual(serotonin["additive"], -1.3041, places=4) + self.assertEqual(serotonin["geno_chr"], "16") + + # The serotonin transporter (SERT) in the same tissue and sex + # has the higher LOD: "serotonin" is ambiguous between the two. + top = male_midbrain[0] + self.assertEqual(top["name"], "17994") + self.assertAlmostEqual(top["lod"], 3.577, places=3) + self.assertAlmostEqual(top["additive"], 233.99, places=2) + + # The peak marker's name comes from PublishXRef (needs a live + # MariaDB); without one `locus` is null and the above still holds. + if database_reachable(): + self.assertEqual(serotonin["locus"], "rsm10000012461") + self.assertEqual(top["locus"], "rsm10000013922") + + def test_search_orders_ascending(self): + self.require_auth() + + with open(SEROTONIN_FIXTURE) as fixture: + min_lrs = min(hit["lrs"] for hit in json.load(fixture)) + status, body = self.get("/search?q=serotonin&sort=lrs&order=asc&limit=3") + self.assertEqual(status, http.client.OK) + lrs = [hit["lrs"] for hit in body["data"]] + self.assertEqual(len(lrs), 3) + self.assertEqual(lrs[0], min_lrs) + self.assertEqual(lrs, sorted(lrs)) + def test_search_forwards_gn3_parse_errors(self): self.require_auth() diff --git a/test/python/test_traits.py b/test/python/test_traits.py index 38bf411..6a44bd8 100644 --- a/test/python/test_traits.py +++ b/test/python/test_traits.py @@ -176,6 +176,25 @@ class TraitsTest(ServerTestCase): error = json.loads(body) self.assertEqual(error["code"], "forbidden") + def test_trait_info_recent_non_public_probeset_dataset_returns_403(self): + """Same as above, but with a dataset genuinely less than a year + old (UTHSC_BXD_Cochlea_0225 above is real but was already ~20 + months old by the time this test was written) -- the age-embargo + rule and the "not yet linked to any gn-auth resource" deny path + both need to hold for freshly-added data, not just for something + that merely hasn't aged into public yet. + UTHSC_BXD_2wk_Cochlea_0926 (public=1, confidentiality=1, created + 2026-09-01) is the most recently added ProbeSetFreeze in the + database as of this writing. + """ + self.require_fixtures() + + response, body = self.get("/traits/UTHSC_BXD_2wk_Cochlea_0926/ENSMUSG00000028180") + self.assertEqual(response.status, 403) + + error = json.loads(body) + self.assertEqual(error["code"], "forbidden") + if __name__ == "__main__": unittest.main() |
