1
1
import json
2
- import operator
3
2
import os
4
3
from copy import deepcopy
5
4
from typing import List , Optional , Type
6
5
7
- import arrow
8
6
import dictdiffer
9
7
import pytest
10
8
import requests
11
9
from click .testing import CliRunner
12
- from pydantic import BaseModel
10
+ from pydantic import BaseModel , TypeAdapter
11
+
12
+ from stac_pydantic .shared import UtcDatetime
13
13
14
14
15
15
def request (url : str , path : Optional [List [str ]] = None ):
@@ -30,6 +30,10 @@ def request(url: str, path: Optional[List[str]] = None):
30
30
return json .loads (full_file )
31
31
32
32
33
+ # Use a TypeAdapter to parse any datetime strings in a consistent manner
34
+ UtcDatetimeAdapter = TypeAdapter (UtcDatetime )
35
+
36
+
33
37
def dict_match (d1 : dict , d2 : dict ):
34
38
test = dictdiffer .diff (d1 , d2 )
35
39
for diff in test :
@@ -39,16 +43,17 @@ def dict_match(d1: dict, d2: dict):
39
43
# same for bbox
40
44
elif "bbox" in diff [1 ]:
41
45
assert list (diff [2 ][0 ]) == list (diff [2 ][1 ])
42
- # test data is pretty variable with how it represents datetime, RFC3339 is quite flexible
43
- # but stac-pydantic only supports a single datetime format, so just validate to the day.
46
+ # RFC3339 is quite flexible and the test data uses various options to represent datetimes.
47
+ # The datetime string stac-pydantic outputs may not be identical to the input. So we need
48
+ # to compare the values as datetime objects.
44
49
elif "datetime" in diff [1 ]:
45
- dates = []
46
- for date in diff [ 2 ]:
47
- if isinstance (date , str ):
48
- date = arrow . get ( date )
49
- dates . append ( date )
50
- dates . sort ( reverse = True )
51
- assert operator . sub ( * dates ). days == 0
50
+ dates = [
51
+ UtcDatetimeAdapter . validate_strings ( date )
52
+ if isinstance (date , str )
53
+ else date
54
+ for date in diff [ 2 ]
55
+ ]
56
+ assert dates [ 0 ] == dates [ 1 ]
52
57
# any other differences are errors
53
58
elif "stac_extensions" in diff [1 ]:
54
59
url1 , url2 = map (str , diff [2 ])
0 commit comments