-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_search_api.py
More file actions
635 lines (567 loc) · 19.6 KB
/
test_search_api.py
File metadata and controls
635 lines (567 loc) · 19.6 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# coding: utf-8
import pytest
from fastapi.testclient import TestClient
from feeds_gen.models.search_feeds200_response import SearchFeeds200Response # noqa: F401
from tests.test_utils.database import TEST_GTFS_FEED_STABLE_IDS, TEST_GTFS_RT_FEED_STABLE_ID
@pytest.mark.parametrize(
"search_query",
[
# Provider
"MobilityDataTest provider",
# Provider using a lexically similar word
"MobilityDataTest PROVIDE",
# Feed name
"MobilityDataTest Feed name",
# Feed name using a lexically similar word
"MobilityDataTest feedING",
],
)
def test_search_feeds_all_feeds(client: TestClient, search_query: str):
"""
Retrieve all feeds with a search query using provider or feed name.
"""
params = [("limit", 100), ("offset", 0), ("feed_id", ""), ("data_type", ""), ("search_query", search_query)]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
assert response_body.total == len(TEST_GTFS_FEED_STABLE_IDS)
assert len(response_body.results) == len(TEST_GTFS_FEED_STABLE_IDS)
@pytest.mark.parametrize(
"search_query",
[
# Provider
"MobilityDataTest provider",
# Provider using a lexically similar word
"MobilityDataTest PROVIDE",
# Feed name
"MobilityDataTest Feed name",
# Feed name using a lexically similar word
"MobilityDataTest feedING",
],
)
def test_search_feeds_all_feeds_with_limit(client: TestClient, search_query: str):
"""
Retrieve 2 feeds using limit with a search query using provider or feed name.
"""
params = [("limit", 2), ("offset", 0), ("feed_id", ""), ("data_type", ""), ("search_query", search_query)]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
assert response_body.total == len(TEST_GTFS_FEED_STABLE_IDS)
assert len(response_body.results) == 2
@pytest.mark.parametrize(
"search_query",
[
# Provider
f"{TEST_GTFS_FEED_STABLE_IDS[0]}-MobilityDataTest provider",
# Provider using a lexically similar word
f"{TEST_GTFS_FEED_STABLE_IDS[0]}-MobilityDataTest provide",
# Feed name
f"{TEST_GTFS_FEED_STABLE_IDS[0]}-MobilityDataTest Feed name",
# Feed name using a lexically similar word
f"{TEST_GTFS_FEED_STABLE_IDS[0]}-MobilityDataTest Feeding name",
],
)
def test_search_feeds_provider_one_feed(client: TestClient, search_query: str):
"""
Retrieve a single feed with a search query using provider or feed name.
"""
params = [
("limit", 10),
("offset", 0),
("status", ""),
("feed_id", ""),
("data_type", ""),
("search_query", search_query),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
assert response_body.total == 1
assert response_body.results[0].id == TEST_GTFS_FEED_STABLE_IDS[0]
assert response_body.results[0].provider == f"{TEST_GTFS_FEED_STABLE_IDS[0]}-MobilityDataTest provider"
@pytest.mark.parametrize(
"data_type, expected_results_total",
[("gtfs", 10), ("not_valid_gtfs", 0), ("gtfs_rt", 3), ("gtfs,gtfs_rt", 13)],
)
def test_search_feeds_filter_data_type(client: TestClient, data_type: str, expected_results_total: int):
"""
Retrieve feeds with a specific data type.
"""
params = [
("limit", 100),
("offset", 0),
("status", ""),
("feed_id", ""),
("data_type", data_type),
("search_query", ""),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
assert response_body.total == expected_results_total
assert len(response_body.results) == expected_results_total
if expected_results_total > 1:
for result in response_body.results:
assert result.data_type in data_type.split(",")
@pytest.mark.parametrize(
"status, expected_results_total",
[
("active", 12),
("not_valid_status", 0),
("inactive", 2),
("active,inactive", 14),
],
)
def test_search_feeds_filter_status(client: TestClient, status: str, expected_results_total: int):
"""
Retrieve feeds with a specific status.
"""
params = [
("limit", 100),
("offset", 0),
("status", status),
("feed_id", ""),
("data_type", ""),
("search_query", ""),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
assert response_body.total == expected_results_total
assert len(response_body.results) == expected_results_total
if expected_results_total > 0:
for result in response_body.results:
assert result.status in status
@pytest.mark.parametrize(
"feed_id, expected_results_total",
[
(TEST_GTFS_FEED_STABLE_IDS[0], 1),
(TEST_GTFS_FEED_STABLE_IDS[1], 1),
("this_is_not_valid", 0),
],
)
def test_search_feeds_filter_feed_id(client: TestClient, feed_id: str, expected_results_total: int):
"""
Retrieve feeds with a specific feed ID.
"""
params = [
("limit", 100),
("offset", 0),
("status", ""),
("feed_id", feed_id),
("data_type", ""),
("search_query", ""),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
assert response_body.total == expected_results_total
assert len(response_body.results) == expected_results_total
if expected_results_total > 1:
for result in response_body.results:
assert result.id == feed_id
@pytest.mark.parametrize(
"feed_id, status, data_type, search_query, expected_results_total",
[
(TEST_GTFS_FEED_STABLE_IDS[0], "active", "gtfs", "MobilityDataTest", 1),
(TEST_GTFS_FEED_STABLE_IDS[0], "deprecated", "gtfs", "MobilityDataTest", 0),
(TEST_GTFS_FEED_STABLE_IDS[0], "active", "gtfs_rt", "MobilityDataTest", 0), # GTFS-rt is not the right type
("", "active", "gtfs", "MobilityDataTest", len(TEST_GTFS_FEED_STABLE_IDS)),
(TEST_GTFS_FEED_STABLE_IDS[0], "", "gtfs", "MobilityDataTest", 1),
(TEST_GTFS_RT_FEED_STABLE_ID, "active", "gtfs_rt", "", 1),
],
)
def test_search_feeds_filter_combine_filters_and_query(
client: TestClient, feed_id: str, status: str, data_type: str, search_query: str, expected_results_total: int
):
"""
Retrieve feeds combining feed ID, status, data type, and search query.
"""
params = [
("limit", 100),
("offset", 0),
("status", status),
("feed_id", feed_id),
("data_type", data_type),
("search_query", search_query),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
assert response_body.total == expected_results_total
assert len(response_body.results) == expected_results_total
if expected_results_total > 1:
for result in response_body.results:
if feed_id:
assert result.id == feed_id
if status:
assert result.status == status
if data_type:
assert result.data_type == data_type
def test_search_feeds_filter_reference_id(client: TestClient):
"""
Retrieve feeds combining feed ID, status, data type, and search query.
"""
params = [
("limit", 100),
("offset", 0),
("feed_id", TEST_GTFS_RT_FEED_STABLE_ID),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
assert response_body.total == 1
assert len(response_body.results) == 1
assert response_body.results[0].id == TEST_GTFS_RT_FEED_STABLE_ID
assert response_body.results[0].data_type == "gtfs_rt"
assert response_body.results[0].status == "active"
assert len(response_body.results[0].feed_references) == 1
assert response_body.results[0].feed_references[0] == TEST_GTFS_FEED_STABLE_IDS[0]
@pytest.mark.parametrize(
"values",
[
{"search_query": "éèàçíóúč", "expected_ids": ["mdb-1562"]},
{"search_query": "eeaciouc", "expected_ids": ["mdb-1562"]},
{"search_query": "ŘŤÜî", "expected_ids": ["mdb-1562"]},
{"search_query": "rtui", "expected_ids": ["mdb-1562"]},
],
ids=[
"Search query with accents and special characters against a provider",
"Search query with the normalized version of the accents against a provider",
"Search query with accents and special characters against the feed name",
"Search query with the normalized version of the accents against the feed name",
],
)
def test_search_feeds_filter_accents(client: TestClient, values: dict):
"""
Retrieve feeds with accents in the provider name and/or feed name.
"""
params = [
("limit", 100),
("offset", 0),
("search_query", values["search_query"]),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
assert len(response_body.results) == len(values["expected_ids"])
assert response_body.total == len(values["expected_ids"])
assert all(result.id in values["expected_ids"] for result in response_body.results)
@pytest.mark.parametrize(
"values",
[
{"official": True, "expected_count": 2},
{"official": False, "expected_count": 14},
{"official": None, "expected_count": 16},
],
ids=[
"Official",
"Not official",
"Not specified",
],
)
def test_search_filter_by_official_status(client: TestClient, values: dict):
"""
Retrieve feeds with the official status.
"""
params = None
if values["official"] is not None:
params = [
("is_official", str(values["official"]).lower()),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
expected_count = values["expected_count"]
assert (
response_body.total == expected_count
), f"There should be {expected_count} feeds for official={values['official']}"
@pytest.mark.parametrize(
"values",
[
{"versions": "1.0", "expected_count": 0},
{"versions": "2.3,3.0", "expected_count": 2},
{"versions": "3.0", "expected_count": 1},
{"versions": "2.3", "expected_count": 2},
{"versions": None, "expected_count": 16},
],
ids=[
"Version 1.0",
"Versions 2.3 and 3.0",
"Version 3.0",
"Version 2.3",
"No version specified",
],
)
def test_search_filter_by_versions(client: TestClient, values: dict):
"""
Retrieve feeds with the version.
"""
params = None
if values["versions"] is not None:
params = [
("version", values["versions"]),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
expected_count = values["expected_count"]
assert (
response_body.total == expected_count
), f"There should be {expected_count} feeds for versions={values['versions']}"
@pytest.mark.parametrize(
"values",
[
{"license_ids": "CC BY 4.0", "expected_count": 1},
{"license_ids": "ODbL-1.0", "expected_count": 1},
{"license_ids": "ODbL-1.0,CC BY 4.0", "expected_count": 2},
{"license_ids": "", "expected_count": 16},
],
ids=[
"License ID CC BY 4.0",
"License ID ODbL-1.0",
"License IDs ODbL-1.0 and CC BY 4.0",
"No license IDs specified",
],
)
def test_search_filter_by_license_ids(client: TestClient, values: dict):
"""
Retrieve feeds that contain specific license IDs.
"""
params = None
if values["license_ids"] is not None:
params = [
("license_ids", values["license_ids"]),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.parse_obj(response.json())
expected_count = values["expected_count"]
assert (
response_body.total == expected_count
), f"There should be {expected_count} feeds for license_ids={values['license_ids']}"
@pytest.mark.parametrize(
"values",
[
{"feature": "", "expected_count": 16},
{"feature": "Bike Allowed", "expected_count": 2},
{"feature": "Stops Wheelchair Accessibility", "expected_count": 0},
],
ids=[
"All",
"Bike Allowed",
"Stops Wheelchair Accessibility",
],
)
def test_search_filter_by_feature(client: TestClient, values: dict):
"""
Retrieve feeds that contain specific features.
"""
params = None
if values["feature"] is not None:
params = [
("feature", values["feature"]),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
# Assert the status code of the HTTP response
assert response.status_code == 200
# Parse the response body into a Python object
response_body = SearchFeeds200Response.model_validate(response.json())
expected_count = values["expected_count"]
assert (
response_body.total == expected_count
), f"There should be {expected_count} feeds with feature={values['feature']}"
# Verify all returned feeds have at least one of the requested features
if values["feature"] and expected_count > 0:
requested_features = set(values["feature"].split(","))
for result in response_body.results:
features = result.latest_dataset.validation_report.features
# Check that at least one of the feed's features is in the requested features
assert requested_features.intersection(features), (
f"Feed {result.id} with features {features} does not match " f"requested features {requested_features}"
)
@pytest.mark.parametrize(
"values",
[
{"license_tags": "family:ODC", "expected_count": 1},
{"license_tags": "license:open-data-commons", "expected_count": 1},
# AND semantics: feed must contain ALL requested tags
{"license_tags": "family:ODC,license:open-data-commons", "expected_count": 1},
{"license_tags": "nonexistent:tag", "expected_count": 0},
{"license_tags": "license:open-data-commons,nonexistent:tag", "expected_count": 0},
{"license_tags": "", "expected_count": 16},
],
ids=[
"Filter by single tag family:ODC",
"Filter by single tag license:open-data-commons",
"Filter by multiple tags (AND semantics)",
"No feed matches nonexistent tag",
"Mixed existing and nonexistent tag (AND semantics)",
"No filter returns all feeds",
],
)
def test_search_filter_by_license_tags(client: TestClient, values: dict):
"""Retrieve feeds that have licenses associated with specific license tag IDs.
The ``license_tags`` parameter accepts a comma-separated list of tag IDs.
The filter uses AND semantics: the feed's ``license_tags`` array must
contain **all** of the requested tags for the feed to be returned.
"""
params = None
if values["license_tags"]:
params = [("license_tags", values["license_tags"])]
headers = {"Authentication": "special-key"}
response = client.request("GET", "/v1/search", headers=headers, params=params)
assert response.status_code == 200
response_body = SearchFeeds200Response.model_validate(response.json())
expected_count = values["expected_count"]
assert (
response_body.total == expected_count
), f"There should be {expected_count} feeds for license_tags={values['license_tags']}"
def test_search_result_contains_license_tags(client: TestClient):
"""
Verify that the search results include license_tags for feeds with license tags.
"""
params = [
("feed_id", "mdb-70"),
]
headers = {
"Authentication": "special-key",
}
response = client.request(
"GET",
"/v1/search",
headers=headers,
params=params,
)
assert response.status_code == 200
response_body = SearchFeeds200Response.parse_obj(response.json())
assert response_body.total == 1
result = response_body.results[0]
assert result.source_info.license_tags is not None
assert "family:ODC" in result.source_info.license_tags[0]
assert "license:open-data-commons" in result.source_info.license_tags[1]