Skip to content

Commit 40b6a84

Browse files
author
Vinit Kumar
committed
feat: add some more tests
1 parent dff4620 commit 40b6a84

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

json2xml/dicttoxml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from defusedxml.minidom import parseString
1111

1212
# Create a safe random number generator
13-
safe_random = SystemRandom()
1413

1514
# Set up logging
1615
LOG = logging.getLogger("dicttoxml")
@@ -28,6 +27,7 @@ def make_id(element: str, start: int = 100000, end: int = 999999) -> str:
2827
Returns:
2928
str: The generated ID.
3029
"""
30+
safe_random = SystemRandom()
3131
return f"{element}_{safe_random.randint(start, end)}"
3232

3333

tests/test_dict2xml.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,58 @@ def test_datetime_conversion(self):
444444
data = {"key": datetime.datetime(2023, 2, 15, 12, 30, 45)}
445445
result = dicttoxml.dicttoxml(data, attr_type=False)
446446
assert b"<key>2023-02-15T12:30:45</key>" in result
447+
448+
def test_list_to_xml_with_primitive_items(self):
449+
data = {"items": [1, 2, 3]}
450+
result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True)
451+
assert result == b"<items><item>1</item><item>2</item><item>3</item></items>"
452+
453+
def test_list_to_xml_with_dict_items(self):
454+
data = {"items": [{"key1": "value1"}, {"key2": "value2"}]}
455+
result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True)
456+
assert result == b"<items><item><key1>value1</key1></item><item><key2>value2</key2></item></items>"
457+
458+
def test_list_to_xml_with_mixed_items(self):
459+
data = {"items": [1, "string", {"key": "value"}]}
460+
result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True)
461+
assert result == b"<items><item>1</item><item>string</item><item><key>value</key></item></items>"
462+
463+
def test_list_to_xml_with_empty_list(self):
464+
data = {"items": []}
465+
result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True)
466+
assert result == b"<items></items>"
467+
468+
def test_list_to_xml_with_special_characters(self):
469+
data = {"items": ["<tag>", "&", '"quote"', "'single quote'"]}
470+
result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True)
471+
assert result == b"<items><item>&lt;tag&gt;</item><item>&amp;</item><item>&quot;quote&quot;</item><item>&apos;single quote&apos;</item></items>"
472+
473+
def datetime_conversion_with_isoformat(self):
474+
data = {"key": datetime.datetime(2023, 2, 15, 12, 30, 45)}
475+
result = dicttoxml.dicttoxml(data, attr_type=False)
476+
assert b"<key>2023-02-15T12:30:45</key>" in result
477+
478+
def test_date_conversion_with_isoformat(self):
479+
data = {"key": datetime.date(2023, 2, 15)}
480+
result = dicttoxml.dicttoxml(data, attr_type=False)
481+
assert b"<key>2023-02-15</key>" in result
482+
483+
def test_datetime_conversion_with_attr_type(self):
484+
data = {"key": datetime.datetime(2023, 2, 15, 12, 30, 45)}
485+
result = dicttoxml.dicttoxml(data, attr_type=True)
486+
assert b'<key type="str">2023-02-15T12:30:45</key>' in result
487+
488+
def test_date_conversion_with_attr_type(self):
489+
data = {"key": datetime.date(2023, 2, 15)}
490+
result = dicttoxml.dicttoxml(data, attr_type=True)
491+
assert b'<key type="str">2023-02-15</key>' in result
492+
493+
def test_datetime_conversion_with_custom_attributes(self):
494+
data = {"key": datetime.datetime(2023, 2, 15, 12, 30, 45)}
495+
result = dicttoxml.dicttoxml(data, attr_type=False, custom_root="custom")
496+
assert b"<custom><key>2023-02-15T12:30:45</key></custom>" in result
497+
498+
def test_date_conversion_with_custom_attributes(self):
499+
data = {"key": datetime.date(2023, 2, 15)}
500+
result = dicttoxml.dicttoxml(data, attr_type=False, custom_root="custom")
501+
assert b"<custom><key>2023-02-15</key></custom>" in result

0 commit comments

Comments
 (0)