File: test_helpers.py

package info (click to toggle)
python-zeep 4.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,600 kB
  • sloc: python: 15,551; makefile: 13
file content (214 lines) | stat: -rw-r--r-- 8,455 bytes parent folder | download | duplicates (3)
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
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_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)