Skip to content

Commit

Permalink
mapping tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ssssarah committed Oct 13, 2023
1 parent 725eb93 commit 58bcd06
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
13 changes: 5 additions & 8 deletions kgforge/core/archetypes/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions kgforge/specializations/mappings/dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
40 changes: 40 additions & 0 deletions tests/specializations/mappings/test_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,43 @@
# along with Blue Brain Nexus Forge. If not, see <https://choosealicense.com/licenses/lgpl-3.0/>.

# 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)
2 changes: 1 addition & 1 deletion tests/specializations/models/test_demo_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down

0 comments on commit 58bcd06

Please sign in to comment.