Skip to content

Commit

Permalink
test_xml_deserialization: Add tests for _tag_replace_namespace (#284)
Browse files Browse the repository at this point in the history
Previously, the function `_tag_replace_namespace` in 
`adapter.xml.xml_deserialization` did not have
corresponding unittests yet. 
This adds the tests for the most common use cases.
  • Loading branch information
JAB1305 committed Jul 16, 2024
1 parent 77952c2 commit d31e4f8
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/adapter/xml/test_xml_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from basyx.aas import model
from basyx.aas.adapter.xml import StrictAASFromXmlDecoder, XMLConstructables, read_aas_xml_file, \
read_aas_xml_file_into, read_aas_xml_element
from basyx.aas.adapter.xml.xml_deserialization import _tag_replace_namespace
from basyx.aas.adapter._generic import XML_NS_MAP
from lxml import etree
from typing import Iterable, Type, Union
Expand Down Expand Up @@ -449,3 +450,29 @@ def construct_submodel(cls, element: etree._Element, object_class=EnhancedSubmod
self.assertIsInstance(submodel, EnhancedSubmodel)
assert isinstance(submodel, EnhancedSubmodel)
self.assertEqual(submodel.enhanced_attribute, "fancy!")


class TestTagReplaceNamespace(unittest.TestCase):
def test_known_namespace(self):
tag = '{https://admin-shell.io/aas/3/0}tag'
expected = 'aas:tag'
self.assertEqual(_tag_replace_namespace(tag, XML_NS_MAP), expected)

def test_empty_prefix(self):
# Empty prefix should not be replaced as otherwise it would apply everywhere
tag = '{https://admin-shell.io/aas/3/0}tag'
nsmap = {"": "https://admin-shell.io/aas/3/0"}
expected = '{https://admin-shell.io/aas/3/0}tag'
self.assertEqual(_tag_replace_namespace(tag, nsmap), expected)

def test_empty_namespace(self):
# Empty namespaces should also have no effect
tag = '{https://admin-shell.io/aas/3/0}tag'
nsmap = {"aas": ""}
expected = '{https://admin-shell.io/aas/3/0}tag'
self.assertEqual(_tag_replace_namespace(tag, nsmap), expected)

def test_unknown_namespace(self):
tag = '{http://unknownnamespace.com}unknown'
expected = '{http://unknownnamespace.com}unknown' # Unknown namespace should remain unchanged
self.assertEqual(_tag_replace_namespace(tag, XML_NS_MAP), expected)

0 comments on commit d31e4f8

Please sign in to comment.