From 58bcd06532e441825339a666b9625f2077fd49f1 Mon Sep 17 00:00:00 2001 From: mouffok Date: Fri, 13 Oct 2023 15:42:39 +0200 Subject: [PATCH] mapping tests --- kgforge/core/archetypes/mapping.py | 13 +++--- .../specializations/mappings/dictionaries.py | 8 ++++ .../specializations/mappings/test_mappings.py | 40 +++++++++++++++++++ .../specializations/models/test_demo_model.py | 2 +- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/kgforge/core/archetypes/mapping.py b/kgforge/core/archetypes/mapping.py index 75f5a420..feb4cb64 100644 --- a/kgforge/core/archetypes/mapping.py +++ b/kgforge/core/archetypes/mapping.py @@ -84,7 +84,7 @@ def load_file(cls, filepath, raise_ex=True): return cls(filepath.read_text()) else: if raise_ex: - raise Exception("Invalid file") + raise FileNotFoundError return None @classmethod @@ -93,18 +93,15 @@ def load_url(cls, url, raise_ex=True): response = requests.get(url) response.raise_for_status() return cls(response.text) - except RequestException: + except RequestException as e: if raise_ex: - raise Exception(f"Could not find url {url}") + raise e return None @classmethod + @abstractmethod def load_text(cls, source: str, raise_ex=True): - if len(source.strip()) > 0 and source.strip()[0] != "{": - if raise_ex: - raise Exception("Invalid mapping string") - return None - return cls(source) + ... def save(self, path: str) -> None: # path: FilePath. diff --git a/kgforge/specializations/mappings/dictionaries.py b/kgforge/specializations/mappings/dictionaries.py index 8d2f823d..ddbd5698 100644 --- a/kgforge/specializations/mappings/dictionaries.py +++ b/kgforge/specializations/mappings/dictionaries.py @@ -32,3 +32,11 @@ def _load_rules(mapping: str) -> OrderedDict: @staticmethod def _normalize_rules(rules: OrderedDict) -> str: return hjson.dumps(rules, indent=4, item_sort_key=sort_attrs) + + @classmethod + def load_text(cls, source: str, raise_ex=True): + if len(source.strip()) > 0 and source.strip()[0] != "{": + if raise_ex: + raise Exception("Invalid mapping string") + return None + return cls(source) diff --git a/tests/specializations/mappings/test_mappings.py b/tests/specializations/mappings/test_mappings.py index 6dcdc9a5..d07be0c4 100644 --- a/tests/specializations/mappings/test_mappings.py +++ b/tests/specializations/mappings/test_mappings.py @@ -13,3 +13,43 @@ # along with Blue Brain Nexus Forge. If not, see . # Placeholder for the generic parameterizable test suite for mappings. + + +import pytest +from contextlib import nullcontext as does_not_raise + +from kgforge.core.archetypes.mapping import MappingType +from kgforge.specializations.mappings import DictionaryMapping +from utils import full_path_relative_to_root + +mapping_url_valid = "https://raw.githubusercontent.com/BlueBrain/nexus-forge/master/examples" \ + "/configurations/nexus-store/file-to-resource-mapping.hjson" + +mapping_path_valid = full_path_relative_to_root( + "tests/data/nexus-store/file-to-resource-mapping.hjson" +) + + +@pytest.mark.parametrize( + "source, exception", + [ + (mapping_path_valid, does_not_raise()), + (mapping_url_valid, does_not_raise()), + ], +) +def test_mapping_load_no_mapping_type(source, exception): + with exception: + mapping = DictionaryMapping.load(source) + + +@pytest.mark.parametrize( + "source, mapping_type, exception", + [ + (mapping_path_valid, MappingType.FILE, does_not_raise()), + (mapping_url_valid, MappingType.URL, does_not_raise()), + ("i", MappingType.URL, pytest.raises(Exception)), + ], +) +def test_mapping_load_mapping_type(source, mapping_type, exception): + with exception: + mapping = DictionaryMapping.load(source, mapping_type) diff --git a/tests/specializations/models/test_demo_model.py b/tests/specializations/models/test_demo_model.py index 1ccde05a..53c7f4e9 100644 --- a/tests/specializations/models/test_demo_model.py +++ b/tests/specializations/models/test_demo_model.py @@ -15,8 +15,8 @@ from pytest_bdd import given, parsers, scenarios, when from kgforge.specializations.models.demo_model import DemoModel -from tests.conftest import check_report from utils import full_path_relative_to_root +from tests.conftest import check_report # TODO To be port to the generic parameterizable test suite for models in test_models.py. DKE-135.