-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathpy2xsd_test.py
35 lines (28 loc) · 1.23 KB
/
py2xsd_test.py
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
import unittest
from lxml import etree
from soapfish import xsd
from soapfish.py2xsd import generate_xsd
class py2xsdTest(unittest.TestCase):
def test_can_generate_schema_xml_containing_types_with_pattern_restriction(self):
ns = 'http://soap.example/pattern.xsd'
class Container(xsd.ComplexType):
code = xsd.Element(xsd.String(pattern='[0-9]{0,5}'))
schema = xsd.Schema(ns,
location=ns,
elementFormDefault=xsd.ElementFormDefault.QUALIFIED,
complexTypes=(
Container,
),
elements={
'foo': xsd.Element(Container),
},
)
# previously this would fail
xsd_element = generate_xsd(schema)
xmlschema = etree.XMLSchema(xsd_element)
valid_xml = f'<foo xmlns="{ns}"><code>1234</code></foo>'
def is_valid(s):
return xmlschema.validate(etree.fromstring(s))
self.assertIs(is_valid(valid_xml), True)
bad_xml = f'<foo xmlns="{ns}"><code>abc</code></foo>'
self.assertIs(is_valid(bad_xml), False)