From 156fa23537695aad4ad463eede979a50677b1772 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Fri, 22 Jul 2022 22:59:55 +0530 Subject: [PATCH 01/12] feat: use pytest fully now --- tests/test_dict2xml.py | 3 ++- tests/test_json2xml.py | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index c3b4fed3..2b1b5587 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -1,9 +1,10 @@ import unittest +import pytest 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'} diff --git a/tests/test_json2xml.py b/tests/test_json2xml.py index beaab636..cef55186 100644 --- a/tests/test_json2xml.py +++ b/tests/test_json2xml.py @@ -2,9 +2,8 @@ """Tests for `json2xml` package.""" - +import pytest import json -import unittest from collections import OrderedDict import pytest @@ -17,7 +16,7 @@ readfromurl) -class TestJson2xml(unittest.TestCase): +class TestJson2xml: """Tests for `json2xml` package.""" def setUp(self): From 737e7b1258f94745aa840842a9909fda77b0ac25 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Fri, 22 Jul 2022 23:12:39 +0530 Subject: [PATCH 02/12] feat: better logs --- pytest.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytest.ini b/pytest.ini index 1fd9f400..48a7edab 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,4 +1,4 @@ [pytest] -log_cli = True -log_cli_level = INFO +log_cli=True +log_cli_level=INFO From faae9960af23cecaf6daf804a849bf3afac678f0 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Fri, 22 Jul 2022 23:51:39 +0530 Subject: [PATCH 03/12] fix: globally shared ids list that gets mutated We have a ids variable that stores a list of unique ids. The issue is that it is globally shared and can mutate across tests. This caused a failure in one of the test that shouldn't contain id but it does in this case. When the global ids is removed and put into the function where it is used and the reference of ids in other functions properly instantiated, we can see that the crash is gone. - Github Issue: #150 Authored-by: Vinit Kumar Signed-off-by: Vinit Kumar --- json2xml/dicttoxml.py | 4 +++- tests/test_dict2xml.py | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index d81115ca..fe65635b 100755 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -25,7 +25,6 @@ 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: @@ -35,6 +34,7 @@ def make_id(element: str, start: int = 100000, end: int = 999999) -> str: 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: @@ -233,6 +233,7 @@ def dict2xml_str( """ parse dict2xml """ + ids = [] keys_str = ", ".join(str(key) for key in item) if DEBUGMODE: # pragma: no cover LOG.info( @@ -276,6 +277,7 @@ def list2xml_str( item_wrap: bool, list_headers: bool = False, ) -> str: + ids = [] if attr_type: attr["type"] = get_xml_type(item) flat = False diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index 2b1b5587..90926a34 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -1,4 +1,3 @@ -import unittest import pytest from json2xml import dicttoxml From 9aecda9431610bc6a2f003a76e6767da9a00d039 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Fri, 22 Jul 2022 23:59:29 +0530 Subject: [PATCH 04/12] fix: lint errors --- tests/test_dict2xml.py | 2 -- tests/test_json2xml.py | 1 - 2 files changed, 3 deletions(-) diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index 90926a34..7568527e 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -1,5 +1,3 @@ -import pytest - from json2xml import dicttoxml diff --git a/tests/test_json2xml.py b/tests/test_json2xml.py index cef55186..0d3f1bb6 100644 --- a/tests/test_json2xml.py +++ b/tests/test_json2xml.py @@ -6,7 +6,6 @@ import json from collections import OrderedDict -import pytest import xmltodict from pyexpat import ExpatError From 9fe4cf3736157198795dbd80a8c8fbf35ebd6f7e Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 23 Jul 2022 00:04:29 +0530 Subject: [PATCH 05/12] fix: remove blank lines --- json2xml/dicttoxml.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index fe65635b..fbf7f59b 100755 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -25,8 +25,6 @@ LOG = logging.getLogger("dicttoxml") # pragma: no cover - - def make_id(element: str, start: int = 100000, end: int = 999999) -> str: """Returns a random integer""" return f"{element}_{randint(start, end)}" From 5980e61766414672f6e4dc4301cbc586e3ae56d6 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 23 Jul 2022 00:14:41 +0530 Subject: [PATCH 06/12] fix: add type hints --- json2xml/dicttoxml.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index fbf7f59b..31e21ffe 100755 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -231,7 +231,7 @@ def dict2xml_str( """ parse dict2xml """ - ids = [] + ids: list[str] = [] # initialize list of unique ids keys_str = ", ".join(str(key) for key in item) if DEBUGMODE: # pragma: no cover LOG.info( @@ -275,7 +275,7 @@ def list2xml_str( item_wrap: bool, list_headers: bool = False, ) -> str: - ids = [] + ids: list[str] = [] # initialize list of unique ids if attr_type: attr["type"] = get_xml_type(item) flat = False From 9397a18413793f8b680e918303a6f07ebd85f2fc Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 23 Jul 2022 00:35:25 +0530 Subject: [PATCH 07/12] feat: improve test coverage --- tests/test_dict2xml.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index 7568527e..feae7dc6 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -2,7 +2,6 @@ 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'} @@ -117,6 +116,12 @@ def test_get_xml_type(self): assert dicttoxml.get_xml_type(True) == "bool" assert dicttoxml.get_xml_type({}) == "dict" + def test_escape_xml(self): + elem = "&" + escaped_string = dicttoxml.escape_xml(elem) + assert "&" != escaped_string + assert "&" == escaped_string + def test_list_parent_elements(self): default_item_func = dicttoxml.default_item_func From c83a45ec4359f5b8c02d65bd727af012fd7f85cb Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 23 Jul 2022 00:41:54 +0530 Subject: [PATCH 08/12] feat: improve test coverage --- tests/test_dict2xml.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index feae7dc6..752b13c5 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -116,6 +116,11 @@ def test_get_xml_type(self): 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) == True + assert dicttoxml.is_primitive_type("abc") == True + assert dicttoxml.is_primitive_type({}) == False + def test_escape_xml(self): elem = "&" escaped_string = dicttoxml.escape_xml(elem) From a483e00187988e903fabaec5acd645b04c6b5fad Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 23 Jul 2022 00:43:45 +0530 Subject: [PATCH 09/12] feat: improve test coverage --- tests/test_dict2xml.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index 752b13c5..ca65b1d9 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -117,9 +117,9 @@ def test_get_xml_type(self): assert dicttoxml.get_xml_type({}) == "dict" def test_is_primitive_type(self): - assert dicttoxml.is_primitive_type(True) == True - assert dicttoxml.is_primitive_type("abc") == True - assert dicttoxml.is_primitive_type({}) == False + 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 = "&" From 7e97468df380f4464bd62db2cc15fb0e58b735ab Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 23 Jul 2022 00:56:44 +0530 Subject: [PATCH 10/12] feat: test some more --- tests/test_dict2xml.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index ca65b1d9..ccceb8b2 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -110,6 +110,12 @@ 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) == True + assert dicttoxml.key_is_valid_xml(invalid_key) == False + def test_get_xml_type(self): assert dicttoxml.get_xml_type("abc") == "str" assert dicttoxml.get_xml_type(1) == "int" From 2f8b31f28db18946fc940a5cfabb03c09c7235bc Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 23 Jul 2022 01:02:36 +0530 Subject: [PATCH 11/12] fix: lint --- tests/test_dict2xml.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index ccceb8b2..6e3e480a 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -113,8 +113,8 @@ def test_get_unique_id(self): def test_key_is_valid_xml(self): valid_key = "li" invalid_key = "/li" - assert dicttoxml.key_is_valid_xml(valid_key) == True - assert dicttoxml.key_is_valid_xml(invalid_key) == False + 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" From 8345dff45449c1ccdf76b2fd5a66a2411a73a7d9 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 23 Jul 2022 01:08:51 +0530 Subject: [PATCH 12/12] fix: lint --- tests/test_dict2xml.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_dict2xml.py b/tests/test_dict2xml.py index 6e3e480a..dbb2e752 100644 --- a/tests/test_dict2xml.py +++ b/tests/test_dict2xml.py @@ -133,6 +133,10 @@ def test_escape_xml(self): 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