-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor process test example parsing/listing to openeo_test_suite.li…
- Loading branch information
Showing
4 changed files
with
112 additions
and
37 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/openeo_test_suite/lib/internal-tests/processes/test_registry.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import pytest | ||
|
||
from openeo_test_suite.lib.processes.registry import ProcessRegistry | ||
|
||
|
||
class TestProcessRegistry: | ||
@pytest.fixture(scope="class") | ||
def process_registry(self) -> ProcessRegistry: | ||
return ProcessRegistry() | ||
|
||
def test_get_all_processes_basic(self, process_registry): | ||
processes = list(process_registry.get_all_processes()) | ||
assert len(processes) > 0 | ||
|
||
def test_get_all_processes_add(self, process_registry): | ||
(add,) = [ | ||
p for p in process_registry.get_all_processes() if p.process_id == "add" | ||
] | ||
|
||
assert add.level == "L1" | ||
assert add.experimental is False | ||
assert add.path.name == "add.json5" | ||
assert len(add.tests) | ||
|
||
add00 = {"arguments": {"x": 0, "y": 0}, "returns": 0} | ||
assert add00 in add.tests | ||
|
||
def test_get_all_processes_divide(self, process_registry): | ||
(divide,) = [ | ||
p for p in process_registry.get_all_processes() if p.process_id == "divide" | ||
] | ||
|
||
assert divide.level == "L1" | ||
assert divide.experimental is False | ||
assert divide.path.name == "divide.json5" | ||
assert len(divide.tests) | ||
|
||
divide0 = { | ||
"arguments": {"x": 1, "y": 0}, | ||
"returns": float("inf"), | ||
"throws": "DivisionByZero", | ||
} | ||
assert divide0 in divide.tests |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import logging | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import Any, Iterator, List, Optional | ||
|
||
import json5 | ||
|
||
import openeo_test_suite | ||
|
||
_log = logging.getLogger(__name__) | ||
|
||
DEFAULT_EXAMPLES_ROOT = ( | ||
Path(openeo_test_suite.__file__).parents[2] / "assets/processes/tests" | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ProcessData: | ||
"""Process data, including profile level and list of tests""" | ||
|
||
process_id: str | ||
level: str | ||
tests: List[dict] # TODO: also make dataclass for each test? | ||
experimental: bool | ||
path: Path | ||
|
||
|
||
class ProcessRegistry: | ||
""" | ||
Registry of processes and related tests defined in openeo-processes project | ||
""" | ||
|
||
def __init__(self, root: Path = DEFAULT_EXAMPLES_ROOT): | ||
""" | ||
:param root: Root directory of the tests folder in openeo-processes project | ||
""" | ||
self._root = root | ||
|
||
def get_all_processes(self) -> Iterator[ProcessData]: | ||
"""Collect all processes""" | ||
# TODO: cache or preload this in __init__? | ||
for path in self._root.glob("*.json5"): | ||
try: | ||
with path.open() as f: | ||
data = json5.load(f) | ||
assert data["id"] == path.stem | ||
yield ProcessData( | ||
process_id=data["id"], | ||
level=data.get("level"), | ||
tests=data.get("tests", []), | ||
experimental=data.get("experimental", False), | ||
path=path, | ||
) | ||
except Exception as e: | ||
# TODO: good idea to skip broken definitions? Why not just fail hard? | ||
_log.error(f"Failed to load process data from {path}: {e!r}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters