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

Source Code for Module lxml.tests.test_io

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  IO test cases that apply to both etree and ElementTree 
  5  """ 
  6   
  7  import unittest 
  8  import tempfile, gzip, os, gc 
  9   
 10  from common_imports import etree, ElementTree, fileInTestDir 
 11  from common_imports import SillyFileLike, LargeFileLike 
 12   
13 -class IOTestCaseBase(unittest.TestCase):
14 """(c)ElementTree compatibility for IO functions/methods 15 """ 16 etree = None 17
18 - def setUp(self):
19 """Setting up a minimal tree 20 """ 21 self.root = self.etree.Element('a') 22 self.root_str = self.etree.tostring(self.root) 23 self.tree = self.etree.ElementTree(self.root)
24
25 - def tearDown(self):
26 gc.collect()
27
28 - def test_write_filename(self):
29 # (c)ElementTree supports filename strings as write argument 30 31 handle, filename = tempfile.mkstemp(suffix=".xml") 32 self.tree.write(filename) 33 try: 34 self.assertEqual(open(filename).read(), self.root_str) 35 finally: 36 os.close(handle) 37 os.remove(filename)
38
40 # (c)ElementTree supports gzip instance as parse argument 41 handle, filename = tempfile.mkstemp(suffix=".xml.gz") 42 f = gzip.open(filename, 'wb') 43 f.write(self.root_str) 44 f.close() 45 try: 46 f_gz = gzip.open(filename, 'r') 47 tree = self.etree.parse(f_gz) 48 f_gz.close() 49 self.assertEqual(self.etree.tostring(tree.getroot()), self.root_str) 50 finally: 51 os.close(handle) 52 os.remove(filename)
53
55 # (c)ElementTree class ElementTree has a 'parse' method that returns 56 # the root of the tree 57 58 # parse from filename 59 60 handle, filename = tempfile.mkstemp(suffix=".xml") 61 open(filename, 'wb').write(self.root_str) 62 try: 63 tree = self.etree.ElementTree() 64 root = tree.parse(filename) 65 self.assertEqual(self.etree.tostring(root), self.root_str) 66 finally: 67 os.close(handle) 68 os.remove(filename)
69
71 handle, filename = tempfile.mkstemp(suffix=".xml") 72 open(filename, 'wb').write(self.root_str) 73 try: 74 tree = self.etree.ElementTree() 75 root = tree.parse(filename) 76 # and now do it again; previous content should still be there 77 root2 = tree.parse(filename) 78 self.assertEquals('a', root.tag) 79 self.assertEquals('a', root2.tag) 80 # now remove all references to root2, and parse again 81 del root2 82 root3 = tree.parse(filename) 83 self.assertEquals('a', root.tag) 84 self.assertEquals('a', root3.tag) 85 # root2's memory should've been freed here 86 # XXX how to check? 87 finally: 88 os.close(handle) 89 os.remove(filename)
90
92 # (c)ElementTree class ElementTree has a 'parse' method that returns 93 # the root of the tree 94 95 # parse from file object 96 97 handle, filename = tempfile.mkstemp(suffix=".xml") 98 os.write(handle, self.root_str) 99 try: 100 f = open(filename, 'r') 101 tree = self.etree.ElementTree() 102 root = tree.parse(f) 103 f.close() 104 self.assertEqual(self.etree.tostring(root), self.root_str) 105 finally: 106 os.close(handle) 107 os.remove(filename)
108
110 # (c)ElementTree class ElementTree has a 'parse' method that returns 111 # the root of the tree 112 113 # parse from unamed file object 114 f = SillyFileLike() 115 root = self.etree.ElementTree().parse(f) 116 self.assert_(root.tag.endswith('foo'))
117
119 # parse from unamed file object 120 f = LargeFileLike() 121 tree = self.etree.parse(f) 122 root = tree.getroot() 123 self.assert_(root.tag.endswith('root'))
124
126 class LocalError(Exception): 127 pass
128 class TestFile: 129 def read(*args): 130 raise LocalError
131 f = TestFile() 132 self.assertRaises(LocalError, self.etree.parse, f) 133
134 - def test_module_parse_fileobject_late_error(self):
135 class LocalError(Exception): 136 pass
137 class TestFile: 138 data = '<root>test</' 139 next_char = iter(data).next 140 counter = 0 141 def read(self, amount=None): 142 if amount is None: 143 while True: 144 self.read(1) 145 else: 146 try: 147 self.counter += 1 148 return self.next_char() 149 except StopIteration: 150 raise LocalError 151 f = TestFile() 152 self.assertRaises(LocalError, self.etree.parse, f) 153 self.assertEquals(f.counter, len(f.data)+1) 154
155 - def test_module_parse_fileobject_type_error(self):
156 class TestFile: 157 def read(*args): 158 return 1
159 f = TestFile() 160 self.assertRaises(TypeError, self.etree.parse, f) 161 162
163 -class ETreeIOTestCase(IOTestCaseBase):
164 etree = etree
165 166 if ElementTree:
167 - class ElementTreeIOTestCase(IOTestCaseBase):
168 etree = ElementTree
169
170 -def test_suite():
171 suite = unittest.TestSuite() 172 suite.addTests([unittest.makeSuite(ETreeIOTestCase)]) 173 if ElementTree: 174 suite.addTests([unittest.makeSuite(ElementTreeIOTestCase)]) 175 return suite
176 177 if __name__ == '__main__': 178 unittest.main() 179