def test_module_parse_gzipobject(self): # (c)ElementTree supports gzip instance as parse argument with tmpfile(suffix=".xml.gz") as filename: with gzip.open(filename, 'wb') as f: f.write(self.root_str) with gzip.open(filename, 'rb') as f_gz: tree = self.etree.parse(f_gz) self.assertEqual(self.etree.tostring(tree.getroot()), self.root_str)
def test_class_parse_filename(self): # (c)ElementTree class ElementTree has a 'parse' method that returns # the root of the tree # parse from filename with tmpfile(suffix=".xml") as filename: write_to_file(filename, self.root_str, 'wb') tree = self.etree.ElementTree() root = tree.parse(filename) self.assertEqual(self.etree.tostring(root), self.root_str)
def test_class_parse_filename_remove_previous(self): with tmpfile(suffix=".xml") as filename: write_to_file(filename, self.root_str, 'wb') tree = self.etree.ElementTree() root = tree.parse(filename) # and now do it again; previous content should still be there root2 = tree.parse(filename) self.assertEqual('a', root.tag) self.assertEqual('a', root2.tag) # now remove all references to root2, and parse again del root2 root3 = tree.parse(filename) self.assertEqual('a', root.tag) self.assertEqual('a', root3.tag)
def test_write_filename_special_percent(self): # '%20' is a URL escaped space character. before_test = os.listdir(tempfile.gettempdir()) def difference(filenames): return sorted(fn for fn in set(filenames).difference(before_test) if fn.startswith('lxmltmp-')) with tmpfile(prefix="lxmltmp-p%20p", suffix=".xml") as filename: try: before_write = os.listdir(tempfile.gettempdir()) self.tree.write(filename) after_write = os.listdir(tempfile.gettempdir()) self.assertEqual( read_file(filename, 'rb').replace(b'\n', b''), self.root_str) except (AssertionError, IOError, OSError): print("Before write: %s, after write: %s" % (difference(before_write), difference(after_write))) raise
def test_write_filename(self): # (c)ElementTree supports filename strings as write argument with tmpfile(prefix="p", suffix=".xml") as filename: self.tree.write(filename) self.assertEqual( read_file(filename, 'rb').replace(b'\n', b''), self.root_str)
def test_write_filename_special_plus(self): # '+' is used as an escaped space character in URLs. with tmpfile(prefix="p+", suffix=".xml") as filename: self.tree.write(filename) self.assertEqual( read_file(filename, 'rb').replace(b'\n', b''), self.root_str)