about summary refs log tree commit diff
path: root/test/python
diff options
context:
space:
mode:
Diffstat (limited to 'test/python')
-rw-r--r--test/python/test_dataset_probeset_info.py2
-rw-r--r--test/python/test_group_trait_info.py12
-rw-r--r--test/python/test_group_traits.py21
-rw-r--r--test/python/test_groups.py39
-rw-r--r--test/python/test_publications.py56
-rw-r--r--test/python/test_search.py38
6 files changed, 161 insertions, 7 deletions
diff --git a/test/python/test_dataset_probeset_info.py b/test/python/test_dataset_probeset_info.py
index 04241ba..861e9ca 100644
--- a/test/python/test_dataset_probeset_info.py
+++ b/test/python/test_dataset_probeset_info.py
@@ -56,7 +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)
+        self.assertAlmostEqual(info["lod"], info["lrs"] / 4.604)
 
     def test_dataset_probeset_info_without_a_token_returns_401(self):
         if not database_reachable():
diff --git a/test/python/test_group_trait_info.py b/test/python/test_group_trait_info.py
index 1e4fab8..7375524 100644
--- a/test/python/test_group_trait_info.py
+++ b/test/python/test_group_trait_info.py
@@ -38,7 +38,7 @@ class GroupTraitInfoTest(ServerTestCase):
 
     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.
+        # test_search.py. `lod` is `lrs` / 4.604.
         if not database_reachable():
             self.skipTest("no database on 127.0.0.1:3306")
 
@@ -47,8 +47,14 @@ class GroupTraitInfoTest(ServerTestCase):
 
         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.assertEqual(info["abbreviation"], "5HT_MB_M")
+        self.assertEqual(info["dataset"], "BXDPublish")
+        self.assertEqual(
+            info["url"],
+            "https://genenetwork.org/show_trait?trait_id=17985&dataset=BXDPublish",
+        )
+        self.assertAlmostEqual(info["lod"], info["lrs"] / 4.604)
+        self.assertAlmostEqual(info["lod"], 2.6063, places=4)
         self.assertAlmostEqual(info["additive"], -1.3041, places=4)
 
     def test_group_trait_info_bxd_returns_phenotype_and_publication(self):
diff --git a/test/python/test_group_traits.py b/test/python/test_group_traits.py
index d918688..c9d2125 100644
--- a/test/python/test_group_traits.py
+++ b/test/python/test_group_traits.py
@@ -64,6 +64,27 @@ class GroupTraitsTest(ServerTestCase):
         self.assertEqual(error["operation_id"], "group_traits")
         self.assertEqual(error["next"], "/groups")
 
+    def test_group_traits_filters_by_abbreviation(self):
+        if not database_reachable():
+            self.skipTest("no database on 127.0.0.1:3306")
+
+        response, body = self.get("/groups/BXD/traits?abbreviation=5HT_MB_M")
+        self.assertEqual(response.status, 200)
+        traits = json.loads(body)
+        self.assertEqual(len(traits), 1)
+        self.assertEqual(traits[0]["id"], 17985)
+        self.assertEqual(traits[0]["abbreviation"], "5HT_MB_M")
+        self.assertEqual(traits[0]["dataset"], "BXDPublish")
+        self.assertEqual(
+            traits[0]["url"],
+            "https://genenetwork.org/show_trait?trait_id=17985&dataset=BXDPublish",
+        )
+
+        # An unknown abbreviation is an empty list, not an error.
+        response, body = self.get("/groups/BXD/traits?abbreviation=no_such_trait")
+        self.assertEqual(response.status, 200)
+        self.assertEqual(json.loads(body), [])
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/test/python/test_groups.py b/test/python/test_groups.py
index dc38b33..0c55a8f 100644
--- a/test/python/test_groups.py
+++ b/test/python/test_groups.py
@@ -37,6 +37,45 @@ class GroupsTest(ServerTestCase):
         names = {record["name"] for record in groups}
         self.assertIn("BXD", names)
 
+    def test_groups_filters_by_family_species_and_text(self):
+        # Every group has `family` and `species`; /groups takes `family`,
+        # `species` and `q` filters (case-insensitive, combined with AND).
+        if not database_reachable():
+            self.skipTest("no database on 127.0.0.1:3306")
+        token = auth_token()
+        if not token:
+            self.skipTest("no token (see docs/gn-auth.md)")
+
+        def groups(path):
+            conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
+            try:
+                conn.request("GET", path, headers={"Authorization": f"Bearer {token}"})
+                response = conn.getresponse()
+                body = response.read()
+                self.assertEqual(response.status, 200, path)
+                return json.loads(body)
+            finally:
+                conn.close()
+
+        bxd = next(g for g in groups("/groups") if g["name"] == "BXD")
+        self.assertEqual(bxd["species"], "mouse")
+        self.assertTrue(bxd["family"].startswith("Reference Populations"))
+
+        reference = {g["name"] for g in groups("/groups?family=reference%20populations")}
+        for expected in ("AKXD", "AXBXA", "BXD", "BXH", "CC", "CXB", "HXBBXH", "LXS", "MDP"):
+            self.assertIn(expected, reference)
+        self.assertNotIn("HSNIH-Palmer", reference)
+
+        rat = groups("/groups?species=RAT")
+        self.assertTrue(rat)
+        self.assertTrue(all(g["species"] == "rat" for g in rat))
+
+        self.assertIn("BXD-NIA-AD", {g["name"] for g in groups("/groups?q=alzheimer")})
+
+        rat_reference = {g["name"] for g in groups("/groups?species=rat&family=reference")}
+        self.assertIn("HXBBXH", rat_reference)
+        self.assertNotIn("BXD", rat_reference)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/test/python/test_publications.py b/test/python/test_publications.py
new file mode 100644
index 0000000..15c8ab7
--- /dev/null
+++ b/test/python/test_publications.py
@@ -0,0 +1,56 @@
+"""Mirrors tests/publications.rs. Needs a live MariaDB (SQL_URI) and a
+valid token (every endpoint needs one now -- see docs/gn-auth.md);
+skips with a message rather than failing when either is missing, same
+as test_group_traits.py.
+"""
+
+import http.client
+import json
+import unittest
+
+from auth_helpers import TOKEN_ENV, auth_token
+from server_test_case import HOST, PORT, ServerTestCase, database_reachable
+
+
+class PublicationTraitsTest(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=30)
+        try:
+            conn.request("GET", path, headers={"Authorization": f"Bearer {token}"})
+            response = conn.getresponse()
+            return response, response.read()
+        finally:
+            conn.close()
+
+    def test_publication_traits_lists_the_traits_a_paper_reports(self):
+        # PubMed 24102824 is the Ye et al. serotonin homeostasis paper
+        # behind BXDPublish trait 17985.
+        if not database_reachable():
+            self.skipTest("no database on 127.0.0.1:3306")
+
+        response, body = self.get("/publications/24102824/traits")
+        self.assertEqual(response.status, 200)
+        traits = json.loads(body)
+        self.assertTrue(all(t["pubmed_id"] == 24102824 for t in traits))
+        midbrain = next(t for t in traits if t["id"] == 17985)
+        self.assertEqual(midbrain["dataset"], "BXDPublish")
+        self.assertEqual(midbrain["abbreviation"], "5HT_MB_M")
+
+    def test_publication_traits_unknown_pubmed_id_returns_404(self):
+        if not database_reachable():
+            self.skipTest("no database on 127.0.0.1:3306")
+
+        response, body = self.get("/publications/999999999/traits")
+        self.assertEqual(response.status, 404)
+        error = json.loads(body)
+        self.assertEqual(error["code"], "publication_not_found")
+        self.assertEqual(error["operation_id"], "publication_traits")
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/test/python/test_search.py b/test/python/test_search.py
index bfd1d38..aea1e20 100644
--- a/test/python/test_search.py
+++ b/test/python/test_search.py
@@ -49,6 +49,10 @@ class Gn3StubHandler(http.server.BaseHTTPRequestHandler):
                 with open(SEROTONIN_FIXTURE, "rb") as fixture:
                     body = fixture.read()
                 self.send_response(http.client.OK)
+        elif "alcohol" in query.get("query", [""])[0] and query.get("page") == ["2"]:
+            # Past the end: an unsorted /search forwards `page` to gn3.
+            body = b"[]"
+            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)
@@ -176,7 +180,7 @@ class SearchTest(ServerTestCase):
         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)
+            self.assertAlmostEqual(hit["lod"], hit["lrs"] / 4.604)
 
         male_midbrain = [
             hit
@@ -189,7 +193,7 @@ class SearchTest(ServerTestCase):
             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["lod"], 2.6063, places=4)
         self.assertAlmostEqual(serotonin["additive"], -1.3041, places=4)
         self.assertEqual(serotonin["geno_chr"], "16")
 
@@ -197,14 +201,16 @@ class SearchTest(ServerTestCase):
         # 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["lod"], 3.5819, places=4)
         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.
+        self.assertEqual(serotonin["pubmed_id"], 24102824)
         if database_reachable():
             self.assertEqual(serotonin["locus"], "rsm10000012461")
             self.assertEqual(top["locus"], "rsm10000013922")
+            self.assertEqual(serotonin["abbreviation"], "5HT_MB_M")
 
     def test_search_orders_ascending(self):
         self.require_auth()
@@ -218,6 +224,32 @@ class SearchTest(ServerTestCase):
         self.assertEqual(lrs[0], min_lrs)
         self.assertEqual(lrs, sorted(lrs))
 
+    def test_search_pages(self):
+        # Unsorted, `page` is forwarded to gn3 (the stub's page 2 is
+        # empty); sorted, pages come from the ranked list (the recorded
+        # 119 serotonin hits: 100, then 19, then none).
+        self.require_auth()
+
+        status, body = self.get("/search?q=alcohol&page=2")
+        self.assertEqual(status, http.client.OK)
+        self.assertEqual(body["data"], [])
+
+        sorted_uri = (
+            "/search?q=serotonin%20AND%20midbrain%20AND%20group:BXD"
+            "&sort=lod&limit=100"
+        )
+        _, page1 = self.get(sorted_uri)
+        _, page2 = self.get(sorted_uri + "&page=2")
+        _, page3 = self.get(sorted_uri + "&page=3")
+        self.assertEqual(len(page1["data"]), 100)
+        self.assertEqual(len(page2["data"]), 19)
+        self.assertEqual(page3["data"], [])
+        self.assertGreaterEqual(page1["data"][-1]["lod"], page2["data"][0]["lod"])
+
+        status, body = self.get("/search?q=alcohol&page=0")
+        self.assertEqual(status, http.client.BAD_REQUEST)
+        self.assertIn("page", body["detail"])
+
     def test_search_forwards_gn3_parse_errors(self):
         self.require_auth()