Skip to content

Commit 4f3d8c0

Browse files
committed
Issue #17 introduce initial JSON Schema for benchmark scenario
1 parent a4db8cf commit 4f3d8c0

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

qa/tools/apex_algorithm_qa_tools/scenarios.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77
from typing import List
88

9+
import jsonschema
910
import requests
1011
from apex_algorithm_qa_tools.common import get_project_root
1112

@@ -15,6 +16,11 @@
1516
# TODO #15 Flatten apex_algorithm_qa_tools to a single module and push as much functionality to https://github.com/ESA-APEx/esa-apex-toolbox-python
1617

1718

19+
def _get_benchmark_scenario_schema() -> dict:
20+
with open(get_project_root() / "schema/benchmark_scenario.json") as f:
21+
return json.load(f)
22+
23+
1824
@dataclasses.dataclass(kw_only=True)
1925
class BenchmarkScenario:
2026
# TODO #14 need for differentiation between different types of scenarios?
@@ -25,6 +31,9 @@ class BenchmarkScenario:
2531

2632
@classmethod
2733
def from_dict(cls, data: dict) -> BenchmarkScenario:
34+
jsonschema.validate(instance=data, schema=_get_benchmark_scenario_schema())
35+
# TODO: also include the `lint_benchmark_scenario` stuff here (maybe with option to toggle deep URL inspection)?
36+
2837
# TODO #14 standardization of these types? What about other types and how to support them?
2938
assert data["type"] == "openeo"
3039
return cls(
@@ -55,9 +64,9 @@ def lint_benchmark_scenario(scenario: BenchmarkScenario):
5564
"""
5665
# TODO #17 use JSON Schema based validation instead of ad-hoc checks?
5766
# TODO integrate this as a pre-commit hook
58-
# TODO raise descriptive exceptions instead of asserts?
67+
# TODO #17 raise descriptive exceptions instead of asserts?
5968
assert re.match(r"^[a-zA-Z0-9_-]+$", scenario.id)
60-
# TODO: proper allow-list of backends?
69+
# TODO proper allow-list of backends or leave this freeform?
6170
assert scenario.backend in ["openeofed.dataspace.copernicus.eu"]
6271
# TODO: more advanced process graph validation?
6372
assert isinstance(scenario.process_graph, dict)

qa/tools/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ requires-python = ">=3.10"
1919
dynamic = ["version"]
2020
dependencies = [
2121
"requests>=2.32.0",
22+
"jsonschema>=4.0.0",
2223
]

qa/unittests/tests/test_scenarios.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import jsonschema
12
import pytest
23
from apex_algorithm_qa_tools.scenarios import (
34
BenchmarkScenario,
@@ -24,3 +25,22 @@ def test_get_benchmark_scenarios():
2425
)
2526
def test_lint_scenario(scenario: BenchmarkScenario):
2627
lint_benchmark_scenario(scenario)
28+
29+
30+
class TestBenchmarkScenario:
31+
def test_validation_minimal(self):
32+
bs = BenchmarkScenario.from_dict(
33+
{
34+
"id": "foo",
35+
"type": "openeo",
36+
"backend": "openeo.test",
37+
"process_graph": {},
38+
}
39+
)
40+
assert bs.id == "foo"
41+
assert bs.backend == "openeo.test"
42+
assert bs.process_graph == {}
43+
44+
def test_validation_missing_essentials(self):
45+
with pytest.raises(jsonschema.ValidationError):
46+
BenchmarkScenario.from_dict({})

schema/benchmark_scenario.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"title": "Benchmark Scenario",
4+
"type": "object",
5+
"properties": {
6+
"id": {
7+
"type": "string",
8+
"description": "The unique identifier for the benchmark scenario."
9+
},
10+
"type": {
11+
"type": "string",
12+
"description": "The type of the benchmark scenario.",
13+
"enum": [
14+
"openeo"
15+
]
16+
},
17+
"description": {
18+
"type": "string",
19+
"description": "A description of the benchmark scenario."
20+
},
21+
"backend": {
22+
"type": "string",
23+
"description": "The openEO backend URL to connect to."
24+
},
25+
"process_graph": {
26+
"type": "object",
27+
"description": "The openEO process graph to execute."
28+
}
29+
},
30+
"required": [
31+
"id",
32+
"type",
33+
"backend",
34+
"process_graph"
35+
]
36+
}

0 commit comments

Comments
 (0)