class SimpleFileLikeXmlFileTestCase(_XmlFileTestCaseBase): class SimpleFileLike(object): def __init__(self, target): self._target = target self.write = target.write self.closed = False def close(self): assert not self.closed self.closed = True self._target.close() def setUp(self): self._target = BytesIO() self._file = self.SimpleFileLike(self._target) def _read_file(self): return self._target.getvalue() def _parse_file(self): self._target.seek(0) return etree.parse(self._target) def test_filelike_not_closing(self): with etree.xmlfile(self._file) as xf: with xf.element('test'): pass self.assertFalse(self._file.closed) def test_filelike_close(self): with etree.xmlfile(self._file, close=True) as xf: with xf.element('test'): pass self.assertTrue(self._file.closed) self._file = None # prevent closing in tearDown()
class SimpleFileLikeXmlFileTestCase(_XmlFileTestCaseBase): class SimpleFileLike(object): def __init__(self, target): self._target = target self.write = target.write self.close = target.close def setUp(self): self._target = BytesIO() self._file = self.SimpleFileLike(self._target) def _read_file(self): return self._target.getvalue() def _parse_file(self): self._target.seek(0) return etree.parse(self._target)
def test_write_compressed_text(self): Element = self.etree.Element SubElement = self.etree.SubElement ElementTree = self.etree.ElementTree text = _str("qwrtioüöä") root = Element('root') root.text = text child = SubElement(root, 'sub') child.text = 'TEXT' child.tail = 'TAIL' SubElement(root, 'sub').text = text tree = ElementTree(root) out = BytesIO() tree.write(out, method='text', encoding='utf8', compression=9) out.seek(0) f = gzip.GzipFile(fileobj=out) try: result = f.read().decode('utf8') finally: f.close() self.assertEqual(text + 'TEXTTAIL' + text, result)
def test_write_compressed_text(self): Element = self.etree.Element SubElement = self.etree.SubElement ElementTree = self.etree.ElementTree text = _str("qwrtioüöä") root = Element('root') root.text = text child = SubElement(root, 'sub') child.text = 'TEXT' child.tail = 'TAIL' SubElement(root, 'sub').text = text tree = ElementTree(root) out = BytesIO() tree.write(out, method='text', encoding='utf8', compression=9) out.seek(0) f = gzip.GzipFile(fileobj=out) try: result = f.read().decode('utf8') finally: f.close() self.assertEqual(text+'TEXTTAIL'+text, result)