예제 #1
0
    def test_document_init(self):
        with self.assertRaises(XmlDocumentError) as context:
            XmlDocument()
        self.assertEqual(str(context.exception),
                         "missing schema for XML data!")

        schema = os.path.join(self.schemas_dir, 'qes.xsd')

        with self.assertRaises(XmlDocumentError) as context:
            XmlDocument(schema)
        self.assertEqual(str(context.exception), "source is an XSD schema")

        document = XmlDocument(schema=schema)
        self.assertIsInstance(document, XmlDocument)
        self.assertIsNone(document.root)
        self.assertIsNone(document.filename)
        self.assertIsNone(document.format)
        self.assertIsInstance(document.schema, XMLSchema)
        if platform.system() == 'Linux':
            self.assertTrue(document.schema.url.endswith(schema))
        else:
            self.assertTrue(
                document.schema.url.endswith('qeschema/schemas/qes.xsd'))

        document = XmlDocument(schema=XMLSchema(schema))
        self.assertIsInstance(document.schema, XMLSchema)
        if platform.system() == 'Linux':
            self.assertTrue(document.schema.url.endswith(schema))
        else:
            self.assertTrue(
                document.schema.url.endswith('qeschema/schemas/qes.xsd'))
예제 #2
0
    def test_document_init(self):
        schema = os.path.join(self.schemas_dir, 'qes.xsd')

        document = XmlDocument(schema)
        self.assertIsInstance(document, XmlDocument)
        self.assertIsNone(document.root)
        self.assertIsNone(document.filename)
        self.assertIsNone(document.format)
        self.assertTrue(document.schema.url.endswith(schema))
        self.assertIsInstance(document.schema, XMLSchema)

        document = XmlDocument(schema=XMLSchema(schema))
        self.assertTrue(document.schema.url.endswith(schema))
        self.assertIsInstance(document.schema, XMLSchema)

        self.assertIsInstance(PwDocument(), PwDocument)
        self.assertIsInstance(PwDocument(schema=schema), PwDocument)

        schema = os.path.join(self.schemas_dir, 'ph_temp.xsd')
        self.assertIsInstance(PhononDocument(), PhononDocument)
        self.assertIsInstance(PhononDocument(schema=schema), PhononDocument)

        schema = os.path.join(self.schemas_dir, 'qes_neb.xsd')
        self.assertIsInstance(NebDocument(), NebDocument)
        self.assertIsInstance(NebDocument(schema=schema), NebDocument)

        schema = os.path.join(self.schemas_dir, 'tddfpt.xsd')
        self.assertIsInstance(TdDocument(), TdDocument)
        self.assertIsInstance(TdDocument(schema=schema), TdDocument)

        schema = os.path.join(self.schemas_dir, 'qes_spectrum.xsd')
        self.assertIsInstance(TdSpectrumDocument(), TdSpectrumDocument)
        self.assertIsInstance(TdSpectrumDocument(schema=schema),
                              TdSpectrumDocument)
예제 #3
0
    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]}}')
예제 #4
0
    def test_to_yaml_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(filename, schema)

        self.assertEqual(
            document.to_yaml(),
            "root:\n  node:\n  - '@a': 10\n  - value\n  - null\n")
        self.assertFalse(os.path.isfile(self.output_file))

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

        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_yaml(filename=self.output_file)
        with open(self.output_file) as f:
            self.assertEqual(
                f.read(),
                "root:\n  node:\n  - '@a': 10\n  - value\n  - null\n")
예제 #5
0
    def test_from_xml_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.from_xml(filename)
        self.assertTrue(hasattr(document.root, 'tag'))
        self.assertEqual(document.root.tag, 'root')
        self.assertListEqual(document.errors, [])

        document.from_xml(filename, validation='skip')
        self.assertTrue(hasattr(document.root, 'tag'))
        self.assertEqual(document.root.tag, 'root')
        self.assertListEqual(document.errors, [])

        root = ElementTree.parse(filename).getroot()
        document.from_xml(root)
        self.assertIs(root, document.root)

        with self.assertRaises(XMLSchemaValidationError):
            document.from_xml("<root><node/><unknown/></root>")

        document.from_xml("<root><node/><unknown/></root>", validation='lax')
        self.assertTrue(hasattr(document.root, 'tag'))
        self.assertEqual(document.root.tag, 'root')
        self.assertEqual(len(document.errors), 1)
예제 #6
0
    def test_from_yaml_method(self):
        document = XmlDocument(
            schema=os.path.join(self.test_dir, 'resources/dummy/schema.xsd'))
        filename = os.path.join(self.test_dir, 'resources/dummy/instance.yaml')

        document.from_yaml(filename)
        self.assertTrue(hasattr(document.root, 'tag'))
        self.assertEqual(document.root.tag, 'root')
        self.assertListEqual(document.errors, [])

        document.from_yaml(filename, path='root')
        self.assertTrue(hasattr(document.root, 'tag'))
        self.assertEqual(document.root.tag, 'root')
        self.assertListEqual(document.errors, [])

        with self.assertRaises(TypeError):
            with open(filename) as f:
                document.from_yaml(f)

        with self.assertRaises(XMLSchemaValidationError):
            document.from_yaml('root:\n  node: null\n  unknown: null\n')

        document.from_yaml('root:\n  node: null\n  unknown: null\n',
                           validation='lax')
        self.assertTrue(hasattr(document.root, 'tag'))
        self.assertEqual(document.root.tag, 'root')
        self.assertGreaterEqual(len(document.errors), 1)
예제 #7
0
    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])
예제 #8
0
    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[:])
예제 #9
0
 def test_schema_namespaces(self):
     schema = os.path.join(self.schemas_dir, 'qes.xsd')
     document = XmlDocument(schema=schema)
     self.assertEqual(
         document.schema.namespaces, {
             '': 'http://www.w3.org/2001/XMLSchema',
             'qes': 'http://www.quantum-espresso.org/ns/qes/qes-1.0',
             'xml': 'http://www.w3.org/XML/1998/namespace'
         })
예제 #10
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]])
예제 #11
0
    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)
예제 #12
0
    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]})
예제 #13
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)
예제 #14
0
    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]})
예제 #15
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'))
예제 #16
0
    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]}}')
예제 #17
0
    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')
예제 #18
0
    def test_from_dict_method(self):
        document = XmlDocument(
            schema=os.path.join(self.test_dir, 'resources/dummy/schema.xsd'))

        document.from_dict({'root': {'node': [None, None, None]}})
        self.assertTrue(hasattr(document.root, 'tag'))
        self.assertEqual(document.root.tag, 'root')
        self.assertListEqual(document.errors, [])

        with self.assertRaises(XMLSchemaValidationError):
            document.from_dict({'root': {'node': None, 'unknown': None}})

        document.from_dict({'root': {
            'node': None,
            'unknown': None
        }},
                           validation='lax')
        self.assertTrue(hasattr(document.root, 'tag'))
        self.assertEqual(document.root.tag, 'root')
        self.assertGreaterEqual(len(document.errors), 1)
예제 #19
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))
예제 #20
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')
예제 #21
0
    def test_from_json_method(self):
        document = XmlDocument(
            schema=os.path.join(self.test_dir, 'resources/dummy/schema.xsd'))
        filename = os.path.join(self.test_dir, 'resources/dummy/instance.json')

        document.from_json(filename)
        self.assertTrue(hasattr(document.root, 'tag'))
        self.assertEqual(document.root.tag, 'root')
        self.assertListEqual(document.errors, [])

        with self.assertRaises(TypeError):
            with open(filename) as f:
                document.from_json(f)

        with self.assertRaises(XMLSchemaValidationError):
            document.from_json('{"root": {"node": null, "unknown": null}}')

        document.from_json('{"root": {"node": null, "unknown": null}}',
                           validation='lax')
        self.assertTrue(hasattr(document.root, 'tag'))
        self.assertEqual(document.root.tag, 'root')
        self.assertGreaterEqual(len(document.errors), 1)
예제 #22
0
    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)