-
-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathtest_helpers.py
253 lines (220 loc) · 9.37 KB
/
test_helpers.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import datetime
from collections import OrderedDict
from lxml import etree
from tests.utils import assert_nodes_equal, load_xml, render_node
from zeep import helpers, xsd
from zeep.helpers import serialize_object
def test_serialize_simple():
custom_type = xsd.Element(
etree.QName("http://tests.python-zeep.org/", "authentication"),
xsd.ComplexType(
xsd.Sequence(
[
xsd.Element(
etree.QName("http://tests.python-zeep.org/", "name"),
xsd.String(),
),
xsd.Attribute(
etree.QName("http://tests.python-zeep.org/", "attr"),
xsd.String(),
),
]
)
),
)
obj = custom_type(name="foo", attr="x")
assert obj.name == "foo"
assert obj.attr == "x"
result = serialize_object(obj)
assert result == {"name": "foo", "attr": "x"}
def test_serialize_nested_complex_type():
custom_type = xsd.Element(
etree.QName("http://tests.python-zeep.org/", "authentication"),
xsd.ComplexType(
xsd.Sequence(
[
xsd.Element(
etree.QName("http://tests.python-zeep.org/", "items"),
xsd.ComplexType(
xsd.Sequence(
[
xsd.Element(
etree.QName(
"http://tests.python-zeep.org/", "x"
),
xsd.String(),
),
xsd.Element(
etree.QName(
"http://tests.python-zeep.org/", "y"
),
xsd.ComplexType(
xsd.Sequence(
[
xsd.Element(
etree.QName(
"http://tests.python-zeep.org/",
"x",
),
xsd.String(),
)
]
)
),
),
]
)
),
max_occurs=2,
)
]
)
),
)
obj = custom_type(
items=[{"x": "bla", "y": {"x": "deep"}}, {"x": "foo", "y": {"x": "deeper"}}]
)
assert len(obj.items) == 2
obj.items[0].x == "bla"
obj.items[0].y.x == "deep"
obj.items[1].x == "foo"
obj.items[1].y.x == "deeper"
result = serialize_object(obj)
assert result == {
"items": [{"x": "bla", "y": {"x": "deep"}}, {"x": "foo", "y": {"x": "deeper"}}]
}
def test_nested_complex_types():
schema = xsd.Schema(
load_xml(
"""
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tests.python-zeep.org/"
targetNamespace="http://tests.python-zeep.org/"
elementFormDefault="qualified">
<xsd:element name="container">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="item" type="tns:item"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="item">
<xsd:sequence>
<xsd:element name="item_1" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
"""
)
)
container_elm = schema.get_element("{http://tests.python-zeep.org/}container")
item_type = schema.get_type("{http://tests.python-zeep.org/}item")
instance = container_elm(item=item_type(item_1="foo"))
result = serialize_object(instance)
assert isinstance(result, dict), type(result)
assert isinstance(result["item"], dict), type(result["item"])
assert result["item"]["item_1"] == "foo"
def test_nested_complex_types_with_etree_input():
custom_type = xsd.Element(
etree.QName("http://tests.python-zeep.org/", "authentication"),
xsd.ComplexType(xsd.Sequence([
xsd.Element(
etree.QName("http://tests.python-zeep.org/", "data"),
xsd.ComplexType(xsd.Sequence([xsd.Any()])),
)
])),
)
root = etree.Element("values")
etree.SubElement(root, "item_1").text = "foo"
etree.SubElement(root, "item_2").text = "bar"
obj = custom_type(root)
expected = load_xml(
"""
<document>
<ns0:authentication xmlns:ns0="http://tests.python-zeep.org/">
<ns0:data>
<values>
<item_1>foo</item_1>
<item_2>bar</item_2>
</values>
</ns0:data>
</ns0:authentication>
</document>
"""
)
result = render_node(custom_type, obj)
assert_nodes_equal(result, expected)
obj = custom_type.parse(result[0], schema=xsd.Schema())
result = serialize_object(obj)
assert result == {"data": {"_value_1": root}}
def test_serialize_any_array():
custom_type = xsd.Element(
etree.QName("http://tests.python-zeep.org/", "authentication"),
xsd.ComplexType(xsd.Sequence([xsd.Any(max_occurs=2)])),
)
any_obj = etree.Element("{http://tests.python-zeep.org}lxml")
etree.SubElement(any_obj, "node").text = "foo"
obj = custom_type(any_obj)
expected = """
<document>
<ns0:authentication xmlns:ns0="http://tests.python-zeep.org/">
<ns0:lxml xmlns:ns0="http://tests.python-zeep.org">
<node>foo</node>
</ns0:lxml>
</ns0:authentication>
</document>
"""
node = etree.Element("document")
custom_type.render(node, obj)
assert_nodes_equal(expected, node)
schema = xsd.Schema()
obj = custom_type.parse(list(node)[0], schema=schema)
result = serialize_object(obj)
assert result == {"_value_1": [any_obj]}
def test_create_xml_soap_map():
data = OrderedDict(
[
("text", "String"),
("bytes", b"Bytes"),
("boolean", True),
("integer", 100),
("float", 100.1234),
("datetime", datetime.datetime(2017, 10, 28, 12, 30, 10)),
("date", datetime.date(2016, 1, 14)),
]
)
value = helpers.create_xml_soap_map(data)
expected = """
<document>
<item>
<key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">text</key>
<value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">String</value>
</item>
<item>
<key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">bytes</key>
<value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Bytes</value>
</item>
<item>
<key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">boolean</key>
<value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:boolean">true</value>
</item>
<item>
<key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">integer</key>
<value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:integer">100</value>
</item>
<item>
<key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">float</key>
<value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:float">100.1234</value>
</item>
<item>
<key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">datetime</key>
<value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:dateTime">2017-10-28T12:30:10</value>
</item>
<item>
<key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">date</key>
<value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:date">2016-01-14</value>
</item>
</document>
""" # noqa
node = render_node(value._xsd_type, value)
assert_nodes_equal(expected, node)