diff options
| author | Alexander_Kabui <alexanderkabua@gmail.com> | 2026-10-04 11:39:19 +0300 |
|---|---|---|
| committer | Alexander_Kabui <alexanderkabua@gmail.com> | 2026-10-05 21:53:57 +0300 |
| commit | 1b40aa1f557eadd8894996fff6081d2917e28f20 (patch) | |
| tree | cf67c183b87141bc7b85eed28b2a83d889f62853 /test/python | |
| parent | a6a4a0efd270e8aa32a8f45eb6cc0203af240528 (diff) | |
| download | genenetwork4-1b40aa1f557eadd8894996fff6081d2917e28f20.tar.gz | |
* test/python/test_search.py (search): contract mirror over the real
binary Spawns the compiled server with GN3_URL pointed at a local gn3 stub serving the recorded fixture; asserts the envelope shape, parameter validation, and the 422 parser-error forwarding. Mirrors tests/search.rs per the test-pair discipline; runs in the Guix sandbox (the stub is loopback-only).
Diffstat (limited to 'test/python')
| -rw-r--r-- | test/python/test_search.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/test/python/test_search.py b/test/python/test_search.py new file mode 100644 index 0000000..1d2118e --- /dev/null +++ b/test/python/test_search.py @@ -0,0 +1,102 @@ +"""Mirrors tests/search.rs. Spawns the real binary plus a local gn3 stub +(HTTP server serving the recorded fixture), so no external network is +needed; runs in the Guix sandbox. +""" + +import http.client +import http.server +import json +import os +import threading +import unittest +import urllib.parse + +from server_test_case import HOST, PORT, ServerTestCase, wait_for_port + +FIXTURE = os.path.join( + os.path.dirname(__file__), "..", "data", "search_alcohol_phenotype.json" +) + + +class Gn3StubHandler(http.server.BaseHTTPRequestHandler): + """Stands in for gn3's /api/search during tests.""" + + def do_GET(self): + query = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query) + if "&&" in query.get("query", [""])[0]: + body = json.dumps( + { + "error": "parsererror", + "error_message": "Syntax error in query: unexpected &&", + } + ).encode() + self.send_response(http.client.BAD_REQUEST) + else: + with open(FIXTURE, "rb") as fixture: + body = fixture.read() + self.send_response(http.client.OK) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def log_message(self, *args): + pass + + +class SearchTest(ServerTestCase): + @classmethod + def setUpClass(cls): + cls.stub = http.server.ThreadingHTTPServer( + (HOST, 0), Gn3StubHandler + ) + stub_port = cls.stub.server_address[1] + threading.Thread(target=cls.stub.serve_forever, daemon=True).start() + os.environ["GN3_URL"] = f"http://127.0.0.1:{stub_port}/api/" + super().setUpClass() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() + cls.stub.shutdown() + + def get(self, uri): + conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) + try: + conn.request("GET", uri) + response = conn.getresponse() + return response.status, json.loads(response.read()) + finally: + conn.close() + + def test_search_wraps_gn3_hits_in_envelope(self): + status, body = self.get("/search?q=alcohol&type=phenotype&limit=5") + self.assertEqual(status, http.client.OK) + hits = body["data"] + self.assertEqual(len(hits), 5) + self.assertEqual(hits[0]["name"], "10199") + self.assertEqual(hits[0]["dataset"], "C57BL-6JxC57BL-6NJF2Publish") + self.assertEqual(body["next"], []) + + def test_search_validates_parameters(self): + status, body = self.get("/search?q=%20%20") + self.assertEqual(status, http.client.BAD_REQUEST) + self.assertEqual(body["code"], "invalid_parameter") + + status, body = self.get("/search?q=alcohol&type=genotype") + self.assertEqual(status, http.client.BAD_REQUEST) + self.assertIn("phenotype", body["detail"]) + + def test_search_forwards_gn3_parse_errors(self): + status, body = self.get("/search?q=a%26%26b") + self.assertEqual(status, http.client.UNPROCESSABLE_ENTITY) + self.assertEqual(body["code"], "invalid_query") + self.assertIn("Syntax error in query", body["detail"]) + + def test_search_caps_limit(self): + status, _ = self.get("/search?q=alcohol&limit=9999") + self.assertEqual(status, http.client.OK) + + +if __name__ == "__main__": + unittest.main() |
