Skip to content

Commit

Permalink
refactor(xmlupload): split up _check_consistency_with_ontology() into…
Browse files Browse the repository at this point in the history
… smaller functions (DEV-2669) (#521)
  • Loading branch information
jnussbaum committed Sep 15, 2023
1 parent 439a139 commit 79a50cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
46 changes: 37 additions & 9 deletions src/dsp_tools/utils/xml_upload.py
Expand Up @@ -405,7 +405,8 @@ def _check_consistency_with_ontology(
verbose: bool = False,
) -> None:
"""
Checks if the resource types and properties in the XML are consistent with the ontology.
Checks if the "default-ontology" of the <knora> tag of the XML file exists on the DSP server,
and if the resource types and property types in the XML are consistent with the ontology.
Args:
resources: a list of parsed XMLResources
Expand All @@ -422,20 +423,29 @@ def _check_consistency_with_ontology(
if verbose:
print("Check if the resource types and properties are consistent with the ontology...")
logger.info("Check if the resource types and properties are consistent with the ontology...")
if not any(x.startswith(ontoname) for x in resclass_name_2_type.keys()):
err_msg = (
f"The <knora> tag of your XML file references the ontology '{ontoname}', "
f"but the project {shortcode} on the DSP server doesn't contain an ontology with this name."
)
logger.error(err_msg)
raise UserError(err_msg)
_check_if_onto_name_exists(
resclass_name_2_type=resclass_name_2_type,
ontoname=ontoname,
shortcode=shortcode,
)
knora_properties = resclass_name_2_type[resources[0].restype].knora_properties # type: ignore[attr-defined]
_check_if_resource_types_exist(resources=resources, resclass_name_2_type=resclass_name_2_type)
_check_if_property_types_exist(resources=resources, resclass_name_2_type=resclass_name_2_type)


def _check_if_resource_types_exist(
resources: list[XMLResource],
resclass_name_2_type: dict[str, type],
) -> None:
"""
Check if the resource types in the XML file are consistent with the ontology.
Args:
resources: a list of parsed XMLResources
resclass_name_2_type: infos about the resource classes that exist on the DSP server for the current ontology
Raises:
UserError: if there is an inconsistency between the ontology and the data
"""
for resource in resources:
# check that the resource type is consistent with the ontology
if resource.restype not in resclass_name_2_type:
Expand All @@ -460,6 +470,24 @@ def _check_consistency_with_ontology(
logger.error(err_msg)
raise UserError(err_msg)


def _check_if_property_types_exist(
resources: list[XMLResource],
resclass_name_2_type: dict[str, type],
) -> None:
"""
Check if the property types in the XML file are either a DSP base property
or a property that was defined for this specific resource (not every resource can have every property).
Args:
resources: a list of parsed XMLResources
resclass_name_2_type: infos about the resource classes that exist on the DSP server for the current ontology
Raises:
UserError: if there is an inconsistency between the ontology and the data
"""
knora_properties = resclass_name_2_type[resources[0].restype].knora_properties # type: ignore[attr-defined]
for resource in resources:
# check that the property types are consistent with the ontology
resource_properties = resclass_name_2_type[resource.restype].properties.keys() # type: ignore[attr-defined]
for propname in [prop.name for prop in resource.properties]:
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/test_xmlupload.py
Expand Up @@ -31,8 +31,8 @@ def test_xml_upload(self) -> None:

with self.assertRaisesRegex(
UserError,
r"The <knora> tag of your XML file references the ontology 'notexistingfantasyonto', "
r"but the project 4124 on the DSP server doesn't contain an ontology with this name",
r"The <knora> tag of your XML file references the default-ontology 'notexistingfantasyonto', "
r"but the project 4124 on the DSP server contains only the ontologies {'testonto'}",
):
xml_upload(
input_file="testdata/invalid-testdata/xml-data/inexistent-ontoname.xml",
Expand Down

0 comments on commit 79a50cf

Please sign in to comment.