Package lxml :: Package tests :: Module test_incremental_xmlfile
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_incremental_xmlfile

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  Tests for the incremental XML serialisation API. 
  5   
  6  Tests require Python 2.5 or later. 
  7  """ 
  8   
  9  from __future__ import with_statement 
 10   
 11  import unittest 
 12  import tempfile, os, sys 
 13   
 14  this_dir = os.path.dirname(__file__) 
 15  if this_dir not in sys.path: 
 16      sys.path.insert(0, this_dir) # needed for Py3 
 17   
 18  from common_imports import etree, BytesIO 
 19  from common_imports import HelperTestCase 
 20   
21 -class _XmlFileTestCaseBase(HelperTestCase):
22 _file = None # to be set by specific subtypes below 23
24 - def test_element(self):
25 with etree.xmlfile(self._file) as xf: 26 with xf.element('test'): 27 pass 28 self.assertXml('<test></test>')
29
30 - def test_element_write_text(self):
31 with etree.xmlfile(self._file) as xf: 32 with xf.element('test'): 33 xf.write('toast') 34 self.assertXml('<test>toast</test>')
35
36 - def test_element_nested(self):
37 with etree.xmlfile(self._file) as xf: 38 with xf.element('test'): 39 with xf.element('toast'): 40 with xf.element('taste'): 41 xf.write('conTent') 42 self.assertXml('<test><toast><taste>conTent</taste></toast></test>')
43
45 with etree.xmlfile(self._file) as xf: 46 with xf.element('test'): 47 xf.write('con') 48 with xf.element('toast'): 49 xf.write('tent') 50 with xf.element('taste'): 51 xf.write('inside') 52 xf.write('tnet') 53 xf.write('noc') 54 self.assertXml('<test>con<toast>tent<taste>inside</taste>' 55 'tnet</toast>noc</test>')
56
57 - def test_write_Element(self):
58 with etree.xmlfile(self._file) as xf: 59 xf.write(etree.Element('test')) 60 self.assertXml('<test/>')
61
63 element = etree.Element('test') 64 with etree.xmlfile(self._file) as xf: 65 with xf.element('test'): 66 for i in range(100): 67 xf.write(element) 68 69 tree = self._parse_file() 70 self.assertTrue(tree is not None) 71 self.assertEqual(100, len(tree.getroot())) 72 self.assertEqual(set(['test']), set(el.tag for el in tree.getroot()))
73
74 - def test_namespace_nsmap(self):
75 with etree.xmlfile(self._file) as xf: 76 with xf.element('{nsURI}test', nsmap={'x': 'nsURI'}): 77 pass 78 self.assertXml('<x:test xmlns:x="nsURI"></x:test>')
79
81 with etree.xmlfile(self._file) as xf: 82 with xf.element('test', nsmap={'x': 'nsURI'}): 83 with xf.element('{nsURI}toast'): 84 pass 85 self.assertXml('<test xmlns:x="nsURI"><x:toast></x:toast></test>')
86
87 - def test_anonymous_namespace(self):
88 with etree.xmlfile(self._file) as xf: 89 with xf.element('{nsURI}test'): 90 pass 91 self.assertXml('<ns0:test xmlns:ns0="nsURI"></ns0:test>')
92
94 with etree.xmlfile(self._file) as xf: 95 with xf.element('test'): 96 with xf.element('{nsURI}toast'): 97 pass 98 self.assertXml('<test><ns0:toast xmlns:ns0="nsURI"></ns0:toast></test>')
99
100 - def test_default_namespace(self):
101 with etree.xmlfile(self._file) as xf: 102 with xf.element('{nsURI}test', nsmap={None: 'nsURI'}): 103 pass 104 self.assertXml('<test xmlns="nsURI"></test>')
105
107 with etree.xmlfile(self._file) as xf: 108 with xf.element('{nsURI}test', nsmap={None: 'nsURI'}): 109 with xf.element('{nsURI}toast'): 110 pass 111 self.assertXml('<test xmlns="nsURI"><toast></toast></test>')
112
113 - def test_pi(self):
114 with etree.xmlfile(self._file) as xf: 115 xf.write(etree.ProcessingInstruction('pypi')) 116 with xf.element('test'): 117 pass 118 self.assertXml('<?pypi ?><test></test>')
119
120 - def test_comment(self):
121 with etree.xmlfile(self._file) as xf: 122 xf.write(etree.Comment('a comment')) 123 with xf.element('test'): 124 pass 125 self.assertXml('<!--a comment--><test></test>')
126
127 - def test_attribute(self):
128 with etree.xmlfile(self._file) as xf: 129 with xf.element('test', attrib={'k': 'v'}): 130 pass 131 self.assertXml('<test k="v"></test>')
132
133 - def test_escaping(self):
134 with etree.xmlfile(self._file) as xf: 135 with xf.element('test'): 136 xf.write('Comments: <!-- text -->\n') 137 xf.write('Entities: &amp;') 138 self.assertXml( 139 '<test>Comments: &lt;!-- text --&gt;\nEntities: &amp;amp;</test>')
140
141 - def test_encoding(self):
142 with etree.xmlfile(self._file, encoding='utf16') as xf: 143 with xf.element('test'): 144 xf.write('toast') 145 self.assertXml('<test>toast</test>', encoding='utf16')
146
148 try: 149 with etree.xmlfile(self._file) as xf: 150 xf.write('toast') 151 except etree.LxmlSyntaxError: 152 self.assertTrue(True) 153 else: 154 self.assertTrue(False)
155
157 with etree.xmlfile(self._file) as xf: 158 with xf.element('test'): 159 pass 160 try: 161 xf.write('toast') 162 except etree.LxmlSyntaxError: 163 self.assertTrue(True) 164 else: 165 self.assertTrue(False)
166
168 with etree.xmlfile(self._file) as xf: 169 with xf.element('test'): 170 pass 171 try: 172 xf.write(etree.Element('test')) 173 except etree.LxmlSyntaxError: 174 self.assertTrue(True) 175 else: 176 self.assertTrue(False)
177
178 - def _read_file(self):
179 self._file.seek(0) 180 return self._file.read()
181
182 - def _parse_file(self):
183 self._file.seek(0) 184 return etree.parse(self._file)
185
186 - def tearDown(self):
187 self._file.close()
188
189 - def assertXml(self, expected, encoding='utf8'):
190 self.assertEqual(self._read_file().decode(encoding), expected)
191 192
193 -class BytesIOXmlFileTestCase(_XmlFileTestCaseBase):
194 - def setUp(self):
195 self._file = BytesIO()
196
197 -class TempXmlFileTestCase(_XmlFileTestCaseBase):
198 - def setUp(self):
199 self._file = tempfile.NamedTemporaryFile()
200
201 -class SimpleFileLikeXmlFileTestCase(_XmlFileTestCaseBase):
202 - class SimpleFileLike(object):
203 - def __init__(self, target):
204 self._target = target 205 self.write = target.write 206 self.close = target.close
207
208 - def setUp(self):
209 self._target = BytesIO() 210 self._file = self.SimpleFileLike(self._target)
211
212 - def _read_file(self):
213 return self._target.getvalue()
214
215 - def _parse_file(self):
216 self._target.seek(0) 217 return etree.parse(self._target)
218 219
220 -def test_suite():
221 suite = unittest.TestSuite() 222 suite.addTests([unittest.makeSuite(BytesIOXmlFileTestCase), 223 unittest.makeSuite(TempXmlFileTestCase), 224 unittest.makeSuite(SimpleFileLikeXmlFileTestCase), 225 ]) 226 return suite
227 228 if __name__ == '__main__': 229 print('to test use test.py %s' % __file__) 230