-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_batch_operations.py
More file actions
460 lines (357 loc) · 18.7 KB
/
Copy pathtest_batch_operations.py
File metadata and controls
460 lines (357 loc) · 18.7 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
"""Unit tests for batch SDK methods (search_batch, count_batch, record, lookup, summary).
Tests use mocked HTTP responses to validate:
1. Correct URL construction for batch endpoints
2. Correct request payload structure
3. Proper response parsing via parse_* functions
4. Constraint validation (max 100 searches per batch)
5. Error handling for HTTP failures and invalid inputs
"""
import json
from unittest.mock import MagicMock, mock_open, patch
import pytest
from cli_generator import QueryBuilder
class TestBatchConstraints:
"""Test constraint validation for batch operations."""
def test_search_batch_enforces_max_100_queries(self):
"""search_batch should reject >100 queries."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon") for _ in range(101)]
with pytest.raises(ValueError, match="maximum 100 searches per batch request"):
qb.search_batch(queries)
def test_count_batch_enforces_max_100_queries(self):
"""count_batch should reject >100 queries."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon") for _ in range(101)]
with pytest.raises(ValueError, match="maximum 100 searches per batch request"):
qb.count_batch(queries)
def test_search_batch_accepts_1_query(self):
"""search_batch should accept 1 query."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps(
{"status": {"success": True}, "results": [{"results": [], "total": 0}]}
).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
result = qb.search_batch(queries)
assert isinstance(result, list)
def test_search_batch_accepts_100_queries(self):
"""search_batch should accept exactly 100 queries."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon") for _ in range(100)]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
results = [{"results": [], "total": i} for i in range(100)]
response_data = {"status": {"success": True}, "results": results}
mock_resp.read.return_value = json.dumps(response_data).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
result = qb.search_batch(queries)
assert len(result) == 100
class TestSearchBatchHTTPHandling:
"""Test HTTP request/response handling for search_batch."""
def test_search_batch_constructs_correct_url(self):
"""search_batch should construct correct endpoint URL."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon").set_taxa(["Mammalia"])]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps({"status": {"success": True}, "results": []}).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
qb.search_batch(queries, api_base="http://localhost:3000/api")
# Verify the URL was called
call_args = mock_urlopen.call_args
request_obj = call_args[0][0]
assert "http://localhost:3000/api/v3/search/batch" in request_obj.full_url
def test_search_batch_uses_custom_api_version(self):
"""search_batch should respect custom api_version parameter."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps({"status": {"success": True}, "results": []}).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
qb.search_batch(queries, api_base="http://localhost:3000/api", api_version="v4")
call_args = mock_urlopen.call_args
request_obj = call_args[0][0]
assert "v4/search/batch" in request_obj.full_url
def test_search_batch_request_has_json_content_type(self):
"""search_batch should set Content-Type to application/json."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps({"status": {"success": True}, "results": []}).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
qb.search_batch(queries)
call_args = mock_urlopen.call_args
request_obj = call_args[0][0]
assert request_obj.headers.get("Content-type") == "application/json"
def test_search_batch_payload_structure(self):
"""search_batch should send payload with 'searches' array."""
qb = QueryBuilder("taxon")
queries = [
QueryBuilder("taxon").set_taxa(["Mammalia"]),
QueryBuilder("taxon").set_taxa(["Aves"]),
]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps({"status": {"success": True}, "results": [{}, {}]}).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
qb.search_batch(queries)
call_args = mock_urlopen.call_args
request_obj = call_args[0][0]
payload = json.loads(request_obj.data.decode("utf-8"))
assert "searches" in payload
assert len(payload["searches"]) == 2
assert all("query_yaml" in s and "params_yaml" in s for s in payload["searches"])
class TestCountBatchHTTPHandling:
"""Test HTTP request/response handling for count_batch."""
def test_count_batch_constructs_correct_url(self):
"""count_batch should construct correct endpoint URL."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon").set_taxa(["Mammalia"])]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps(
{"status": {"success": True, "hits": 100}, "results": [{"hits": 100}]}
).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
qb.count_batch(queries)
call_args = mock_urlopen.call_args
request_obj = call_args[0][0]
assert "v3/count/batch" in request_obj.full_url
def test_count_batch_returns_hit_counts(self):
"""count_batch should return list of hit counts."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon"), QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
with patch("cli_generator.parse_batch_json") as mock_parse:
mock_resp = MagicMock()
# parse_batch_json normalises totals into "total", not "status.hits"
response_data = {
"status": {"success": True},
"results": [
{"records": [], "total": 1000, "error": None},
{"records": [], "total": 2000, "error": None},
],
}
mock_parse.return_value = json.dumps(response_data)
mock_resp.read.return_value = json.dumps(response_data).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
result = qb.count_batch(queries)
assert result == [1000, 2000]
class TestRecordHTTPHandling:
"""Test HTTP request/response handling for record."""
def test_record_constructs_correct_url(self):
"""record should construct correct endpoint URL."""
qb = QueryBuilder("taxon").set_taxa(["9646"])
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps({"status": {"success": True}, "results": []}).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
qb.record("9646", api_base="http://localhost:3000/api")
call_args = mock_urlopen.call_args
url_called = call_args[0][0]
assert "v3/record" in url_called
def test_record_uses_get_method(self):
"""record should use GET method with query params."""
qb = QueryBuilder("taxon").set_taxa(["9646"])
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps({"status": {"success": True}, "results": []}).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
qb.record("9646")
call_args = mock_urlopen.call_args
url_called = call_args[0][0]
assert "recordId=9646" in url_called
class TestLookupHTTPHandling:
"""Test HTTP request/response handling for lookup."""
def test_lookup_constructs_correct_url(self):
"""lookup should construct correct endpoint URL."""
qb = QueryBuilder("taxon").set_taxa(["9646"])
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps({"status": {"success": True}, "results": []}).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
qb.lookup("9646", api_base="http://localhost:3000/api")
call_args = mock_urlopen.call_args
url_called = call_args[0][0]
assert "v3/lookup" in url_called
class TestSummaryHTTPHandling:
"""Test HTTP request/response handling for summary."""
def test_summary_constructs_correct_url(self):
"""summary should construct correct endpoint URL."""
qb = QueryBuilder("taxon").add_field("genome_size")
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps({"status": {"success": True}, "results": []}).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
qb.summary("9646", "genome_size", api_base="http://localhost:3000/api")
call_args = mock_urlopen.call_args
url_called = call_args[0][0]
assert "v3/summary" in url_called
class TestErrorHandling:
"""Test error handling in batch methods."""
def test_search_batch_handles_http_error(self):
"""search_batch should propagate HTTP errors."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_urlopen.side_effect = Exception("HTTP 500: Server Error")
with pytest.raises(Exception, match="HTTP 500"):
qb.search_batch(queries)
def test_count_batch_handles_http_error(self):
"""count_batch should propagate HTTP errors."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_urlopen.side_effect = Exception("HTTP 500: Server Error")
with pytest.raises(Exception, match="HTTP 500"):
qb.count_batch(queries)
def test_search_batch_handles_malformed_response(self):
"""search_batch should raise JSONDecodeError on invalid server JSON."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = b"invalid json"
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
with pytest.raises(json.JSONDecodeError):
qb.search_batch(queries)
class TestResponseParsing:
"""Test response parsing integration."""
def test_search_batch_returns_results_array(self):
"""search_batch should return list of raw search-response-like dicts."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon"), QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
# Batch API format: results[i] has {total, results:[{index,result}...], error}
hit = {"index": "taxon", "result": {"taxon_id": "9606"}}
response_data = {
"status": {"success": True},
"results": [
{"total": 100, "results": [hit], "error": None},
{"total": 50, "results": [], "error": None},
],
}
mock_resp.read.return_value = json.dumps(response_data).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
result = qb.search_batch(queries)
assert isinstance(result, list)
assert len(result) == 2
# Each item is a search-response-like dict with "results" and "status"
assert result[0]["status"]["hits"] == 100
assert result[0]["results"] == [hit]
assert result[1]["status"]["hits"] == 50
def test_count_batch_extracts_hits_from_each_result(self):
"""count_batch should extract total from each parse_batch_json result."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon"), QueryBuilder("taxon"), QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
with patch("cli_generator.parse_batch_json") as mock_parse:
mock_resp = MagicMock()
# parse_batch_json normalises counts into "total"
response_data = {
"status": {"success": True},
"results": [
{"records": [], "total": 150, "error": None},
{"records": [], "total": 250, "error": None},
{"records": [], "total": 350, "error": None},
],
}
mock_parse.return_value = json.dumps(response_data)
mock_resp.read.return_value = json.dumps(response_data).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
result = qb.count_batch(queries)
assert result == [150, 250, 350]
def test_search_batch_handles_empty_results(self):
"""search_batch should handle responses with no results."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps({"status": {"success": True}, "results": []}).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
result = qb.search_batch(queries)
assert result == []
def test_count_batch_handles_empty_results(self):
"""count_batch should handle responses with no results."""
qb = QueryBuilder("taxon")
queries = [QueryBuilder("taxon")]
with patch("urllib.request.urlopen") as mock_urlopen:
with patch("cli_generator.parse_batch_json") as mock_parse:
mock_resp = MagicMock()
response_data = {"status": {"success": True}, "results": []}
mock_parse.return_value = json.dumps(response_data)
mock_resp.read.return_value = json.dumps(response_data).encode("utf-8")
mock_resp.__enter__.return_value = mock_resp
mock_urlopen.return_value = mock_resp
result = qb.count_batch(queries)
assert result == []
class TestToFlatRecordsRawResponse:
"""Test to_flat_records and to_tidy_records with a pre-fetched raw_response."""
_SEARCH_RESPONSE = {
"status": {"hits": 1, "success": True},
"results": [
{
"index": "taxon",
"result": {
"taxon_id": "9606",
"scientific_name": "Homo sapiens",
"taxon_rank": "species",
"fields": {},
},
}
],
}
def test_to_flat_records_with_raw_response_parses_correctly(self) -> None:
"""to_flat_records(raw_response=...) should parse a search response dict."""
qb = QueryBuilder("taxon")
records = qb.to_flat_records(raw_response=self._SEARCH_RESPONSE)
assert isinstance(records, list)
assert len(records) == 1
assert records[0]["taxon_id"] == "9606"
assert records[0]["scientific_name"] == "Homo sapiens"
def test_to_flat_records_with_search_batch_item(self) -> None:
"""to_flat_records(raw_response=batch_item) works on search_batch() output."""
qb = QueryBuilder("taxon")
# search_batch() restructures each result into {"results": [...hits...], "status": {"hits": N}}
batch_item = {
"results": self._SEARCH_RESPONSE["results"],
"status": {"hits": 1},
}
records = qb.to_flat_records(raw_response=batch_item)
assert isinstance(records, list)
assert len(records) == 1
assert records[0]["taxon_id"] == "9606"
def test_to_tidy_records_with_raw_response_parses_correctly(self) -> None:
"""to_tidy_records(raw_response=...) should parse then reshape."""
qb = QueryBuilder("taxon")
tidy = qb.to_tidy_records(raw_response=self._SEARCH_RESPONSE)
assert isinstance(tidy, list)
assert all("field" in row for row in tidy)
def test_to_flat_records_records_takes_priority_over_raw_response(self) -> None:
"""When both records and raw_response are given, records wins."""
qb = QueryBuilder("taxon")
flat = [{"taxon_id": "1234", "scientific_name": "Test species", "taxon_rank": "species"}]
tidy = qb.to_tidy_records(records=flat, raw_response=self._SEARCH_RESPONSE)
assert isinstance(tidy, list)
assert all(r["taxon_id"] == "1234" for r in tidy)