Skip to content

Commit d2fd4a6

Browse files
Make str_to_interval not return a tuple for single-value input (#692)
* Do not return tuple for single-value input * Add PR reference to changelog * update from main --------- Co-authored-by: vincentsarago <[email protected]>
1 parent 5a4d5b9 commit d2fd4a6

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

CHANGES.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
* Switch from `fastapi` to `fastapi-slim` to avoid installing unwanted dependencies. ([#687](https://github.com/stac-utils/stac-fastapi/pull/687))
88
* Replace Enum with `Literal` for `FilterLang`. ([#686](https://github.com/stac-utils/stac-fastapi/pull/686))
99
* Update stac-pydantic requirement to `~3.1` ([#697](https://github.com/stac-utils/stac-fastapi/pull/697))
10-
* Fix datetime interval for GET Search when passing a single value ([#697](https://github.com/stac-utils/stac-fastapi/pull/697))
1110

1211
### Removed
1312

1413
* Pystac as it was just used for a datetime to string function. ([#690](https://github.com/stac-utils/stac-fastapi/pull/690))
1514

15+
### Fixed
16+
17+
* Make `str_to_interval` not return a tuple for single-value input (fixing `datetime` argument as passed to `get_search`). ([#692](https://github.com/stac-utils/stac-fastapi/pull/692))
18+
1619
## [3.0.0a0] - 2024-05-06
1720

1821
### Added

stac_fastapi/types/stac_fastapi/types/rfc3339.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,17 @@ def parse_single_date(date_str: str) -> datetime:
9494

9595
def str_to_interval(interval: Optional[str]) -> Optional[DateTimeType]:
9696
"""
97-
Extract a tuple of datetime objects from an interval string defined by the OGC API.
98-
The interval can either be a single datetime or a range with start and end datetime.
97+
Extract a single datetime object or a tuple of datetime objects from an
98+
interval string defined by the OGC API. The interval can either be a
99+
single datetime or a range with start and end datetime.
99100
100101
Args:
101102
interval (Optional[str]): The interval string to convert to datetime objects,
102103
or None if no datetime is specified.
103104
104105
Returns:
105-
Optional[DateTimeType]: A tuple of datetime.datetime objects or
106-
None if input is None.
106+
Optional[DateTimeType]: A single datetime.datetime object, a tuple of
107+
datetime.datetime objects, or None if input is None.
107108
108109
Raises:
109110
HTTPException: If the string is not valid for various reasons such as being empty,
@@ -122,11 +123,11 @@ def str_to_interval(interval: Optional[str]) -> Optional[DateTimeType]:
122123
detail="Interval string contains more than one forward slash.",
123124
)
124125

125-
if len(values) == 1:
126-
values = [values[0], values[0]]
127-
128126
try:
129127
start = parse_single_date(values[0]) if values[0] not in ["..", ""] else None
128+
if len(values) == 1:
129+
return start
130+
130131
end = (
131132
parse_single_date(values[1])
132133
if len(values) > 1 and values[1] not in ["..", ""]

stac_fastapi/types/tests/test_rfc3339.py

+30-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import timezone
1+
from datetime import datetime, timezone
22

33
import pytest
44
from fastapi import HTTPException
@@ -86,31 +86,49 @@ def test_parse_valid_str_to_datetime(test_input):
8686

8787

8888
@pytest.mark.parametrize("test_input", invalid_intervals)
89-
def test_parse_invalid_interval_to_datetime(test_input):
89+
def test_str_to_interval_with_invalid_interval(test_input):
9090
with pytest.raises(HTTPException) as exc_info:
9191
str_to_interval(test_input)
9292
assert (
9393
exc_info.value.status_code == 400
94-
), "Should return a 400 status code for invalid intervals"
94+
), "str_to_interval should return a 400 status code for invalid interval"
9595

9696

97-
@pytest.mark.parametrize("test_input", valid_intervals)
98-
def test_parse_valid_interval_to_datetime(test_input):
99-
assert str_to_interval(test_input)
97+
@pytest.mark.parametrize("test_input", invalid_datetimes)
98+
def test_str_to_interval_with_invalid_datetime(test_input):
99+
with pytest.raises(HTTPException) as exc_info:
100+
str_to_interval(test_input)
101+
assert (
102+
exc_info.value.status_code == 400
103+
), "str_to_interval should return a 400 status code for invalid datetime"
100104

101105

102-
def test_now_functions() -> None:
103-
now1 = now_in_utc()
104-
now2 = now_in_utc()
106+
@pytest.mark.parametrize("test_input", valid_intervals)
107+
def test_str_to_interval_with_valid_interval(test_input):
108+
assert isinstance(
109+
str_to_interval(test_input), tuple
110+
), "str_to_interval should return tuple for multi-value input"
105111

106-
assert now1 < now2
107-
assert now1.tzinfo == timezone.utc
108112

109-
rfc3339_str_to_datetime(now_to_rfc3339_str())
113+
@pytest.mark.parametrize("test_input", valid_datetimes)
114+
def test_str_to_interval_with_valid_datetime(test_input):
115+
assert isinstance(
116+
str_to_interval(test_input), datetime
117+
), "str_to_interval should return single datetime for single-value input"
110118

111119

112120
def test_str_to_interval_with_none():
113121
"""Test that str_to_interval returns None when provided with None."""
114122
assert (
115123
str_to_interval(None) is None
116124
), "str_to_interval should return None when input is None"
125+
126+
127+
def test_now_functions() -> None:
128+
now1 = now_in_utc()
129+
now2 = now_in_utc()
130+
131+
assert now1 < now2
132+
assert now1.tzinfo == timezone.utc
133+
134+
rfc3339_str_to_datetime(now_to_rfc3339_str())

0 commit comments

Comments
 (0)