Skip to content

Commit 600b653

Browse files
confirm missing datetime when using pydantic serialization (#878)
* confirm missing datetime when using pydantic serialization * update lock file
1 parent 12b86fd commit 600b653

3 files changed

Lines changed: 299 additions & 166 deletions

File tree

‎stac_fastapi/api/tests/conftest.py‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ def _item():
5353
)
5454

5555

56+
@pytest.fixture
57+
def _item_start_end():
58+
return Item(
59+
id="test_item_datetime",
60+
type="Feature",
61+
geometry={"type": "Point", "coordinates": [0, 0]},
62+
bbox=[-180, -90, 180, 90],
63+
properties={
64+
"datetime": None,
65+
"start_datetime": "2000-01-01T00:00:00Z",
66+
"end_datetime": "2000-01-02T00:00:00Z",
67+
},
68+
links=item_links,
69+
assets={},
70+
)
71+
72+
5673
@pytest.fixture
5774
def item(_item: Item):
5875
return _item.model_dump_json()
@@ -119,6 +136,71 @@ def item_collection(
119136
return CoreClient
120137

121138

139+
@pytest.fixture
140+
def TestCoreClientDatetime(collection_dict, _item_start_end):
141+
class CoreClient(core.BaseCoreClient):
142+
def post_search(
143+
self, search_request: BaseSearchPostRequest, **kwargs
144+
) -> stac.ItemCollection:
145+
return stac.ItemCollection(
146+
type="FeatureCollection",
147+
features=[
148+
_item_start_end.model_dump(),
149+
],
150+
)
151+
152+
def get_search(
153+
self,
154+
collections: Optional[List[str]] = None,
155+
ids: Optional[List[str]] = None,
156+
bbox: Optional[List[NumType]] = None,
157+
intersects: Optional[str] = None,
158+
datetime: Optional[Union[str, datetime]] = None,
159+
limit: Optional[int] = 10,
160+
**kwargs,
161+
) -> stac.ItemCollection:
162+
return stac.ItemCollection(
163+
type="FeatureCollection",
164+
features=[
165+
_item_start_end.model_dump(),
166+
],
167+
)
168+
169+
def get_item(self, item_id: str, collection_id: str, **kwargs) -> stac.Item:
170+
return stac.Item(**_item_start_end.model_dump())
171+
172+
def all_collections(self, **kwargs) -> stac.Collections:
173+
return stac.Collections(
174+
collections=[stac.Collection(**collection_dict)],
175+
links=[
176+
{"href": "test", "rel": "root"},
177+
{"href": "test", "rel": "self"},
178+
{"href": "test", "rel": "parent"},
179+
],
180+
)
181+
182+
def get_collection(self, collection_id: str, **kwargs) -> stac.Collection:
183+
return stac.Collection(**collection_dict)
184+
185+
def item_collection(
186+
self,
187+
collection_id: str,
188+
bbox: Optional[List[Union[float, int]]] = None,
189+
datetime: Optional[Union[str, datetime]] = None,
190+
limit: int = 10,
191+
token: str = None,
192+
**kwargs,
193+
) -> stac.ItemCollection:
194+
return stac.ItemCollection(
195+
type="FeatureCollection",
196+
features=[
197+
_item_start_end.model_dump(),
198+
],
199+
)
200+
201+
return CoreClient
202+
203+
122204
@pytest.fixture
123205
def AsyncTestCoreClient(collection_dict, item_dict):
124206
class AsyncCoreClient(core.AsyncBaseCoreClient):

‎stac_fastapi/api/tests/test_app.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,3 +610,29 @@ async def async_health_check(request: Request):
610610
"version": "0.1.0",
611611
},
612612
}
613+
614+
615+
def test_client_datetime(TestCoreClientDatetime):
616+
"""Check valid items with missing datetime."""
617+
618+
test_app = app.StacApi(
619+
settings=ApiSettings(
620+
enable_direct_response=False,
621+
enable_response_models=False,
622+
),
623+
client=TestCoreClientDatetime(),
624+
)
625+
with TestClient(test_app.app) as client:
626+
item = client.get("/collections/test/items/test_item_datetime")
627+
assert "datetime" in item.json()["properties"]
628+
629+
test_app = app.StacApi(
630+
settings=ApiSettings(
631+
enable_direct_response=False,
632+
enable_response_models=True,
633+
),
634+
client=TestCoreClientDatetime(),
635+
)
636+
with TestClient(test_app.app) as client:
637+
item = client.get("/collections/test/items/test_item_datetime")
638+
assert "datetime" in item.json()["properties"]

0 commit comments

Comments
 (0)