From 73590e30d89e2c9f590a20f42c99fdc1b36ba480 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Mon, 18 Sep 2023 16:30:03 +0200 Subject: [PATCH] fixup! Issue #421/#68 drop "round up" feature for end_date for better consistency --- openeo/rest/datacube.py | 9 +- tests/rest/datacube/test_datacube.py | 122 ++++++++++----------------- 2 files changed, 48 insertions(+), 83 deletions(-) diff --git a/openeo/rest/datacube.py b/openeo/rest/datacube.py index 018389ca0..88bbe63ab 100644 --- a/openeo/rest/datacube.py +++ b/openeo/rest/datacube.py @@ -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: diff --git a/tests/rest/datacube/test_datacube.py b/tests/rest/datacube/test_datacube.py index 41b4b8a6f..2e9707525 100644 --- a/tests/rest/datacube/test_datacube.py +++ b/tests/rest/datacube/test_datacube.py @@ -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]), @@ -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) @@ -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", @@ -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 @@ -366,7 +338,7 @@ 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]), @@ -374,17 +346,17 @@ def test_filter_bbox_default_handling(s2cube, kwargs, expected): ((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() @@ -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