test_dicttoxml.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import sys
  2. from xmltodict import parse, unparse
  3. from collections import OrderedDict
  4. import unittest
  5. import re
  6. from textwrap import dedent
  7. IS_JYTHON = sys.platform.startswith('java')
  8. _HEADER_RE = re.compile(r'^[^\n]*\n')
  9. def _strip(fullxml):
  10. return _HEADER_RE.sub('', fullxml)
  11. class DictToXMLTestCase(unittest.TestCase):
  12. def test_root(self):
  13. obj = {'a': None}
  14. self.assertEqual(obj, parse(unparse(obj)))
  15. self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
  16. def test_simple_cdata(self):
  17. obj = {'a': 'b'}
  18. self.assertEqual(obj, parse(unparse(obj)))
  19. self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
  20. def test_cdata(self):
  21. obj = {'a': {'#text': 'y'}}
  22. self.assertEqual(obj, parse(unparse(obj), force_cdata=True))
  23. self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
  24. def test_attrib(self):
  25. obj = {'a': {'@href': 'x'}}
  26. self.assertEqual(obj, parse(unparse(obj)))
  27. self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
  28. def test_attrib_and_cdata(self):
  29. obj = {'a': {'@href': 'x', '#text': 'y'}}
  30. self.assertEqual(obj, parse(unparse(obj)))
  31. self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
  32. def test_list(self):
  33. obj = {'a': {'b': ['1', '2', '3']}}
  34. self.assertEqual(obj, parse(unparse(obj)))
  35. self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
  36. def test_generator(self):
  37. obj = {'a': {'b': ['1', '2', '3']}}
  38. def lazy_obj():
  39. return {'a': {'b': (i for i in ('1', '2', '3'))}}
  40. self.assertEqual(obj, parse(unparse(lazy_obj())))
  41. self.assertEqual(unparse(lazy_obj()),
  42. unparse(parse(unparse(lazy_obj()))))
  43. def test_no_root(self):
  44. self.assertRaises(ValueError, unparse, {})
  45. def test_multiple_roots(self):
  46. self.assertRaises(ValueError, unparse, {'a': '1', 'b': '2'})
  47. self.assertRaises(ValueError, unparse, {'a': ['1', '2', '3']})
  48. def test_no_root_nofulldoc(self):
  49. self.assertEqual(unparse({}, full_document=False), '')
  50. def test_multiple_roots_nofulldoc(self):
  51. obj = OrderedDict((('a', 1), ('b', 2)))
  52. xml = unparse(obj, full_document=False)
  53. self.assertEqual(xml, '<a>1</a><b>2</b>')
  54. obj = {'a': [1, 2]}
  55. xml = unparse(obj, full_document=False)
  56. self.assertEqual(xml, '<a>1</a><a>2</a>')
  57. def test_nested(self):
  58. obj = {'a': {'b': '1', 'c': '2'}}
  59. self.assertEqual(obj, parse(unparse(obj)))
  60. self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
  61. obj = {'a': {'b': {'c': {'@a': 'x', '#text': 'y'}}}}
  62. self.assertEqual(obj, parse(unparse(obj)))
  63. self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
  64. def test_semistructured(self):
  65. xml = '<a>abc<d/>efg</a>'
  66. self.assertEqual(_strip(unparse(parse(xml))),
  67. '<a><d></d>abcefg</a>')
  68. def test_preprocessor(self):
  69. obj = {'a': OrderedDict((('b:int', [1, 2]), ('b', 'c')))}
  70. def p(key, value):
  71. try:
  72. key, _ = key.split(':')
  73. except ValueError:
  74. pass
  75. return key, value
  76. self.assertEqual(_strip(unparse(obj, preprocessor=p)),
  77. '<a><b>1</b><b>2</b><b>c</b></a>')
  78. def test_preprocessor_skipkey(self):
  79. obj = {'a': {'b': 1, 'c': 2}}
  80. def p(key, value):
  81. if key == 'b':
  82. return None
  83. return key, value
  84. self.assertEqual(_strip(unparse(obj, preprocessor=p)),
  85. '<a><c>2</c></a>')
  86. if not IS_JYTHON:
  87. # Jython's SAX does not preserve attribute order
  88. def test_attr_order_roundtrip(self):
  89. xml = '<root a="1" b="2" c="3"></root>'
  90. self.assertEqual(xml, _strip(unparse(parse(xml))))
  91. def test_pretty_print(self):
  92. obj = {'a': OrderedDict((
  93. ('b', [{'c': [1, 2]}, 3]),
  94. ('x', 'y'),
  95. ))}
  96. newl = '\n'
  97. indent = '....'
  98. xml = dedent('''\
  99. <?xml version="1.0" encoding="utf-8"?>
  100. <a>
  101. ....<b>
  102. ........<c>1</c>
  103. ........<c>2</c>
  104. ....</b>
  105. ....<b>3</b>
  106. ....<x>y</x>
  107. </a>''')
  108. self.assertEqual(xml, unparse(obj, pretty=True,
  109. newl=newl, indent=indent))
  110. def test_encoding(self):
  111. try:
  112. value = unichr(39321)
  113. except NameError:
  114. value = chr(39321)
  115. obj = {'a': value}
  116. utf8doc = unparse(obj, encoding='utf-8')
  117. latin1doc = unparse(obj, encoding='iso-8859-1')
  118. self.assertEqual(parse(utf8doc), parse(latin1doc))
  119. self.assertEqual(parse(utf8doc), obj)
  120. def test_fulldoc(self):
  121. xml_declaration_re = re.compile(
  122. '^' + re.escape('<?xml version="1.0" encoding="utf-8"?>'))
  123. self.assertTrue(xml_declaration_re.match(unparse({'a': 1})))
  124. self.assertFalse(
  125. xml_declaration_re.match(unparse({'a': 1}, full_document=False)))
  126. def test_non_string_value(self):
  127. obj = {'a': 1}
  128. self.assertEqual('<a>1</a>', _strip(unparse(obj)))
  129. def test_non_string_attr(self):
  130. obj = {'a': {'@attr': 1}}
  131. self.assertEqual('<a attr="1"></a>', _strip(unparse(obj)))
  132. def test_short_empty_elements(self):
  133. if sys.version_info[0] < 3:
  134. return
  135. obj = {'a': None}
  136. self.assertEqual('<a/>', _strip(unparse(obj, short_empty_elements=True)))
  137. def test_namespace_support(self):
  138. obj = OrderedDict((
  139. ('http://defaultns.com/:root', OrderedDict((
  140. ('@xmlns', OrderedDict((
  141. ('', 'http://defaultns.com/'),
  142. ('a', 'http://a.com/'),
  143. ('b', 'http://b.com/'),
  144. ))),
  145. ('http://defaultns.com/:x', OrderedDict((
  146. ('@http://a.com/:attr', 'val'),
  147. ('#text', '1'),
  148. ))),
  149. ('http://a.com/:y', '2'),
  150. ('http://b.com/:z', '3'),
  151. ))),
  152. ))
  153. ns = {
  154. 'http://defaultns.com/': '',
  155. 'http://a.com/': 'a',
  156. 'http://b.com/': 'b',
  157. }
  158. expected_xml = '''<?xml version="1.0" encoding="utf-8"?>
  159. <root xmlns="http://defaultns.com/" xmlns:a="http://a.com/" \
  160. xmlns:b="http://b.com/"><x a:attr="val">1</x><a:y>2</a:y><b:z>3</b:z></root>'''
  161. xml = unparse(obj, namespaces=ns)
  162. self.assertEqual(xml, expected_xml)
  163. def test_boolean_unparse(self):
  164. expected_xml = '<?xml version="1.0" encoding="utf-8"?>\n<x>true</x>'
  165. xml = unparse(dict(x=True))
  166. self.assertEqual(xml, expected_xml)
  167. expected_xml = '<?xml version="1.0" encoding="utf-8"?>\n<x>false</x>'
  168. xml = unparse(dict(x=False))
  169. self.assertEqual(xml, expected_xml)