Skip to content

Commit 5731518

Browse files
Update tests (#111)
* update test item tests * test collection tests * lint * test postgres * test api * more * update item * revert * remove hack * Update stac_fastapi/pgstac/core.py Co-authored-by: Vincent Sarago <[email protected]> * fix types --------- Co-authored-by: Vincent Sarago <[email protected]>
1 parent 9edc01a commit 5731518

File tree

8 files changed

+200
-157
lines changed

8 files changed

+200
-157
lines changed

stac_fastapi/pgstac/core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ async def item_collection(
277277
# If collection does not exist, NotFoundError wil be raised
278278
await self.get_collection(collection_id, request=request)
279279

280+
if datetime:
281+
datetime = format_datetime_range(datetime)
282+
280283
base_args = {
281284
"collections": [collection_id],
282285
"bbox": bbox,
@@ -393,7 +396,7 @@ async def get_search( # noqa: C901
393396
base_args["filter-lang"] = "cql2-json"
394397

395398
if datetime:
396-
base_args["datetime"] = datetime
399+
base_args["datetime"] = format_datetime_range(datetime)
397400

398401
if intersects:
399402
base_args["intersects"] = orjson.loads(unquote_plus(intersects))

stac_fastapi/pgstac/transactions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ async def update_collection(
162162
**kwargs,
163163
) -> Optional[Union[stac_types.Collection, Response]]:
164164
"""Update collection."""
165+
165166
col = collection.model_dump(mode="json")
166167

167168
async with request.app.state.get_connection(request, "w") as conn:

stac_fastapi/pgstac/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def dict_deep_update(merge_to: Dict[str, Any], merge_from: Dict[str, Any]) -> No
116116
merge_to[k] = v
117117

118118

119-
def format_datetime_range(dt_range: DateTimeType) -> Union[str, Any]:
119+
def format_datetime_range(dt_range: Union[DateTimeType, str]) -> str:
120120
"""
121121
Convert a datetime object or a tuple of datetime objects to a formatted string for datetime ranges.
122122
@@ -132,7 +132,7 @@ def format_datetime_range(dt_range: DateTimeType) -> Union[str, Any]:
132132
return dt_range.isoformat().replace("+00:00", "Z")
133133

134134
# Handle a tuple containing datetime objects or None
135-
if isinstance(dt_range, tuple):
135+
elif isinstance(dt_range, tuple):
136136
start, end = dt_range
137137

138138
# Convert start datetime to string if not None, otherwise use ".."

tests/api/test_api.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@ async def test_get_queryables_content_type(app_client, load_test_collection):
7272
assert resp.headers["content-type"] == "application/schema+json"
7373

7474
coll = load_test_collection
75-
resp = await app_client.get(f"collections/{coll.id}/queryables")
75+
resp = await app_client.get(f"collections/{coll['id']}/queryables")
7676
assert resp.headers["content-type"] == "application/schema+json"
7777

7878

7979
async def test_get_features_content_type(app_client, load_test_collection):
8080
coll = load_test_collection
81-
resp = await app_client.get(f"collections/{coll.id}/items")
81+
resp = await app_client.get(f"collections/{coll['id']}/items")
8282
assert resp.headers["content-type"] == "application/geo+json"
8383

8484

8585
async def test_get_features_self_link(app_client, load_test_collection):
8686
# https://github.com/stac-utils/stac-fastapi/issues/483
87-
resp = await app_client.get(f"collections/{load_test_collection.id}/items")
87+
resp = await app_client.get(f"collections/{load_test_collection['id']}/items")
8888
assert resp.status_code == 200
8989
resp_json = resp.json()
9090
self_link = next((link for link in resp_json["links"] if link["rel"] == "self"), None)
@@ -94,7 +94,7 @@ async def test_get_features_self_link(app_client, load_test_collection):
9494

9595
async def test_get_feature_content_type(app_client, load_test_collection, load_test_item):
9696
resp = await app_client.get(
97-
f"collections/{load_test_collection.id}/items/{load_test_item.id}"
97+
f"collections/{load_test_collection['id']}/items/{load_test_item['id']}"
9898
)
9999
assert resp.headers["content-type"] == "application/geo+json"
100100

@@ -141,14 +141,14 @@ async def test_app_transaction_extension(
141141
):
142142
coll = load_test_collection
143143
item = load_test_data("test_item.json")
144-
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
144+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=item)
145145
assert resp.status_code == 201
146146

147147

148148
async def test_app_query_extension(load_test_data, app_client, load_test_collection):
149149
coll = load_test_collection
150150
item = load_test_data("test_item.json")
151-
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
151+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=item)
152152
assert resp.status_code == 201
153153

154154
params = {"query": {"proj:epsg": {"eq": item["properties"]["proj:epsg"]}}}
@@ -169,7 +169,7 @@ async def test_app_query_extension_limit_1(
169169
):
170170
coll = load_test_collection
171171
item = load_test_data("test_item.json")
172-
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
172+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=item)
173173
assert resp.status_code == 201
174174

175175
params = {"limit": 1}
@@ -190,7 +190,7 @@ async def test_app_query_extension_limit_lt0(
190190
):
191191
coll = load_test_collection
192192
item = load_test_data("test_item.json")
193-
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
193+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=item)
194194
assert resp.status_code == 201
195195

196196
params = {"limit": -1}
@@ -203,7 +203,7 @@ async def test_app_query_extension_limit_gt10000(
203203
):
204204
coll = load_test_collection
205205
item = load_test_data("test_item.json")
206-
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
206+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=item)
207207
assert resp.status_code == 201
208208

209209
params = {"limit": 10001}
@@ -214,7 +214,7 @@ async def test_app_query_extension_limit_gt10000(
214214
async def test_app_query_extension_gt(load_test_data, app_client, load_test_collection):
215215
coll = load_test_collection
216216
item = load_test_data("test_item.json")
217-
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
217+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=item)
218218
assert resp.status_code == 201
219219

220220
params = {"query": {"proj:epsg": {"gt": item["properties"]["proj:epsg"]}}}
@@ -227,7 +227,7 @@ async def test_app_query_extension_gt(load_test_data, app_client, load_test_coll
227227
async def test_app_query_extension_gte(load_test_data, app_client, load_test_collection):
228228
coll = load_test_collection
229229
item = load_test_data("test_item.json")
230-
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
230+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=item)
231231
assert resp.status_code == 201
232232

233233
params = {"query": {"proj:epsg": {"gte": item["properties"]["proj:epsg"]}}}
@@ -243,7 +243,7 @@ async def test_app_sort_extension(load_test_data, app_client, load_test_collecti
243243
item_date = datetime.strptime(
244244
first_item["properties"]["datetime"], "%Y-%m-%dT%H:%M:%SZ"
245245
)
246-
resp = await app_client.post(f"/collections/{coll.id}/items", json=first_item)
246+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=first_item)
247247
assert resp.status_code == 201
248248

249249
second_item = load_test_data("test_item.json")
@@ -252,11 +252,11 @@ async def test_app_sort_extension(load_test_data, app_client, load_test_collecti
252252
second_item["properties"]["datetime"] = another_item_date.strftime(
253253
"%Y-%m-%dT%H:%M:%SZ"
254254
)
255-
resp = await app_client.post(f"/collections/{coll.id}/items", json=second_item)
255+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=second_item)
256256
assert resp.status_code == 201
257257

258258
params = {
259-
"collections": [coll.id],
259+
"collections": [coll["id"]],
260260
"sortby": [{"field": "datetime", "direction": "desc"}],
261261
}
262262

@@ -267,7 +267,7 @@ async def test_app_sort_extension(load_test_data, app_client, load_test_collecti
267267
assert resp_json["features"][1]["id"] == second_item["id"]
268268

269269
params = {
270-
"collections": [coll.id],
270+
"collections": [coll["id"]],
271271
"sortby": [{"field": "datetime", "direction": "asc"}],
272272
}
273273
resp = await app_client.post("/search", json=params)
@@ -280,12 +280,12 @@ async def test_app_sort_extension(load_test_data, app_client, load_test_collecti
280280
async def test_search_invalid_date(load_test_data, app_client, load_test_collection):
281281
coll = load_test_collection
282282
first_item = load_test_data("test_item.json")
283-
resp = await app_client.post(f"/collections/{coll.id}/items", json=first_item)
283+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=first_item)
284284
assert resp.status_code == 201
285285

286286
params = {
287287
"datetime": "2020-XX-01/2020-10-30",
288-
"collections": [coll.id],
288+
"collections": [coll["id"]],
289289
}
290290

291291
resp = await app_client.post("/search", json=params)
@@ -295,13 +295,13 @@ async def test_search_invalid_date(load_test_data, app_client, load_test_collect
295295
async def test_bbox_3d(load_test_data, app_client, load_test_collection):
296296
coll = load_test_collection
297297
first_item = load_test_data("test_item.json")
298-
resp = await app_client.post(f"/collections/{coll.id}/items", json=first_item)
298+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=first_item)
299299
assert resp.status_code == 201
300300

301301
australia_bbox = [106.343365, -47.199523, 0.1, 168.218365, -19.437288, 0.1]
302302
params = {
303303
"bbox": australia_bbox,
304-
"collections": [coll.id],
304+
"collections": [coll["id"]],
305305
}
306306
resp = await app_client.post("/search", json=params)
307307
assert resp.status_code == 200
@@ -313,7 +313,7 @@ async def test_bbox_3d(load_test_data, app_client, load_test_collection):
313313
async def test_app_search_response(load_test_data, app_client, load_test_collection):
314314
coll = load_test_collection
315315
params = {
316-
"collections": [coll.id],
316+
"collections": [coll["id"]],
317317
}
318318
resp = await app_client.post("/search", json=params)
319319
assert resp.status_code == 200
@@ -328,7 +328,7 @@ async def test_app_search_response(load_test_data, app_client, load_test_collect
328328
async def test_search_point_intersects(load_test_data, app_client, load_test_collection):
329329
coll = load_test_collection
330330
item = load_test_data("test_item.json")
331-
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
331+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=item)
332332
assert resp.status_code == 201
333333

334334
new_coordinates = []
@@ -337,7 +337,7 @@ async def test_search_point_intersects(load_test_data, app_client, load_test_col
337337
item["id"] = "test-item-other-hemispheres"
338338
item["geometry"]["coordinates"] = [new_coordinates]
339339
item["bbox"] = [value * -1 for value in item["bbox"]]
340-
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
340+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=item)
341341
assert resp.status_code == 201
342342

343343
point = [150.04, -33.14]
@@ -364,7 +364,7 @@ async def test_search_line_string_intersects(
364364
):
365365
coll = load_test_collection
366366
item = load_test_data("test_item.json")
367-
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
367+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=item)
368368
assert resp.status_code == 201
369369

370370
line = [[150.04, -33.14], [150.22, -33.89]]
@@ -384,7 +384,7 @@ async def test_search_line_string_intersects(
384384
async def test_landing_forwarded_header(load_test_data, app_client, load_test_collection):
385385
coll = load_test_collection
386386
item = load_test_data("test_item.json")
387-
await app_client.post(f"/collections/{coll.id}/items", json=item)
387+
await app_client.post(f"/collections/{coll['id']}/items", json=item)
388388
response = (
389389
await app_client.get(
390390
"/",
@@ -403,7 +403,7 @@ async def test_landing_forwarded_header(load_test_data, app_client, load_test_co
403403
async def test_search_forwarded_header(load_test_data, app_client, load_test_collection):
404404
coll = load_test_collection
405405
item = load_test_data("test_item.json")
406-
await app_client.post(f"/collections/{coll.id}/items", json=item)
406+
await app_client.post(f"/collections/{coll['id']}/items", json=item)
407407
resp = await app_client.post(
408408
"/search",
409409
json={
@@ -424,7 +424,7 @@ async def test_search_x_forwarded_headers(
424424
):
425425
coll = load_test_collection
426426
item = load_test_data("test_item.json")
427-
await app_client.post(f"/collections/{coll.id}/items", json=item)
427+
await app_client.post(f"/collections/{coll['id']}/items", json=item)
428428
resp = await app_client.post(
429429
"/search",
430430
json={
@@ -448,7 +448,7 @@ async def test_search_duplicate_forward_headers(
448448
):
449449
coll = load_test_collection
450450
item = load_test_data("test_item.json")
451-
await app_client.post(f"/collections/{coll.id}/items", json=item)
451+
await app_client.post(f"/collections/{coll['id']}/items", json=item)
452452
resp = await app_client.post(
453453
"/search",
454454
json={
@@ -495,17 +495,17 @@ async def test_item_collection_filter_bbox(
495495
):
496496
coll = load_test_collection
497497
first_item = load_test_data("test_item.json")
498-
resp = await app_client.post(f"/collections/{coll.id}/items", json=first_item)
498+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=first_item)
499499
assert resp.status_code == 201
500500

501501
bbox = "100,-50,170,-20"
502-
resp = await app_client.get(f"/collections/{coll.id}/items", params={"bbox": bbox})
502+
resp = await app_client.get(f"/collections/{coll['id']}/items", params={"bbox": bbox})
503503
assert resp.status_code == 200
504504
resp_json = resp.json()
505505
assert len(resp_json["features"]) == 1
506506

507507
bbox = "1,2,3,4"
508-
resp = await app_client.get(f"/collections/{coll.id}/items", params={"bbox": bbox})
508+
resp = await app_client.get(f"/collections/{coll['id']}/items", params={"bbox": bbox})
509509
assert resp.status_code == 200
510510
resp_json = resp.json()
511511
assert len(resp_json["features"]) == 0
@@ -517,20 +517,20 @@ async def test_item_collection_filter_datetime(
517517
):
518518
coll = load_test_collection
519519
first_item = load_test_data("test_item.json")
520-
resp = await app_client.post(f"/collections/{coll.id}/items", json=first_item)
520+
resp = await app_client.post(f"/collections/{coll['id']}/items", json=first_item)
521521
assert resp.status_code == 201
522522

523523
datetime_range = "2020-01-01T00:00:00.00Z/.."
524524
resp = await app_client.get(
525-
f"/collections/{coll.id}/items", params={"datetime": datetime_range}
525+
f"/collections/{coll['id']}/items", params={"datetime": datetime_range}
526526
)
527527
assert resp.status_code == 200
528528
resp_json = resp.json()
529529
assert len(resp_json["features"]) == 1
530530

531531
datetime_range = "2018-01-01T00:00:00.00Z/2019-01-01T00:00:00.00Z"
532532
resp = await app_client.get(
533-
f"/collections/{coll.id}/items", params={"datetime": datetime_range}
533+
f"/collections/{coll['id']}/items", params={"datetime": datetime_range}
534534
)
535535
assert resp.status_code == 200
536536
resp_json = resp.json()
@@ -577,7 +577,7 @@ async def test_deleting_items_with_identical_ids(app_client):
577577

578578
@pytest.mark.parametrize("direction", ("asc", "desc"))
579579
async def test_sorting_and_paging(app_client, load_test_collection, direction: str):
580-
collection_id = load_test_collection.id
580+
collection_id = load_test_collection["id"]
581581
for i in range(10):
582582
item = Item(
583583
id=f"item-{i}",

0 commit comments

Comments
 (0)