Пример #1
0
def test_include_relative():
    node_a = etree.fromstring(
        """
        <?xml version="1.0"?>
        <xs:schema
            xmlns="http://tests.python-zeep.org/tns"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://tests.python-zeep.org/a"
            elementFormDefault="qualified">

            <xs:include schemaLocation="http://tests.python-zeep.org/subdir/b.xsd"/>

        </xs:schema>
    """.strip()
    )

    node_b = etree.fromstring(
        """
        <?xml version="1.0"?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
            <xs:include schemaLocation="c.xsd"/>
            <xs:element name="bar" type="xs:string"/>
        </xs:schema>
    """.strip()
    )

    node_c = etree.fromstring(
        """
        <?xml version="1.0"?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
            <xs:element name="foo" type="xs:string"/>
        </xs:schema>
    """.strip()
    )

    transport = DummyTransport()
    transport.bind("http://tests.python-zeep.org/subdir/b.xsd", node_b)
    transport.bind("http://tests.python-zeep.org/subdir/c.xsd", node_c)

    schema = xsd.Schema(node_a, transport=transport)
    schema.get_element("{http://tests.python-zeep.org/a}foo")
    schema.get_element("{http://tests.python-zeep.org/a}bar")
def test_choice_in_sequence():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="container">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="something" type="xsd:string" />
                <xsd:choice>
                  <xsd:element name="item_1" type="xsd:string" />
                  <xsd:element name="item_2" type="xsd:string" />
                  <xsd:element name="item_3" type="xsd:string" />
                </xsd:choice>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """.strip())
    schema = xsd.Schema(node)
    container_elm = schema.get_element('ns0:container')

    assert container_elm.type.signature(schema=schema) == (
        'ns0:container(something: xsd:string, ({item_1: xsd:string} | {item_2: xsd:string} | {item_3: xsd:string}))'
    )
    value = container_elm(something='foobar', item_1='item-1')

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:something>foobar</ns0:something>
          <ns0:item_1>item-1</ns0:item_1>
        </ns0:container>
      </document>
    """
    node = etree.Element('document')
    container_elm.render(node, value)
    assert_nodes_equal(expected, node)
    value = container_elm.parse(node[0], schema)
Пример #3
0
def test_sequence_parse_regression():
    schema_doc = load_xml(b"""
        <?xml version="1.0" encoding="utf-8"?>
        <xsd:schema xmlns:tns="http://tests.python-zeep.org/attr"
          xmlns:xsd="https://www.w3.org/2001/XMLSchema"
          elementFormDefault="qualified"
          targetNamespace="http://tests.python-zeep.org/attr">
          <xsd:complexType name="Result">
            <xsd:attribute name="id" type="xsd:int" use="required"/>
          </xsd:complexType>
          <xsd:element name="Response">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element minOccurs="0" maxOccurs="1" name="Result" type="tns:Result"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """)

    response_doc = load_xml(b"""
        <s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
          <s:Body xmlns:xsd="https://www.w3.org/2001/XMLSchema">
            <Response xmlns="http://tests.python-zeep.org/attr">
                <Result id="2"/>
            </Response>
          </s:Body>
        </s:Envelope>
    """)

    schema = xsd.Schema(schema_doc)
    elm = schema.get_element("{http://tests.python-zeep.org/attr}Response")

    node = response_doc.xpath(
        "//ns0:Response",
        namespaces={
            "xsd": "https://www.w3.org/2001/XMLSchema",
            "ns0": "http://tests.python-zeep.org/attr",
        },
    )
    response = elm.parse(node[0], None)
    assert response.Result.id == 2
Пример #4
0
def test_any_with_ref():
    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" type="string"/>
          <element name="container">
            <complexType>
              <sequence>
                <element ref="tns:item"/>
                <any/>
                <any maxOccurs="unbounded"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """))

    item_elm = schema.get_element('{http://tests.python-zeep.org/}item')
    assert isinstance(item_elm.type, xsd.String)

    container_elm = schema.get_element('{http://tests.python-zeep.org/}container')
    obj = container_elm(
        item='bar',
        _value_1=xsd.AnyObject(item_elm, item_elm('argh')))

    node = etree.Element('document')
    container_elm.render(node, obj)
    expected = """
        <document>
            <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:item>bar</ns0:item>
                <ns0:item>argh</ns0:item>
            </ns0:container>
        </document>
    """
    assert_nodes_equal(expected, node)
    item = container_elm.parse(node.getchildren()[0], schema)
    assert item.item == 'bar'
    assert item._value_1 == 'argh'
Пример #5
0
def test_include_no_default_namespace():
    node_a = etree.fromstring("""
        <?xml version="1.0"?>
        <xs:schema
            xmlns="http://tests.python-zeep.org/tns"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://tests.python-zeep.org/tns"
            elementFormDefault="qualified">

            <xs:include
                schemaLocation="http://tests.python-zeep.org/b.xsd"/>

            <xs:element name="container" type="foo"/>
        </xs:schema>
    """.strip())

    # include without default namespace, other xsd prefix
    node_b = etree.fromstring("""
        <?xml version="1.0"?>
        <xsd:schema
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://tests.python-zeep.org/b">

            <xsd:simpleType name="my-string">
              <xsd:restriction base="xsd:boolean"/>
            </xsd:simpleType>

            <xsd:complexType name="foo">
              <xsd:sequence>
                <xsd:element name="item" type="my-string"/>
              </xsd:sequence>

            </xsd:complexType>
        </xsd:schema>
    """.strip())

    transport = DummyTransport()
    transport.bind('http://tests.python-zeep.org/b.xsd', node_b)

    schema = xsd.Schema(node_a, transport=transport)
    item = schema.get_element('{http://tests.python-zeep.org/tns}container')
    assert item
Пример #6
0
def test_anyattribute():
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <element name="container">
            <complexType>
              <sequence>
                <element name="foo" type="xsd:string" />
              </sequence>
              <xsd:anyAttribute processContents="lax"/>
            </complexType>
          </element>
        </schema>
    """))

    container_elm = schema.get_element(
        '{http://tests.python-zeep.org/}container')
    assert container_elm.signature() == ('foo: xsd:string, _attr_1: {}')
    obj = container_elm(foo='bar',
                        _attr_1=OrderedDict([('hiep', 'hoi'),
                                             ('hoi', 'hiep')]))

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/" hiep="hoi" hoi="hiep">
          <ns0:foo>bar</ns0:foo>
        </ns0:container>
      </document>
    """

    node = etree.Element('document')
    container_elm.render(node, obj)
    assert_nodes_equal(expected, node)

    item = container_elm.parse(node.getchildren()[0], schema)
    assert item._attr_1 == {'hiep': 'hoi', 'hoi': 'hiep'}
    assert item.foo == 'bar'
Пример #7
0
def test_any_optional():
    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 name="item">
                  <complexType>
                    <sequence>
                      <any minOccurs="0" maxOccurs="unbounded"/>
                    </sequence>
                  </complexType>
                </element>
              </sequence>
            </complexType>
          </element>
        </schema>
    """
        )
    )

    container_elm = schema.get_element("{http://tests.python-zeep.org/}container")
    obj = container_elm({})

    node = etree.Element("document")
    container_elm.render(node, obj)
    expected = """
        <document>
            <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:item/>
            </ns0:container>
        </document>
    """
    assert_nodes_equal(expected, node)
    item = container_elm.parse(list(node)[0], schema)
    assert item.item is None
Пример #8
0
def test_xml_sequence_recover_from_missing_element():
    schema = xsd.Schema(
        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/"
            elementFormDefault="qualified"
            targetNamespace="http://tests.python-zeep.org/">
          <complexType name="container">
            <sequence>
              <element name="item_1" type="xsd:string"/>
              <element name="item_2" type="xsd:string"/>
              <element name="item_3" type="xsd:string"/>
              <element name="item_4" type="xsd:string"/>
            </sequence>
          </complexType>
        </schema>
    """
        ),
        settings=Settings(strict=False),
    )

    xml = load_xml(
        """
        <tns:container
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:tns="http://tests.python-zeep.org/">
          <tns:item_1>text-1</tns:item_1>
          <tns:item_3>text-3</tns:item_3>
          <tns:item_4>text-4</tns:item_4>
        </tns:container>
    """
    )
    elm_type = schema.get_type("{http://tests.python-zeep.org/}container")
    result = elm_type.parse_xmlelement(xml, schema)
    assert result.item_1 == "text-1"
    assert result.item_2 is None
    assert result.item_3 == "text-3"
    assert result.item_4 == "text-4"
Пример #9
0
def test_choice_element():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="container">
            <xsd:complexType>
              <xsd:choice>
                <xsd:element name="item_1" type="xsd:string" />
                <xsd:element name="item_2" type="xsd:string" />
                <xsd:element name="item_3" type="xsd:string" />
              </xsd:choice>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """.strip())
    schema = xsd.Schema(node)
    element = schema.get_element('ns0:container')

    value = element(item_1="foo")
    assert value.item_1 == 'foo'
    assert value.item_2 is None
    assert value.item_3 is None

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item_1>foo</ns0:item_1>
        </ns0:container>
      </document>
    """
    node = etree.Element('document')
    element.render(node, value)
    assert_nodes_equal(expected, node)

    value = element.parse(node.getchildren()[0], schema)
    assert value.item_1 == 'foo'
    assert value.item_2 is None
    assert value.item_3 is None
Пример #10
0
def test_choice_with_sequence_change_named():
    node = load_xml("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name='ElementName'>
            <xsd:complexType xmlns:xsd="http://www.w3.org/2001/XMLSchema">
              <xsd:choice>
                <xsd:sequence>
                    <xsd:element name="item_1" type="xsd:string"/>
                    <xsd:element name="item_2" type="xsd:string"/>
                </xsd:sequence>
                <xsd:element name="item_3" type="xsd:string"/>
              </xsd:choice>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """)
    schema = xsd.Schema(node)
    element = schema.get_element('ns0:ElementName')
    elm = element(item_3='foo')
    elm = element(item_1='foo', item_2='bar')
    assert elm['item_1'] == 'foo'
    assert elm['item_2'] == 'bar'

    elm['item_1'] = 'bla-1'
    elm['item_2'] = 'bla-2'

    expected = """
      <document>
        <ns0:ElementName xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item_1>bla-1</ns0:item_1>
          <ns0:item_2>bla-2</ns0:item_2>
        </ns0:ElementName>
      </document>
    """
    node = etree.Element('document')
    element.render(node, elm)
    assert_nodes_equal(expected, node)
Пример #11
0
def test_validate_required_attribute():
    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>
              <attribute name="item" type="string" use="required"/>
            </complexType>
          </element>
        </schema>
    """
        )
    )
    schema.set_ns_prefix("tns", "http://tests.python-zeep.org/")

    container_elm = schema.get_element("tns:container")
    obj = container_elm()

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/" item="bar"/>
      </document>
    """

    with pytest.raises(exceptions.ValidationError) as exc:
        result = render_node(container_elm, obj)
    assert "The attribute item is not valid: Value is required (container.item)" in str(
        exc
    )

    obj.item = "bar"
    result = render_node(container_elm, obj)

    assert_nodes_equal(result, expected)

    obj = container_elm.parse(result[0], schema)
    assert obj.item == "bar"
Пример #12
0
def test_choice_element_with_any():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="container">
            <xsd:complexType>
              <xsd:choice minOccurs="0">
                <xsd:element name="item_1" type="xsd:string" />
                <xsd:element name="item_2" type="xsd:string" />
                <xsd:element name="item_3" type="xsd:string" />
                <xsd:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
              </xsd:choice>
              <xsd:attribute name="name" type="xsd:QName" use="required" />
              <xsd:attribute name="something" type="xsd:boolean" use="required" />
              <xsd:anyAttribute namespace="##other" processContents="lax"/>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """.strip())
    schema = xsd.Schema(node)
    element = schema.get_element('ns0:container')
    value = element(item_1="foo", name="foo", something="bar")

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/" name="foo" something="true">
          <ns0:item_1>foo</ns0:item_1>
        </ns0:container>
      </document>
    """
    node = etree.Element('document')
    element.render(node, value)
    assert_nodes_equal(expected, node)

    result = element.parse(node.getchildren()[0], schema)
    assert result.name == 'foo'
    assert result.something is True
    assert result.item_1 == 'foo'
Пример #13
0
def test_choice_with_sequence_second():
    node = load_xml("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="container">
            <xsd:complexType xmlns:xsd="http://www.w3.org/2001/XMLSchema">
              <xsd:choice>
                <xsd:sequence>
                    <xsd:element name="item_1" type="xsd:string"/>
                    <xsd:element name="item_2" type="xsd:string"/>
                </xsd:sequence>
                <xsd:sequence>
                    <xsd:element name="item_3" type="xsd:string"/>
                    <xsd:element name="item_4" type="xsd:string"/>
                </xsd:sequence>
              </xsd:choice>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """)
    schema = xsd.Schema(node)
    element = schema.get_element('ns0:container')
    assert element.type.signature() == (
        '({item_1: xsd:string, item_2: xsd:string} | {item_3: xsd:string, item_4: xsd:string})'
    )
    value = element(item_3='foo', item_4='bar')

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item_3>foo</ns0:item_3>
          <ns0:item_4>bar</ns0:item_4>
        </ns0:container>
      </document>
    """
    node = etree.Element('document')
    element.render(node, value)
    assert_nodes_equal(expected, node)
Пример #14
0
def test_xml_group_via_ref():
    schema = xsd.Schema(
        load_xml(
            """
        <?xml version="1.0"?>
        <xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema"
                   xmlns:tns="http://tests.python-zeep.org/"
                   targetNamespace="http://tests.python-zeep.org/"
                   elementFormDefault="qualified">

          <xs:element name="Address">
            <xs:complexType>
              <xs:group ref="tns:Name" />
            </xs:complexType>
          </xs:element>

          <xs:group name="Name">
            <xs:sequence>
              <xs:element name="first_name" type="xs:string" />
              <xs:element name="last_name" type="xs:string" />
            </xs:sequence>
          </xs:group>

        </xs:schema>
    """
        )
    )
    address_type = schema.get_element("{http://tests.python-zeep.org/}Address")

    obj = address_type(first_name="foo", last_name="bar")

    node = etree.Element("document")
    address_type.render(node, obj)
    expected = """
        <document>
            <ns0:Address xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:first_name>foo</ns0:first_name>
                <ns0:last_name>bar</ns0:last_name>
            </ns0:Address>
        </document>
    """
    assert_nodes_equal(expected, node)
Пример #15
0
def test_duplicate_target_namespace():
    schema_a = etree.fromstring("""
        <?xml version="1.0"?>
        <xs:schema
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://tests.python-zeep.org/a"
            targetNamespace="http://tests.python-zeep.org/a"
            elementFormDefault="qualified">

            <xs:import
                schemaLocation="http://tests.python-zeep.org/b.xsd"
                namespace="http://tests.python-zeep.org/duplicate"/>
            <xs:import
                schemaLocation="http://tests.python-zeep.org/c.xsd"
                namespace="http://tests.python-zeep.org/duplicate"/>
        </xs:schema>
    """.strip())

    schema_b = etree.fromstring("""
        <?xml version="1.0"?>
        <xsd:schema
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://tests.python-zeep.org/duplicate"
            elementFormDefault="qualified">
        </xsd:schema>
    """.strip())

    schema_c = etree.fromstring("""
        <?xml version="1.0"?>
        <xsd:schema
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://tests.python-zeep.org/duplicate"
            elementFormDefault="qualified">
        </xsd:schema>
    """.strip())

    transport = DummyTransport()
    transport.bind('http://tests.python-zeep.org/a.xsd', schema_a)
    transport.bind('http://tests.python-zeep.org/b.xsd', schema_b)
    transport.bind('http://tests.python-zeep.org/c.xsd', schema_c)
    with pytest.warns(ZeepWarning):
        xsd.Schema(schema_a, transport=transport)
Пример #16
0
def test_xml_schema_as_payload():
    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 ref="xsd:schema"/>
                <xsd:any/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """))
    elm_class = schema.get_element('{http://tests.python-zeep.org/}container')

    node = load_xml("""
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <xsd:schema
              targetNamespace="http://tests.python-zeep.org/inline-schema"
              elementFormDefault="qualified">
            <xsd:element name="sub-element">
              <xsd:complexType>
                <xsd:sequence>
                  <xsd:element name="item-1" type="xsd:string"/>
                  <xsd:element name="item-2" type="xsd:string"/>
                </xsd:sequence>
              </xsd:complexType>
            </xsd:element>
          </xsd:schema>
          <ns1:sub-element xmlns:ns1="http://tests.python-zeep.org/inline-schema">
            <ns1:item-1>value-1</ns1:item-1>
            <ns1:item-2>value-2</ns1:item-2>
          </ns1:sub-element>
        </ns0:container>
    """)
    value = elm_class.parse(node, schema)
    assert value._value_1['item-1'] == 'value-1'
    assert value._value_1['item-2'] == 'value-2'
Пример #17
0
def test_simple_type(transport):
    schema = xsd.Schema(load_xml("""
    <xsd:schema
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
        xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
        targetNamespace="http://tests.python-zeep.org/tns">
      <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
      <xsd:complexType name="ArrayOfString">
        <xsd:complexContent>
          <xsd:restriction base="SOAP-ENC:Array">
            <xsd:attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="xsd:string[]"/>
          </xsd:restriction>
        </xsd:complexContent>
      </xsd:complexType>
    </xsd:schema>
    """),
                        transport=transport)

    ArrayOfString = schema.get_type('ns0:ArrayOfString')
    print(ArrayOfString.__dict__)

    value = ArrayOfString(['item', 'and', 'even', 'more', 'items'])

    node = etree.Element('document')
    ArrayOfString.render(node, value)

    expected = """
        <document>
            <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">item</item>
            <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">and</item>
            <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">even</item>
            <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">more</item>
            <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">items</item>
        </document>
    """  # noqa

    assert_nodes_equal(expected, node)

    data = ArrayOfString.parse_xmlelement(node, schema)
    assert data == ['item', 'and', 'even', 'more', 'items']
    assert data.as_value_object()
Пример #18
0
def test_xml_sequence_with_choice():
    schema = xsd.Schema(
        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/tst"
            elementFormDefault="qualified"
            targetNamespace="http://tests.python-zeep.org/tst">
          <element name="container">
            <complexType>
              <sequence>
                <choice>
                  <element name="item_1" type="xsd:string" />
                  <element name="item_2" type="xsd:string" />
                </choice>
                <element name="item_3" type="xsd:string" />
              </sequence>
            </complexType>
          </element>
        </schema>
    """
        )
    )

    xml = load_xml(
        """
        <tst:container
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:tst="http://tests.python-zeep.org/tst">
          <tst:item_1>blabla</tst:item_1>
          <tst:item_3>haha</tst:item_3>
        </tst:container>
    """
    )

    elm = schema.get_element("{http://tests.python-zeep.org/tst}container")
    result = elm.parse(xml, schema)
    assert result.item_1 == "blabla"
    assert result.item_3 == "haha"
Пример #19
0
def test_xml_complex_type_array_to_other_complex_object():
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
          <xs:complexType name="Address">
            <xs:sequence>
              <xs:element minOccurs="0" maxOccurs="1" name="foo" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
          <xs:complexType name="ArrayOfAddress">
            <xs:sequence>
              <xs:element minOccurs="0" maxOccurs="unbounded" name="Address" nillable="true" type="Address" />
            </xs:sequence>
          </xs:complexType>
          <xs:element name="ArrayOfAddress" type="ArrayOfAddress"/>
        </xs:schema>
    """))

    address_array = schema.get_element('ArrayOfAddress')
    obj = address_array()
    assert obj.Address == []

    obj.Address.append(schema.get_type('Address')(foo='foo'))
    obj.Address.append(schema.get_type('Address')(foo='bar'))

    expected = """
        <?xml version="1.0"?>
        <document>
            <ArrayOfAddress>
                <Address>
                    <foo>foo</foo>
                </Address>
                <Address>
                    <foo>bar</foo>
                </Address>
            </ArrayOfAddress>
        </document>
    """

    result = render_node(address_array, obj)
    assert_nodes_equal(expected, result)
def test_extension_abstract_complex_type():
    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:complexType name="Package" abstract="true"/>

            <xsd:complexType name="SpecialPackage">
              <xsd:complexContent>
                <xsd:extension base="tns:Package">
                    <xsd:sequence>
                        <xsd:element name="item" type="xsd:string"/>
                    </xsd:sequence>
                </xsd:extension>
              </xsd:complexContent>
            </xsd:complexType>

          <xsd:element name="SpecialPackage" type="tns:SpecialPackage"/>
        </xsd:schema>
    """
        )
    )
    package_cls = schema.get_element("{http://tests.python-zeep.org/}SpecialPackage")

    obj = package_cls(item="foo")

    node = etree.Element("document")
    package_cls.render(node, obj)
    expected = """
        <document>
            <ns0:SpecialPackage xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:item>foo</ns0:item>
            </ns0:SpecialPackage>
        </document>
    """
    assert_nodes_equal(expected, node)
def test_complex_with_simple():
    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/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="Address">
            <xsd:complexType>
              <xsd:simpleContent>
                <xsd:extension base="tns:DateTimeType">
                  <xsd:attribute name="name" type="xsd:token"/>
                </xsd:extension>
              </xsd:simpleContent>
            </xsd:complexType>
          </xsd:element>

          <xsd:simpleType name="DateTimeType">
            <xsd:restriction base="xsd:dateTime"/>
          </xsd:simpleType>
        </xsd:schema>
    """
        )
    )
    address_type = schema.get_element("ns0:Address")

    assert address_type.type.signature()
    val = datetime.datetime(2016, 5, 29, 11, 13, 45)
    obj = address_type(val, name="foobie")

    expected = """
      <document>
        <ns0:Address xmlns:ns0="http://tests.python-zeep.org/"
            name="foobie">2016-05-29T11:13:45</ns0:Address>
      </document>
    """
    node = etree.Element("document")
    address_type.render(node, obj)
    assert_nodes_equal(expected, node)
Пример #22
0
def test_simple_type_nested_inline_type(transport):
    schema = xsd.Schema(load_xml("""
    <xsd:schema
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
        xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
        targetNamespace="http://tests.python-zeep.org/tns"
        xmlns:tns="http://tests.python-zeep.org/tns">
      <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
      <xsd:complexType name="container">
        <xsd:sequence>
          <xsd:element name="strings" type="tns:ArrayOfString"/>
        </xsd:sequence>
      </xsd:complexType>

      <xsd:complexType name="ArrayOfString">
        <xsd:complexContent>
          <xsd:restriction base="SOAP-ENC:Array">
            <xsd:attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="xsd:string[]"/>
          </xsd:restriction>
        </xsd:complexContent>
      </xsd:complexType>
    </xsd:schema>
    """),
                        transport=transport)

    Container = schema.get_type('ns0:container')
    node = load_xml("""
        <document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <strings xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
              <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">item</item>
              <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">and</item>
              <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">even</item>
              <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">more</item>
              <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">items</item>
            </strings>
        </document>
    """)  # noqa

    data = Container.parse_xmlelement(node, schema)
    assert data.strings == ['item', 'and', 'even', 'more', 'items']
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"
def test_attribute_value_retrieval():
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <complexType name="Address">
            <sequence>
              <element name="Street" type="tns:Street"/>
            </sequence>
          </complexType>
          <complexType name="Street">
            <sequence>
                <element name="Name" type="string"/>
                <element name="Something" type="string" minOccurs="0"/>
            </sequence>
            <attribute name="ID" type="int" use="required"/>
            <attribute name="Postcode" type="string"/>
          </complexType>
        </schema>
    """))

    Addr = schema.get_type("{http://tests.python-zeep.org/}Address")

    address = Addr()
    address.Street = {"ID": 100, "Name": "Foo"}

    expected = """
      <document>
        <ns0:Street xmlns:ns0="http://tests.python-zeep.org/" ID="100">
            <ns0:Name>Foo</ns0:Name>
        </ns0:Street>
      </document>
    """

    node = etree.Element("document")
    Addr.render(node, address)
    assert_nodes_equal(expected, node)
def test_xml_sequence_in_sequence():
    schema = xsd.Schema(
        load_xml(
            """
        <?xml version="1.0"?>
        <schema
                xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <element name="container">
            <complexType>
              <sequence>
                <sequence>
                  <element name="item_1" type="xsd:string"/>
                  <element name="item_2" type="xsd:string"/>
                </sequence>
              </sequence>
            </complexType>
          </element>
          <element name="foobar" type="xsd:string"/>
        </schema>
    """
        )
    )
    element = schema.get_element("ns0:container")
    value = element(item_1="foo", item_2="bar")

    node = etree.Element("document")
    element.render(node, value)

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item_1>foo</ns0:item_1>
          <ns0:item_2>bar</ns0:item_2>
        </ns0:container>
      </document>
    """
    assert_nodes_equal(expected, node)
Пример #26
0
def test_parse_invalid_values():
    schema = xsd.Schema(
        load_xml(b"""
        <?xml version="1.0" encoding="utf-8"?>
        <schema
            xmlns="http://www.w3.org/2001/XMLSchema"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://tests.python-zeep.org/"
            elementFormDefault="qualified"
            targetNamespace="http://tests.python-zeep.org/">
          <element name="container">
            <complexType>
              <sequence>
                <element name="item_1" type="xsd:dateTime" />
                <element name="item_2" type="xsd:date" />
              </sequence>
              <attribute name="attr_1" type="xsd:dateTime" />
              <attribute name="attr_2" type="xsd:date" />
            </complexType>
          </element>
        </schema>
    """))

    xml = load_xml(b"""
        <?xml version="1.0" encoding="utf-8"?>
        <tns:container
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:tns="http://tests.python-zeep.org/"
            attr_1="::" attr_2="2013-10-20">
          <tns:item_1>foo</tns:item_1>
          <tns:item_2>2016-10-20</tns:item_2>
        </tns:container>
    """)

    elm = schema.get_element('{http://tests.python-zeep.org/}container')
    result = elm.parse(xml, schema)
    assert result.item_1 is None
    assert result.item_2 == datetime.date(2016, 10, 20)
    assert result.attr_1 is None
    assert result.attr_2 == datetime.date(2013, 10, 20)
Пример #27
0
def test_xml_sequence_with_nil_element():
    schema = xsd.Schema(
        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/"
            elementFormDefault="qualified"
            targetNamespace="http://tests.python-zeep.org/">
          <element name="container">
            <complexType>
              <sequence>
                <element name="item" type="xsd:string" maxOccurs="unbounded"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """))

    xml = load_xml("""
        <tns:container
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:tns="http://tests.python-zeep.org/">
          <tns:item>text-1</tns:item>
          <tns:item>text-2</tns:item>
          <tns:item/>
          <tns:item>text-4</tns:item>
          <tns:item>text-5</tns:item>
        </tns:container>
    """)

    elm = schema.get_element('{http://tests.python-zeep.org/}container')
    result = elm.parse(xml, schema)
    assert result.item == [
        'text-1',
        'text-2',
        None,
        'text-4',
        'text-5',
    ]
Пример #28
0
def test_validate_element_value():
    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="1" maxOccurs="1" 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()

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item>bar</ns0:item>
        </ns0:container>
      </document>
    """

    with pytest.raises(exceptions.ValidationError) as exc:
        result = render_node(container_elm, obj)
    assert 'Missing element item (container.item)' in str(exc)

    obj.item = 'bar'
    result = render_node(container_elm, obj)

    assert_nodes_equal(result, expected)

    obj = container_elm.parse(result[0], schema)
    assert obj.item == 'bar'
Пример #29
0
def test_choice_optional_values():
    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:complexType name="Transport">
            <xsd:sequence>
                <xsd:choice minOccurs="0" maxOccurs="1">
                    <xsd:element name="item" type="xsd:string"/>
                </xsd:choice>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:schema>
    """)
    schema = xsd.Schema(schema)

    node = load_xml("<Transport></Transport>")
    elm = schema.get_type('ns0:Transport')
    elm.parse_xmlelement(node, schema)
Пример #30
0
def get_any_schema():
    return 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" type="string"/>
          <element name="container">
            <complexType>
              <sequence>
                <any/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """
        )
    )