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')
def test_choice_sequences_no_match_nested(): xsd_type = xsd.ComplexType( xsd.Sequence([ xsd.Choice([ xsd.Sequence([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String()) ]), ]) ])) args = tuple([]) kwargs = {'item_1': 'value-1'} value = valueobjects._process_signature(xsd_type, args, kwargs) assert value == { 'item_1': 'value-1', 'item_2': None, }
def abstract_message_output(): abstract = definitions.AbstractMessage( etree.QName('{http://test.python-zeep.org/tests/msg}Response')) abstract.parts = OrderedDict([ ('result', definitions.MessagePart( element=None, type=xsd.String())), ]) return abstract
def test_xsi(): org_type = xsd.Element( '{https://tests.python-zeep.org/}original', xsd.ComplexType( xsd.Sequence([ xsd.Element('username', xsd.String()), xsd.Element('password', xsd.String()), ]))) alt_type = xsd.Element( '{https://tests.python-zeep.org/}alternative', xsd.ComplexType( xsd.Sequence([ xsd.Element('username', xsd.String()), xsd.Element('password', xsd.String()), ]))) instance = alt_type(username='******', password='******') render_node(org_type, instance)
def test_element_any_type_elements(): node = etree.fromstring(""" <?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="something" type="anyType"/> </sequence> </complexType> </element> </schema> """.strip()) schema = xsd.Schema(node) Child = xsd.ComplexType( xsd.Sequence([ xsd.Element('{http://tests.python-zeep.org/}item_1', xsd.String()), xsd.Element('{http://tests.python-zeep.org/}item_2', xsd.String()), ])) child = Child(item_1='item-1', item_2='item-2') container_elm = schema.get_element( '{http://tests.python-zeep.org/}container') obj = container_elm(something=child) node = etree.Element('document') container_elm.render(node, obj) expected = """ <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:something> <ns0:item_1>item-1</ns0:item_1> <ns0:item_2>item-2</ns0:item_2> </ns0:something> </ns0:container> </document> """ assert_nodes_equal(expected, node) item = container_elm.parse(node.getchildren()[0], schema) assert len(item.something) == 2 assert item.something[0].text == 'item-1' assert item.something[1].text == 'item-2'
def test_attr_name(): 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(), attr_name='username'), xsd.Element(etree.QName('http://tests.python-zeep.org/', 'Password_x'), xsd.String(), attr_name='password'), ]))) # sequences custom_type(username='******', password='******')
def test_complex_type(): custom_type = 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(), ), ] ) ) obj = custom_type("user", "pass") assert {key: obj[key] for key in obj} == {"username": "******", "password": "******"}
def test_build_min_occurs_2_max_occurs_2(): 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/', 'item_1'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_2'), xsd.String()), ], min_occurs=2, max_occurs=2) )) assert custom_type.signature() elm = custom_type(_value_1=[ {'item_1': 'foo-1', 'item_2': 'bar-1'}, {'item_1': 'foo-2', 'item_2': 'bar-2'}, ]) assert elm._value_1 == [ {'item_1': 'foo-1', 'item_2': 'bar-1'}, {'item_1': 'foo-2', 'item_2': 'bar-2'}, ] expected = load_xml(""" <ns0:container 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:container> """) obj = custom_type.parse(expected, None) assert obj._value_1 == [ { 'item_1': 'foo', 'item_2': 'bar', }, { 'item_1': 'foo', 'item_2': 'bar', }, ]
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='******')
def test_build_objects(): 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.Group( etree.QName("http://tests.python-zeep.org/", "groupie"), xsd.Sequence( [ xsd.Element( etree.QName( "http://tests.python-zeep.org/", "password" ), xsd.String(), ) ] ), ), ] ) ), ) assert custom_type.signature() obj = custom_type(username="******", password="******") expected = """ <document> <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/"> <ns0:username>foo</ns0:username> <ns0:password>bar</ns0:password> </ns0:authentication> </document> """ node = etree.Element("document") custom_type.render(node, obj) assert_nodes_equal(expected, node) obj = custom_type.parse(node[0], None) assert obj.username == "foo" assert obj.password == "bar"
def test_build_group_min_occurs_1(): 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=1, ) ), ) obj = custom_type(item_1="foo", item_2="bar") assert obj.item_1 == "foo" assert obj.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:authentication> </document> """ ) assert_nodes_equal(result, expected) obj = custom_type.parse(result[0], None) assert obj.item_1 == "foo" assert obj.item_2 == "bar" assert not hasattr(obj, "foobar")
def test_choice_sequences_no_match_last(): xsd_type = xsd.ComplexType( xsd.Sequence([ xsd.Choice([ xsd.Sequence([ xsd.Element("item_1", xsd.String()), xsd.Element("item_2", xsd.String()), ]), xsd.Sequence([ xsd.Element("item_3", xsd.String()), xsd.Element("item_4", xsd.String()), ]), ]) ])) args = tuple([]) with pytest.raises(TypeError): kwargs = {"item_2": "value-2", "item_4": "value-4"} valueobjects._process_signature(xsd_type, args, kwargs)
def test_choice_sequences_no_match_last(): xsd_type = xsd.ComplexType( xsd.Sequence([ xsd.Choice([ xsd.Sequence([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String()) ]), xsd.Sequence([ xsd.Element('item_3', xsd.String()), xsd.Element('item_4', xsd.String()) ]), ]) ])) args = tuple([]) with pytest.raises(TypeError): kwargs = {'item_2': 'value-2', 'item_4': 'value-4'} valueobjects._process_signature(xsd_type, args, kwargs)
def test_parse_basic(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType(children=[ 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()), ])) expected = etree.fromstring(""" <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> """) obj = custom_type.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2 == 'bar'
def test_simple_args_attributes_as_kwargs(): xsd_type = xsd.ComplexType( xsd.Sequence([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String()) ]), [ xsd.Attribute('attr_1', xsd.String()) ] ) args = tuple(['value-1', 'value-2']) kwargs = {'attr_1': 'bla'} result = valueobjects._process_signature(xsd_type, args, kwargs) assert result == { 'item_1': 'value-1', 'item_2': 'value-2', 'attr_1': 'bla', }
def test_invalid_kwarg(): 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(), ), ])), ) with pytest.raises(TypeError): custom_type(something="is-wrong")
def test_build_group_min_occurs_1_parse_args(): 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=1))) obj = custom_type('foo', 'bar') assert obj.item_1 == 'foo' assert obj.item_2 == 'bar'
def test_build_min_occurs_2_max_occurs_2_error(): 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/', 'item_1'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_2'), xsd.String()), ], min_occurs=2, max_occurs=2) )) with pytest.raises(TypeError): custom_type(_value_1={ 'item_1': 'foo-1', 'item_2': 'bar-1', 'error': True })
def test_build_group_occurs_1_invalid_kwarg(): 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=1, max_occurs=1))) with pytest.raises(TypeError): custom_type(item_1='foo', item_2='bar', error=True)
def test_choice_sequences_max_occur(): xsd_type = xsd.ComplexType( xsd.Sequence([ xsd.Choice( [ xsd.Sequence([ xsd.Element("item_1", xsd.String()), xsd.Element("item_2", xsd.String()), ]), xsd.Sequence([ xsd.Element("item_2", xsd.String()), xsd.Element("item_3", xsd.String()), ]), ], max_occurs=2, ) ])) args = tuple([]) kwargs = { "_value_1": [ { "item_1": "value-1", "item_2": "value-2" }, { "item_2": "value-2", "item_3": "value-3" }, ] } result = valueobjects._process_signature(xsd_type, args, kwargs) assert result == { "_value_1": [ { "item_1": "value-1", "item_2": "value-2" }, { "item_2": "value-2", "item_3": "value-3" }, ] }
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="******")
def test_xsi(): org_type = xsd.Element( "{https://tests.python-zeep.org/}original", xsd.ComplexType( xsd.Sequence([ xsd.Element("username", xsd.String()), xsd.Element("password", xsd.String()), ])), ) alt_type = xsd.Element( "{https://tests.python-zeep.org/}alternative", xsd.ComplexType( xsd.Sequence([ xsd.Element("username", xsd.String()), xsd.Element("password", xsd.String()), ])), ) instance = alt_type(username="******", password="******") render_node(org_type, instance)
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)
def test_choice(): root = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'kies'), xsd.ComplexType( children=[ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'pre'), xsd.String()), 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.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_3'), xsd.String()), ], max_occurs=3), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'post'), xsd.String()), ] ) ) obj = root('foo', _choice_1=[{'item_1': [20, 30]}, {'item_2': 'nyet'}]) node = etree.Element('document') root.render(node, obj) assert etree.tostring(node) expected = """ <document> <ns0:kies xmlns:ns0="http://tests.python-zeep.org/"> <ns0:pre>foo</ns0:pre> <ns0:item_1>20</ns0:item_1> <ns0:item_1>30</ns0:item_1> <ns0:item_2>nyet</ns0:item_2> <ns0:post/> </ns0:kies> </document> """.strip() assert_nodes_equal(expected, node)
def test_choice_sequences_no_match_nested(): fields = [ ('_choice_1', xsd.Choice([ xsd.Sequence([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String()) ]), ])), ] args = tuple([]) kwargs = {'_choice_1': {'item_1': 'value-1'}} try: utils.process_signature(fields, args, kwargs) except TypeError as exc: assert six.text_type(exc) == ( "No complete xsd:Sequence found for the xsd:Choice '_choice_1'.\n" + "The signature is: _choice_1: {item_1: xsd:string, item_2: xsd:string}") else: assert False, "TypeError not raised"
def test_choice_max_occurs_simple_interface(): fields = [ ('_choice_1', xsd.Choice([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String()) ], max_occurs=2)) ] args = tuple([]) kwargs = { 'item_1': 'foo', 'item_2': 'bar', } result = utils.process_signature(fields, args, kwargs) assert result == { '_choice_1': [ utils.ChoiceItem(0, {'item_1': 'foo'}), utils.ChoiceItem(1, {'item_2': 'bar'}), ] }
def test_document_message_parse_with_header_other_message(): xmlelement = load_xml(""" <input xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://tests.python-zeep.org/tns"> <soap:header message="tns:InputHeader" part="authentication" use="literal" /> <soap:body use="literal"/> </input> """) operation = stub() definitions_ = stub( target_namespace='', messages={}, get=lambda name, key: getattr(definitions_, name).get(key), wsdl=stub()) msg = messages.DocumentMessage.parse( definitions=definitions_, xmlelement=xmlelement, operation=operation, nsmap={}, type='input') abstract_header = definitions.AbstractMessage( etree.QName('{http://tests.python-zeep.org/tns}InputHeader')) abstract_header.parts['authentication'] = definitions.MessagePart( element=xsd.Element(etree.QName('authentication'), xsd.String()), type=None) definitions_.messages[abstract_header.name.text] = abstract_header abstract_body = definitions.AbstractMessage( etree.QName('{http://test.python-zeep.org/tests/msg}Input')) abstract_body.parts['params'] = definitions.MessagePart( element=xsd.Element(etree.QName('input'), xsd.String()), type=None) msg.resolve(definitions_, abstract_body) assert msg.headerfault is None assert msg.header == abstract_header.parts['authentication'].element assert msg.body == abstract_body.parts['params'].element
def test_default_soap_headers_extra(): header = xsd.ComplexType( xsd.Sequence([ xsd.Element("{http://tests.python-zeep.org}name", xsd.String()), xsd.Element("{http://tests.python-zeep.org}password", xsd.String()), ])) header_value = header(name="ik", password="******") extra_header = xsd.ComplexType( xsd.Sequence([ xsd.Element("{http://tests.python-zeep.org}name", xsd.String()), xsd.Element("{http://tests.python-zeep.org}password", xsd.String()), ])) extra_header_value = extra_header(name="ik", password="******") client_obj = client.Client("tests/wsdl_files/soap.wsdl") client_obj.set_default_soapheaders([header_value]) response = """ <?xml version="1.0"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stoc="http://example.com/stockquote.xsd"> <soapenv:Header/> <soapenv:Body> <stoc:TradePrice> <price>120.123</price> </stoc:TradePrice> </soapenv:Body> </soapenv:Envelope> """.strip() with requests_mock.mock() as m: m.post("http://example.com/stockquote", text=response) client_obj.service.GetLastTradePrice("foobar", _soapheaders=[extra_header_value]) doc = load_xml(m.request_history[0].body) header = doc.find("{http://schemas.xmlsoap.org/soap/envelope/}Header") assert header is not None assert len(list(header)) == 4
def test_choice_mixed(): xsd_type = xsd.ComplexType( xsd.Sequence([ xsd.Choice([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String()), ]), xsd.Element('item_2', xsd.String()) ])) expected = '({item_1: xsd:string} | {item_2: xsd:string}), item_2__1: xsd:string' assert xsd_type.signature() == expected args = tuple([]) kwargs = {'item_1': 'value-1', 'item_2__1': 'value-2'} result = valueobjects._process_signature(xsd_type, args, kwargs) assert result == { 'item_1': 'value-1', 'item_2__1': 'value-2', }
def test_choice_sequences_optional_elms(): xsd_type = xsd.ComplexType( xsd.Sequence([ xsd.Choice([ xsd.Sequence([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String(), min_occurs=0) ]), xsd.Sequence([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String()), xsd.Element('item_3', xsd.String()) ]), ]) ])) args = tuple([]) kwargs = {'item_1': 'value-1'} result = valueobjects._process_signature(xsd_type, args, kwargs) assert result == {'item_1': 'value-1'}