forked from Open-EO/openeo-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_example.py
313 lines (273 loc) · 10.8 KB
/
test_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# TODO rename this module (`test_example.py` was originally just meant as an example)
import logging
import math
import warnings
from pathlib import Path
from typing import List, Tuple, Union
import json5
import pytest
from deepdiff import DeepDiff
from openeo_test_suite.lib.process_runner.base import ProcessTestRunner
from openeo_test_suite.lib.process_runner.util import isostr_to_datetime
from openeo_test_suite.lib.process_selection import get_selected_processes
_log = logging.getLogger(__name__)
def get_examples() -> List[Tuple[str, dict, Path, str, bool]]:
"""Collect process examples/tests from examples root folder containing JSON5 files."""
return [
(
process.process_id,
test,
process.path,
test.get("level", process.level),
test.get("experimental", process.experimental),
)
for process in get_selected_processes()
for test in process.tests
]
@pytest.mark.parametrize(
["process_id", "example", "file", "level", "experimental"], get_examples()
)
def test_process(
connection,
process_id,
example,
file,
level,
experimental,
skipper,
):
# Check whether the process (and additional extra required ones, if any) is supported on the backend
skipper.skip_if_unsupported_process([process_id] + example.get("required", []))
# prepare the arguments from test JSON encoding to internal backend representations
# or skip if not supported by the test runner
try:
arguments = _prepare_arguments(
arguments=example["arguments"],
process_id=process_id,
connection=connection,
file=file,
)
except NotImplementedError as e:
pytest.skip(str(e))
throws = bool(example.get("throws"))
returns = "returns" in example
# execute the process
try:
result = connection.execute(process_id, arguments)
except Exception as e:
result = e
# check the process results / behavior
if throws and returns:
if isinstance(result, Exception):
check_exception(example, result)
else:
check_return_value(example, result, connection, file)
elif throws:
check_exception(example, result)
elif returns:
check_return_value(example, result, connection, file)
else:
pytest.skip(
f"Test for process {process_id} doesn't provide an expected result for arguments: {example['arguments']}"
)
def _prepare_arguments(
arguments: dict, process_id: str, connection: ProcessTestRunner, file: Path
) -> dict:
return {
k: _prepare_argument(
arg=v, process_id=process_id, name=k, connection=connection, file=file
)
for k, v in arguments.items()
}
def _prepare_argument(
arg: Union[dict, str, int, float],
process_id: str,
name: str,
connection: ProcessTestRunner,
file: Path,
):
# handle external references to files
if isinstance(arg, dict) and "$ref" in arg:
arg = _load_ref(arg["$ref"], file)
# handle custom types of data
if isinstance(arg, dict):
if "type" in arg:
# labeled arrays
if arg["type"] == "labeled-array":
arg = connection.encode_labeled_array(arg)
# datacubes
elif arg["type"] == "datacube":
arg = connection.encode_datacube(arg)
# nodata-values
elif arg["type"] == "nodata":
arg = connection.get_nodata_value()
else:
# TODO: raise NotImplementedError?
_log.warning(f"Unhandled argument type: {arg}")
elif "process_graph" in arg:
arg = connection.encode_process_graph(
process=arg, parent_process_id=process_id, parent_parameter=name
)
else:
arg = {
k: _prepare_argument(
arg=v,
process_id=process_id,
name=name,
connection=connection,
file=file,
)
for k, v in arg.items()
}
elif isinstance(arg, list):
arg = [
_prepare_argument(
arg=a,
process_id=process_id,
name=name,
connection=connection,
file=file,
)
for a in arg
]
arg = connection.encode_data(arg)
if connection.is_json_only():
check_non_json_values(arg)
return arg
def _prepare_results(connection: ProcessTestRunner, file: Path, example, result=None):
# go through the example and result recursively and convert datetimes to iso strings
# could be used for more conversions in the future...
if isinstance(example, dict):
# handle external references to files
if isinstance(example, dict) and "$ref" in example:
example = _load_ref(example["$ref"], file)
if "type" in example:
if example["type"] == "datetime":
example = isostr_to_datetime(example["value"])
try:
result = isostr_to_datetime(result)
except Exception:
pass
elif example["type"] == "nodata":
example = connection.get_nodata_value()
else:
# TODO: avoid in-place dict mutation
for key in example:
if key not in result:
(example[key], _) = _prepare_results(
connection=connection, file=file, example=example[key]
)
else:
(example[key], result[key]) = _prepare_results(
connection=connection,
file=file,
example=example[key],
result=result[key],
)
elif isinstance(example, list):
# TODO: avoid in-place list mutation
for i in range(len(example)):
if i >= len(result):
(example[i], _) = _prepare_results(
connection=connection, file=file, example=example[i]
)
else:
(example[i], result[i]) = _prepare_results(
connection=connection,
file=file,
example=example[i],
result=result[i],
)
return (example, result)
def _load_ref(ref: str, file: Path):
try:
path = file.parent / ref
if ref.endswith(".json") or ref.endswith(".json5") or ref.endswith(".geojson"):
with open(path) as f:
return json5.load(f)
elif ref.endswith(".txt") or ref.endswith(".wkt2"):
with open(path) as f:
return f.read()
else:
raise NotImplementedError(f"Unhandled external reference {ref}.")
except Exception as e:
# TODO: is this try-except actually useful?
raise RuntimeError(f"Failed to load external reference {ref}") from e
def check_non_json_values(value):
# TODO: shouldn't this check be an aspect of Http(ProcessTestRunner)?
if isinstance(value, float):
if math.isnan(value):
raise ValueError("HTTP JSON APIs don't support NaN values")
elif math.isinf(value):
raise ValueError("HTTP JSON APIs don't support infinity values")
elif isinstance(value, dict):
for v in value.values():
check_non_json_values(v)
elif isinstance(value, list):
for item in value:
check_non_json_values(item)
def check_exception(example, result):
assert isinstance(result, Exception), f"Expected an exception, but got {result}"
if isinstance(example["throws"], str):
# todo: we should assert here and remove the warning, but right now tooling doesn't really implement this
# assert result.__class__.__name__ == example["throws"]
if result.__class__.__name__ != example["throws"]:
warnings.warn(f"Expected exception {example['throws']} but got {result!r}")
def check_return_value(example, result, connection, file):
assert not isinstance(result, Exception), f"Unexpected exception: {result}"
# handle custom types of data
result = connection.decode_data(result, example["returns"])
# decode special types (currently mostly datetimes and nodata)
(example["returns"], result) = _prepare_results(
connection=connection, file=file, example=example["returns"], result=result
)
delta = example.get("delta", 0.0000000001)
if isinstance(example["returns"], dict):
assert isinstance(result, dict), f"Expected a dict but got {type(result)}"
exclude_regex_paths = []
exclude_paths = []
ignore_order_func = None
if "type" in example["returns"] and example["returns"]["type"] == "datacube":
# todo: non-standardized
exclude_regex_paths.append(
r"root\['dimensions'\]\[[^\]]+\]\['reference_system'\]"
)
# todo: non-standardized
exclude_paths.append("root['nodata']")
# ignore data if operation is not changing data
if example["returns"]["data"] is None:
exclude_paths.append("root['data']")
diff = DeepDiff(
example["returns"],
result,
math_epsilon=delta,
ignore_numeric_type_changes=True,
ignore_nan_inequality=True,
exclude_paths=exclude_paths,
exclude_regex_paths=exclude_regex_paths,
ignore_order_func=ignore_order_func,
)
assert {} == diff, f"Differences: {diff!s}"
elif isinstance(example["returns"], list):
assert isinstance(result, list), f"Expected a list but got {type(result)}"
diff = DeepDiff(
example["returns"],
result,
math_epsilon=delta,
ignore_numeric_type_changes=True,
ignore_nan_inequality=True,
)
assert {} == diff, f"Differences: {diff!s}"
elif isinstance(example["returns"], float) and math.isnan(example["returns"]):
assert isinstance(result, float) and math.isnan(
result
), f"Got {result} instead of NaN"
elif isinstance(example["returns"], float) or isinstance(example["returns"], int):
msg = f"Expected a numerical result but got {result} of type {type(result)}"
assert isinstance(result, float) or isinstance(result, int), msg
assert not math.isnan(result), "Got unexpected NaN as result"
# handle numerical data with a delta
assert result == pytest.approx(example["returns"], rel=delta)
else:
msg = f"Expected {example['returns']} but got {result}"
assert result == example["returns"], msg