Skip to content

Commit

Permalink
fixup! Issue #421/#68 drop "round up" feature for end_date for better…
Browse files Browse the repository at this point in the history
… consistency
  • Loading branch information
soxofaan committed Sep 18, 2023
1 parent 93a0985 commit 73590e3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 83 deletions.
9 changes: 5 additions & 4 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ def convertor(d: Any) -> Any:

@openeo_process
def filter_temporal(
self, *args,
start_date: Union[str, datetime.datetime, datetime.date] = None,
end_date: Union[str, datetime.datetime, datetime.date] = None,
extent: Union[list, tuple] = None
self,
*args,
start_date: Union[str, datetime.datetime, datetime.date] = None,
end_date: Union[str, datetime.datetime, datetime.date] = None,
extent: Union[list, tuple, str] = None,
) -> DataCube:
"""
Limit the DataCube to a certain date range, which can be specified in several ways:
Expand Down
122 changes: 43 additions & 79 deletions tests/rest/datacube/test_datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,27 @@ def _get_leaf_node(cube, force_flat=True) -> dict:
raise ValueError(repr(cube))



def test_filter_temporal(s2cube):
def test_filter_temporal_basic_positional_args(s2cube):
im = s2cube.filter_temporal("2016-01-01", "2016-03-10")
graph = _get_leaf_node(im)
assert graph['process_id'] == 'filter_temporal'
assert graph['arguments']['extent'] == ["2016-01-01", "2016-03-10"]


def test_filter_temporal_start_end(s2cube):
def test_filter_temporal_basic_start_end(s2cube):
im = s2cube.filter_temporal(start_date="2016-01-01", end_date="2016-03-10")
graph = _get_leaf_node(im)
assert graph['process_id'] == 'filter_temporal'
assert graph['arguments']['extent'] == ["2016-01-01", "2016-03-10"]


def test_filter_temporal_extent(s2cube):
def test_filter_temporal_basic_extent(s2cube):
im = s2cube.filter_temporal(extent=("2016-01-01", "2016-03-10"))
graph = _get_leaf_node(im)
assert graph['process_id'] == 'filter_temporal'
assert graph['arguments']['extent'] == ["2016-01-01", "2016-03-10"]


@pytest.mark.parametrize("args,kwargs,extent", [
((), {}, [None, None]),
(("2016-01-01",), {}, ["2016-01-01", None]),
Expand Down Expand Up @@ -124,27 +124,17 @@ def test_filter_temporal_generic(s2cube, args, kwargs, extent):
@pytest.mark.parametrize(
["extent", "expected"],
[
(["2016", None], ["2016-01-01", "2017-01-01"]),
(["2016-01", None], ["2016-01-01", "2016-02-01"]),
(["2016-04", None], ["2016-04-01", "2016-05-01"]),
(["2016-12", None], ["2016-12-01", "2017-01-01"]),
(["2016", "2016"], ["2016-01-01", "2017-01-01"]),
(["2016", "2017"], ["2016-01-01", "2018-01-01"]),
(["2016", "2023"], ["2016-01-01", "2024-01-01"]),
(["2016-01", "2016-01"], ["2016-01-01", "2016-02-01"]),
(["2016-03", "2016-04"], ["2016-03-01", "2016-05-01"]),
(["2016-03", "2016-12"], ["2016-03-01", "2017-01-01"]),
(["2016-03", "2018-05"], ["2016-03-01", "2018-06-01"]),
(["2016-03", "2018-12"], ["2016-03-01", "2019-01-01"]),
(["2016", "2016-01"], ["2016-01-01", "2016-02-01"]),
(["2016", "2016-04"], ["2016-01-01", "2016-05-01"]),
(["2016", "2016-12"], ["2016-01-01", "2017-01-01"]),
([None, "2016-01"], [None, "2016-02-01"]),
([None, "2016-04"], [None, "2016-05-01"]),
([None, "2016-12"], [None, "2017-01-01"]),
(["2016", None], ["2016-01-01", None]),
(["2016-03", None], ["2016-03-01", None]),
(["2016", "2017"], ["2016-01-01", "2017-01-01"]),
(["2016-03", "2016-04"], ["2016-03-01", "2016-04-01"]),
(["2016-03", "2018-12"], ["2016-03-01", "2018-12-01"]),
(["2016", "2016-01"], ["2016-01-01", "2016-01-01"]),
(["2016", "2016-04"], ["2016-01-01", "2016-04-01"]),
([None, "2016-04"], [None, "2016-04-01"]),
],
)
def test_filter_temporal_extent_with_shorthand_date(s2cube: DataCube, extent, expected):
def test_filter_temporal_extent_tuple_with_shorthand_dates(s2cube: DataCube, extent, expected):
"""Verify it supports shorthand dates that represent years or months."""
im = s2cube.filter_temporal(extent=extent)
graph = _get_leaf_node(im)
Expand All @@ -153,38 +143,23 @@ def test_filter_temporal_extent_with_shorthand_date(s2cube: DataCube, extent, ex


@pytest.mark.parametrize(
["start_date", "expected"],
["extent", "expected"],
[
("2016", ["2016-01-01", "2017-01-01"]),
("2016-01", ["2016-01-01", "2016-02-01"]),
("2016-04", ["2016-04-01", "2016-05-01"]),
("2016-12", ["2016-12-01", "2017-01-01"]),
("2016-04-11", ["2016-12-01", "2017-01-01"]),
],
)
def test_filter_temporal_extent_with_shorthand_date_as_single_positional_arg(s2cube: DataCube, start_date, expected):
"""Verify it supports the shortest way to define the extent: as a single positional arg."""
im = s2cube.filter_temporal(start_date)
def test_filter_temporal_extent_single_date_string(s2cube: DataCube, extent, expected):
"""Verify it supports single string extent."""
im = s2cube.filter_temporal(extent=extent)
graph = _get_leaf_node(im)
assert graph["process_id"] == "filter_temporal"
assert graph["arguments"]["extent"] == expected


@pytest.mark.parametrize(
["start_date", "expected"],
[
("2016", ["2016-01-01", "2017-01-01"]),
("2016-01", ["2016-01-01", "2016-02-01"]),
("2016-04", ["2016-04-01", "2016-05-01"]),
("2016-12", ["2016-12-01", "2017-01-01"]),
],
)
def test_filter_temporal_extent_with_shorthand_date_as_start_date(s2cube: DataCube, start_date, expected):
"""Verify it supports shorthand dates via the keyword argument start_date."""
im = s2cube.filter_temporal(start_date=start_date)
graph = _get_leaf_node(im)
assert graph["process_id"] == "filter_temporal"
assert graph["arguments"]["extent"] == expected


@pytest.mark.parametrize(
"extent,expected",
Expand All @@ -207,21 +182,18 @@ def test_load_collection_filter_temporal(connection, api_version, extent, expect
# Test that the simplest/shortest syntax works: temporal_extent="2016"
("2016", ["2016-01-01", "2017-01-01"]),
("2016-02", ["2016-02-01", "2016-03-01"]),
("2016-02-03", ["2016-02-03", "2016-03-04"]),
# Test date abbreviations using tuples for the extent
(["2016", None], ["2016-01-01", "2017-01-01"]),
(["2016-02", None], ["2016-02-01", "2016-03-01"]),
(["2016", "2016"], ["2016-01-01", "2017-01-01"]),
(["2016-02", "2016-02"], ["2016-02-01", "2016-03-01"]),
(["2016", "2017"], ["2016-01-01", "2018-01-01"]),
(["2016-02", "2016-08"], ["2016-02-01", "2016-09-01"]),
([None, "2016"], [None, "2017-01-01"]),
([None, "2016-02"], [None, "2016-03-01"]),
(["2016", None], ["2016-01-01", None]),
(["2016-02", None], ["2016-02-01", None]),
(["2016", "2017"], ["2016-01-01", "2017-01-01"]),
(["2016-02", "2016-08"], ["2016-02-01", "2016-08-01"]),
([None, "2016"], [None, "2016-01-01"]),
([None, "2016-02"], [None, "2016-02-01"]),
],
)
def test_load_collection_filter_temporal_with_shorthand_date_string(connection, api_version, extent, expected):
"""Verify it supports abbreviated date strings.
For example: temporal_extent="2016"
"""
def test_load_collection_temporal_extent_with_shorthand_date_strings(connection, api_version, extent, expected):
"""Verify it supports abbreviated date strings."""
cube: DataCube = connection.load_collection("S2", temporal_extent=extent)
flat_graph = cube.flat_graph()
assert flat_graph["loadcollection1"]["arguments"]["temporal_extent"] == expected
Expand Down Expand Up @@ -366,25 +338,25 @@ def test_filter_bbox_default_handling(s2cube, kwargs, expected):


@pytest.mark.parametrize(
"extent,expected",
["extent", "expected"],
[
# test regular extents
(("2016-01-01", None), ["2016-01-01", None]),
(("2016-01-01", "2016-03-10"), ["2016-01-01", "2016-03-10"]),
((None, "2016-03-10"), [None, "2016-03-10"]),
(["2016-01-01", None], ["2016-01-01", None]),
# test the date abbreviations
(["2016", None], ["2016-01-01", "2017-01-01"]),
(["2016-02", None], ["2016-02-01", "2016-03-01"]),
(["2016", "2016"], ["2016-01-01", "2017-01-01"]),
(["2016-02", "2016-02"], ["2016-02-01", "2016-03-01"]),
(["2016", "2017"], ["2016-01-01", "2018-01-01"]),
(["2016-02", "2016-08"], ["2016-02-01", "2016-09-01"]),
([None, "2016"], [None, "2017-01-01"]),
([None, "2016-02"], [None, "2016-03-01"]),
(["2016", None], ["2016-01-01", None]),
(["2016-02", None], ["2016-02-01", None]),
(["2016", "2017"], ["2016-01-01", "2017-01-01"]),
(["2016-02", "2016-02"], ["2016-02-01", "2016-02-01"]),
(["2016", "2017"], ["2016-01-01", "2017-01-01"]),
(["2016-02", "2016-08"], ["2016-02-01", "2016-08-01"]),
([None, "2016"], [None, "2016-01-01"]),
([None, "2016-02"], [None, "2016-02-01"]),
],
)
def test_filter_temporal(s2cube: DataCube, api_version, extent, expected):
def test_filter_temporal_general(s2cube: DataCube, api_version, extent, expected):
# First test it via positional args
cube_pos_args: DataCube = s2cube.filter_temporal(extent[0], extent[1])
flat_graph_pos_args = cube_pos_args.flat_graph()
Expand All @@ -402,25 +374,17 @@ def test_filter_temporal(s2cube: DataCube, api_version, extent, expected):


@pytest.mark.parametrize(
"extent,expected",
["extent", "expected"],
[
("2016", ["2016-01-01", "2017-01-01"]),
("2016-02", ["2016-02-01", "2016-03-01"]),
("2016-02-03", ["2016-02-03", None]),
("2016-12", ["2016-12-01", "2017-01-01"]),
("2016-03-04", ["2016-03-04", "2016-03-05"]),
],
)
def test_filter_temporal_with_one_abbreviated_date_string(s2cube: DataCube, api_version, extent, expected):
"""It should support the simplest way to provide a shorthand date string for a year or month, without a tuple."""

# 1) It must work with just one positional argument, which isn't a tuple but a string
cube_pos_arg: DataCube = s2cube.filter_temporal(extent)
flat_graph_pos_arg = cube_pos_arg.flat_graph()
assert flat_graph_pos_arg["filtertemporal1"]["arguments"]["extent"] == expected

# 2) It must also work with just the keyword argument `extent` when extent
# receives a string instead of a tuple.
flat_graph_extent: DataCube = s2cube.filter_temporal(extent=extent)
flat_graph_extent = flat_graph_extent.flat_graph()
def test_filter_temporal_extent_single_date_string(s2cube: DataCube, api_version, extent, expected):
cube_extent: DataCube = s2cube.filter_temporal(extent=extent)
flat_graph_extent = cube_extent.flat_graph()
assert flat_graph_extent["filtertemporal1"]["arguments"]["extent"] == expected


Expand Down

0 comments on commit 73590e3

Please sign in to comment.