Skip to content

Commit

Permalink
chore: fix analyse_circles_in_data() (remove namespaces) (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 26, 2023
1 parent f2d4488 commit 72f710c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/dsp_tools/analyse_xml_data/construct_and_analyze_graph.py
Expand Up @@ -28,7 +28,7 @@ def create_info_from_xml_for_graph(
resptr_links = []
xml_links = []
all_resource_ids = []
for resource in root.iter(tag="{https://dasch.swiss/schema}resource"):
for resource in root.iter(tag="resource"):
resptr, xml = _create_info_from_xml_for_graph_from_one_resource(resource)
all_resource_ids.append(resource.attrib["id"])
resptr_links.extend(resptr)
Expand All @@ -43,9 +43,9 @@ def _create_info_from_xml_for_graph_from_one_resource(
xml_links: list[XMLLink] = []
for prop in resource.getchildren():
match prop.tag:
case "{https://dasch.swiss/schema}resptr-prop":
case "resptr-prop":
resptr_links.extend(_create_resptr_link_objects(resource.attrib["id"], prop))
case "{https://dasch.swiss/schema}text-prop":
case "text-prop":
xml_links.extend(_create_text_link_objects(resource.attrib["id"], prop))
return resptr_links, xml_links

Expand Down
Expand Up @@ -40,6 +40,7 @@ def test_create_info_from_xml_for_graph_from_one_resource() -> None:
</text-prop>
</resource>"""
)
remove_namespace(test_ele)
res_resptr_links, res_xml_links = _create_info_from_xml_for_graph_from_one_resource(test_ele)
res_B_19 = [obj.target_id for obj in res_resptr_links]
assert "res_B_19" in res_B_19
Expand All @@ -64,6 +65,7 @@ def test_create_info_from_xml_for_graph_from_one_resource_one() -> None:
</resource>
"""
)
remove_namespace(test_ele)
res_resptr, res_xml = _create_info_from_xml_for_graph_from_one_resource(test_ele)
assert res_resptr[0].target_id == "res_B_11"
assert isinstance(res_resptr[0], ResptrLink)
Expand All @@ -75,6 +77,7 @@ def test_create_info_from_xml_for_graph_from_one_resource_no_links() -> None:
test_ele = etree.fromstring(
'<resource label="res_B_18" restype=":TestThing" id="res_B_18" permissions="res-default"/>'
)
remove_namespace(test_ele)
res_resptr, res_xml = _create_info_from_xml_for_graph_from_one_resource(test_ele)
assert (res_resptr, res_xml) == ([], [])

Expand All @@ -95,6 +98,7 @@ def test_text_only_create_info_from_xml_for_graph_from_one_resource() -> None:
</resource>
"""
)
remove_namespace(test_ele)
res_resptr, res_xml = _create_info_from_xml_for_graph_from_one_resource(test_ele)
assert not res_resptr
res_xml_ids = [x.target_ids for x in res_xml]
Expand All @@ -109,6 +113,7 @@ def test_extract_id_one_text_with_one_id() -> None:
</text>
"""
)
remove_namespace(test_ele)
res = _extract_ids_from_one_text_value(test_ele)
assert res == {"res_A_11"}

Expand All @@ -118,6 +123,7 @@ def test_extract_id_one_text_with_iri() -> None:
'<text permissions="prop-default" encoding="xml"><a class="salsah-link" '
'href="http://rdfh.ch/0801/RDE7_KU1STuDhHnGr5uu0g">res_A_11</a></text>'
)
remove_namespace(test_ele)
res = _extract_ids_from_one_text_value(test_ele)
assert res == set()

Expand All @@ -132,6 +138,7 @@ def test_extract_id_one_text_with_several_id() -> None:
</text>
"""
)
remove_namespace(test_ele)
res = _extract_ids_from_one_text_value(test_ele)
assert res == {"res_A_11", "res_B_11"}

Expand All @@ -143,6 +150,7 @@ def test_extract_ids_from_text_prop_with_several_text_links() -> None:
'href="IRI:res_A_18:IRI">res_A_18</a></text><text permissions="prop-default" encoding="xml"><a '
'class="salsah-link" href="IRI:res_B_18:IRI">res_B_18</a></text></text-prop>'
)
remove_namespace(test_ele)
res = _create_text_link_objects("res_C_18", test_ele)
res_ids = [x.target_ids for x in res]
assert unordered(res_ids) == [{"res_A_18"}, {"res_B_18"}]
Expand All @@ -157,6 +165,7 @@ def test_create_class_instance_resptr_link_one_link() -> None:
</resptr-prop>
"""
)
remove_namespace(test_ele)
res = _create_resptr_link_objects("res_A_15", test_ele)
assert res[0].target_id == "res_C_15"

Expand All @@ -172,6 +181,7 @@ def test_create_class_instance_resptr_link_several() -> None:
</resptr-prop>
"""
)
remove_namespace(test_ele)
res = _create_resptr_link_objects("res_D_13", test_ele)
assert all(isinstance(x, ResptrLink) for x in res)
assert res[0].target_id == "res_A_13"
Expand All @@ -192,15 +202,16 @@ def test_create_info_from_xml_for_graph_check_UUID_in_root() -> None:
b'href="IRI:res_C_11:IRI">res_C_11</a>end text.</text></text-prop></resource><resource label="res_C_11" '
b'restype=":TestThing" id="res_C_11" permissions="res-default"></resource></knora>'
)
remove_namespace(root)
res_resptr_li, res_xml_li, res_all_ids = create_info_from_xml_for_graph(root)
res_resptr = res_resptr_li[0]
assert isinstance(res_resptr, ResptrLink)
res_xml = res_xml_li[0]
assert isinstance(res_xml, XMLLink)
assert unordered(res_all_ids) == ["res_A_11", "res_B_11", "res_C_11"]
xml_res_resptr = root.find(".//{https://dasch.swiss/schema}resptr")
xml_res_resptr = root.find(".//resptr")
assert xml_res_resptr.attrib["stashUUID"] == res_resptr.link_uuid # type: ignore[union-attr]
xml_res_text = root.find(".//{https://dasch.swiss/schema}text")
xml_res_text = root.find(".//text")
assert xml_res_text.attrib["stashUUID"] == res_xml.link_uuid # type: ignore[union-attr]


Expand Down Expand Up @@ -558,5 +569,11 @@ def test_generate_upload_order_two_circles() -> None:
assert not list(graph.nodes())


def remove_namespace(tag: etree._Element) -> None:
"""Remove namespace URI from the element's name, including all its children."""
for elem in tag.iter():
elem.tag = etree.QName(elem).localname


if __name__ == "__main__":
pytest.main([__file__])

0 comments on commit 72f710c

Please sign in to comment.