Skip to content

Commit

Permalink
refactor(test_construct_and_analyze_graph): remove namespace, add sem…
Browse files Browse the repository at this point in the history
…antic line breaks (#602)
  • Loading branch information
jnussbaum committed Oct 27, 2023
1 parent c430b67 commit b693a94
Showing 1 changed file with 38 additions and 62 deletions.
Expand Up @@ -26,8 +26,7 @@

def test_create_info_from_xml_for_graph_from_one_resource() -> None:
test_ele = etree.fromstring(
"""<resource xmlns="https://dasch.swiss/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
label="res_A_19" restype=":TestThing" id="res_A_19" permissions="res-default">
"""<resource label="res_A_19" restype=":TestThing" id="res_A_19" permissions="res-default">
<resptr-prop name=":hasResource1">
<resptr permissions="prop-default">res_B_19</resptr>
<resptr permissions="prop-default">res_C_19</resptr>
Expand All @@ -40,7 +39,6 @@ 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 @@ -51,9 +49,7 @@ def test_create_info_from_xml_for_graph_from_one_resource() -> None:

def test_create_info_from_xml_for_graph_from_one_resource_one() -> None:
test_ele = etree.fromstring(
"""
<resource xmlns="https://dasch.swiss/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
label="res_A_11" restype=":TestThing" id="res_A_11" permissions="res-default">
"""<resource label="res_A_11" restype=":TestThing" id="res_A_11" permissions="res-default">
<text-prop name=":hasRichtext">
<text permissions="prop-default" encoding="xml">
<a class="salsah-link" href="IRI:res_B_11:IRI">res_B_11</a>
Expand All @@ -62,10 +58,8 @@ def test_create_info_from_xml_for_graph_from_one_resource_one() -> None:
<resptr-prop name=":hasResource1">
<resptr permissions="prop-default">res_B_11</resptr>
</resptr-prop>
</resource>
"""
</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 @@ -77,16 +71,13 @@ 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) == ([], [])


def test_text_only_create_info_from_xml_for_graph_from_one_resource() -> None:
test_ele = etree.fromstring(
"""
<resource xmlns="https://dasch.swiss/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
label="res_C_18" restype=":TestThing" id="res_C_18" permissions="res-default">
"""<resource label="res_C_18" restype=":TestThing" id="res_C_18" permissions="res-default">
<text-prop name=":hasRichtext">
<text permissions="prop-default" encoding="xml">
<a class="salsah-link" href="IRI:res_A_18:IRI">res_A_18</a>
Expand All @@ -95,10 +86,8 @@ def test_text_only_create_info_from_xml_for_graph_from_one_resource() -> None:
<a class="salsah-link" href="IRI:res_B_18:IRI">res_B_18</a>
</text>
</text-prop>
</resource>
"""
</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 @@ -107,81 +96,70 @@ def test_text_only_create_info_from_xml_for_graph_from_one_resource() -> None:

def test_extract_id_one_text_with_one_id() -> None:
test_ele = etree.fromstring(
"""
<text permissions="prop-default" encoding="xml">
"""<text permissions="prop-default" encoding="xml">
<a class="salsah-link" href="IRI:res_A_11:IRI">res_A_11</a>
</text>
"""
</text>"""
)
remove_namespace(test_ele)
res = _extract_ids_from_one_text_value(test_ele)
assert res == {"res_A_11"}


def test_extract_id_one_text_with_iri() -> None:
test_ele = etree.fromstring(
'<text permissions="prop-default" encoding="xml"><a class="salsah-link" '
'href="http://rdfh.ch/0801/RDE7_KU1STuDhHnGr5uu0g">res_A_11</a></text>'
'<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()


def test_extract_id_one_text_with_several_id() -> None:
test_ele = etree.fromstring(
"""
<text permissions="prop-default" encoding="xml">
"""<text permissions="prop-default" encoding="xml">
<a class="salsah-link" href="IRI:res_A_11:IRI">res_A_11</a>
<a class="salsah-link" href="IRI:res_B_11:IRI">res_A_11</a>
<a class="salsah-link" href="IRI:res_B_11:IRI">res_A_11</a>
</text>
"""
</text>"""
)
remove_namespace(test_ele)
res = _extract_ids_from_one_text_value(test_ele)
assert res == {"res_A_11", "res_B_11"}


def test_extract_ids_from_text_prop_with_several_text_links() -> None:
test_ele = etree.fromstring(
'<text-prop xmlns="https://dasch.swiss/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
'name=":hasRichtext"><text permissions="prop-default" encoding="xml"><a class="salsah-link" '
'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>'
"""<text-prop name=":hasRichtext">
<text permissions="prop-default" encoding="xml">
<a class="salsah-link" 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"}]


def test_create_class_instance_resptr_link_one_link() -> None:
test_ele = etree.fromstring(
"""
<resptr-prop xmlns="https://dasch.swiss/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
"""<resptr-prop xmlns="https://dasch.swiss/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name=":hasResource1">
<resptr permissions="prop-default">res_C_15</resptr>
</resptr-prop>
"""
</resptr-prop>"""
)
remove_namespace(test_ele)
res = _create_resptr_link_objects("res_A_15", test_ele)
assert res[0].target_id == "res_C_15"


def test_create_class_instance_resptr_link_several() -> None:
test_ele = etree.fromstring(
"""
<resptr-prop xmlns="https://dasch.swiss/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name=":hasResource1">
"""<resptr-prop name=":hasResource1">
<resptr permissions="prop-default">res_A_13</resptr>
<resptr permissions="prop-default">res_B_13</resptr>
<resptr permissions="prop-default">res_C_13</resptr>
</resptr-prop>
"""
</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 @@ -191,18 +169,22 @@ def test_create_class_instance_resptr_link_several() -> None:

def test_create_info_from_xml_for_graph_check_UUID_in_root() -> None:
root = etree.fromstring(
b'<?xml version="1.0" encoding="UTF-8"?><knora xmlns="https://dasch.swiss/schema" '
b'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://dasch.swiss/schema '
b'https://raw.githubusercontent.com/dasch-swiss/dsp-tools/main/src/dsp_tools/resources/schema/data.xsd" '
b'shortcode="0700" default-ontology="simcir"><resource label="res_A_11" restype=":TestThing" id="res_A_11" '
b'permissions="res-default"><resptr-prop name=":hasResource1"><resptr '
b'permissions="prop-default">res_B_11</resptr></resptr-prop></resource><resource label="res_B_11" '
b'restype=":TestThing" id="res_B_11" permissions="res-default"><text-prop name=":hasRichtext"><text '
b'permissions="prop-default" encoding="xml">Start text<a class="salsah-link" '
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>'
"""<knora shortcode="0700" default-ontology="simcir">
<resource label="res_A_11" restype=":TestThing" id="res_A_11" permissions="res-default">
<resptr-prop name=":hasResource1">
<resptr permissions="prop-default">res_B_11</resptr>
</resptr-prop>
</resource>
<resource label="res_B_11" restype=":TestThing" id="res_B_11" permissions="res-default">
<text-prop name=":hasRichtext">
<text permissions="prop-default" encoding="xml">
Start text<a class="salsah-link" href="IRI:res_C_11:IRI">res_C_11</a>end text.
</text>
</text-prop>
</resource>
<resource label="res_C_11" 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)
Expand Down Expand Up @@ -569,11 +551,5 @@ 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 b693a94

Please sign in to comment.