7
7
from random import randint
8
8
from urllib .parse import parse_qs , urlparse , urlsplit
9
9
10
+ import ciso8601
10
11
import pystac
11
12
import pytest
12
13
from geojson_pydantic .geometries import Polygon
13
- from stac_pydantic . shared import DATETIME_RFC339
14
+ from pystac . utils import datetime_to_str
14
15
15
16
from stac_fastapi .elasticsearch .core import CoreCrudClient
17
+ from stac_fastapi .elasticsearch .datetime_utils import now_to_rfc3339_str
16
18
from stac_fastapi .types .core import LandingPageMixin
17
19
18
20
21
+ def rfc3339_str_to_datetime (s : str ) -> datetime :
22
+ return ciso8601 .parse_rfc3339 (s )
23
+
24
+
19
25
@pytest .mark .skip (reason = "unknown" )
20
26
def test_create_and_delete_item (app_client , load_test_data ):
21
27
"""Test creation and deletion of a single item (transactions extension)"""
@@ -275,7 +281,7 @@ def test_pagination(app_client, load_test_data):
275
281
def test_item_timestamps (app_client , load_test_data ):
276
282
"""Test created and updated timestamps (common metadata)"""
277
283
test_item = load_test_data ("test_item.json" )
278
- start_time = datetime . utcnow (). strftime ( DATETIME_RFC339 )
284
+ start_time = now_to_rfc3339_str ( )
279
285
time .sleep (1 )
280
286
# Confirm `created` timestamp
281
287
resp = app_client .post (
@@ -285,9 +291,7 @@ def test_item_timestamps(app_client, load_test_data):
285
291
created_dt = item ["properties" ]["created" ]
286
292
time .sleep (1 )
287
293
assert resp .status_code == 200
288
- assert (
289
- str (start_time ) < created_dt < str (datetime .utcnow ().strftime (DATETIME_RFC339 ))
290
- )
294
+ assert start_time < created_dt < now_to_rfc3339_str ()
291
295
292
296
time .sleep (1 )
293
297
# Confirm `updated` timestamp
@@ -364,13 +368,13 @@ def test_item_search_temporal_query_post(app_client, load_test_data):
364
368
)
365
369
assert resp .status_code == 200
366
370
367
- item_date = datetime . strptime (test_item ["properties" ]["datetime" ], DATETIME_RFC339 )
371
+ item_date = rfc3339_str_to_datetime (test_item ["properties" ]["datetime" ])
368
372
item_date = item_date + timedelta (seconds = 1 )
369
373
370
374
params = {
371
375
"collections" : [test_item ["collection" ]],
372
376
"intersects" : test_item ["geometry" ],
373
- "datetime" : f"../{ item_date . strftime ( DATETIME_RFC339 )} " ,
377
+ "datetime" : f"../{ datetime_to_str ( item_date )} " ,
374
378
}
375
379
resp = app_client .post ("/search" , json = params )
376
380
resp_json = resp .json ()
@@ -391,14 +395,14 @@ def test_item_search_temporal_window_post(app_client, load_test_data):
391
395
)
392
396
assert resp .status_code == 200
393
397
394
- item_date = datetime . strptime (test_item ["properties" ]["datetime" ], DATETIME_RFC339 )
398
+ item_date = rfc3339_str_to_datetime (test_item ["properties" ]["datetime" ])
395
399
item_date_before = item_date - timedelta (seconds = 1 )
396
400
item_date_after = item_date + timedelta (seconds = 1 )
397
401
398
402
params = {
399
403
"collections" : [test_item ["collection" ]],
400
404
"intersects" : test_item ["geometry" ],
401
- "datetime" : f"{ item_date_before . strftime ( DATETIME_RFC339 )} /{ item_date_after . strftime ( DATETIME_RFC339 )} " ,
405
+ "datetime" : f"{ datetime_to_str ( item_date_before )} /{ datetime_to_str ( item_date_after )} " ,
402
406
}
403
407
resp = app_client .post ("/search" , json = params )
404
408
resp_json = resp .json ()
@@ -438,7 +442,7 @@ def test_item_search_temporal_open_window(app_client, load_test_data):
438
442
def test_item_search_sort_post (app_client , load_test_data ):
439
443
"""Test POST search with sorting (sort extension)"""
440
444
first_item = load_test_data ("test_item.json" )
441
- item_date = datetime . strptime (first_item ["properties" ]["datetime" ], DATETIME_RFC339 )
445
+ item_date = rfc3339_str_to_datetime (first_item ["properties" ]["datetime" ])
442
446
resp = app_client .post (
443
447
f"/collections/{ first_item ['collection' ]} /items" , json = first_item
444
448
)
@@ -447,7 +451,7 @@ def test_item_search_sort_post(app_client, load_test_data):
447
451
second_item = load_test_data ("test_item.json" )
448
452
second_item ["id" ] = "another-item"
449
453
another_item_date = item_date - timedelta (days = 1 )
450
- second_item ["properties" ]["datetime" ] = another_item_date . strftime ( DATETIME_RFC339 )
454
+ second_item ["properties" ]["datetime" ] = datetime_to_str ( another_item_date )
451
455
resp = app_client .post (
452
456
f"/collections/{ second_item ['collection' ]} /items" , json = second_item
453
457
)
@@ -542,14 +546,14 @@ def test_item_search_temporal_window_get(app_client, load_test_data):
542
546
)
543
547
assert resp .status_code == 200
544
548
545
- item_date = datetime . strptime (test_item ["properties" ]["datetime" ], DATETIME_RFC339 )
549
+ item_date = rfc3339_str_to_datetime (test_item ["properties" ]["datetime" ])
546
550
item_date_before = item_date - timedelta (seconds = 1 )
547
551
item_date_after = item_date + timedelta (seconds = 1 )
548
552
549
553
params = {
550
554
"collections" : test_item ["collection" ],
551
555
"bbox" : "," .join ([str (coord ) for coord in test_item ["bbox" ]]),
552
- "datetime" : f"{ item_date_before . strftime ( DATETIME_RFC339 )} /{ item_date_after . strftime ( DATETIME_RFC339 )} " ,
556
+ "datetime" : f"{ datetime_to_str ( item_date_before )} /{ datetime_to_str ( item_date_after )} " ,
553
557
}
554
558
resp = app_client .get ("/search" , params = params )
555
559
resp_json = resp .json ()
@@ -565,7 +569,7 @@ def test_item_search_temporal_window_get(app_client, load_test_data):
565
569
def test_item_search_sort_get (app_client , load_test_data ):
566
570
"""Test GET search with sorting (sort extension)"""
567
571
first_item = load_test_data ("test_item.json" )
568
- item_date = datetime . strptime (first_item ["properties" ]["datetime" ], DATETIME_RFC339 )
572
+ item_date = rfc3339_str_to_datetime (first_item ["properties" ]["datetime" ])
569
573
resp = app_client .post (
570
574
f"/collections/{ first_item ['collection' ]} /items" , json = first_item
571
575
)
@@ -574,7 +578,7 @@ def test_item_search_sort_get(app_client, load_test_data):
574
578
second_item = load_test_data ("test_item.json" )
575
579
second_item ["id" ] = "another-item"
576
580
another_item_date = item_date - timedelta (days = 1 )
577
- second_item ["properties" ]["datetime" ] = another_item_date . strftime ( DATETIME_RFC339 )
581
+ second_item ["properties" ]["datetime" ] = datetime_to_str ( another_item_date )
578
582
resp = app_client .post (
579
583
f"/collections/{ second_item ['collection' ]} /items" , json = second_item
580
584
)
0 commit comments