about summary refs log tree commit diff
path: root/test/python/test_search.py
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-08 11:24:58 +0000
committerPjotr <wrk@bosa.lan>2026-10-08 11:55:30 +0000
commit22540223c43342473d7d428dffeeab9d94afb55c (patch)
treef81c6af04ea9157054a58d3fc20e99f55973a0ae /test/python/test_search.py
parent5650076cdf45a26aed8c6166069d74281a7f4bd7 (diff)
downloadgenenetwork4-22540223c43342473d7d428dffeeab9d94afb55c.tar.gz
* src/response.rs: wrap every success response in one envelope.
Breaking: every success body is now {"data", "meta": {"state",
"took_ms", "request_id"}, "notes", "next"}; clients read `data`. Only
`/` (plain text) and `/openapi.json` are unwrapped.

* src/response.rs (Envelope, Meta, State, Ctx, RequestInfo): handlers
take Ctx first and build the response with ctx.envelope(data), so
timing and the request id are never filled in by hand. `state` is
`complete` or `partial`; notes are server-authored only; `next` are
concrete relative hrefs (path_segment percent-encodes names).
* src/routes/*.rs, src/lib.rs (version): every handler returns
Envelope<..> with next links; /search is `partial` when the locus
lookup fails or a sorted 1000-hit window may be truncated, and
/publications/{pubmed_id}/traits when datasets were left out for
authorization.
* src/error.rs (ApiError): add `took_ms`, an RFC 7807 extension member.
* src/auth.rs (AuthToken): the 401 now carries request_id and took_ms
(it never had a request_id); Ctx stores RequestInfo in the request
extensions for it.

* tests: common::data / envelope_data parse every success body through
the envelope; tests/envelope.rs and test/python/test_envelope.py check
the shape, errors, and that every `next` link resolves;
tests/envelope_partial.rs checks the `partial` search without a
database; tests/openapi.rs fails if a JSON success response is not an
Envelope_* schema.
* README.md, docs/SKILL.md, docs/gn-auth.md, RELEASE_NOTES.md: document
the envelope and took_ms on errors.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'test/python/test_search.py')
-rw-r--r--test/python/test_search.py50
1 files changed, 48 insertions, 2 deletions
diff --git a/test/python/test_search.py b/test/python/test_search.py
index aea1e20..081bdf7 100644
--- a/test/python/test_search.py
+++ b/test/python/test_search.py
@@ -39,7 +39,17 @@ class Gn3StubHandler(http.server.BaseHTTPRequestHandler):
 
     def do_GET(self):
         query = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query)
-        if "serotonin" in query.get("query", [""])[0]:
+        if query.get("query") == ["wide"]:
+            # 1000 hits, the size of a sorted search's window: more
+            # matches may exist beyond it.
+            body = json.dumps(
+                [
+                    {"name": str(i), "dataset": "WideUnknown", "lrs": float(i)}
+                    for i in range(1000)
+                ]
+            ).encode()
+            self.send_response(http.client.OK)
+        elif "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"]:
@@ -139,7 +149,18 @@ class SearchTest(ServerTestCase):
         self.assertEqual(len(hits), 5)
         self.assertEqual(hits[0]["name"], "10199")
         self.assertEqual(hits[0]["dataset"], "C57BL-6JxC57BL-6NJF2Publish")
-        self.assertEqual(body["next"], [])
+        # The envelope: meta, a note on `lod`, and follow-ups -- the next
+        # page (5 hits came back for limit=5) and the top hit's metadata.
+        self.assertIsInstance(body["meta"]["took_ms"], int)
+        self.assertIsInstance(body["meta"]["request_id"], str)
+        self.assertIn("lod = lrs / 4.604", body["notes"])
+        self.assertEqual(
+            [n["href"] for n in body["next"]],
+            [
+                "/search?q=alcohol&limit=5&type=phenotype&page=2",
+                "/traits/C57BL-6JxC57BL-6NJF2Publish/10199",
+            ],
+        )
 
     def test_search_validates_parameters(self):
         self.require_auth()
@@ -147,6 +168,8 @@ class SearchTest(ServerTestCase):
         status, body = self.get("/search?q=%20%20")
         self.assertEqual(status, http.client.BAD_REQUEST)
         self.assertEqual(body["code"], "invalid_parameter")
+        self.assertIsInstance(body["request_id"], str)
+        self.assertIsInstance(body["took_ms"], int)
 
         status, body = self.get("/search?q=alcohol&type=genotype")
         self.assertEqual(status, http.client.BAD_REQUEST)
@@ -250,6 +273,29 @@ class SearchTest(ServerTestCase):
         self.assertEqual(status, http.client.BAD_REQUEST)
         self.assertIn("page", body["detail"])
 
+    def test_search_over_a_full_window_is_partial(self):
+        # A sorted search over a full 1000-hit window cannot know it saw
+        # every match: the response is `partial` and says why. (These hits
+        # are not phenotype datasets, so no database is involved.)
+        self.require_auth()
+
+        status, body = self.get("/search?q=wide&sort=lrs&limit=3")
+        self.assertEqual(status, http.client.OK)
+        self.assertEqual(body["meta"]["state"], "partial")
+        self.assertIn("first 1000 matches", body["notes"][1])
+        self.assertEqual(body["data"][0]["lrs"], 999.0)
+
+        # A window that was not full is complete (the 119 recorded
+        # serotonin hits are BXDPublish ones: complete only if their
+        # locus lookup could reach the database).
+        _, body = self.get(
+            "/search?q=serotonin%20AND%20midbrain%20AND%20group:BXD"
+            "&sort=lod&limit=100"
+        )
+        self.assertEqual(
+            body["meta"]["state"], "complete" if database_reachable() else "partial"
+        )
+
     def test_search_forwards_gn3_parse_errors(self):
         self.require_auth()