Skip to content

Commit

Permalink
Tweak and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
eseglem committed Feb 16, 2024
1 parent af87bcf commit 28461bf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
10 changes: 4 additions & 6 deletions tests/api/test_search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
from datetime import datetime, timedelta, timezone

import pytest
Expand Down Expand Up @@ -56,7 +55,7 @@ def test_invalid_spatial_search():

def test_temporal_search_single_tailed():
# Test single tailed
utcnow = datetime.now(timezone.utc).replace(microsecond=0, tzinfo=timezone.utc)
utcnow = datetime.now(timezone.utc)
utcnow_str = utcnow.isoformat()
search = Search(collections=["collection1"], datetime=utcnow_str)
assert search.start_date is None
Expand All @@ -65,7 +64,7 @@ def test_temporal_search_single_tailed():

def test_temporal_search_two_tailed():
# Test two tailed
utcnow = datetime.now(timezone.utc).replace(microsecond=0, tzinfo=timezone.utc)
utcnow = datetime.now(timezone.utc)
utcnow_str = utcnow.isoformat()
search = Search(collections=["collection1"], datetime=f"{utcnow_str}/{utcnow_str}")
assert search.start_date == search.end_date == utcnow
Expand Down Expand Up @@ -108,12 +107,11 @@ def test_invalid_temporal_search_too_many():
def test_invalid_temporal_search_date_wrong_order():
# End date is before start date
start = datetime.now(timezone.utc)
time.sleep(2)
end = datetime.now(timezone.utc)
end = start - timedelta(seconds=100)
with pytest.raises(ValidationError):
Search(
collections=["collection1"],
datetime=f"{end.isoformat()}/{start.isoformat()}",
datetime=f"{start.isoformat()}/{end.isoformat()}",
)


Expand Down
31 changes: 31 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,37 @@ def test_api_context_extension() -> None:
ContextExtension(**context)


@pytest.mark.parametrize(
"args",
[
{"datetime": "2024-01-01T00:00:00Z"},
{
"datetime": None,
"start_datetime": "2024-01-01T00:00:00Z",
"end_datetime": "2024-01-02T00:00:00Z",
},
],
)
def test_item_properties_dates(args) -> None:
ItemProperties(**args)


@pytest.mark.parametrize(
"args",
[
{"datetime": None},
{"datetime": None, "start_datetime": "2024-01-01T00:00:00Z"},
{"datetime": None, "end_datetime": "2024-01-01T00:00:00Z"},
],
)
def test_item_properties_no_dates(args) -> None:
with pytest.raises(
ValueError,
match="start_datetime and end_datetime must be specified when datetime is null",
):
ItemProperties(**args)


def test_declared_model() -> None:
class TestProperties(ItemProperties):
foo: str
Expand Down

0 comments on commit 28461bf

Please sign in to comment.