Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add some more tests #217

Merged
merged 4 commits into from
Sep 20, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
cache: 'pip'
- run: pip install --upgrade mypy types-requests types-urllib3
- name: mypy
uses: liskin/gh-problem-matcher-wrap@v1
uses: liskin/gh-problem-matcher-wrap@v2
with:
linters: mypy
run: |
Expand Down
2 changes: 1 addition & 1 deletion json2xml/dicttoxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from defusedxml.minidom import parseString

# Create a safe random number generator
safe_random = SystemRandom()

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


Expand Down
55 changes: 55 additions & 0 deletions tests/test_dict2xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,58 @@ def test_datetime_conversion(self):
data = {"key": datetime.datetime(2023, 2, 15, 12, 30, 45)}
result = dicttoxml.dicttoxml(data, attr_type=False)
assert b"<key>2023-02-15T12:30:45</key>" in result

def test_list_to_xml_with_primitive_items(self):
data = {"items": [1, 2, 3]}
result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True)
assert result == b"<items><item>1</item><item>2</item><item>3</item></items>"

def test_list_to_xml_with_dict_items(self):
data = {"items": [{"key1": "value1"}, {"key2": "value2"}]}
result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True)
assert result == b"<items><item><key1>value1</key1></item><item><key2>value2</key2></item></items>"

def test_list_to_xml_with_mixed_items(self):
data = {"items": [1, "string", {"key": "value"}]}
result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True)
assert result == b"<items><item>1</item><item>string</item><item><key>value</key></item></items>"

def test_list_to_xml_with_empty_list(self):
data = {"items": []}
result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True)
assert result == b"<items></items>"

def test_list_to_xml_with_special_characters(self):
data = {"items": ["<tag>", "&", '"quote"', "'single quote'"]}
result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True)
assert result == b"<items><item>&lt;tag&gt;</item><item>&amp;</item><item>&quot;quote&quot;</item><item>&apos;single quote&apos;</item></items>"

def test_datetime_conversion_with_isoformat(self):
data = {"key": datetime.datetime(2023, 2, 15, 12, 30, 45)}
result = dicttoxml.dicttoxml(data, attr_type=False)
assert b"<key>2023-02-15T12:30:45</key>" in result

def test_date_conversion_with_isoformat(self):
data = {"key": datetime.date(2023, 2, 15)}
result = dicttoxml.dicttoxml(data, attr_type=False)
assert b"<key>2023-02-15</key>" in result

def test_datetime_conversion_with_attr_type(self):
data = {"key": datetime.datetime(2023, 2, 15, 12, 30, 45)}
result = dicttoxml.dicttoxml(data, attr_type=True)
assert b'<key type="str">2023-02-15T12:30:45</key>' in result

def test_date_conversion_with_attr_type(self):
data = {"key": datetime.date(2023, 2, 15)}
result = dicttoxml.dicttoxml(data, attr_type=True)
assert b'<key type="str">2023-02-15</key>' in result

def test_datetime_conversion_with_custom_attributes(self):
data = {"key": datetime.datetime(2023, 2, 15, 12, 30, 45)}
result = dicttoxml.dicttoxml(data, attr_type=False, custom_root="custom")
assert b"<custom><key>2023-02-15T12:30:45</key></custom>" in result

def test_date_conversion_with_custom_attributes(self):
data = {"key": datetime.date(2023, 2, 15)}
result = dicttoxml.dicttoxml(data, attr_type=False, custom_root="custom")
assert b"<custom><key>2023-02-15</key></custom>" in result
Loading