示例#1
0
    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)
示例#2
0
    def test_write_method_from_unbound(self):
        schema = os.path.join(self.test_dir, 'resources/dummy/schema.xsd')
        filename = os.path.join(self.test_dir, 'resources/dummy/instance.xml')

        with open(filename) as fp:
            xml_data = fp.read()
        document = XmlDocument(xml_data, schema=schema)
        self.assertIsNone(document.filename)

        document.write(self.output_file)
        self.assertIsInstance(ElementTree.parse(filename),
                              ElementTree.ElementTree)
        self.assertEqual(document.format, 'xml')
        if platform.system() == 'Linux':
            self.assertEqual(document.filename, self.output_file)
        else:
            self.assertTrue(document.filename.endswith('write_test_file'))
示例#3
0
    def test_write_other_formats(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(filename, schema)
        document.write(self.output_file, output_format='json')
        with open(self.output_file) as f:
            self.assertEqual(f.read().replace(' ', '').replace('\n', ''),
                             '{"root":{"node":[{"@a":10},"value",null]}}')

        document.write(self.output_file, output_format='yaml')
        with open(self.output_file) as f:
            self.assertEqual(
                f.read(),
                "root:\n  node:\n  - '@a': 10\n  - value\n  - null\n")

        with self.assertRaises(TypeError):
            with open(self.output_file, mode='w+') as f:
                document.write(f)

        with self.assertRaises(ValueError):
            document.write(self.output_file, output_format='csv')
示例#4
0
    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))