Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions json2xml/dicttoxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
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)}"


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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[pytest]
log_cli = True
log_cli_level = INFO
log_cli=True
log_cli_level=INFO

26 changes: 22 additions & 4 deletions tests/test_dict2xml.py
Original file line number Diff line number Diff line change
@@ -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'}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions tests/test_json2xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -17,7 +15,7 @@
readfromurl)


class TestJson2xml(unittest.TestCase):
class TestJson2xml:
"""Tests for `json2xml` package."""

def setUp(self):
Expand Down
Loading