about summary refs log tree commit diff
path: root/test/python/test_search.py
blob: 081bdf7f4cecfee9c2145d698f594af1a2b50dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""Mirrors tests/search.rs. Spawns the real binary plus a local gn3 stub
(HTTP server serving the recorded fixture), so gn3 needs no external
network. /search now requires a bearer token (AuthToken), so the
token-gated scenarios also need a live gn-auth and a token (see
docs/gn-auth.md) and skip with a message rather than failing when
either is missing, same as the other gated endpoint tests.
auth_token() tries this server's own POST /get-test-token first
(EXTRA_ENV below makes that possible), falling back to
GN4_TEST_AUTH_TOKEN.
"""

import http.client
import http.server
import json
import os
import threading
import unittest
import urllib.parse

from auth_helpers import TOKEN_ENV, auth_server_reachable, auth_token
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):
    """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 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"]:
                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 "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)
        elif "&&" in query.get("query", [""])[0]:
            # Mirrors production gn3's shape: diagnostic under
            # "error", Python repr under "error_type".
            body = json.dumps(
                {
                    "error": "Syntax: <expression> AND <expression>",
                    "error_type": "b'QueryParserError'",
                }
            ).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):
    # Lets this test's own server answer POST /get-test-token so
    # auth_token() can self-issue one (see auth_helpers.TOKEN_ENV).
    EXTRA_ENV = TOKEN_ENV

    # The bearer token, fetched once by require_auth() and reused by
    # every token-gated request (one round trip, not one per call).
    token = None

    @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 require_auth(self):
        token = auth_token()
        if not token:
            self.skipTest("no token (see docs/gn-auth.md)")
        if not auth_server_reachable():
            self.skipTest("no gn-auth reachable")
        # Stash it so get() reuses this one fetch instead of fetching
        # again (a failed second fetch would send "Bearer None").
        type(self).token = token

    def get(self, uri, with_token=True):
        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
        try:
            headers = {"Authorization": f"Bearer {self.token}"} if with_token else {}
            conn.request("GET", uri, headers=headers)
            response = conn.getresponse()
            return response.status, json.loads(response.read())
        finally:
            conn.close()

    def test_search_without_a_token_returns_401(self):
        # No gn-auth call happens for a missing header, so this needs
        # neither a token nor a live server.
        status, body = self.get("/search?q=alcohol", with_token=False)
        self.assertEqual(status, http.client.UNAUTHORIZED)
        self.assertEqual(body["code"], "invalid_token")

    def test_search_wraps_gn3_hits_in_envelope(self):
        self.require_auth()

        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")
        # 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()

        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)
        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.604)

        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.6063, places=4)
        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.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()

        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_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_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()

        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: <expression> AND <expression>", body["detail"])

    def test_search_caps_limit(self):
        self.require_auth()

        status, _ = self.get("/search?q=alcohol&limit=9999")
        self.assertEqual(status, http.client.OK)

    def test_search_maps_gn3_failures_to_502(self):
        self.require_auth()

        status, body = self.get("/search?q=boom")
        self.assertEqual(status, http.client.BAD_GATEWAY)
        self.assertEqual(body["code"], "backend_error")


if __name__ == "__main__":
    unittest.main()