def test_assertXmlValidXSchema_filename(self):
        """Asserts assertXmlValidXSchema raises when schema does not valid XML.
        """
        test_case = XmlTestCase(methodName='assertXmlValidXSchema')

        xschema = b"""<?xml version="1.0" encoding="utf-8"?>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <xsd:element name="root">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="child" minOccurs="1" maxOccurs="1">
                            <xsd:complexType>
                                <xsd:simpleContent>
                                    <xsd:extension base="xsd:string">
                                        <xsd:attribute name="id" type="xsd:string" use="required" />
                                    </xsd:extension>
                                </xsd:simpleContent>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
        """

        filename = 'test_assertXmlValidXSchema_filename.xml'
        with open(filename, 'w') as xchema_file:
            xchema_file.write(xschema.encode('utf8'))

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        try:
            test_case.assertXmlValidXSchema(root, filename=filename)
        except:
            os.unlink(filename)
            raise

        data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
            <child id="tooManyChild"/>
        </root>
        """
        root = test_case.assertXmlDocument(data_invalid)

        try:
            with self.assertRaises(test_case.failureException):
                test_case.assertXmlValidXSchema(root, filename=filename)
        finally:
            os.unlink(filename)
    def test_assertXmlValidXSchema_filename(self):
        """Asserts assertXmlValidXSchema raises when schema does not valid XML.
        """
        test_case = XmlTestCase(methodName='assertXmlValidXSchema')

        xschema = """<?xml version="1.0" encoding="utf-8"?>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <xsd:element name="root">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="child" minOccurs="1" maxOccurs="1">
                            <xsd:complexType>
                                <xsd:simpleContent>
                                    <xsd:extension base="xsd:string">
                                        <xsd:attribute name="id" type="xsd:string" use="required" />
                                    </xsd:extension>
                                </xsd:simpleContent>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
        """

        filename = 'test_assertXmlValidXSchema_filename.xml'
        with open(filename, 'w') as xchema_file:
            xchema_file.write(xschema)

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        try:
            test_case.assertXmlValidXSchema(root, filename=filename)
        except:
            os.unlink(filename)
            raise

        data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
            <child id="tooManyChild"/>
        </root>
        """
        root = test_case.assertXmlDocument(data_invalid)

        try:
            with self.assertRaises(test_case.failureException):
                test_case.assertXmlValidXSchema(root, filename=filename)
        finally:
            os.unlink(filename)
    def test_assertXmlValidXSchema_no_xchema(self):
        """Asserts assertXmlValidXSchema raises ValueError without any schema.
        """
        test_case = XmlTestCase(methodName='assertXmlValidXSchema')

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="child1"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        # No DTD: ValueError
        with self.assertRaises(ValueError):
            test_case.assertXmlValidXSchema(root)
    def test_assertXmlValidXSchema_no_xchema(self):
        """Asserts assertXmlValidXSchema raises ValueError without any schema.
        """
        test_case = XmlTestCase(methodName='assertXmlValidXSchema')

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="child1"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        # No DTD: ValueError
        with self.assertRaises(ValueError):
            test_case.assertXmlValidXSchema(root)
    def test_assertXmlValidXSchema_xschema(self):
        """Asserts assertXmlValidXSchema raises when schema does not valid XML.
        """
        test_case = XmlTestCase(methodName='assertXmlValidXSchema')

        xschema = b"""<?xml version="1.0" encoding="utf-8"?>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <xsd:element name="root">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="child" minOccurs="1" maxOccurs="1">
                            <xsd:complexType>
                                <xsd:simpleContent>
                                    <xsd:extension base="xsd:string">
                                        <xsd:attribute name="id" type="xsd:string" use="required" />
                                    </xsd:extension>
                                </xsd:simpleContent>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
        """

        xml_schema = etree.XMLSchema(etree.XML(xschema))

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        test_case.assertXmlValidXSchema(root, xml_schema)

        data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
            <child id="tooManyChild"/>
        </root>
        """
        root = test_case.assertXmlDocument(data_invalid)

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlValidXSchema(root, xml_schema)
    def test_assertXmlValidXSchema_xschema(self):
        """Asserts assertXmlValidXSchema raises when schema does not valid XML.
        """
        test_case = XmlTestCase(methodName='assertXmlValidXSchema')

        xschema = b"""<?xml version="1.0" encoding="utf-8"?>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <xsd:element name="root">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="child" minOccurs="1" maxOccurs="1">
                            <xsd:complexType>
                                <xsd:simpleContent>
                                    <xsd:extension base="xsd:string">
                                        <xsd:attribute name="id" type="xsd:string" use="required" />
                                    </xsd:extension>
                                </xsd:simpleContent>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
        """

        xml_schema = etree.XMLSchema(etree.XML(xschema))

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        test_case.assertXmlValidXSchema(root, xml_schema)

        data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
            <child id="tooManyChild"/>
        </root>
        """
        root = test_case.assertXmlDocument(data_invalid)

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlValidXSchema(root, xml_schema)