-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtest_collection.py
More file actions
677 lines (576 loc) · 21.5 KB
/
test_collection.py
File metadata and controls
677 lines (576 loc) · 21.5 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
import json
from typing import Callable, Optional
import pystac
import pytest
from stac_pydantic import Collection
from ..conftest import requires_pgstac_0_9_2
async def test_create_collection(app_client, load_test_data: Callable):
in_json = load_test_data("test_collection.json")
in_coll = Collection.model_validate(in_json)
resp = await app_client.post(
"/collections",
json=in_json,
)
assert resp.status_code == 201
post_coll = Collection.model_validate(resp.json())
assert in_coll.model_dump(exclude={"links"}) == post_coll.model_dump(
exclude={"links"}
)
resp = await app_client.get(f"/collections/{post_coll.id}")
assert resp.status_code == 200
get_coll = Collection.model_validate(resp.json())
assert post_coll.model_dump(exclude={"links"}) == get_coll.model_dump(
exclude={"links"}
)
post_coll = post_coll.model_dump(mode="json")
get_coll = get_coll.model_dump(mode="json")
post_self_link = next(
(link for link in post_coll["links"] if link["rel"] == "self"), None
)
get_self_link = next(
(link for link in get_coll["links"] if link["rel"] == "self"), None
)
assert post_self_link is not None and get_self_link is not None
assert post_self_link["href"] == get_self_link["href"]
async def test_update_collection(app_client, load_test_data, load_test_collection):
in_coll = load_test_collection
in_coll["keywords"].append("newkeyword")
resp = await app_client.put(f"/collections/{in_coll['id']}", json=in_coll)
assert resp.status_code == 200
put_coll = Collection.model_validate(resp.json())
resp = await app_client.get(f"/collections/{in_coll['id']}")
assert resp.status_code == 200
get_coll = Collection.model_validate(resp.json())
in_coll = Collection(**in_coll)
assert in_coll.model_dump(exclude={"links"}) == get_coll.model_dump(exclude={"links"})
assert "newkeyword" in get_coll.keywords
get_coll = get_coll.model_dump(mode="json")
put_coll = put_coll.model_dump(mode="json")
put_self_link = next(
(link for link in put_coll["links"] if link["rel"] == "self"), None
)
get_self_link = next(
(link for link in get_coll["links"] if link["rel"] == "self"), None
)
assert put_self_link is not None and get_self_link is not None
assert put_self_link["href"] == get_self_link["href"]
async def test_delete_collection(
app_client, load_test_data: Callable, load_test_collection
):
in_coll = load_test_collection
resp = await app_client.delete(f"/collections/{in_coll['id']}")
assert resp.status_code == 200
resp = await app_client.get(f"/collections/{in_coll['id']}")
assert resp.status_code == 404
async def test_create_collection_conflict(app_client, load_test_data: Callable):
in_json = load_test_data("test_collection.json")
Collection.model_validate(in_json)
resp = await app_client.post(
"/collections",
json=in_json,
)
assert resp.status_code == 201
Collection.model_validate(resp.json())
resp = await app_client.post(
"/collections",
json=in_json,
)
assert resp.status_code == 409
async def test_delete_missing_collection(
app_client,
):
resp = await app_client.delete("/collections")
assert resp.status_code == 405
async def test_update_new_collection(app_client, load_test_collection):
in_coll = load_test_collection
in_coll["id"] = "test-updatenew"
resp = await app_client.put(f"/collections/{in_coll['id']}", json=in_coll)
assert resp.status_code == 404
async def test_patch_collection_partialcollection(
app_client, load_test_collection: Collection
):
"""Test patching a collection with a PartialCollection."""
partial = {
"id": load_test_collection["id"],
"description": "Patched description",
}
resp = await app_client.patch(f"/collections/{partial['id']}", json=partial)
assert resp.status_code == 200
resp = await app_client.get(f"/collections/{partial['id']}")
assert resp.status_code == 200
get_coll = Collection.model_validate(resp.json())
assert get_coll.description == "Patched description"
async def test_patch_collection_operations(app_client, load_test_collection: Collection):
"""Test patching a collection with PatchOperations ."""
operations = [
{"op": "replace", "path": "/description", "value": "Patched description"}
]
resp = await app_client.patch(
f"/collections/{load_test_collection['id']}", json=operations
)
assert resp.status_code == 200
resp = await app_client.get(f"/collections/{load_test_collection['id']}")
assert resp.status_code == 200
get_coll = Collection.model_validate(resp.json())
assert get_coll.description == "Patched description"
async def test_nocollections(
app_client,
):
resp = await app_client.get("/collections")
assert resp.status_code == 200
assert resp.json()["numberReturned"] == 0
async def test_returns_valid_collection(app_client, load_test_data):
"""Test updating a collection which already exists"""
in_json = load_test_data("test_collection.json")
resp = await app_client.post(
"/collections",
json=in_json,
)
assert resp.status_code == 201
resp = await app_client.get(f"/collections/{in_json['id']}")
assert resp.status_code == 200
resp_json = resp.json()
# Mock root to allow validation
mock_root = pystac.Catalog(
id="test", description="test desc", href="https://example.com"
)
collection = pystac.Collection.from_dict(
resp_json, root=mock_root, preserve_dict=False
)
collection.validate()
async def test_returns_valid_links_in_collections(app_client, load_test_data):
"""Test links from listing collections"""
in_json = load_test_data("test_collection.json")
resp = await app_client.post(
"/collections",
json=in_json,
)
assert resp.status_code == 201
# Get collection by ID
resp = await app_client.get(f"/collections/{in_json['id']}")
assert resp.status_code == 200
resp_json = resp.json()
# Mock root to allow validation
mock_root = pystac.Catalog(
id="test", description="test desc", href="https://example.com"
)
collection = pystac.Collection.from_dict(
resp_json, root=mock_root, preserve_dict=False
)
assert collection.validate()
# List collections
resp = await app_client.get("/collections")
assert resp.status_code == 200
resp_json = resp.json()
assert resp.json()["numberReturned"]
assert resp.json()["numberMatched"]
collections = resp_json["collections"]
# Find collection in list by ID
single_coll = next(coll for coll in collections if coll["id"] == in_json["id"])
is_coll_from_list_valid = False
single_coll_mocked_link: Optional[pystac.Collection] = None
if single_coll is not None:
single_coll_mocked_link = pystac.Collection.from_dict(
single_coll, root=mock_root, preserve_dict=False
)
is_coll_from_list_valid = single_coll_mocked_link.validate()
assert is_coll_from_list_valid
# Check links from the collection GET and list
assert [
i
for i in collection.to_dict()["links"]
if i not in single_coll_mocked_link.to_dict()["links"]
] == []
async def test_returns_license_link(app_client, load_test_collection):
coll = load_test_collection
resp = await app_client.get(f"/collections/{coll['id']}")
assert resp.status_code == 200
resp_json = resp.json()
link_rel_types = [link["rel"] for link in resp_json["links"]]
assert "license" in link_rel_types
@pytest.mark.asyncio
async def test_get_collection_forwarded_header(app_client, load_test_collection):
coll = load_test_collection
resp = await app_client.get(
f"/collections/{coll['id']}",
headers={"Forwarded": "proto=https;host=test:1234"},
)
for link in [
link
for link in resp.json()["links"]
if link["rel"] in ["items", "parent", "root", "self"]
]:
assert link["href"].startswith("https://test:1234/")
@pytest.mark.asyncio
async def test_get_collection_x_forwarded_headers(app_client, load_test_collection):
coll = load_test_collection
resp = await app_client.get(
f"/collections/{coll['id']}",
headers={
"X-Forwarded-Port": "1234",
"X-Forwarded-Proto": "https",
},
)
for link in [
link
for link in resp.json()["links"]
if link["rel"] in ["items", "parent", "root", "self"]
]:
assert link["href"].startswith("https://test:1234/")
@pytest.mark.asyncio
async def test_get_collection_duplicate_forwarded_headers(
app_client, load_test_collection
):
coll = load_test_collection
resp = await app_client.get(
f"/collections/{coll['id']}",
headers={
"Forwarded": "proto=https;host=test:1234",
"X-Forwarded-Port": "4321",
"X-Forwarded-Proto": "http",
},
)
for link in [
link
for link in resp.json()["links"]
if link["rel"] in ["items", "parent", "root", "self"]
]:
assert link["href"].startswith("https://test:1234/")
@pytest.mark.asyncio
async def test_get_collections_forwarded_header(app_client, load_test_collection):
resp = await app_client.get(
"/collections",
headers={"Forwarded": "proto=https;host=test:1234"},
)
for link in resp.json()["links"]:
assert link["href"].startswith("https://test:1234/")
@pytest.mark.asyncio
async def test_get_collections_queryables_links(app_client, load_test_collection):
resp = await app_client.get(
"/collections",
)
assert "Queryables" in [
link.get("title") for link in resp.json()["collections"][0]["links"]
]
collection_id = resp.json()["collections"][0]["id"]
resp = await app_client.get(
f"/collections/{collection_id}",
)
assert "Queryables" in [link.get("title") for link in resp.json()["links"]]
@pytest.mark.asyncio
async def test_get_collections_search(
app_client, load_test_collection, load_test2_collection
):
# this search should only return a single collection
resp = await app_client.get(
"/collections",
params={"datetime": "2010-01-01T00:00:00Z/2010-01-02T00:00:00Z"},
)
assert len(resp.json()["collections"]) == 1
assert resp.json()["collections"][0]["id"] == load_test2_collection.id
# same with this one
resp = await app_client.get(
"/collections",
params={"datetime": "2020-01-01T00:00:00Z/.."},
)
assert len(resp.json()["collections"]) == 1
assert resp.json()["collections"][0]["id"] == load_test_collection["id"]
# no params should return both collections
resp = await app_client.get(
"/collections",
)
assert len(resp.json()["collections"]) == 2
@requires_pgstac_0_9_2
@pytest.mark.asyncio
async def test_collection_search_freetext(
app_client, load_test_collection, load_test2_collection
):
# free-text
resp = await app_client.get(
"/collections",
params={"q": "temperature"},
)
assert resp.json()["numberReturned"] == 1
assert resp.json()["numberMatched"] == 1
assert len(resp.json()["collections"]) == 1
assert resp.json()["collections"][0]["id"] == load_test2_collection.id
resp = await app_client.get(
"/collections",
params={"q": "temperature,calibrated"},
)
assert resp.json()["numberReturned"] == 2
assert resp.json()["numberMatched"] == 2
assert len(resp.json()["collections"]) == 2
resp = await app_client.get(
"/collections",
params={"q": "temperature,yo"},
)
assert resp.json()["numberReturned"] == 1
assert resp.json()["numberMatched"] == 1
assert len(resp.json()["collections"]) == 1
assert resp.json()["collections"][0]["id"] == load_test2_collection.id
resp = await app_client.get(
"/collections",
params={"q": "nosuchthing"},
)
assert len(resp.json()["collections"]) == 0
@requires_pgstac_0_9_2
@pytest.mark.asyncio
async def test_collection_search_freetext_advanced(
app_client_advanced_freetext, load_test_collection, load_test2_collection
):
# free-text
resp = await app_client_advanced_freetext.get(
"/collections",
params={"q": "temperature"},
)
assert resp.json()["numberReturned"] == 1
assert resp.json()["numberMatched"] == 1
assert len(resp.json()["collections"]) == 1
assert resp.json()["collections"][0]["id"] == load_test2_collection.id
resp = await app_client_advanced_freetext.get(
"/collections",
params={"q": "temperature,calibrated"},
)
assert resp.json()["numberReturned"] == 2
assert resp.json()["numberMatched"] == 2
assert len(resp.json()["collections"]) == 2
resp = await app_client_advanced_freetext.get(
"/collections",
params={"q": "temperature,yo"},
)
assert resp.json()["numberReturned"] == 1
assert resp.json()["numberMatched"] == 1
assert len(resp.json()["collections"]) == 1
assert resp.json()["collections"][0]["id"] == load_test2_collection.id
resp = await app_client_advanced_freetext.get(
"/collections",
params={"q": "temperature OR yo"},
)
assert resp.json()["numberReturned"] == 1
assert resp.json()["numberMatched"] == 1
assert len(resp.json()["collections"]) == 1
assert resp.json()["collections"][0]["id"] == load_test2_collection.id
resp = await app_client_advanced_freetext.get(
"/collections",
params={"q": "nosuchthing"},
)
assert len(resp.json()["collections"]) == 0
@requires_pgstac_0_9_2
@pytest.mark.asyncio
async def test_all_collections_with_pagination(app_client, load_test_data):
data = load_test_data("test_collection.json")
collection_id = data["id"]
for ii in range(0, 12):
data["id"] = collection_id + f"_{ii}"
resp = await app_client.post(
"/collections",
json=data,
)
assert resp.status_code == 201
resp = await app_client.get("/collections")
assert resp.json()["numberReturned"] == 10
assert resp.json()["numberMatched"] == 12
cols = resp.json()["collections"]
assert len(cols) == 10
links = resp.json()["links"]
assert len(links) == 3
assert {"root", "self", "next"} == {link["rel"] for link in links}
resp = await app_client.get("/collections", params={"limit": 12})
assert resp.json()["numberReturned"] == 12
assert resp.json()["numberMatched"] == 12
cols = resp.json()["collections"]
assert len(cols) == 12
links = resp.json()["links"]
assert len(links) == 2
assert {"root", "self"} == {link["rel"] for link in links}
@requires_pgstac_0_9_2
@pytest.mark.asyncio
async def test_all_collections_without_pagination(app_client_no_ext, load_test_data):
data = load_test_data("test_collection.json")
collection_id = data["id"]
for ii in range(0, 12):
data["id"] = collection_id + f"_{ii}"
resp = await app_client_no_ext.post(
"/collections",
json=data,
)
assert resp.status_code == 201
resp = await app_client_no_ext.get("/collections")
assert resp.json()["numberReturned"] == 12
assert resp.json()["numberMatched"] == 12
cols = resp.json()["collections"]
assert len(cols) == 12
links = resp.json()["links"]
assert len(links) == 2
assert {"root", "self"} == {link["rel"] for link in links}
@requires_pgstac_0_9_2
@pytest.mark.asyncio
async def test_get_collections_search_pagination(
app_client, load_test_collection, load_test2_collection
):
resp = await app_client.get("/collections")
assert resp.json()["numberReturned"] == 2
assert resp.json()["numberMatched"] == 2
cols = resp.json()["collections"]
assert len(cols) == 2
links = resp.json()["links"]
assert len(links) == 2
assert {"root", "self"} == {link["rel"] for link in links}
###################
# limit should be positive
resp = await app_client.get("/collections", params={"limit": 0})
assert resp.status_code == 400
###################
# limit=1, should have a `next` link
resp = await app_client.get(
"/collections",
params={"limit": 1},
)
cols = resp.json()["collections"]
links = resp.json()["links"]
assert len(cols) == 1
assert cols[0]["id"] == load_test_collection["id"]
assert len(links) == 3
assert {"root", "self", "next"} == {link["rel"] for link in links}
next_link = list(filter(lambda link: link["rel"] == "next", links))[0]
assert next_link["href"].endswith("?limit=1&offset=1")
###################
# limit=2, there should not be a next link
resp = await app_client.get(
"/collections",
params={"limit": 2},
)
cols = resp.json()["collections"]
links = resp.json()["links"]
assert len(cols) == 2
assert cols[0]["id"] == load_test_collection["id"]
assert cols[1]["id"] == load_test2_collection.id
assert len(links) == 2
assert {"root", "self"} == {link["rel"] for link in links}
###################
# limit=3, there should not be a next/previous link
resp = await app_client.get(
"/collections",
params={"limit": 3},
)
cols = resp.json()["collections"]
links = resp.json()["links"]
assert len(cols) == 2
assert cols[0]["id"] == load_test_collection["id"]
assert cols[1]["id"] == load_test2_collection.id
assert len(links) == 2
assert {"root", "self"} == {link["rel"] for link in links}
###################
# offset=3, because there are 2 collections, we should not have `next` or `prev` links
resp = await app_client.get(
"/collections",
params={"offset": 3},
)
cols = resp.json()["collections"]
links = resp.json()["links"]
assert len(cols) == 0
assert len(links) == 2
assert {"root", "self"} == {link["rel"] for link in links}
###################
# offset=3,limit=1
resp = await app_client.get(
"/collections",
params={"limit": 1, "offset": 3},
)
cols = resp.json()["collections"]
links = resp.json()["links"]
assert len(cols) == 0
assert len(links) == 3
assert {"root", "self", "previous"} == {link["rel"] for link in links}
prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0]
assert prev_link["href"].endswith("?limit=1&offset=2")
###################
# limit=2, offset=3, there should not be a next link
resp = await app_client.get(
"/collections",
params={"limit": 2, "offset": 3},
)
cols = resp.json()["collections"]
links = resp.json()["links"]
assert len(cols) == 0
assert len(links) == 3
assert {"root", "self", "previous"} == {link["rel"] for link in links}
prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0]
assert prev_link["href"].endswith("?limit=2&offset=1")
###################
# offset=1,limit=1 should have a `previous` link
resp = await app_client.get(
"/collections",
params={"offset": 1, "limit": 1},
)
cols = resp.json()["collections"]
links = resp.json()["links"]
assert len(cols) == 1
assert cols[0]["id"] == load_test2_collection.id
assert len(links) == 3
assert {"root", "self", "previous"} == {link["rel"] for link in links}
prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0]
assert "offset" in prev_link["href"]
###################
# offset=0, should not have next/previous link
resp = await app_client.get(
"/collections",
params={"offset": 0},
)
cols = resp.json()["collections"]
links = resp.json()["links"]
assert len(cols) == 2
assert len(links) == 2
assert {"root", "self"} == {link["rel"] for link in links}
@requires_pgstac_0_9_2
@pytest.mark.xfail(strict=False)
@pytest.mark.asyncio
async def test_get_collections_search_offset_1(
app_client, load_test_collection, load_test2_collection
):
# BUG: pgstac doesn't return a `prev` link when limit is not set
# offset=1, should have a `previous` link
resp = await app_client.get(
"/collections",
params={"offset": 1},
)
cols = resp.json()["collections"]
links = resp.json()["links"]
assert len(cols) == 1
assert cols[0]["id"] == load_test2_collection.id
assert len(links) == 3
assert {"root", "self", "previous"} == {link["rel"] for link in links}
prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0]
# offset=0 should not be in the previous link (because it's useless)
assert "offset" not in prev_link["href"]
@pytest.mark.parametrize(
"filter, filter_lang, expected_count",
[
("true", "cql2-text", 1),
("1=1", "cql2-text", 1),
("true", "cql2-json", 1),
(json.dumps({"op": "=", "args": [1.0, 1.0]}), "cql2-json", 1),
("false", "cql2-text", 0),
("1=0", "cql2-text", 0),
("false", "cql2-json", 0),
(json.dumps({"op": "=", "args": [1.0, 1.0]}), "cql2-json", 0),
],
)
async def test_get_collections_filter(
app_client,
load_test_collection,
load_test2_collection,
filter,
filter_lang,
expected_count,
):
"""
Test CQL2 filters on the collections endpoint
"""
resp = await app_client.get(
"/collections",
params={"filter": filter, "filter-lang": filter_lang},
)
assert resp.status_code == 200
assert len(resp.json()["collections"]) == expected_count