Skip to content

Commit

Permalink
feat: add some more tests (#217)
Browse files Browse the repository at this point in the history
* feat: add some more tests

* fix: lint

* fix: old action errors for old nodejs

* fix: add missing test_ that didn't made the test run
  • Loading branch information
vinitkumar authored Sep 20, 2024
1 parent dff4620 commit 56613b8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
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

0 comments on commit 56613b8

Please sign in to comment.