Skip to content

Commit

Permalink
issue #693 add mock for sar_backscatter test for processes
Browse files Browse the repository at this point in the history
  • Loading branch information
ElienVandermaesenVITO committed Jan 9, 2025
1 parent b506a11 commit 71b2b79
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 31 deletions.
19 changes: 0 additions & 19 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2105,22 +2105,3 @@ def extract_connections(
connections.add(item.connection)

return connections


def search_list_for_dict_key(lst: list, key: str) -> Union[dict, list]:
"""
Searches a value of the dict that matches with the key in the list.
:param lst: list with dictionaries.
:param key: The key for which the value is searched for.
:return: value that matches key.
"""
result = None
for item in lst:
if key in item:
if result is None:
result = item[key]
else:
raise OpenEoClientException("Multiple keys found with value {v}.".format(v=key))
return result
20 changes: 12 additions & 8 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,20 @@
from openeo.rest.service import Service
from openeo.rest.udp import RESTUserDefinedProcess
from openeo.rest.vectorcube import VectorCube
from openeo.util import dict_no_none, guess_format, load_json, normalize_crs, rfc3339
from openeo.util import (
dict_no_none,
guess_format,
load_json,
normalize_crs,
rfc3339,
search_list_for_dict_key,
)

if typing.TYPE_CHECKING:
# Imports for type checking only (circular import issue at runtime).
import xarray

from openeo.rest.connection import Connection, search_list_for_dict_key
from openeo.rest.connection import Connection
from openeo.udf import XarrayDataCube


Expand Down Expand Up @@ -2724,13 +2731,10 @@ def sar_backscatter(
.. versionadded:: 0.4.9
.. versionchanged:: 0.4.10 replace `orthorectify` and `rtc` arguments with `coefficient`.
"""
if self.connection == None:
coefficient_options = [None]
else:
coefficient_options = [None]
if self.connection:
schema = self.connection.get_schema_from_process_parameter("sar_backscatter", "coefficient")
coefficient_options = search_list_for_dict_key(schema, "enum")
if coefficient_options is None:
coefficient_options = [None]
coefficient_options += search_list_for_dict_key(schema, "enum")
if coefficient not in coefficient_options:
raise OpenEoClientException("Invalid `sar_backscatter` coefficient {c!r}. Should be one of {o}".format(
c=coefficient, o=coefficient_options
Expand Down
19 changes: 19 additions & 0 deletions openeo/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,22 @@ def normalize_crs(crs: Any, *, use_pyproj: bool = True) -> Union[None, int, str]
raise ValueError(f"Can not normalize CRS data {type(crs)}")

return crs


def search_list_for_dict_key(lst: list, key: str) -> Union[dict, list]:
"""
Searches a value of the dict that matches with the key in the list.
:param lst: list with dictionaries.
:param key: The key for which the value is searched for.
:return: value that matches key.
"""
result = None
for item in lst:
if key in item:
if result is None:
result = item[key]
else:
raise ValueError("Multiple keys found with value {v}.".format(v=key))
return result
124 changes: 120 additions & 4 deletions tests/rest/datacube/test_datacube100.py
Original file line number Diff line number Diff line change
Expand Up @@ -2744,7 +2744,36 @@ def test_print_json_file_path(con100, tmp_path, path_factory):
assert path.read_text() == EXPECTED_JSON_EXPORT_S2_NDVI + "\n"


def test_sar_backscatter_defaults(con100):
def test_sar_backscatter_defaults(con100, requests_mock):
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
processes = [
{
"id": "sar_backscatter",
"description": "Computes backscatter from SAR input",
"summary": "Computes backscatter from SAR input",
"parameters": [
{
"default": "gamma0-terrain",
"description": "Select the radiometric correction coefficient.",
"name": "coefficient",
"schema": [
{
"enum": [
"beta0",
"sigma0-ellipsoid",
"sigma0-terrain",
"gamma0-ellipsoid",
"gamma0-terrain",
],
"type": "string",
},
],
},
],
"returns": {"description": "incremented value", "schema": {"type": "integer"}},
}
]
requests_mock.get(API_URL + "/processes", json={"processes": processes})
cube = con100.load_collection("S2").sar_backscatter()
assert _get_leaf_node(cube) == {
"process_id": "sar_backscatter",
Expand All @@ -2762,7 +2791,36 @@ def test_sar_backscatter_defaults(con100):
}


def test_sar_backscatter_custom(con100):
def test_sar_backscatter_custom(con100, requests_mock):
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
processes = [
{
"id": "sar_backscatter",
"description": "Computes backscatter from SAR input",
"summary": "Computes backscatter from SAR input",
"parameters": [
{
"default": "gamma0-terrain",
"description": "Select the radiometric correction coefficient.",
"name": "coefficient",
"schema": [
{
"enum": [
"beta0",
"sigma0-ellipsoid",
"sigma0-terrain",
"gamma0-ellipsoid",
"gamma0-terrain",
],
"type": "string",
},
],
},
],
"returns": {"description": "incremented value", "schema": {"type": "integer"}},
}
]
requests_mock.get(API_URL + "/processes", json={"processes": processes})
cube = con100.load_collection("S2")
cube = cube.sar_backscatter(
coefficient="sigma0-ellipsoid",
Expand All @@ -2786,13 +2844,71 @@ def test_sar_backscatter_custom(con100):
}


def test_sar_backscatter_coefficient_none(con100):
def test_sar_backscatter_coefficient_none(con100, requests_mock):
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
processes = [
{
"id": "sar_backscatter",
"description": "Computes backscatter from SAR input",
"summary": "Computes backscatter from SAR input",
"parameters": [
{
"default": "gamma0-terrain",
"description": "Select the radiometric correction coefficient.",
"name": "coefficient",
"schema": [
{
"enum": [
"beta0",
"sigma0-ellipsoid",
"sigma0-terrain",
"gamma0-ellipsoid",
"gamma0-terrain",
],
"type": "string",
},
],
},
],
"returns": {"description": "incremented value", "schema": {"type": "integer"}},
}
]
requests_mock.get(API_URL + "/processes", json={"processes": processes})
cube = con100.load_collection("S2")
cube = cube.sar_backscatter(coefficient=None)
assert _get_leaf_node(cube)["arguments"]["coefficient"] is None


def test_sar_backscatter_coefficient_invalid(con100):
def test_sar_backscatter_coefficient_invalid(con100, requests_mock):
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
processes = [
{
"id": "sar_backscatter",
"description": "Computes backscatter from SAR input",
"summary": "Computes backscatter from SAR input",
"parameters": [
{
"default": "gamma0-terrain",
"description": "Select the radiometric correction coefficient.",
"name": "coefficient",
"schema": [
{
"enum": [
"beta0",
"sigma0-ellipsoid",
"sigma0-terrain",
"gamma0-ellipsoid",
"gamma0-terrain",
],
"type": "string",
},
],
},
],
"returns": {"description": "incremented value", "schema": {"type": "integer"}},
}
]
requests_mock.get(API_URL + "/processes", json={"processes": processes})
cube = con100.load_collection("S2")
with pytest.raises(OpenEoClientException, match="Invalid.*coef.*unicorn.*Should.*sigma0-ellipsoid.*gamma0-terrain"):
cube.sar_backscatter(coefficient="unicorn")
Expand Down

0 comments on commit 71b2b79

Please sign in to comment.