Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: renaming functions of ontology checks in xmlupload #817

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/dsp_tools/commands/xmlupload/check_consistency_with_ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
InvalidOntologyElementsInData,
)
from dsp_tools.commands.xmlupload.ontology_client import OntologyClient
from dsp_tools.models.exceptions import UserError
from dsp_tools.models.exceptions import InputError

defaultOntologyColon: Pattern[str] = regex.compile(r"^:\w+$")
knoraUndeclared: Pattern[str] = regex.compile(r"^\w+$")
Expand All @@ -22,30 +22,30 @@ def do_xml_consistency_check_with_ontology(onto_client: OntologyClient, root: et
This function takes an OntologyClient and the root of an XML.
It retrieves the ontologies from the server.
It iterates over the root.
If it finds any invalid properties or classes, they are printed out and a UserError is raised.
If it finds any invalid properties or classes, they are printed out and a InputError is raised.

Args:
onto_client: client for the ontology retrieval
root: root of the XML

Raises:
UserError: if there are any invalid properties or classes
InputError: if there are any invalid properties or classes
"""
onto_check_info = ProjectOntosInformation(
default_ontology_prefix=onto_client.default_ontology,
onto_lookup=onto_client.get_all_ontologies_from_server(),
)
classes_in_data, properties_in_data = _get_all_classes_and_properties_from_data(root)
_find_if_all_classes_and_properties_exist_in_onto(classes_in_data, properties_in_data, onto_check_info)
_find_all_classes_and_properties_exist_in_onto(classes_in_data, properties_in_data, onto_check_info)


def _find_if_all_classes_and_properties_exist_in_onto(
def _find_all_classes_and_properties_exist_in_onto(
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
classes_in_data: dict[str, list[str]],
properties_in_data: dict[str, list[str]],
onto_check_info: ProjectOntosInformation,
) -> None:
class_problems = _check_if_all_class_types_exist(classes_in_data, onto_check_info)
property_problems = _check_if_all_properties_exist(properties_in_data, onto_check_info)
class_problems = _check_all_class_types_exist(classes_in_data, onto_check_info)
property_problems = _check_all_properties_exist(properties_in_data, onto_check_info)
if not class_problems and not property_problems:
return None
problems = InvalidOntologyElementsInData(
Expand All @@ -59,7 +59,7 @@ def _find_if_all_classes_and_properties_exist_in_onto(
"\n\n---------------------------------------\n\n"
f"\nAll the problems are listed in the file: '{Path.cwd()}/{csv_file}'"
)
raise UserError(msg)
raise InputError(msg)


def _get_all_classes_and_properties_from_data(
Expand Down Expand Up @@ -96,17 +96,17 @@ def _get_all_property_names_and_resource_ids_one_resource(
return prop_dict


def _check_if_all_class_types_exist(
def _check_all_class_types_exist(
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
classes: dict[str, list[str]], onto_check_info: ProjectOntosInformation
) -> list[tuple[str, list[str], str]]:
problem_list = []
for cls_type, ids in classes.items():
if problem := _check_if_one_class_type_exists(cls_type, onto_check_info):
if problem := _check_one_class_type_exists(cls_type, onto_check_info):
problem_list.append((cls_type, ids, problem))
return problem_list


def _check_if_one_class_type_exists(cls_type: str, onto_check_info: ProjectOntosInformation) -> str | None:
def _check_one_class_type_exists(cls_type: str, onto_check_info: ProjectOntosInformation) -> str | None:
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
prefix, cls_ = _get_separate_prefix_and_iri_from_onto_prop_or_cls(cls_type, onto_check_info.default_ontology_prefix)
if not prefix:
return "Class name does not follow a known ontology pattern"
Expand All @@ -116,17 +116,17 @@ def _check_if_one_class_type_exists(cls_type: str, onto_check_info: ProjectOntos
return "Unknown ontology prefix"


def _check_if_all_properties_exist(
def _check_all_properties_exist(
properties: dict[str, list[str]], onto_check_info: ProjectOntosInformation
) -> list[tuple[str, list[str], str]]:
problem_list = []
for prop_name, ids in properties.items():
if problem := _check_if_one_property_exists(prop_name, onto_check_info):
if problem := _check_one_property_exists(prop_name, onto_check_info):
problem_list.append((prop_name, ids, problem))
return problem_list


def _check_if_one_property_exists(prop_name: str, onto_check_info: ProjectOntosInformation) -> str | None:
def _check_one_property_exists(prop_name: str, onto_check_info: ProjectOntosInformation) -> str | None:
prefix, prop = _get_separate_prefix_and_iri_from_onto_prop_or_cls(
prop_name, onto_check_info.default_ontology_prefix
)
Expand Down
24 changes: 12 additions & 12 deletions src/dsp_tools/commands/xmlupload/models/ontology_lookup_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,39 @@ class ProjectOntosInformation:
onto_lookup: dict[str, OntoInfo]


def extract_classes_properties_from_onto(onto_graph: list[dict[str, Any]]) -> OntoInfo:
def extract_classes_properties_from_onto(onto_json: list[dict[str, Any]]) -> OntoInfo:
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
"""
This function takes an ontology graph from the DSP-API.
It extracts the classes and properties.
And saves them in an instance of the class Ontology.

Args:
onto_graph: graph from DSP-API
onto_json: graph from DSP-API

Returns:
Ontology instance with the classes and properties
"""
classes = _get_all_cleaned_classes_from_graph(onto_graph)
properties = _get_all_cleaned_properties_from_graph(onto_graph)
classes = _get_all_cleaned_classes_from_json(onto_json)
properties = _get_all_cleaned_properties_from_json(onto_json)
return OntoInfo(classes, properties)


def _get_all_cleaned_classes_from_graph(onto_graph: list[dict[str, Any]]) -> list[str]:
classes = _get_all_classes_from_graph(onto_graph)
def _get_all_cleaned_classes_from_json(onto_json: list[dict[str, Any]]) -> list[str]:
classes = _get_all_classes_from_json(onto_json)
return _remove_prefixes(classes)


def _get_all_classes_from_graph(onto_graph: list[dict[str, Any]]) -> list[str]:
return [elem["@id"] for elem in onto_graph if elem.get("knora-api:isResourceClass")]
def _get_all_classes_from_json(onto_json: list[dict[str, Any]]) -> list[str]:
return [elem["@id"] for elem in onto_json if elem.get("knora-api:isResourceClass")]


def _get_all_cleaned_properties_from_graph(onto_graph: list[dict[str, Any]]) -> list[str]:
props = _get_all_properties_from_graph(onto_graph)
def _get_all_cleaned_properties_from_json(onto_json: list[dict[str, Any]]) -> list[str]:
props = _get_all_properties_from_json(onto_json)
return _remove_prefixes(props)


def _get_all_properties_from_graph(onto_graph: list[dict[str, Any]]) -> list[str]:
return [elem["@id"] for elem in onto_graph if not elem.get("knora-api:isResourceClass")]
def _get_all_properties_from_json(onto_json: list[dict[str, Any]]) -> list[str]:
return [elem["@id"] for elem in onto_json if not elem.get("knora-api:isResourceClass")]


def _remove_prefixes(ontology_elements: list[str]) -> list[str]:
Expand Down
1 change: 1 addition & 0 deletions src/dsp_tools/commands/xmlupload/xmlupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def xmlupload(
Raises:
BaseError: in case of permanent network or software failure
UserError: in case of permanent network or software failure, or if the XML file is invalid
InputError: in case of permanent network or software failure, or if the XML file is invalid

Returns:
True if all resources could be uploaded without errors; False if one of the resources could not be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from dsp_tools.commands.xmlupload.check_consistency_with_ontology import do_xml_consistency_check_with_ontology
from dsp_tools.commands.xmlupload.ontology_client import OntologyClientLive
from dsp_tools.models.exceptions import BaseError, UserError
from dsp_tools.models.exceptions import BaseError, InputError, UserError


@dataclass
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_error_on_nonexistent_onto_name() -> None:
" - the_only_resource\n\n"
"---------------------------------------\n\n"
)
with pytest.raises(UserError, match=expected):
with pytest.raises(InputError, match=expected):
do_xml_consistency_check_with_ontology(ontology_client, root)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
from pytest_unordered import unordered

from dsp_tools.commands.xmlupload.check_consistency_with_ontology import (
_check_if_all_class_types_exist,
_check_if_all_properties_exist,
_check_if_one_class_type_exists,
_check_if_one_property_exists,
_find_if_all_classes_and_properties_exist_in_onto,
_check_all_class_types_exist,
_check_all_properties_exist,
_check_one_class_type_exists,
_check_one_property_exists,
_find_all_classes_and_properties_exist_in_onto,
_get_all_class_types_and_ids_from_data,
_get_all_classes_and_properties_from_data,
_get_all_property_names_and_resource_ids_one_resource,
_get_separate_prefix_and_iri_from_onto_prop_or_cls,
)
from dsp_tools.commands.xmlupload.models.ontology_lookup_models import OntoInfo, ProjectOntosInformation
from dsp_tools.models.exceptions import UserError
from dsp_tools.models.exceptions import InputError


class TestCheckClassType:
Expand All @@ -26,7 +26,7 @@ def test_no_knora_prefix(self) -> None:
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
},
)
assert not _check_if_one_class_type_exists("knoraClassA", onto_check_info)
assert not _check_one_class_type_exists("knoraClassA", onto_check_info)

def test_knora_prefix(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -36,7 +36,7 @@ def test_knora_prefix(self) -> None:
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
},
)
assert not _check_if_one_class_type_exists("knora-api:knoraClassA", onto_check_info)
assert not _check_one_class_type_exists("knora-api:knoraClassA", onto_check_info)

def test_no_default_prefix(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -46,7 +46,7 @@ def test_no_default_prefix(self) -> None:
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
},
)
assert not _check_if_one_class_type_exists(":classA", onto_check_info)
assert not _check_one_class_type_exists(":classA", onto_check_info)

def test_default_prefix(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -56,7 +56,7 @@ def test_default_prefix(self) -> None:
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
},
)
assert not _check_if_one_class_type_exists("test:classB", onto_check_info)
assert not _check_one_class_type_exists("test:classB", onto_check_info)

def test_unknown_class(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -66,7 +66,7 @@ def test_unknown_class(self) -> None:
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
},
)
assert _check_if_one_class_type_exists("test:classC", onto_check_info) == "Invalid Class Type"
assert _check_one_class_type_exists("test:classC", onto_check_info) == "Invalid Class Type"

def test_unknown_prefix(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -76,7 +76,7 @@ def test_unknown_prefix(self) -> None:
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
},
)
assert _check_if_one_class_type_exists("other:classC", onto_check_info) == "Unknown ontology prefix"
assert _check_one_class_type_exists("other:classC", onto_check_info) == "Unknown ontology prefix"

def test_diagnose_all_classes_no_problems(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -87,7 +87,7 @@ def test_diagnose_all_classes_no_problems(self) -> None:
},
)
test_classes = {"knora-api:knoraClassA": ["idA"], ":classB": ["idB"]}
assert not _check_if_all_class_types_exist(test_classes, onto_check_info)
assert not _check_all_class_types_exist(test_classes, onto_check_info)

def test_diagnose_all_classes_problems(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -98,7 +98,7 @@ def test_diagnose_all_classes_problems(self) -> None:
},
)
test_classes = {"knoraClassA": ["idA"], ":classD": ["idD"]}
assert _check_if_all_class_types_exist(test_classes, onto_check_info) == [
assert _check_all_class_types_exist(test_classes, onto_check_info) == [
(":classD", ["idD"], "Invalid Class Type")
]

Expand All @@ -112,7 +112,7 @@ def test_no_knora_prefix(self) -> None:
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
},
)
assert not _check_if_one_property_exists("knoraPropA", onto_check_info)
assert not _check_one_property_exists("knoraPropA", onto_check_info)

def test_knora_prefix(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -122,7 +122,7 @@ def test_knora_prefix(self) -> None:
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
},
)
assert not _check_if_one_property_exists("knora-api:knoraPropA", onto_check_info)
assert not _check_one_property_exists("knora-api:knoraPropA", onto_check_info)

def test_no_default_prefix(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -132,7 +132,7 @@ def test_no_default_prefix(self) -> None:
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
},
)
assert not _check_if_one_property_exists(":propA", onto_check_info)
assert not _check_one_property_exists(":propA", onto_check_info)

def test_default_prefix(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -142,7 +142,7 @@ def test_default_prefix(self) -> None:
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
},
)
assert not _check_if_one_property_exists("test:propB", onto_check_info)
assert not _check_one_property_exists("test:propB", onto_check_info)

def test_unknown_prefix(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -152,7 +152,7 @@ def test_unknown_prefix(self) -> None:
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
},
)
assert _check_if_one_property_exists("other:propB", onto_check_info) == "Unknown ontology prefix"
assert _check_one_property_exists("other:propB", onto_check_info) == "Unknown ontology prefix"

def test_unknown_property(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -162,7 +162,7 @@ def test_unknown_property(self) -> None:
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
},
)
assert _check_if_one_property_exists("test:propC", onto_check_info) == "Invalid Property"
assert _check_one_property_exists("test:propC", onto_check_info) == "Invalid Property"

def test_diagnose_all_properties_problems(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -174,7 +174,7 @@ def test_diagnose_all_properties_problems(self) -> None:
)
test_properties = {"test:propB": ["idB"], "other:propB": ["idB"], "test:propD": ["idD"]}
expected = [("other:propB", ["idB"], "Unknown ontology prefix"), ("test:propD", ["idD"], "Invalid Property")]
assert unordered(_check_if_all_properties_exist(test_properties, onto_check_info)) == expected
assert unordered(_check_all_properties_exist(test_properties, onto_check_info)) == expected

def test_diagnose_all_properties_no_problems(self) -> None:
onto_check_info = ProjectOntosInformation(
Expand All @@ -185,10 +185,10 @@ def test_diagnose_all_properties_no_problems(self) -> None:
},
)
test_properties = {"test:propB": ["idB"], "knoraPropA": ["idA"]}
assert not unordered(_check_if_all_properties_exist(test_properties, onto_check_info))
assert not unordered(_check_all_properties_exist(test_properties, onto_check_info))


def test_find_if_all_classes_and_properties_exist_in_onto() -> None:
def test_find_all_classes_and_properties_exist_in_onto() -> None:
onto_check_info = ProjectOntosInformation(
default_ontology_prefix="test",
onto_lookup={
Expand All @@ -198,10 +198,10 @@ def test_find_if_all_classes_and_properties_exist_in_onto() -> None:
)
classes = {"knoraClassA": ["idA"]}
properties = {"knora-api:knoraPropA": ["idA"]}
_find_if_all_classes_and_properties_exist_in_onto(classes, properties, onto_check_info)
_find_all_classes_and_properties_exist_in_onto(classes, properties, onto_check_info)


def test_find_if_all_classes_and_properties_exist_in_onto_problem() -> None:
def test_find_all_classes_and_properties_exist_in_onto_problem() -> None:
onto_check_info = ProjectOntosInformation(
default_ontology_prefix="test",
onto_lookup={
Expand All @@ -211,8 +211,8 @@ def test_find_if_all_classes_and_properties_exist_in_onto_problem() -> None:
)
classes = {"knora": ["idA"]}
properties = {"knora-api:knoraPropA": ["idA"]}
with pytest.raises(UserError):
_find_if_all_classes_and_properties_exist_in_onto(classes, properties, onto_check_info)
with pytest.raises(InputError):
_find_all_classes_and_properties_exist_in_onto(classes, properties, onto_check_info)


def test_get_prefix_and_prop_cls_identifier() -> None:
Expand Down