-
Notifications
You must be signed in to change notification settings - Fork 4
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
6 changed files
with
136 additions
and
37 deletions.
There are no files selected for viewing
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
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
Empty file.
43 changes: 43 additions & 0 deletions
43
src/openeo_test_suite/lib/internal-tests/test_process_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.process_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 |
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,76 @@ | ||
import logging | ||
import os | ||
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__) | ||
|
||
|
||
@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: Optional[Path] = None): | ||
""" | ||
:param root: Root directory of the tests folder in openeo-processes project | ||
""" | ||
self._root = Path( | ||
root | ||
# TODO: eliminate need for this env var? | ||
or os.environ.get("OPENEO_TEST_SUITE_PROCESSES_TEST_ROOT") | ||
or self._guess_root() | ||
) | ||
|
||
def _guess_root(self): | ||
# TODO: avoid need for guessing and properly include assets in (installed) package | ||
project_root = Path(openeo_test_suite.__file__).parents[2] | ||
candidates = [ | ||
project_root / "assets/processes/tests", | ||
Path("./assets/processes/tests"), | ||
Path("./openeo-test-suite/assets/processes/tests"), | ||
] | ||
for candidate in candidates: | ||
if candidate.exists() and candidate.is_dir(): | ||
return candidate | ||
raise ValueError( | ||
f"Could not find valid processes test root directory (tried {candidates})" | ||
) | ||
|
||
def get_all_processes(self) -> Iterator[ProcessData]: | ||
"""Collect all processes""" | ||
# TODO: cache or preload this in __init__? | ||
if not self._root.is_dir(): | ||
raise ValueError(f"Invalid process test root directory: {self._root}") | ||
_log.info(f"Loading process definitions from {self._root}") | ||
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