|
1 | 1 | from flattentool import unflatten, flatten
|
2 | 2 | import json
|
3 |
| -import pytest |
4 | 3 | import sys
|
5 | 4 | import os
|
| 5 | +import xmltodict |
| 6 | +import pytest |
6 | 7 |
|
7 | 8 |
|
8 | 9 | @pytest.mark.parametrize('output_format', ['xlsx', 'csv'])
|
@@ -93,3 +94,37 @@ def test_roundtrip_360_rollup(tmpdir, use_titles):
|
93 | 94 | original_json = json.load(open(input_name))
|
94 | 95 | roundtripped_json = json.load(tmpdir.join('roundtrip.json'))
|
95 | 96 | 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