Skip to content

Commit 71b2b79

Browse files
issue #693 add mock for sar_backscatter test for processes
1 parent b506a11 commit 71b2b79

File tree

4 files changed

+151
-31
lines changed

4 files changed

+151
-31
lines changed

openeo/rest/connection.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,22 +2105,3 @@ def extract_connections(
21052105
connections.add(item.connection)
21062106

21072107
return connections
2108-
2109-
2110-
def search_list_for_dict_key(lst: list, key: str) -> Union[dict, list]:
2111-
"""
2112-
Searches a value of the dict that matches with the key in the list.
2113-
2114-
:param lst: list with dictionaries.
2115-
:param key: The key for which the value is searched for.
2116-
2117-
:return: value that matches key.
2118-
"""
2119-
result = None
2120-
for item in lst:
2121-
if key in item:
2122-
if result is None:
2123-
result = item[key]
2124-
else:
2125-
raise OpenEoClientException("Multiple keys found with value {v}.".format(v=key))
2126-
return result

openeo/rest/datacube.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,20 @@
5959
from openeo.rest.service import Service
6060
from openeo.rest.udp import RESTUserDefinedProcess
6161
from openeo.rest.vectorcube import VectorCube
62-
from openeo.util import dict_no_none, guess_format, load_json, normalize_crs, rfc3339
62+
from openeo.util import (
63+
dict_no_none,
64+
guess_format,
65+
load_json,
66+
normalize_crs,
67+
rfc3339,
68+
search_list_for_dict_key,
69+
)
6370

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

68-
from openeo.rest.connection import Connection, search_list_for_dict_key
75+
from openeo.rest.connection import Connection
6976
from openeo.udf import XarrayDataCube
7077

7178

@@ -2724,13 +2731,10 @@ def sar_backscatter(
27242731
.. versionadded:: 0.4.9
27252732
.. versionchanged:: 0.4.10 replace `orthorectify` and `rtc` arguments with `coefficient`.
27262733
"""
2727-
if self.connection == None:
2728-
coefficient_options = [None]
2729-
else:
2734+
coefficient_options = [None]
2735+
if self.connection:
27302736
schema = self.connection.get_schema_from_process_parameter("sar_backscatter", "coefficient")
2731-
coefficient_options = search_list_for_dict_key(schema, "enum")
2732-
if coefficient_options is None:
2733-
coefficient_options = [None]
2737+
coefficient_options += search_list_for_dict_key(schema, "enum")
27342738
if coefficient not in coefficient_options:
27352739
raise OpenEoClientException("Invalid `sar_backscatter` coefficient {c!r}. Should be one of {o}".format(
27362740
c=coefficient, o=coefficient_options

openeo/util.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,3 +687,22 @@ def normalize_crs(crs: Any, *, use_pyproj: bool = True) -> Union[None, int, str]
687687
raise ValueError(f"Can not normalize CRS data {type(crs)}")
688688

689689
return crs
690+
691+
692+
def search_list_for_dict_key(lst: list, key: str) -> Union[dict, list]:
693+
"""
694+
Searches a value of the dict that matches with the key in the list.
695+
696+
:param lst: list with dictionaries.
697+
:param key: The key for which the value is searched for.
698+
699+
:return: value that matches key.
700+
"""
701+
result = None
702+
for item in lst:
703+
if key in item:
704+
if result is None:
705+
result = item[key]
706+
else:
707+
raise ValueError("Multiple keys found with value {v}.".format(v=key))
708+
return result

tests/rest/datacube/test_datacube100.py

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,7 +2744,36 @@ def test_print_json_file_path(con100, tmp_path, path_factory):
27442744
assert path.read_text() == EXPECTED_JSON_EXPORT_S2_NDVI + "\n"
27452745

27462746

2747-
def test_sar_backscatter_defaults(con100):
2747+
def test_sar_backscatter_defaults(con100, requests_mock):
2748+
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
2749+
processes = [
2750+
{
2751+
"id": "sar_backscatter",
2752+
"description": "Computes backscatter from SAR input",
2753+
"summary": "Computes backscatter from SAR input",
2754+
"parameters": [
2755+
{
2756+
"default": "gamma0-terrain",
2757+
"description": "Select the radiometric correction coefficient.",
2758+
"name": "coefficient",
2759+
"schema": [
2760+
{
2761+
"enum": [
2762+
"beta0",
2763+
"sigma0-ellipsoid",
2764+
"sigma0-terrain",
2765+
"gamma0-ellipsoid",
2766+
"gamma0-terrain",
2767+
],
2768+
"type": "string",
2769+
},
2770+
],
2771+
},
2772+
],
2773+
"returns": {"description": "incremented value", "schema": {"type": "integer"}},
2774+
}
2775+
]
2776+
requests_mock.get(API_URL + "/processes", json={"processes": processes})
27482777
cube = con100.load_collection("S2").sar_backscatter()
27492778
assert _get_leaf_node(cube) == {
27502779
"process_id": "sar_backscatter",
@@ -2762,7 +2791,36 @@ def test_sar_backscatter_defaults(con100):
27622791
}
27632792

27642793

2765-
def test_sar_backscatter_custom(con100):
2794+
def test_sar_backscatter_custom(con100, requests_mock):
2795+
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
2796+
processes = [
2797+
{
2798+
"id": "sar_backscatter",
2799+
"description": "Computes backscatter from SAR input",
2800+
"summary": "Computes backscatter from SAR input",
2801+
"parameters": [
2802+
{
2803+
"default": "gamma0-terrain",
2804+
"description": "Select the radiometric correction coefficient.",
2805+
"name": "coefficient",
2806+
"schema": [
2807+
{
2808+
"enum": [
2809+
"beta0",
2810+
"sigma0-ellipsoid",
2811+
"sigma0-terrain",
2812+
"gamma0-ellipsoid",
2813+
"gamma0-terrain",
2814+
],
2815+
"type": "string",
2816+
},
2817+
],
2818+
},
2819+
],
2820+
"returns": {"description": "incremented value", "schema": {"type": "integer"}},
2821+
}
2822+
]
2823+
requests_mock.get(API_URL + "/processes", json={"processes": processes})
27662824
cube = con100.load_collection("S2")
27672825
cube = cube.sar_backscatter(
27682826
coefficient="sigma0-ellipsoid",
@@ -2786,13 +2844,71 @@ def test_sar_backscatter_custom(con100):
27862844
}
27872845

27882846

2789-
def test_sar_backscatter_coefficient_none(con100):
2847+
def test_sar_backscatter_coefficient_none(con100, requests_mock):
2848+
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
2849+
processes = [
2850+
{
2851+
"id": "sar_backscatter",
2852+
"description": "Computes backscatter from SAR input",
2853+
"summary": "Computes backscatter from SAR input",
2854+
"parameters": [
2855+
{
2856+
"default": "gamma0-terrain",
2857+
"description": "Select the radiometric correction coefficient.",
2858+
"name": "coefficient",
2859+
"schema": [
2860+
{
2861+
"enum": [
2862+
"beta0",
2863+
"sigma0-ellipsoid",
2864+
"sigma0-terrain",
2865+
"gamma0-ellipsoid",
2866+
"gamma0-terrain",
2867+
],
2868+
"type": "string",
2869+
},
2870+
],
2871+
},
2872+
],
2873+
"returns": {"description": "incremented value", "schema": {"type": "integer"}},
2874+
}
2875+
]
2876+
requests_mock.get(API_URL + "/processes", json={"processes": processes})
27902877
cube = con100.load_collection("S2")
27912878
cube = cube.sar_backscatter(coefficient=None)
27922879
assert _get_leaf_node(cube)["arguments"]["coefficient"] is None
27932880

27942881

2795-
def test_sar_backscatter_coefficient_invalid(con100):
2882+
def test_sar_backscatter_coefficient_invalid(con100, requests_mock):
2883+
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
2884+
processes = [
2885+
{
2886+
"id": "sar_backscatter",
2887+
"description": "Computes backscatter from SAR input",
2888+
"summary": "Computes backscatter from SAR input",
2889+
"parameters": [
2890+
{
2891+
"default": "gamma0-terrain",
2892+
"description": "Select the radiometric correction coefficient.",
2893+
"name": "coefficient",
2894+
"schema": [
2895+
{
2896+
"enum": [
2897+
"beta0",
2898+
"sigma0-ellipsoid",
2899+
"sigma0-terrain",
2900+
"gamma0-ellipsoid",
2901+
"gamma0-terrain",
2902+
],
2903+
"type": "string",
2904+
},
2905+
],
2906+
},
2907+
],
2908+
"returns": {"description": "incremented value", "schema": {"type": "integer"}},
2909+
}
2910+
]
2911+
requests_mock.get(API_URL + "/processes", json={"processes": processes})
27962912
cube = con100.load_collection("S2")
27972913
with pytest.raises(OpenEoClientException, match="Invalid.*coef.*unicorn.*Should.*sigma0-ellipsoid.*gamma0-terrain"):
27982914
cube.sar_backscatter(coefficient="unicorn")

0 commit comments

Comments
 (0)