Skip to content

Commit

Permalink
Issue #401 add tests for current behavior of VectorCube.download
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Jul 14, 2023
1 parent fbf69e6 commit 35bd4eb
Showing 1 changed file with 147 additions and 0 deletions.
147 changes: 147 additions & 0 deletions tests/rest/datacube/test_vectorcube.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
from typing import List

import pytest

from openeo.internal.graph_building import PGNode
from openeo.rest.vectorcube import VectorCube


@pytest.fixture
def vector_cube(con100) -> VectorCube:
pgnode = PGNode(process_id="create_vector_cube")
return VectorCube(graph=pgnode, connection=con100)


class DownloadSpy:
"""
Test helper to track download requests and optionally override next response to return.
"""

__slots__ = ["requests", "next_response"]

def __init__(self):
self.requests: List[dict] = []
self.next_response: bytes = b"Spy data"

@property
def only_request(self) -> dict:
"""Get progress graph of only request done"""
assert len(self.requests) == 1
return self.requests[-1]

@property
def last_request(self) -> dict:
"""Get last progress graph"""
assert len(self.requests) > 0
return self.requests[-1]


@pytest.fixture
def download_spy(requests_mock, con100) -> DownloadSpy:
"""Test fixture to spy on (and mock) `POST /result` (download) requests."""
spy = DownloadSpy()

def post_result(request, context):
pg = request.json()["process"]["process_graph"]
spy.requests.append(pg)
return spy.next_response

requests_mock.post(con100.build_url("/result"), content=post_result)
yield spy


def test_raster_to_vector(con100):
img = con100.load_collection("S2")
vector_cube = img.raster_to_vector()
Expand Down Expand Up @@ -27,3 +79,98 @@ def test_raster_to_vector(con100):
'process_id': 'run_udf',
'result': True}
}


@pytest.mark.parametrize(
["filename", "expected_format"],
[
("result.json", "GeoJSON"), # TODO #401 possible to detect "GeoJSON from ".json" extension?
("result.geojson", "GeoJSON"),
("result.nc", "GeoJSON"), # TODO #401 autodetect format from filename
],
)
def test_download_auto_save_result_only_file(vector_cube, download_spy, tmp_path, filename, expected_format):
vector_cube.download(tmp_path / filename)

assert download_spy.only_request == {
"createvectorcube1": {"process_id": "create_vector_cube", "arguments": {}},
"saveresult1": {
"process_id": "save_result",
"arguments": {
"data": {"from_node": "createvectorcube1"},
"format": expected_format,
"options": {},
},
"result": True,
},
}
assert (tmp_path / filename).read_bytes() == b"Spy data"


@pytest.mark.parametrize(
["filename", "format", "expected_format"],
[
("result.json", "JSON", "JSON"),
("result.geojson", "GeoJSON", "GeoJSON"),
("result.nc", "netCDF", "netCDF"),
# TODO #401 more formats to autodetect?
("result.nc", "NETcDf", "NETcDf"), # TODO #401 normalize format
("result.nc", "inV6l1d!!!", "inV6l1d!!!"), # TODO #401 this should fail
("result.json", None, None), # TODO #401 autodetect format from filename
("result.nc", None, None), # TODO #401 autodetect format from filename
],
)
def test_download_auto_save_result_with_format(vector_cube, download_spy, tmp_path, filename, format, expected_format):
vector_cube.download(tmp_path / filename, format=format)

assert download_spy.only_request == {
"createvectorcube1": {"process_id": "create_vector_cube", "arguments": {}},
"saveresult1": {
"process_id": "save_result",
"arguments": {
"data": {"from_node": "createvectorcube1"},
"format": expected_format,
"options": {},
},
"result": True,
},
}
assert (tmp_path / filename).read_bytes() == b"Spy data"


def test_download_auto_save_result_with_options(vector_cube, download_spy, tmp_path):
vector_cube.download(tmp_path / "result.json", format="GeoJSON", options={"precision": 7})

assert download_spy.only_request == {
"createvectorcube1": {"process_id": "create_vector_cube", "arguments": {}},
"saveresult1": {
"process_id": "save_result",
"arguments": {
"data": {"from_node": "createvectorcube1"},
"format": "GeoJSON",
"options": {"precision": 7},
},
"result": True,
},
}
assert (tmp_path / "result.json").read_bytes() == b"Spy data"


def test_save_result_and_download(vector_cube, download_spy, tmp_path):
"""e.g. https://github.com/Open-EO/openeo-geopyspark-driver/issues/477"""
vector_cube = vector_cube.save_result(format="JSON")
vector_cube.download(tmp_path / "result.json")
# TODO #401 there should only be one save_result node
assert download_spy.only_request == {
"createvectorcube1": {"process_id": "create_vector_cube", "arguments": {}},
"saveresult1": {
"process_id": "save_result",
"arguments": {"data": {"from_node": "createvectorcube1"}, "format": "JSON", "options": {}},
},
"saveresult2": {
"process_id": "save_result",
"arguments": {"data": {"from_node": "saveresult1"}, "format": "GeoJSON", "options": {}},
"result": True,
},
}
assert (tmp_path / "result.json").read_bytes() == b"Spy data"

0 comments on commit 35bd4eb

Please sign in to comment.