Exemplo n.º 1
0
    def test_find_missing(self):
        fp = etree.parse(StringIO(b'<a>\n<b>bar</b>\n</a>'))
        xml = stylechecker.XML(fp)
        xml.xmlschema = etree.XMLSchema(etree.parse(sample_xsd))

        # missing elements fallback to the root element
        self.assertEquals(xml.find_element('c', 2), fp.getroot())
Exemplo n.º 2
0
    def test_invalid(self):
        fp = etree.parse(StringIO(b'<a><c>bar</c></a>'))
        xml = stylechecker.XML(fp)
        xml.xmlschema = etree.XMLSchema(etree.parse(sample_xsd))

        result, _ = xml.validate()
        self.assertFalse(result)
Exemplo n.º 3
0
    def test_find_root_element(self):
        fp = etree.parse(StringIO(b'<a>\n<b>bar</b>\n</a>'))
        xml = stylechecker.XML(fp)
        xml.xmlschema = etree.XMLSchema(etree.parse(sample_xsd))

        elem = xml.find_element('a', 1)
        self.assertEqual(elem.tag, 'a')
        self.assertEqual(elem.sourceline, 1)
Exemplo n.º 4
0
    def test_invalid_errors(self):
        # Default lxml error log.
        fp = etree.parse(StringIO(b'<a><c>bar</c></a>'))
        xml = stylechecker.XML(fp)
        xml.xmlschema = etree.XMLSchema(etree.parse(sample_xsd))

        _, errors = xml.validate()
        self.assertIsInstance(errors, etree._ListErrorLog)
Exemplo n.º 5
0
    def test_validation(self):
        fp = etree.parse(StringIO(b'<a><b>bar</b></a>'))
        xml = stylechecker.XML(fp)
        xml.xmlschema = etree.XMLSchema(etree.parse(sample_xsd))

        result, errors = xml.validate()
        self.assertTrue(result)
        self.assertFalse(errors)
Exemplo n.º 6
0
    def test_find_missing_without_fallback(self):
        fp = etree.parse(StringIO(b'<a>\n<b>bar</b>\n</a>'))
        xml = stylechecker.XML(fp)
        xml.xmlschema = etree.XMLSchema(etree.parse(sample_xsd))

        # missing elements fallback to the root element
        self.assertRaises(ValueError,
                          lambda: xml.find_element('c', 2, fallback=False))
Exemplo n.º 7
0
    def test_invalid_schematron(self):
        fp = etree.parse(
            StringIO(
                b'<Total><Percent>60</Percent><Percent>30</Percent></Total>'))
        xml = stylechecker.XML(fp)
        xml.schematron = isoschematron.Schematron(etree.parse(sample_sch))

        result, errors = xml._validate_sch()
        self.assertFalse(result)
        self.assertTrue(errors)
Exemplo n.º 8
0
    def test_annotate_errors(self):
        fp = etree.parse(StringIO(b'<a><c>bar</c></a>'))
        xml = stylechecker.XML(fp)
        xml.xmlschema = etree.XMLSchema(etree.parse(sample_xsd))

        xml.annotate_errors()
        xml_text = xml.read()

        self.assertIn(
            "<SPS-ERROR>Element 'c': This element is not expected. Expected is ( b ).</SPS-ERROR>",
            xml_text)
        self.assertTrue(isinstance(xml_text, unicode))
Exemplo n.º 9
0
    def test_annotate_errors_schematron(self):
        fp = etree.parse(
            StringIO(
                b'<Total><Percent>60</Percent><Percent>30</Percent></Total>'))
        xml = stylechecker.XML(fp)
        xml.schematron = isoschematron.Schematron(etree.parse(sample_sch))

        xml.annotate_errors()
        xml_text = xml.read()

        self.assertIn("<!--SPS-ERROR: Element 'Total': Sum is not 100%.-->",
                      xml_text)
        self.assertTrue(isinstance(xml_text, unicode))
Exemplo n.º 10
0
    def test_initializes_with_etree(self):
        fp = StringIO(b'<a><b>bar</b></a>')
        et = etree.parse(fp)

        self.assertTrue(stylechecker.XML(et))
Exemplo n.º 11
0
 def test_initializes_with_filepath(self):
     self.assertTrue(stylechecker.XML(self.valid_tmpfile.name))