def test_to_json_method(self): schema = os.path.join(self.test_dir, 'resources/dummy/schema.xsd') document = XmlDocument(schema=schema) filename = os.path.join(self.test_dir, 'resources/dummy/instance.xml') document.read(filename) self.assertEqual(document.to_json().replace(' ', '').replace('\n', ''), '{"root":{"node":[{"@a":10},"value",null]}}') self.assertFalse(os.path.isfile(self.output_file)) document.to_json(filename=self.output_file) with open(self.output_file) as f: self.assertEqual(f.read().replace(' ', '').replace('\n', ''), '{"root":{"node":[{"@a":10},"value",null]}}') if os.path.isfile(self.output_file): os.unlink(self.output_file) self.assertFalse(os.path.isfile(self.output_file)) with open(filename) as f: xml_data = f.read() document = XmlDocument(xml_data, schema) self.assertIsNone(document.filename) document.to_json(filename=self.output_file) with open(self.output_file) as f: self.assertEqual(f.read().replace(' ', '').replace('\n', ''), '{"root":{"node":[{"@a":10},"value",null]}}')
def test_findall_method(self): schema = os.path.join(self.test_dir, 'resources/dummy/schema.xsd') document = XmlDocument(schema=schema) filename = os.path.join(self.test_dir, 'resources/dummy/instance.xml') document.read(filename) self.assertEqual(document.findall('.'), [document.root]) self.assertEqual(document.findall('/node'), document.root[:])
def test_find_method(self): document = XmlDocument( os.path.join(self.test_dir, 'examples/dummy/schema.xsd')) filename = os.path.join(self.test_dir, 'examples/dummy/instance.xml') document.read(filename) self.assertEqual(document.find('.'), document.root) self.assertEqual(document.find('/node'), document.root[0])
def test_iter_method(self): schema = os.path.join(self.test_dir, 'resources/dummy/schema.xsd') document = XmlDocument(schema=schema) filename = os.path.join(self.test_dir, 'resources/dummy/instance.xml') document.read(filename) root = document.root self.assertEqual(list(document.iter()), [root, root[0], root[1], root[2]])
def test_to_dict_method(self): document = XmlDocument( os.path.join(self.test_dir, 'examples/dummy/schema.xsd')) filename = os.path.join(self.test_dir, 'examples/dummy/instance.xml') document.read(filename) self.assertEqual(document.to_dict(), {'root': { 'node': [None, None, None] }}) self.assertEqual(document.to_dict(preserve_root=False), {'node': [None, None, None]})
def test_write_method(self): schema = os.path.join(self.test_dir, 'resources/dummy/schema.xsd') filename = os.path.join(self.test_dir, 'resources/dummy/instance.xml') document = XmlDocument(schema=schema) with self.assertRaises(RuntimeError): document.write(self.output_file) document.read(filename) self.assertFalse(os.path.isfile(self.output_file)) document.write(self.output_file) self.assertIsInstance(ElementTree.parse(self.output_file), ElementTree.ElementTree)
def test_to_dict_method(self): schema = os.path.join(self.test_dir, 'resources/dummy/schema.xsd') document = XmlDocument(schema=schema) filename = os.path.join(self.test_dir, 'resources/dummy/instance.xml') document.read(filename) self.assertEqual(document.to_dict(keep_unknown=True), {'root': { 'node': [{ "@a": 10 }, "value", None] }}) self.assertEqual(document.to_dict(preserve_root=False), {'node': [{ "@a": 10 }, "value", None]})
def test_to_yaml_method(self): document = XmlDocument( os.path.join(self.test_dir, 'examples/dummy/schema.xsd')) filename = os.path.join(self.test_dir, 'examples/dummy/instance.xml') document.read(filename) self.assertEqual(document.to_yaml(), 'root:\n node:\n - null\n - null\n - null\n') filename = os.path.join(self.test_dir, 'examples/dummy/write_test_file') if os.path.isfile(filename): os.unlink(filename) self.assertFalse(os.path.isfile(filename)) document.to_yaml(filename=filename) with open(filename) as f: self.assertEqual(f.read(), 'root:\n node:\n - null\n - null\n - null\n')
def test_to_json_method(self): document = XmlDocument( os.path.join(self.test_dir, 'examples/dummy/schema.xsd')) filename = os.path.join(self.test_dir, 'examples/dummy/instance.xml') document.read(filename) self.assertEqual(document.to_json().replace(' ', '').replace('\n', ''), '{"root":{"node":[null,null,null]}}') filename = os.path.join(self.test_dir, 'examples/dummy/write_test_file') if os.path.isfile(filename): os.unlink(filename) self.assertFalse(os.path.isfile(filename)) document.to_json(filename=filename) with open(filename) as f: self.assertEqual(f.read().replace(' ', '').replace('\n', ''), '{"root":{"node":[null,null,null]}}')
def test_write_method(self): document = XmlDocument( os.path.join(self.test_dir, 'examples/dummy/schema.xsd')) filename = os.path.join(self.test_dir, 'examples/dummy/instance.xml') with self.assertRaises(RuntimeError): document.write(filename) document.read(filename) filename = os.path.join(self.test_dir, 'examples/dummy/write_test_file') if os.path.isfile(filename): os.unlink(filename) self.assertFalse(os.path.isfile(filename)) document.write(filename) self.assertIsInstance(ElementTree.parse(filename), ElementTree.ElementTree) document.write(filename, output_format='json') with open(filename) as f: self.assertEqual(f.read().replace(' ', '').replace('\n', ''), '{"root":{"node":[null,null,null]}}') document.write(filename, output_format='yaml') with open(filename) as f: self.assertEqual(f.read(), 'root:\n node:\n - null\n - null\n - null\n') with self.assertRaises(TypeError): with open(filename, mode='w+') as f: document.write(f) with self.assertRaises(ValueError): document.write(filename, output_format='csv') if os.path.isfile(filename): os.unlink(filename) self.assertFalse(os.path.isfile(filename))
def test_read_method(self): schema = os.path.join(self.test_dir, 'resources/dummy/schema.xsd') document = XmlDocument(schema=schema) filename = os.path.join(self.test_dir, 'resources/dummy/instance.xml') with open(filename) as f: with self.assertRaises(TypeError): document.read(f) with self.assertRaises(ValueError): document.read(os.path.join(self.test_dir, 'resources/unknown.xml')) document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.format, 'xml') if platform.system() == 'Linux': self.assertEqual(document.filename, filename) else: self.assertTrue(document.filename.endswith('instance.xml')) filename = os.path.join(self.test_dir, 'resources/dummy/instance_xml') document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.format, 'xml') if platform.system() == 'Linux': self.assertEqual(document.filename, filename) else: self.assertTrue(document.filename.endswith('instance_xml')) filename = os.path.join(self.test_dir, 'resources/dummy/instance.json') document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.root.tag, 'root') self.assertEqual(document.format, 'json') if platform.system() == 'Linux': self.assertEqual(document.filename, filename) else: self.assertTrue(document.filename.endswith('instance.json')) filename = os.path.join(self.test_dir, 'resources/dummy/instance_json') document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.root.tag, 'root') self.assertEqual(document.format, 'json') if platform.system() == 'Linux': self.assertEqual(document.filename, filename) else: self.assertTrue(document.filename.endswith('instance_json')) filename = os.path.join(self.test_dir, 'resources/dummy/instance.yaml') document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.root.tag, 'root') self.assertEqual(document.format, 'yaml') if platform.system() == 'Linux': self.assertEqual(document.filename, filename) else: self.assertTrue(document.filename.endswith('instance.yaml')) filename = os.path.join(self.test_dir, 'resources/dummy/instance_yaml') document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.root.tag, 'root') self.assertEqual(document.format, 'yaml') if platform.system() == 'Linux': self.assertEqual(document.filename, filename) else: self.assertTrue(document.filename.endswith('instance_yaml')) filename = os.path.join(self.test_dir, 'resources/dummy/instance.csv') with self.assertRaises(ValueError): document.read(filename)
def test_read_method(self): document = XmlDocument( os.path.join(self.test_dir, 'examples/dummy/schema.xsd')) filename = os.path.join(self.test_dir, 'examples/dummy/instance.xml') with open(filename) as f: with self.assertRaises(TypeError): document.read(f) with self.assertRaises(ValueError): document.read(os.path.join(self.test_dir, 'examples/unknown.xml')) document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.filename, filename) self.assertEqual(document.format, 'xml') filename = os.path.join(self.test_dir, 'examples/dummy/instance_xml') document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.filename, filename) self.assertEqual(document.format, 'xml') filename = os.path.join(self.test_dir, 'examples/dummy/instance.json') document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.root.tag, 'root') self.assertEqual(document.filename, filename) self.assertEqual(document.format, 'json') filename = os.path.join(self.test_dir, 'examples/dummy/instance_json') document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.root.tag, 'root') self.assertEqual(document.filename, filename) self.assertEqual(document.format, 'json') filename = os.path.join(self.test_dir, 'examples/dummy/instance.yaml') document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.root.tag, 'root') self.assertEqual(document.filename, filename) self.assertEqual(document.format, 'yaml') filename = os.path.join(self.test_dir, 'examples/dummy/instance_yaml') document.read(filename) self.assertTrue(hasattr(document.root, 'tag')) self.assertEqual(document.root.tag, 'root') self.assertEqual(document.filename, filename) self.assertEqual(document.format, 'yaml') filename = os.path.join(self.test_dir, 'examples/dummy/instance.csv') with self.assertRaises(ValueError): document.read(filename)