Пример #1
0
def test_any():
    some_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'doei'), xsd.String())

    complex_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'complex'),
        xsd.ComplexType(
            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()),
            ])))

    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'hoi'),
        xsd.ComplexType(xsd.Sequence([
            xsd.Any(),
            xsd.Any(),
            xsd.Any(),
        ])))

    any_1 = xsd.AnyObject(some_type, "DOEI!")
    any_2 = xsd.AnyObject(complex_type,
                          complex_type(item_1='val_1', item_2='val_2'))
    any_3 = xsd.AnyObject(complex_type, [
        complex_type(item_1='val_1_1', item_2='val_1_2'),
        complex_type(item_1='val_2_1', item_2='val_2_2'),
    ])

    obj = custom_type(_value_1=any_1, _value_2=any_2, _value_3=any_3)

    expected = """
      <document>
        <ns0:hoi xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:doei>DOEI!</ns0:doei>
          <ns0:complex>
            <ns0:item_1>val_1</ns0:item_1>
            <ns0:item_2>val_2</ns0:item_2>
          </ns0:complex>
          <ns0:complex>
            <ns0:item_1>val_1_1</ns0:item_1>
            <ns0:item_2>val_1_2</ns0:item_2>
          </ns0:complex>
          <ns0:complex>
            <ns0:item_1>val_2_1</ns0:item_1>
            <ns0:item_2>val_2_2</ns0:item_2>
          </ns0:complex>
        </ns0:hoi>
      </document>
    """
    node = etree.Element('document')
    custom_type.render(node, obj)
    assert_nodes_equal(expected, node)
Пример #2
0
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(node.getchildren()[0], schema=schema)
    result = serialize_object(obj)

    assert result == {
        '_value_1': [any_obj],
    }
Пример #3
0
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]}
Пример #4
0
def test_signature_complex_type_sequence_with_anys():
    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'authentication'),
        xsd.ComplexType(
            xsd.Choice([
                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.ComplexType(xsd.Sequence([
                        xsd.Any(),
                        xsd.Any(),
                    ])))
            ])))
    assert custom_type.signature() == (
        '({item_1: xsd:string} | {item_2: {_value_1: ANY, _value_2: ANY}})')
Пример #5
0
def test_any_type_check():
    some_type = xsd.Element(
        etree.QName("http://tests.python-zeep.org/", "doei"), xsd.String())

    custom_type = xsd.Element(
        etree.QName("http://tests.python-zeep.org/", "complex"),
        xsd.ComplexType(xsd.Sequence([xsd.Any()])),
    )
    with pytest.raises(TypeError):
        custom_type(_any_1=some_type)
Пример #6
0
def test_any_type_check():
    some_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'doei'), xsd.String())

    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'complex'),
        xsd.ComplexType(children=[
            xsd.Any(),
        ]))
    with pytest.raises(TypeError):
        custom_type(_any_1=some_type)
Пример #7
0
def test_signature_complex_type_sequence_with_anys():
    custom_type = xsd.Element(
        etree.QName("http://tests.python-zeep.org/", "authentication"),
        xsd.ComplexType(
            xsd.Choice([
                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.ComplexType(xsd.Sequence([xsd.Any(),
                                                  xsd.Any()])),
                ),
            ])),
    )
    assert custom_type.signature() == (
        "{http://tests.python-zeep.org/}authentication(" +
        "({item_1: xsd:string} | {item_2: {_value_1: ANY, _value_2: ANY}})" +
        ")")
Пример #8
0
def test_signature_complex_type_any():
    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'authentication'),
        xsd.ComplexType(
            xsd.Choice([
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'item_1'),
                    xsd.String()),
                xsd.Any()
            ])))
    assert custom_type.signature(
    ) == '({item_1: xsd:string} | {_value_1: ANY})'
    custom_type(item_1='foo')
Пример #9
0
def test_container_elements():
    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/', 'username'),
                    xsd.String()),
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'password'),
                    xsd.String()),
                xsd.Any(),
            ])))

    # sequences
    custom_type(username='******', password='******')
Пример #10
0
def test_signature_complex_type_any():
    custom_type = xsd.Element(
        etree.QName("http://tests.python-zeep.org/", "authentication"),
        xsd.ComplexType(
            xsd.Choice([
                xsd.Element(
                    etree.QName("http://tests.python-zeep.org/", "item_1"),
                    xsd.String(),
                ),
                xsd.Any(),
            ])),
    )
    assert (
        custom_type.signature() ==
        "{http://tests.python-zeep.org/}authentication(({item_1: xsd:string} | {_value_1: ANY}))"
    )
    custom_type(item_1="foo")
Пример #11
0
def test_container_elements():
    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/", "username"),
                    xsd.String(),
                ),
                xsd.Element(
                    etree.QName("http://tests.python-zeep.org/", "password"),
                    xsd.String(),
                ),
                xsd.Any(),
            ])),
    )

    # sequences
    custom_type(username="******", password="******")