Exemplo n.º 1
0
def test_simple_content_extension():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <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="ShoeSize">
            <xsd:complexType>
              <xsd:simpleContent>
                <xsd:extension base="xsd:integer">
                  <xsd:attribute name="sizing" type="xsd:string" />
                </xsd:extension>
              </xsd:simpleContent>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """.strip()))
    shoe_type = schema.get_element('{http://tests.python-zeep.org/}ShoeSize')

    obj = shoe_type(20, sizing='EUR')

    node = render_node(shoe_type, obj)
    expected = """
        <document>
            <ns0:ShoeSize
                xmlns:ns0="http://tests.python-zeep.org/"
                sizing="EUR">20</ns0:ShoeSize>
        </document>
    """
    assert_nodes_equal(expected, node)

    obj = shoe_type.parse(node[0], schema)
    assert obj._value_1 == 20
    assert obj.sizing == 'EUR'
Exemplo n.º 2
0
def test_xml_single_node_array():
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <element name="container">
            <complexType>
              <sequence>
                <element minOccurs="0" maxOccurs="unbounded" name="item" type="string" />
              </sequence>
            </complexType>
          </element>
        </schema>
    """))
    schema.set_ns_prefix('tns', 'http://tests.python-zeep.org/')

    container_elm = schema.get_element('tns:container')
    obj = container_elm(item=['item-1', 'item-2', 'item-3'])
    assert obj.item == ['item-1', 'item-2', 'item-3']

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item>item-1</ns0:item>
          <ns0:item>item-2</ns0:item>
          <ns0:item>item-3</ns0:item>
        </ns0:container>
      </document>
    """
    result = render_node(container_elm, obj)
    assert_nodes_equal(result, expected)

    obj = container_elm.parse(result[0], schema)
    assert obj.item == ['item-1', 'item-2', 'item-3']
Exemplo n.º 3
0
def test_element_attribute_name_conflict():
    custom_type = xsd.Element(
        etree.QName("http://tests.python-zeep.org/", "container"),
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    etree.QName("http://tests.python-zeep.org/", "item"),
                    xsd.String(),
                )
            ]),
            [
                xsd.Attribute("foo", xsd.String()),
                xsd.Attribute("item", xsd.String())
            ],
        ),
    )

    # sequences
    expected = "{http://tests.python-zeep.org/}container(item: xsd:string, foo: xsd:string, attr__item: xsd:string)"
    assert custom_type.signature() == expected
    obj = custom_type(item="foo", foo="x", attr__item="bar")

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/" foo="x" item="bar">
          <ns0:item>foo</ns0:item>
        </ns0:container>
      </document>
    """
    node = render_node(custom_type, obj)
    assert_nodes_equal(expected, node)

    obj = custom_type.parse(list(node)[0], None)
    assert obj.item == "foo"
    assert obj.foo == "x"
    assert obj.attr__item == "bar"
Exemplo n.º 4
0
def test_duplicate_element_names():
    custom_type = xsd.Element(
        etree.QName("http://tests.python-zeep.org/", "container"),
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    etree.QName("http://tests.python-zeep.org/", "item"),
                    xsd.String(),
                ),
                xsd.Element(
                    etree.QName("http://tests.python-zeep.org/", "item"),
                    xsd.String(),
                ),
                xsd.Element(
                    etree.QName("http://tests.python-zeep.org/", "item"),
                    xsd.String(),
                ),
            ])),
    )

    # sequences
    expected = "{http://tests.python-zeep.org/}container(item: xsd:string, item__1: xsd:string, item__2: xsd:string)"
    assert custom_type.signature() == expected
    obj = custom_type(item="foo", item__1="bar", item__2="lala")

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item>foo</ns0:item>
          <ns0:item>bar</ns0:item>
          <ns0:item>lala</ns0:item>
        </ns0:container>
      </document>
    """
    node = render_node(custom_type, obj)
    assert_nodes_equal(expected, node)
Exemplo n.º 5
0
def test_union_same_types():
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <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:simpleType name="MMYY">
            <xsd:restriction base="xsd:int"/>
          </xsd:simpleType>

          <xsd:simpleType name="MMYYYY">
            <xsd:restriction base="xsd:int"/>
          </xsd:simpleType>

          <xsd:simpleType name="Date">
            <xsd:union memberTypes="MMYY MMYYYY"/>
          </xsd:simpleType>
          <xsd:element name="item" type="tns:Date"/>
        </xsd:schema>
    """))

    elm = schema.get_element('ns0:item')
    node = render_node(elm, '102018')
    expected = """
        <document>
          <ns0:item xmlns:ns0="http://tests.python-zeep.org/">102018</ns0:item>
        </document>
    """
    assert_nodes_equal(expected, node)
    value = elm.parse(node.getchildren()[0], schema)
    assert value == 102018
Exemplo n.º 6
0
def test_attribute_any_type(schema_visitor):
    node = load_xml("""
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://tests.python-zeep.org/">
          <element name="foo">
            <complexType>
              <xsd:attribute name="base" type="xsd:anyURI" />
            </complexType>
          </element>
        </schema>
    """)
    schema_visitor.visit_schema(node)
    xsd_element = schema_visitor.schema.get_element(
        '{http://tests.python-zeep.org/}foo')
    value = xsd_element(base='hoi')

    node = render_node(xsd_element, value)
    expected = """
      <document>
        <ns0:foo xmlns:ns0="http://tests.python-zeep.org/" base="hoi"/>
      </document>
    """
    assert_nodes_equal(expected, node)
Exemplo n.º 7
0
def test_any_without_element():
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                   elementFormDefault="qualified">
          <element name="item">
            <complexType>
              <sequence>
                <any minOccurs="0" maxOccurs="1"/>
              </sequence>
              <attribute name="type" type="string"/>
              <attribute name="title" type="string"/>
            </complexType>
          </element>
        </schema>
    """))
    item_elm = schema.get_element('{http://tests.python-zeep.org/}item')
    item = item_elm(xsd.AnyObject(xsd.String(), 'foobar'),
                    type='attr-1',
                    title='attr-2')

    node = render_node(item_elm, item)
    expected = """
        <document>
          <ns0:item xmlns:ns0="http://tests.python-zeep.org/" type="attr-1" title="attr-2">foobar</ns0:item>
        </document>
    """
    assert_nodes_equal(expected, node)

    item = item_elm.parse(node.getchildren()[0], schema)
    assert item.type == 'attr-1'
    assert item.title == 'attr-2'
    assert item._value_1 is None
def test_list_type():
    node = load_xml(
        """
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/">

          <xsd:simpleType name="listOfIntegers">
            <xsd:list itemType="integer" />
          </xsd:simpleType>

          <xsd:element name="foo">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="arg" type="tns:listOfIntegers"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </schema>
    """
    )

    schema = parse_schema_node(node)
    xsd_element = schema.get_element("{http://tests.python-zeep.org/}foo")
    value = xsd_element(arg=[1, 2, 3, 4, 5])

    node = render_node(xsd_element, value)
    expected = """
        <document>
          <ns0:foo xmlns:ns0="http://tests.python-zeep.org/">
            <arg>1 2 3 4 5</arg>
          </ns0:foo>
        </document>
    """
    assert_nodes_equal(expected, node)
Exemplo n.º 9
0
def test_nil_elements():
    custom_type = xsd.Element(
        "{http://tests.python-zeep.org/}container",
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    "{http://tests.python-zeep.org/}item_1",
                    xsd.ComplexType(
                        xsd.Sequence([
                            xsd.Element(
                                "{http://tests.python-zeep.org/}item_1_1",
                                xsd.String(),
                            )
                        ])),
                    nillable=True,
                ),
                xsd.Element(
                    "{http://tests.python-zeep.org/}item_2",
                    xsd.DateTime(),
                    nillable=True,
                ),
                xsd.Element(
                    "{http://tests.python-zeep.org/}item_3",
                    xsd.String(),
                    min_occurs=0,
                    nillable=False,
                ),
                xsd.Element(
                    "{http://tests.python-zeep.org/}item_4",
                    xsd.ComplexType(
                        xsd.Sequence([
                            xsd.Element(
                                "{http://tests.python-zeep.org/}item_4_1",
                                xsd.String(),
                                nillable=True,
                            )
                        ])),
                ),
                xsd.Element(
                    "{http://tests.python-zeep.org/}item_5",
                    xsd.ComplexType(
                        xsd.Sequence([
                            xsd.Element(
                                "{http://tests.python-zeep.org/}item_5_1",
                                xsd.String(),
                                min_occurs=1,
                                nillable=False,
                            )
                        ],
                                     min_occurs=0)),
                ),
            ])),
    )
    obj = custom_type(item_1=None,
                      item_2=None,
                      item_3=None,
                      item_4={},
                      item_5=xsd.Nil)

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
          <ns0:item_2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
          <ns0:item_4>
            <ns0:item_4_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
          </ns0:item_4>
          <ns0:item_5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
        </ns0:container>
      </document>
    """
    node = render_node(custom_type, obj)
    etree.cleanup_namespaces(node)
    assert_nodes_equal(expected, node)
Exemplo n.º 10
0
def test_xml_complex_any_types():
    # see https://github.com/mvantellingen/python-zeep/issues/252
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <element name="container">
            <complexType>
              <sequence>
                <element minOccurs="0" maxOccurs="1" name="auth" type="anyType" />
                <element minOccurs="0" maxOccurs="1" name="params" type="anyType" />
              </sequence>
            </complexType>
          </element>
        </schema>
    """))

    schema.set_ns_prefix('tns', 'http://tests.python-zeep.org/')
    KeyValueData = xsd.Element(
        '{http://xml.apache.org/xml-soap}KeyValueData',
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    'key',
                    xsd.AnyType(),
                ),
                xsd.Element(
                    'value',
                    xsd.AnyType(),
                ),
            ]), ),
    )

    Map = xsd.ComplexType(
        xsd.Sequence([
            xsd.Element('item',
                        xsd.AnyType(),
                        min_occurs=1,
                        max_occurs="unbounded"),
        ]),
        qname=etree.QName('{http://xml.apache.org/xml-soap}Map'))

    header_Username = KeyValueData(xsd.AnyObject(xsd.String(), 'Username'),
                                   value=xsd.AnyObject(xsd.String(), 'abc'))
    header_ShopId = KeyValueData(xsd.AnyObject(xsd.String(), 'ShopId'),
                                 value=xsd.AnyObject(xsd.Int(), 123))
    auth = Map(item=[header_Username, header_ShopId])

    header_LimitNum = KeyValueData(xsd.AnyObject(xsd.String(), 'LimitNum'),
                                   value=xsd.AnyObject(xsd.Int(), 2))
    params = Map(item=[header_LimitNum])

    container = schema.get_element('ns0:container')
    obj = container(auth=auth, params=params)

    result = render_node(container, obj)
    expected = load_xml("""
    <document>
      <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
        <ns0:auth xmlns:ns1="http://xml.apache.org/xml-soap" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns1:Map">
          <item>
            <key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Username</key>
            <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">abc</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">ShopId</key>
            <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:int">123</value>
          </item>
        </ns0:auth>
        <ns0:params xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:Map">
          <item>
            <key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">LimitNum</key>
            <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:int">2</value>
          </item>
        </ns0:params>
      </ns0:container>
    </document>
    """)  # noqa
    assert_nodes_equal(result, expected)
Exemplo n.º 11
0
def test_build_group_min_occurs_2():
    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'authentication'),
        xsd.ComplexType(
            xsd.Group(etree.QName('http://tests.python-zeep.org/', 'foobar'),
                      xsd.Sequence([
                          xsd.Element(
                              etree.QName('http://tests.python-zeep.org/',
                                          'item_1'), xsd.String()),
                          xsd.Element(
                              etree.QName('http://tests.python-zeep.org/',
                                          'item_2'), xsd.String()),
                      ]),
                      min_occurs=2,
                      max_occurs=2)))

    obj = custom_type(_value_1=[
        {
            'item_1': 'foo',
            'item_2': 'bar'
        },
        {
            'item_1': 'foo',
            'item_2': 'bar'
        },
    ])
    assert obj._value_1 == [
        {
            'item_1': 'foo',
            'item_2': 'bar'
        },
        {
            'item_1': 'foo',
            'item_2': 'bar'
        },
    ]

    result = render_node(custom_type, obj)
    expected = load_xml("""
        <document>
          <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/">
            <ns0:item_1>foo</ns0:item_1>
            <ns0:item_2>bar</ns0:item_2>
            <ns0:item_1>foo</ns0:item_1>
            <ns0:item_2>bar</ns0:item_2>
          </ns0:authentication>
        </document>
    """)

    assert_nodes_equal(result, expected)

    obj = custom_type.parse(result[0], None)
    assert obj._value_1 == [
        {
            'item_1': 'foo',
            'item_2': 'bar'
        },
        {
            'item_1': 'foo',
            'item_2': 'bar'
        },
    ]
    assert not hasattr(obj, 'foobar')
Exemplo n.º 12
0
def test_xml_soap_enc_string(transport):
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <xsd:schema
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://tests.python-zeep.org/"
            xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
            xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
            targetNamespace="http://tests.python-zeep.org/"
            elementFormDefault="qualified">
          <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>

          <xsd:element name="value" type="tns:ArrayOfString"/>

          <xsd:complexType name="ArrayOfString">
            <xsd:complexContent>
              <xsd:restriction base="soapenc:Array">
                <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="soapenc:string[]"/>
              </xsd:restriction>
            </xsd:complexContent>
          </xsd:complexType>

        </xsd:schema>
    """),
        transport,
    )
    shoe_type = schema.get_element("{http://tests.python-zeep.org/}value")

    obj = shoe_type(["foo"])
    node = render_node(shoe_type, obj)
    expected = """
        <document>
            <ns0:value xmlns:ns0="http://tests.python-zeep.org/">
              <string>foo</string>
            </ns0:value>
        </document>
    """
    assert_nodes_equal(expected, node)

    obj = shoe_type.parse(node[0], schema)
    assert obj[0]["_value_1"] == "foo"

    # Via string-types
    string_type = schema.get_type(
        "{http://schemas.xmlsoap.org/soap/encoding/}string")
    obj = shoe_type([string_type("foo")])
    node = render_node(shoe_type, obj)
    expected = """
        <document>
            <ns0:value xmlns:ns0="http://tests.python-zeep.org/">
              <string>foo</string>
            </ns0:value>
        </document>
    """
    assert_nodes_equal(expected, node)

    obj = shoe_type.parse(node[0], schema)
    assert obj[0]["_value_1"] == "foo"

    # Via dicts
    string_type = schema.get_type(
        "{http://schemas.xmlsoap.org/soap/encoding/}string")
    obj = shoe_type([{"_value_1": "foo"}])
    node = render_node(shoe_type, obj)
    expected = """
        <document>
            <ns0:value xmlns:ns0="http://tests.python-zeep.org/">
              <string>foo</string>
            </ns0:value>
        </document>
    """
    assert_nodes_equal(expected, node)

    obj = shoe_type.parse(node[0], schema)
    assert obj[0]["_value_1"] == "foo"