From 659339f0bcc88e1e1793b8d127d0071f04b5eca9 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sun, 24 Oct 2021 00:48:41 +0530 Subject: [PATCH] feat: reproduce the error in the test (#90) --- .github/workflows/pythonpackage.yml | 2 +- json2xml/dicttoxml.py | 12 ++++++------ tests/test_json2xml.py | 12 ++++++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index 90aedef..c914f28 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: [3.6, 3.7, 3.8, 3.9, pypy3, 3.10.0] + python-version: [3.6, 3.7, 3.8, 3.9, pypy-3.8, 3.10.0] os: [ ubuntu-20.04, macOS-latest, diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index e0d8370..8c29be3 100755 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -143,10 +143,10 @@ def convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent="root"): item_name = item_func(parent) if isinstance(obj, numbers.Number) or isinstance(obj, str): - return convert_kv(item_name, obj, attr_type, cdata) + return convert_kv(key=item_name, val=obj, attr_type=attr_type, attr={}, cdata=cdata) if hasattr(obj, "isoformat"): - return convert_kv(item_name, obj.isoformat(), attr_type, cdata) + return convert_kv(key=item_name, val=obj.isoformat(), attr_type=attr_type, attr={}, cdata=cdata) if isinstance(obj, bool): return convert_bool(item_name, obj, attr_type, cdata) @@ -183,10 +183,10 @@ def convert_dict(obj, ids, parent, attr_type, item_func, cdata, item_wrap): key, attr = make_valid_xml_name(key, attr) if isinstance(val, numbers.Number) or isinstance(val, str): - addline(convert_kv(key, val, attr_type, attr, cdata)) + addline(convert_kv(key=key, val=val, attr_type=attr_type, attr=attr, cdata=cdata)) elif hasattr(val, "isoformat"): # datetime - addline(convert_kv(key, val.isoformat(), attr_type, attr, cdata)) + addline(convert_kv(key=key, val=val.isoformat(), attr_type=attr_type, attr=attr, cdata=cdata)) elif isinstance(val, bool): addline(convert_bool(key, val, attr_type, attr, cdata)) @@ -246,10 +246,10 @@ def convert_list(items, ids, parent, attr_type, item_func, cdata, item_wrap): ) attr = {} if not ids else {"id": "%s_%s" % (this_id, i + 1)} if isinstance(item, numbers.Number) or isinstance(item, str): - addline(convert_kv(item_name, item, attr_type, attr, cdata)) + addline(convert_kv(key=item_name, val=item, attr_type=attr_type, attr=attr, cdata=cdata)) elif hasattr(item, "isoformat"): # datetime - addline(convert_kv(item_name, item.isoformat(), attr_type, attr, cdata)) + addline(convert_kv(key=item_name, val=item.isoformat(), attr_type=attr_type, attr=attr, cdata=cdata)) elif isinstance(item, bool): addline(convert_bool(item_name, item, attr_type, attr, cdata)) diff --git a/tests/test_json2xml.py b/tests/test_json2xml.py index f309577..8fae659 100644 --- a/tests/test_json2xml.py +++ b/tests/test_json2xml.py @@ -8,6 +8,7 @@ from collections import OrderedDict import pytest import xmltodict +import json from json2xml import json2xml from json2xml.utils import readfromjson, readfromstring, readfromurl, JSONReadError, StringReadError, URLReadError @@ -110,3 +111,14 @@ def test_item_wrap(self): # my_item must be present within my_items print(old_dict['all']['my_items']) assert "my_item" in old_dict['all']['my_items'] + + def test_dicttoxml_bug(self): + input_dict = {'response': {'results': {'user': [{'name': 'Ezequiel', 'age': '33', 'city': 'San Isidro'}, {'name': 'Belén', 'age': '30', 'city': 'San Isidro'}]}}} + + # with pytest.raises(AttributeError) as pytest_wrapped_e: + # json2xml.Json2xml(json.dumps(input_dict), wrapper='response', pretty=False, attr_type=False, item_wrap=False).to_xml() + # assert pytest_wrapped_e.type == AttributeError + + xmldata = json2xml.Json2xml(json.dumps(input_dict), wrapper='response', pretty=False, attr_type=False, item_wrap=False).to_xml() + old_dict = xmltodict.parse(xmldata) + assert 'response' in old_dict.keys()