|
12 | 12 | import json |
13 | 13 |
|
14 | 14 | from json2xml import json2xml |
15 | | -from json2xml import dicttoxml |
16 | 15 | from json2xml.utils import InvalidDataError, readfromjson, readfromstring, readfromurl, \ |
17 | 16 | JSONReadError, StringReadError, URLReadError |
18 | 17 |
|
@@ -151,21 +150,6 @@ def test_dicttoxml_bug(self): |
151 | 150 | old_dict = xmltodict.parse(xmldata) |
152 | 151 | assert 'response' in old_dict.keys() |
153 | 152 |
|
154 | | - def test_dict2xml_no_root(self): |
155 | | - payload = {'mock': 'payload'} |
156 | | - result = dicttoxml.dicttoxml(payload, attr_type=False, root=False) |
157 | | - assert b'<mock>payload</mock>' == result |
158 | | - |
159 | | - def test_dict2xml_with_root(self): |
160 | | - payload = {'mock': 'payload'} |
161 | | - result = dicttoxml.dicttoxml(payload, attr_type=False) |
162 | | - assert b'<?xml version="1.0" encoding="UTF-8" ?><root><mock>payload</mock></root>' == result |
163 | | - |
164 | | - def test_dict2xml_with_custom_root(self): |
165 | | - payload = {'mock': 'payload'} |
166 | | - result = dicttoxml.dicttoxml(payload, attr_type=False, custom_root="element") |
167 | | - assert b'<?xml version="1.0" encoding="UTF-8" ?><element><mock>payload</mock></element>' == result |
168 | | - |
169 | 153 | def test_bad_data(self): |
170 | 154 | data = b"!\0a8f" |
171 | 155 | decoded = data.decode("utf-8") |
@@ -199,152 +183,3 @@ def test_read_boolean_data_from_json2(self): |
199 | 183 | assert dict_from_xml["all"]["string_array"]["item"][0]["#text"] == 'a' |
200 | 184 | assert dict_from_xml["all"]["string_array"]["item"][1]["#text"] == 'b' |
201 | 185 | assert dict_from_xml["all"]["string_array"]["item"][2]["#text"] == 'c' |
202 | | - |
203 | | - def test_dict2xml_with_namespaces(self): |
204 | | - data = {'ns1:node1': 'data in namespace 1', 'ns2:node2': 'data in namespace 2'} |
205 | | - namespaces = {'ns1': 'http://www.google.de/ns1', 'ns2': 'http://www.google.de/ns2'} |
206 | | - result = dicttoxml.dicttoxml(data, attr_type=False, xml_namespaces=namespaces) |
207 | | - assert b'<?xml version="1.0" encoding="UTF-8" ?>' \ |
208 | | - b'<root xmlns:ns1="http://www.google.de/ns1" xmlns:ns2="http://www.google.de/ns2">' \ |
209 | | - b'<ns1:node1>data in namespace 1</ns1:node1>' \ |
210 | | - b'<ns2:node2>data in namespace 2</ns2:node2>' \ |
211 | | - b'</root>' == result |
212 | | - |
213 | | - def test_dict2xml_with_xmlns_namespaces(self): |
214 | | - data = {'ns1:node1': 'data in namespace 1', 'ns2:node2': 'data in namespace 2'} |
215 | | - namespaces = {'xmlns': "http://www.w3.org/1999/XSL/Transform"} |
216 | | - result = dicttoxml.dicttoxml(obj=data, attr_type=False, xml_namespaces=namespaces) |
217 | | - assert b'<?xml version="1.0" encoding="UTF-8" ?>' \ |
218 | | - b'<root xmlns="http://www.w3.org/1999/XSL/Transform">' \ |
219 | | - b'<ns1:node1>data in namespace 1</ns1:node1>' \ |
220 | | - b'<ns2:node2>data in namespace 2</ns2:node2>' \ |
221 | | - b'</root>' == result |
222 | | - |
223 | | - def test_dict2xml_with_xsi_location(self): |
224 | | - data = {'bike': 'blue'} |
225 | | - wrapper = 'vehicle' |
226 | | - namespaces = { |
227 | | - 'xsi': { |
228 | | - 'schemaInstance': "http://www.w3.org/2001/XMLSchema-instance", |
229 | | - 'schemaLocation': "https://www.w3schools.com note.xsd" |
230 | | - } |
231 | | - } |
232 | | - result = dicttoxml.dicttoxml(data, custom_root=wrapper, xml_namespaces=namespaces, attr_type=False) |
233 | | - assert b'<?xml version="1.0" encoding="UTF-8" ?>' \ |
234 | | - b'<vehicle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' \ |
235 | | - b'xsi:schemaLocation="https://www.w3schools.com/ note.xsd">' \ |
236 | | - b'<bike>blue</bike>' |
237 | | - b'</vehicle>' == result |
238 | | - |
239 | | - def test_dict2xml_xsi_xmlns(self): |
240 | | - data = {'bike': 'blue'} |
241 | | - wrapper = 'vehicle' |
242 | | - xml_namespace = { |
243 | | - 'xsd': "https://www.w3schools.com/ note.xsd", |
244 | | - 'xmlns': "http://www.google.de/ns1", |
245 | | - 'xsi': { |
246 | | - 'schemaInstance': "http://www.w3.org/2001/XMLSchema-instance", |
247 | | - 'schemaLocation': "https://www.w3schools.com" |
248 | | - }, |
249 | | - |
250 | | - } |
251 | | - result = dicttoxml.dicttoxml(data, custom_root=wrapper, xml_namespaces=xml_namespace, |
252 | | - attr_type=False).decode() |
253 | | - |
254 | | - assert '<?xml version="1.0" encoding="UTF-8" ?>' |
255 | | - '<vehicle xmlns:xsd="https://www.w3schools.com/ note.xsd" xmlns=http://www.google.de/ns1' |
256 | | - 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.w3schools.com">' |
257 | | - '<bike>blue</bike></vehicle>' == result |
258 | | - |
259 | | - def test_dict2xml_with_flat(self): |
260 | | - data = {'flat_list@flat': [1, 2, 3], 'non_flat_list': [4, 5, 6]} |
261 | | - result = dicttoxml.dicttoxml(data, attr_type=False) |
262 | | - assert b'<?xml version="1.0" encoding="UTF-8" ?>' |
263 | | - b'<root><item>1</item><item>2</item><item>3</item>' |
264 | | - b'<non_flat_list><item>4</item><item>5</item><item>6</item></non_flat_list>' |
265 | | - b'</root>' == result |
266 | | - |
267 | | - def test_dict2xml_omit_list(self): |
268 | | - obj = {'list': [ |
269 | | - {'bike': 'blue'}, |
270 | | - {'wheel': 'black'} |
271 | | - ] |
272 | | - } |
273 | | - dicttoxml.dicttoxml(obj, root=False, attr_type=False, item_wrap=False) |
274 | | - assert b'<list><bike>blue</bike><wheel>black</wheel></list>' |
275 | | - |
276 | | - def test_dict2xml_with_val_and_custom_attr(self): |
277 | | - # in order to use @attr in non-dict objects, we need to lift into a dict and combine with @val as key |
278 | | - data = {'list1': [1, 2, 3], 'list2': {'@attrs': {'myattr1': 'myval1', 'myattr2': 'myval2'}, '@val': [4, 5, 6]}} |
279 | | - result = dicttoxml.dicttoxml(data, attr_type=False) |
280 | | - assert b'<?xml version="1.0" encoding="UTF-8" ?>' \ |
281 | | - b'<root><list1><item>1</item><item>2</item><item>3</item></list1>' \ |
282 | | - b'<list2 myattr1="myval1" myattr2="myval2"><item>4</item><item>5</item><item>6</item></list2>' \ |
283 | | - b'</root>' == result |
284 | | - |
285 | | - def test_make_id(self): |
286 | | - make_id_elem = dicttoxml.make_id("li") |
287 | | - assert 'li' in make_id_elem |
288 | | - |
289 | | - def test_get_unique_id(self): |
290 | | - unique_id_elem_1 = dicttoxml.get_unique_id("li") |
291 | | - unique_id_elem_2 = dicttoxml.get_unique_id("li") |
292 | | - unique_id_elem_3 = dicttoxml.get_unique_id("li") |
293 | | - unique_id_elem_4 = dicttoxml.get_unique_id("li") |
294 | | - assert len(list(set({unique_id_elem_1, unique_id_elem_2, unique_id_elem_3, unique_id_elem_4}))) == 4 |
295 | | - |
296 | | - def test_get_xml_type(self): |
297 | | - assert dicttoxml.get_xml_type("abc") == "str" |
298 | | - assert dicttoxml.get_xml_type(1) == "int" |
299 | | - assert dicttoxml.get_xml_type(True) == "bool" |
300 | | - assert dicttoxml.get_xml_type({}) == "dict" |
301 | | - |
302 | | - def test_list_parent_elements(self): |
303 | | - |
304 | | - default_item_func = dicttoxml.default_item_func |
305 | | - item = [{'frame_color': 'red'}, {'frame_color': 'green'}] |
306 | | - conList = dicttoxml.convert_list(items=item, attr_type=False, cdata=False, ids=None, |
307 | | - item_func=default_item_func, item_wrap=False, parent='Bike', list_headers=True) |
308 | | - assert f'{"<Bike<frame_color>red</frame_color></Bike>"}' |
309 | | - '{"<Bike<frame_color>green</frame_color></Bike>"}' == conList |
310 | | - |
311 | | - def test_dict2xml_str_list_header(self): |
312 | | - from json2xml.dicttoxml import dict2xml_str |
313 | | - item_func = dicttoxml.default_item_func |
314 | | - item = {'frame_color': 'red'} |
315 | | - parent = 'Bike' |
316 | | - xml_str = dict2xml_str(attr_type=False, attr={}, item=item, item_func=item_func, |
317 | | - cdata=False, item_name='item', item_wrap=False, parentIsList=True, |
318 | | - parent=parent, list_headers=True) |
319 | | - |
320 | | - assert f'{"<Bike><frame_color>red</frame_color></Bike>"}' == xml_str |
321 | | - |
322 | | - def test_list_headers(self): |
323 | | - dict = {"Bike": [ |
324 | | - {'frame_color': 'red'}, |
325 | | - {'frame_color': 'green'} |
326 | | - ]} |
327 | | - result = dicttoxml.dicttoxml(dict, root=False, item_wrap=False, attr_type=False, list_headers=True) |
328 | | - assert b'<Bike><frame_color>red</frame_color></Bike>' |
329 | | - '<Bike><frame_color>green</frame_color></Bike>' == result |
330 | | - |
331 | | - def test_list_headers_nested(self): |
332 | | - dict = {"transport": { |
333 | | - "Bike": [ |
334 | | - {'frame_color': 'red'}, |
335 | | - {'frame_color': 'green'} |
336 | | - ]} |
337 | | - } |
338 | | - result = dicttoxml.dicttoxml(dict, root=False, item_wrap=False, attr_type=False, list_headers=True) |
339 | | - assert b'<transport><Bike><frame_color>red</frame_color></Bike>' |
340 | | - b'<Bike><frame_color>green</frame_color></Bike></transport>' == result |
341 | | - |
342 | | - def test_list_headers_root(self): |
343 | | - dict = {"Bike": [ |
344 | | - {'frame_color': 'red'}, |
345 | | - {'frame_color': 'green'} |
346 | | - ]} |
347 | | - result = dicttoxml.dicttoxml(dict, root=True, item_wrap=False, attr_type=False, list_headers=True) |
348 | | - assert b'<?xml version="1.0" encoding="UTF-8" ?><root>' |
349 | | - b'<Bike><frame_color>red</frame_color><Bike>' |
350 | | - b'<Bike><frame_color>green</frame_color></Bike></root>' == result |
0 commit comments