Skip to content

Commit d279b17

Browse files
committed
[#151] Add XML roundtrip test
1 parent 22aed16 commit d279b17

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

flattentool/json_input.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(self, json_filename=None, root_json_dict=None, schema_parser=None,
5454
self.root_id = root_id
5555
self.use_titles = use_titles
5656
self.id_name = id_name
57+
self.xml = xml
5758
if schema_parser:
5859
self.main_sheet = schema_parser.main_sheet
5960
self.sub_sheets = schema_parser.sub_sheets
@@ -63,9 +64,9 @@ def __init__(self, json_filename=None, root_json_dict=None, schema_parser=None,
6364
else:
6465
self.rollup = False
6566

66-
if xml:
67+
if self.xml:
6768
with codecs.open(json_filename, 'rb') as xml_file:
68-
root_json_dict = xmltodict.parse(xml_file, cdata_key='')['iati-activities']
69+
root_json_dict = xmltodict.parse(xml_file)['iati-activities']
6970
json_filename = None
7071

7172
if json_filename is None and root_json_dict is None:
@@ -128,6 +129,10 @@ def parse_json_dict(self, json_dict, sheet, json_key=None, parent_name='', flatt
128129

129130
for key, value in json_dict.items():
130131
if type(value) in BASIC_TYPES:
132+
if self.xml and key == '#text':
133+
# Handle the text output from xmltodict
134+
key = ''
135+
parent_name = parent_name.strip('/')
131136
flattened_dict[sheet_key(sheet, parent_name+key)] = value
132137
elif hasattr(value, 'items'):
133138
self.parse_json_dict(

flattentool/tests/test_roundtrip.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from flattentool import unflatten, flatten
22
import json
3-
import pytest
43
import sys
54
import os
5+
import xmltodict
6+
import pytest
67

78

89
@pytest.mark.parametrize('output_format', ['xlsx', 'csv'])
@@ -93,3 +94,37 @@ def test_roundtrip_360_rollup(tmpdir, use_titles):
9394
original_json = json.load(open(input_name))
9495
roundtripped_json = json.load(tmpdir.join('roundtrip.json'))
9596
assert original_json == roundtripped_json
97+
98+
99+
def to_dict(x):
100+
''' Converts a nested dictlike objects e.g. OrderedDict's, to a dicts. '''
101+
if hasattr(x, 'items'):
102+
return dict((k, to_dict(v)) for k,v in x.items())
103+
elif isinstance(x, list):
104+
return [to_dict(y) for y in x]
105+
else:
106+
return x
107+
108+
109+
@pytest.mark.parametrize('output_format', ['xlsx', 'csv'])
110+
def test_roundtrip_xml(tmpdir, output_format):
111+
input_name = 'examples/iati/expected.xml'
112+
flatten(
113+
input_name=input_name,
114+
output_name=tmpdir.join('flattened').strpath+'.'+output_format,
115+
output_format=output_format,
116+
root_list_path='iati-activity',
117+
id_name='iati-identifier',
118+
xml=True)
119+
unflatten(
120+
input_name=tmpdir.join('flattened').strpath+'.'+output_format,
121+
output_name=tmpdir.join('roundtrip.xml').strpath,
122+
input_format=output_format,
123+
root_list_path='iati-activity',
124+
id_name='iati-identifier',
125+
xml=True)
126+
original_xml = open(input_name, 'rb')
127+
roundtripped_xml = tmpdir.join('roundtrip.xml').open('rb')
128+
129+
# Compare without ordering, by wrapping in to_dict
130+
assert to_dict(xmltodict.parse(original_xml)) == to_dict(xmltodict.parse(roundtripped_xml))

0 commit comments

Comments
 (0)