From 40b6a84c6aa2b374e0db3c1fe3935f46462e288e Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Fri, 20 Sep 2024 23:01:42 +0530 Subject: [PATCH 1/4] feat: add some more tests --- json2xml/dicttoxml.py | 2 +- tests/test_dict2xml.py | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index cb529c9..2ba6af9 100644 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -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") @@ -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)}" diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index 07731a5..12265eb 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -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"2023-02-15T12:30:45" 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"123" + + 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"value1value2" + + 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"1stringvalue" + + 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"" + + def test_list_to_xml_with_special_characters(self): + data = {"items": ["", "&", '"quote"', "'single quote'"]} + result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True) + assert result == b"<tag>&"quote"'single quote'" + + def datetime_conversion_with_isoformat(self): + data = {"key": datetime.datetime(2023, 2, 15, 12, 30, 45)} + result = dicttoxml.dicttoxml(data, attr_type=False) + assert b"2023-02-15T12:30:45" 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"2023-02-15" 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'2023-02-15T12:30:45' 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'2023-02-15' 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"2023-02-15T12:30:45" 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"2023-02-15" in result \ No newline at end of file From 1d011eda4c29d02524d26e644c26d849549edba0 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Fri, 20 Sep 2024 23:06:32 +0530 Subject: [PATCH 2/4] fix: lint --- tests/test_dict2xml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index 12265eb..602b70c 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -498,4 +498,4 @@ def test_datetime_conversion_with_custom_attributes(self): 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"2023-02-15" in result \ No newline at end of file + assert b"2023-02-15" in result From db26e74f2677f5432f6e9f901a84358a8d8ec3df Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Fri, 20 Sep 2024 23:19:01 +0530 Subject: [PATCH 3/4] fix: old action errors for old nodejs --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f8c5e08..836a34b 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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: | From 190b1624d228fbe984ade2907d5c3e40b77b2f08 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Fri, 20 Sep 2024 23:24:05 +0530 Subject: [PATCH 4/4] fix: add missing test_ that didn't made the test run --- tests/test_dict2xml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index 602b70c..7a54ab7 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -470,7 +470,7 @@ def test_list_to_xml_with_special_characters(self): result = dicttoxml.dicttoxml(data, root=False, attr_type=False, item_wrap=True) assert result == b"<tag>&"quote"'single quote'" - def datetime_conversion_with_isoformat(self): + 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"2023-02-15T12:30:45" in result