about summary refs log tree commit diff
path: root/test/python/test_search.py
diff options
context:
space:
mode:
authorAlexander_Kabui <alexanderkabua@gmail.com>2026-10-07 21:01:40 +0300
committerAlexander_Kabui <alexanderkabua@gmail.com>2026-10-07 21:01:40 +0300
commit85b63abcfdbae235d292a6de4bd47ffe6ba6dafd (patch)
tree8b39f26fad90eaa27751fd061ab1cbc91ae95b08 /test/python/test_search.py
parentb68531c40d91cadfd868e4b65ff92989ffd4fb27 (diff)
parente912c87deaf220fd5fce1299d669e8fa992b5338 (diff)
downloadgenenetwork4-85b63abcfdbae235d292a6de4bd47ffe6ba6dafd.tar.gz
Merge origin/main (e912c87: /search gains lod, locus, sort/order) into
native-search-engine.

Resolution: his route-level additions compose above the engine seam —
the handler now validates sort/order, asks the engine for
SORT_FETCH_LIMIT (1000) when ranking, maps SearchError via the seam's
engine_error, then computes lod, sorts, truncates to limit and fills
loci; lod/locus join SearchHit in src/search/mod.rs. His tests pass
through the seam.

KNOWN at this commit: the parity floor is BLOCKED (36 shapes) — the
route's new truncate(limit) exposes that route-level full-set grading
was a calibration-stub artifact (real gn3 caps at per_page; the stub
ignored it). Harness fix queued: set-equality moves to engine level,
status + top-10 stay route level. Landed together with that fix.
Diffstat (limited to 'test/python/test_search.py')
-rw-r--r--test/python/test_search.py93
1 files changed, 91 insertions, 2 deletions
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()