Skip to content

Commit

Permalink
Issue #678/#682 finetune TestDataCube tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Jan 17, 2025
1 parent dda8bcb commit e1fa369
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 24 deletions.
2 changes: 1 addition & 1 deletion openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def load_disk_collection(cls, connection: Connection, file_format: str, glob_pat
def load_stac(
cls,
url: str,
spatial_extent: Union[Dict[str, float], Parameter, None] = None,
spatial_extent: Union[dict, Parameter, shapely.geometry.base.BaseGeometry, str, pathlib.Path, None] = None,
temporal_extent: Union[Sequence[InputDate], Parameter, str, None] = None,
bands: Union[Iterable[str], Parameter, str, None] = None,
properties: Optional[Dict[str, Union[str, PGNode, Callable]]] = None,
Expand Down
140 changes: 122 additions & 18 deletions tests/rest/datacube/test_datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,31 +137,70 @@ def test_load_collection_connectionless_temporal_extent_shortcut(self):
}
}

def test_load_collection_connectionless_shapely_spatial_extent(self):
polygon = shapely.geometry.Polygon(((0.0, 1.0), (2.0, 1.0), (3.0, 2.0), (1.5, 0.0), (0.0, 1.0)))
cube = DataCube.load_collection("T3", spatial_extent=polygon)
assert cube.flat_graph() == {
"loadcollection1": {
"arguments": {"id": "T3", "spatial_extent":
{'coordinates': (((0.0,1.0),(2.0,1.0),(3.0,2.0),(1.5,0.0),(0.0,1.0)),),'type': 'Polygon'},
"temporal_extent": None},
"process_id": "load_collection",
"result": True,
}
def test_load_collection_spatial_extent_bbox(self, dummy_backend):
spatial_extent = {"west": 1, "south": 2, "east": 3, "north": 4}
cube = DataCube.load_collection("S2", spatial_extent=spatial_extent, connection=dummy_backend.connection)
cube.execute()
assert dummy_backend.get_sync_pg()["loadcollection1"]["arguments"] == {
"id": "S2",
"spatial_extent": {"west": 1, "south": 2, "east": 3, "north": 4},
"temporal_extent": None,
}

def test_load_collection_spatial_extent_shapely(self, dummy_backend):
polygon = shapely.geometry.Polygon([(3, 51), (4, 51), (4, 52), (3, 52)])
cube = DataCube.load_collection("S2", spatial_extent=polygon, connection=dummy_backend.connection)
cube.execute()
assert dummy_backend.get_sync_pg()["loadcollection1"]["arguments"] == {
"id": "S2",
"spatial_extent": {
"type": "Polygon",
"coordinates": [[[3, 51], [4, 51], [4, 52], [3, 52], [3, 51]]],
},
"temporal_extent": None,
}

@pytest.mark.parametrize("path_factory", [str, pathlib.Path])
def test_load_collection_connectionless_local_path_spatial_extent(self, path_factory, test_data):
def test_load_collection_spatial_extent_local_path(self, dummy_backend, path_factory, test_data):
path = path_factory(test_data.get_path("geojson/polygon02.json"))
cube = DataCube.load_collection("T3", spatial_extent=path)
assert cube.flat_graph() == {
cube = DataCube.load_collection("S2", spatial_extent=path, connection=dummy_backend.connection)
cube.execute()
assert dummy_backend.get_sync_pg()["loadcollection1"]["arguments"] == {
"id": "S2",
"spatial_extent": {"type": "Polygon", "coordinates": [[[3, 50], [4, 50], [4, 51], [3, 50]]]},
"temporal_extent": None,
}

def test_load_collection_spatial_extent_url(self, dummy_backend):
cube = DataCube.load_collection(
"S2", spatial_extent="https://geo.test/geometry.json", connection=dummy_backend.connection
)
cube.execute()
assert dummy_backend.get_sync_pg() == {
"loadurl1": {
"process_id": "load_url",
"arguments": {"format": "GeoJSON", "url": "https://geo.test/geometry.json"},
},
"loadcollection1": {
"arguments": {"id": "T3", "spatial_extent":
{"type": "Polygon", "coordinates": [[[3, 50], [4, 50], [4, 51], [3, 50]]]},
"temporal_extent": None},
"process_id": "load_collection",
"arguments": {
"id": "S2",
"spatial_extent": {"from_node": "loadurl1"},
"temporal_extent": None,
},
"result": True,
}
},
}

def test_load_collection_spatial_extent_parameter(self, dummy_backend):
cube = DataCube.load_collection(
"S2", spatial_extent=Parameter.geojson("zpatial_extent"), connection=dummy_backend.connection
)
cube.execute()
assert dummy_backend.get_sync_pg()["loadcollection1"]["arguments"] == {
"id": "S2",
"spatial_extent": {"from_parameter": "zpatial_extent"},
"temporal_extent": None,
}

def test_load_collection_connectionless_save_result(self):
Expand Down Expand Up @@ -206,6 +245,71 @@ def test_load_stac_connectionless_save_result(self):
},
}

def test_load_stac_spatial_extent_bbox(self, dummy_backend):
spatial_extent = {"west": 1, "south": 2, "east": 3, "north": 4}
cube = DataCube.load_stac(
"https://stac.test/data", spatial_extent=spatial_extent, connection=dummy_backend.connection
)
cube.execute()
assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == {
"url": "https://stac.test/data",
"spatial_extent": {"west": 1, "south": 2, "east": 3, "north": 4},
}

def test_load_stac_spatial_extent_shapely(self, dummy_backend):
polygon = shapely.geometry.Polygon([(3, 51), (4, 51), (4, 52), (3, 52)])
cube = DataCube.load_stac("https://stac.test/data", spatial_extent=polygon, connection=dummy_backend.connection)
cube.execute()
assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == {
"url": "https://stac.test/data",
"spatial_extent": {
"type": "Polygon",
"coordinates": [[[3, 51], [4, 51], [4, 52], [3, 52], [3, 51]]],
},
}

@pytest.mark.parametrize("path_factory", [str, pathlib.Path])
def test_load_stac_spatial_extent_local_path(self, dummy_backend, path_factory, test_data):
path = path_factory(test_data.get_path("geojson/polygon02.json"))
cube = DataCube.load_stac("https://stac.test/data", spatial_extent=path, connection=dummy_backend.connection)
cube.execute()
assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == {
"url": "https://stac.test/data",
"spatial_extent": {"type": "Polygon", "coordinates": [[[3, 50], [4, 50], [4, 51], [3, 50]]]},
}

def test_load_stac_spatial_extent_url(self, dummy_backend):
cube = DataCube.load_stac(
"https://stac.test/data",
spatial_extent="https://geo.test/geometry.json",
connection=dummy_backend.connection,
)
cube.execute()
assert dummy_backend.get_sync_pg() == {
"loadurl1": {
"process_id": "load_url",
"arguments": {"format": "GeoJSON", "url": "https://geo.test/geometry.json"},
},
"loadstac1": {
"process_id": "load_stac",
"arguments": {
"url": "https://stac.test/data",
"spatial_extent": {"from_node": "loadurl1"},
},
"result": True,
},
}

def test_load_stac_spatial_extent_parameter(self, dummy_backend):
spatial_extent = Parameter.geojson("zpatial_extent")
cube = DataCube.load_stac(
"https://stac.test/data", spatial_extent=spatial_extent, connection=dummy_backend.connection
)
cube.execute()
assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == {
"url": "https://stac.test/data",
"spatial_extent": {"from_parameter": "zpatial_extent"},
}

def test_filter_temporal_basic_positional_args(s2cube):
im = s2cube.filter_temporal("2016-01-01", "2016-03-10")
Expand Down
10 changes: 5 additions & 5 deletions tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2460,7 +2460,7 @@ def test_load_collection_spatial_extent_bbox(self, dummy_backend):
cube.execute()
assert dummy_backend.get_sync_pg()["loadcollection1"]["arguments"] == {
"id": "S2",
"spatial_extent": {"east": 3, "north": 4, "south": 2, "west": 1},
"spatial_extent": {"west": 1, "south": 2, "east": 3, "north": 4},
"temporal_extent": None,
}

Expand Down Expand Up @@ -2527,7 +2527,7 @@ def test_load_collection_spatial_extent_shapely_wrong_type(self, geojson, dummy_
],
)
@pytest.mark.parametrize("path_factory", [str, Path])
def test_load_collection_spatial_extent_path(self, geojson, dummy_backend, tmp_path, path_factory):
def test_load_collection_spatial_extent_local_path(self, geojson, dummy_backend, tmp_path, path_factory):
path = tmp_path / "geometry.json"
with path.open("w") as f:
json.dump(geojson, f)
Expand Down Expand Up @@ -2935,8 +2935,8 @@ def test_load_stac_spatial_extent_bbox(self, dummy_backend):
cube = dummy_backend.connection.load_stac("https://stac.test/data", spatial_extent=spatial_extent)
cube.execute()
assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == {
"spatial_extent": {"east": 3, "north": 4, "south": 2, "west": 1},
"url": "https://stac.test/data",
"spatial_extent": {"west": 1, "south": 2, "east": 3, "north": 4},
}

@pytest.mark.parametrize(
Expand All @@ -2953,8 +2953,8 @@ def test_load_stac_spatial_extent_geojson(self, dummy_backend, spatial_extent):
cube = dummy_backend.connection.load_stac("https://stac.test/data", spatial_extent=spatial_extent)
cube.execute()
assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == {
"spatial_extent": spatial_extent,
"url": "https://stac.test/data",
"spatial_extent": spatial_extent,
}

@pytest.mark.parametrize(
Expand Down Expand Up @@ -3008,7 +3008,7 @@ def test_load_stac_spatial_extent_shapely_wront_type(self, dummy_backend, geojso
],
)
@pytest.mark.parametrize("path_factory", [str, Path])
def test_load_stac_spatial_extent_path(self, geojson, dummy_backend, tmp_path, path_factory):
def test_load_stac_spatial_extent_local_path(self, geojson, dummy_backend, tmp_path, path_factory):
path = tmp_path / "geometry.json"
with path.open("w") as f:
json.dump(geojson, f)
Expand Down

0 comments on commit e1fa369

Please sign in to comment.