diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index d81115ca..31e21ffe 100755 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -25,9 +25,6 @@ LOG = logging.getLogger("dicttoxml") # pragma: no cover -ids: list[str] = [] # initialize list of unique ids - - def make_id(element: str, start: int = 100000, end: int = 999999) -> str: """Returns a random integer""" return f"{element}_{randint(start, end)}" @@ -35,6 +32,7 @@ def make_id(element: str, start: int = 100000, end: int = 999999) -> str: def get_unique_id(element: str) -> str: """Returns a unique id for a given element""" + ids: list[str] = [] # initialize list of unique ids this_id = make_id(element) dup = True while dup: @@ -233,6 +231,7 @@ def dict2xml_str( """ parse dict2xml """ + ids: list[str] = [] # initialize list of unique ids keys_str = ", ".join(str(key) for key in item) if DEBUGMODE: # pragma: no cover LOG.info( @@ -276,6 +275,7 @@ def list2xml_str( item_wrap: bool, list_headers: bool = False, ) -> str: + ids: list[str] = [] # initialize list of unique ids if attr_type: attr["type"] = get_xml_type(item) flat = False diff --git a/pytest.ini b/pytest.ini index 1fd9f400..48a7edab 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,4 +1,4 @@ [pytest] -log_cli = True -log_cli_level = INFO +log_cli=True +log_cli_level=INFO diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index c3b4fed3..dbb2e752 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -1,10 +1,7 @@ -import unittest - from json2xml import dicttoxml -class TestDict2xml(unittest.TestCase): - +class TestDict2xml: def test_dict2xml_with_namespaces(self): data = {'ns1:node1': 'data in namespace 1', 'ns2:node2': 'data in namespace 2'} namespaces = {'ns1': 'http://www.google.de/ns1', 'ns2': 'http://www.google.de/ns2'} @@ -113,12 +110,33 @@ def test_get_unique_id(self): unique_id_elem_4 = dicttoxml.get_unique_id("li") assert len(list(set({unique_id_elem_1, unique_id_elem_2, unique_id_elem_3, unique_id_elem_4}))) == 4 + def test_key_is_valid_xml(self): + valid_key = "li" + invalid_key = "/li" + assert dicttoxml.key_is_valid_xml(valid_key) is True + assert dicttoxml.key_is_valid_xml(invalid_key) is False + def test_get_xml_type(self): assert dicttoxml.get_xml_type("abc") == "str" assert dicttoxml.get_xml_type(1) == "int" assert dicttoxml.get_xml_type(True) == "bool" assert dicttoxml.get_xml_type({}) == "dict" + def test_is_primitive_type(self): + assert dicttoxml.is_primitive_type(True) is True + assert dicttoxml.is_primitive_type("abc") is True + assert dicttoxml.is_primitive_type({}) is False + + def test_escape_xml(self): + elem = "&" + escaped_string = dicttoxml.escape_xml(elem) + assert "&" != escaped_string + assert "&" == escaped_string + + def test_wrap_cdata(self): + elem = "li" + assert "CDATA" in dicttoxml.wrap_cdata(elem) + def test_list_parent_elements(self): default_item_func = dicttoxml.default_item_func diff --git a/tests/test_json2xml.py b/tests/test_json2xml.py index beaab636..0d3f1bb6 100644 --- a/tests/test_json2xml.py +++ b/tests/test_json2xml.py @@ -2,12 +2,10 @@ """Tests for `json2xml` package.""" - +import pytest import json -import unittest from collections import OrderedDict -import pytest import xmltodict from pyexpat import ExpatError @@ -17,7 +15,7 @@ readfromurl) -class TestJson2xml(unittest.TestCase): +class TestJson2xml: """Tests for `json2xml` package.""" def setUp(self):