File: test_assertions.py

package info (click to toggle)
python-xmlschema 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,208 kB
  • sloc: python: 39,174; xml: 1,282; makefile: 36
file content (163 lines) | stat: -rw-r--r-- 6,506 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
#
# Copyright (c), 2016-2020, SISSA (International School for Advanced Studies).
# All rights reserved.
# This file is distributed under the terms of the MIT License.
# See the file 'LICENSE' in the root directory of the present
# distribution, or http://opensource.org/licenses/MIT.
#
# @author Davide Brunato <brunato@sissa.it>
#
import pathlib
from textwrap import dedent
from xmlschema import XMLSchema11
from xmlschema.testing import XsdValidatorTestCase


class TestXsdAssert(XsdValidatorTestCase):

    cases_dir = pathlib.Path(__file__).parent.joinpath('../test_cases')
    schema_class = XMLSchema11

    def test_base_api(self):
        schema = self.schema_class(dedent("""\
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:complexType name="intRange">
                <xs:attribute name="min" type="xs:int"/>
                <xs:attribute name="max" type="xs:int"/>
                <xs:assert test="@min le @max"/>
            </xs:complexType>
            <xs:element name="root" type="intRange"/>
        </xs:schema>
        """))

        self.assertEqual(len(schema.types['intRange'].assertions), 1)
        assertion = schema.types['intRange'].assertions[0]

        self.assertEqual(repr(assertion), "XsdAssert(test='@min le @max')")

        self.assertTrue(assertion.built)
        assertion.build()
        self.assertTrue(assertion.built)

        self.assertEqual(len(list(iter(assertion))), 0)

        schema = self.schema_class(dedent("""\
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:complexType name="intRange">
                <xs:simpleContent>
                    <xs:extension base="xs:string">
                        <xs:attribute name="min" type="xs:int"/>
                        <xs:attribute name="max" type="xs:int"/>
                        <xs:assert test="@min le @max"/>
                    </xs:extension>
                </xs:simpleContent>
            </xs:complexType>
            <xs:element name="root" type="intRange"/>
        </xs:schema>
        """))

        assertion = schema.types['intRange'].assertions[0]
        self.assertEqual(len(list(iter(assertion))), 0)
        self.assertEqual(repr(assertion), "XsdAssert(test='@min le @max')")

        self.assertTrue(schema.is_valid('<root min="2" max="4">foo</root>'))
        self.assertFalse(schema.is_valid('<root min="5" max="4">foo</root>'))

    def test_assertion_on_text_content(self):
        schema = self.schema_class(dedent("""\
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:complexType name="rootType" mixed="true">
                <xs:sequence>
                    <xs:element name="child"/>
                </xs:sequence>
                <xs:assert test="child/text()='foo'"/>
            </xs:complexType>
            <xs:element name="root" type="rootType"/>
        </xs:schema>
        """))

        self.assertTrue(schema.is_valid('<root>bar<child>foo</child></root>'))
        self.assertFalse(schema.is_valid('<root>bar<child></child></root>'))
        self.assertFalse(schema.is_valid('<root>bar<child> foo </child></root>'))

    def test_invalid_assertions(self):
        schema = self.schema_class(dedent("""\
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:simpleType name="rootType">
                <xs:restriction base="xs:string"/>
                <xs:assert test="child/text()='foo'"/>
            </xs:simpleType>
            <xs:element name="root" type="rootType"/>
        </xs:schema>
        """), validation='lax')

        self.assertEqual(len(schema.all_errors), 1)

        schema = self.schema_class(dedent("""\
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:complexType name="rootType" mixed="true">
                <xs:sequence>
                    <xs:element name="child"/>
                </xs:sequence>
                <xs:assert/>
            </xs:complexType>
            <xs:element name="root" type="rootType"/>
        </xs:schema>
        """), validation='lax')

        self.assertEqual(len(schema.all_errors), 1)

    def test_xpath_default_namespace(self):
        schema = self.schema_class(dedent("""\
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://xmlschema.test/ns"
                xmlns="http://xmlschema.test/ns"
                xpathDefaultNamespace="http://xmlschema.test/ns"
                elementFormDefault="qualified">
            <xs:complexType name="rootType" mixed="true">
                <xs:sequence>
                    <xs:element name="child"/>
                </xs:sequence>
                <xs:assert test="child/text()='foo'"/>
                <xs:assert test="true()"
                    xpathDefaultNamespace="http://xmlschema.test/other-ns"/>
            </xs:complexType>
            <xs:element name="root" type="rootType"/>
        </xs:schema>
        """))

        self.assertEqual(len(schema.types['rootType'].assertions), 2)
        assertion = schema.types['rootType'].assertions[0]
        self.assertEqual(assertion.xpath_default_namespace, 'http://xmlschema.test/ns')

        assertion = schema.types['rootType'].assertions[1]
        self.assertEqual(assertion.xpath_default_namespace, 'http://xmlschema.test/other-ns')

        self.assertIsNone(schema.validate(
            '<root xmlns="http://xmlschema.test/ns"><child>foo</child></root>'
        ))
        self.assertFalse(schema.is_valid('<root><child>foo</child></root>'))

    def test_typed_value(self):
        schema = self.schema_class(dedent("""\
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:complexType name="rootType" mixed="true">
                <xs:sequence>
                    <xs:element name="child" type="xs:int"/>
                </xs:sequence>
                <xs:assert test="child le 10"/>
            </xs:complexType>
            <xs:element name="root" type="rootType"/>
        </xs:schema>
        """))

        self.assertTrue(schema.is_valid('<root><child>10</child></root>'))
        self.assertTrue(schema.is_valid('<root><child>9</child></root>'))
        self.assertFalse(schema.is_valid('<root><child>11</child></root>'))
        self.assertFalse(schema.is_valid('<root><child>ten</child></root>'))


if __name__ == '__main__':
    from xmlschema.testing import run_xmlschema_tests
    run_xmlschema_tests('XSD 1.1 assertions')