def test_sequence(): root = xsd.Element( etree.QName("http://tests.python-zeep.org/", "container"), xsd.ComplexType( xsd.Sequence( [ xsd.Sequence( [ 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, ), xsd.Sequence( [ xsd.Element( etree.QName( "http://tests.python-zeep.org/", "item_3" ), xsd.String(), ), xsd.Element( etree.QName( "http://tests.python-zeep.org/", "item_4" ), xsd.String(), ), ] ), ] ) ] ) ), ) root( _value_1=[ {"item_1": "foo", "item_2": "bar"}, {"item_1": "foo", "item_2": "bar"}, ], item_3="foo", item_4="bar", )
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)
def test_build_sequence_with_optional_elements(): 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_1"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_2"), xsd.ComplexType( xsd.Sequence( [ xsd.Element( etree.QName( "http://tests.python-zeep.org/", "item_2_1" ), xsd.String(), nillable=True, ) ] ) ), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_3"), xsd.String(), max_occurs=2, ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_4"), xsd.String(), min_occurs=0, ), ] ) ), ) expected = etree.fromstring( """ <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>1</ns0:item_1> <ns0:item_2/> <ns0:item_3>3</ns0:item_3> </ns0:container> """ ) obj = custom_type.parse(expected, None) assert obj.item_1 == "1" assert obj.item_2 is None assert obj.item_3 == ["3"] assert obj.item_4 is None
def test_mixed_choice(): 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()), xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_3'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_4'), xsd.String()), ]), xsd.Choice([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_5'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_6'), xsd.String()), xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_7'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_8'), xsd.String()), ]) ]) ]) )) item = custom_type( item_1='item-1', item_2='item-2', item_3='item-3', item_4='item-4', item_7='item-7', item_8='item-8', ) assert item.item_1 == 'item-1' assert item.item_2 == 'item-2' assert item.item_3 == 'item-3' assert item.item_4 == 'item-4' assert item.item_7 == 'item-7' assert item.item_8 == 'item-8'
def test_signature_nested_sequences(): 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(), ), xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_3"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_4"), xsd.String(), ), ]), xsd.Choice([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_5"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_6"), xsd.String(), ), xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_5"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_6"), xsd.String(), ), ]), ]), ])), ) assert custom_type.signature() == ( "{http://tests.python-zeep.org/}authentication(item_1: xsd:string, item_2: xsd:string, item_3: xsd:string, item_4: xsd:string, ({item_5: xsd:string} | {item_6: xsd:string} | {item_5: xsd:string, item_6: xsd:string}))" )
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 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'}
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) ]) ) ), ]) )) obj = custom_type(item_1=None, item_2=None, item_3=None, item_4={}) 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:container> </document> """ node = render_node(custom_type, obj) etree.cleanup_namespaces(node) assert_nodes_equal(expected, node)
def test_default_soap_headers_extra(): header = xsd.Element( None, 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.Element( None, 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(header.getchildren()) == 4
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_choice_determinst(): root = xsd.Element( etree.QName("http://tests.python-zeep.org/", "kies"), xsd.ComplexType( xsd.Sequence([ xsd.Choice([ 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(), ), ]), xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_2"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "item_1"), xsd.String(), ), ]), ]) ])), ) obj = root(item_1="item-1", item_2="item-2") node = etree.Element("document") root.render(node, obj) assert etree.tostring(node) expected = """ <document> <ns0:kies xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>item-1</ns0:item_1> <ns0:item_2>item-2</ns0:item_2> </ns0:kies> </document> """.strip() assert_nodes_equal(expected, node)
def test_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) )) 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: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'}, ] assert not hasattr(obj, 'foobar')
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"
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)
def test_create_node(): 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.Attribute('attr', xsd.String()), ])) # sequences obj = custom_type(username='******', password='******', attr='x') expected = """ <document> <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/" attr="x"> <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)
def test_build_group_min_occurs_2_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=2, max_occurs=2))) with pytest.raises(TypeError): custom_type(_value_1=[ { 'item_1': 'foo', 'item_2': 'bar', 'error': True }, { 'item_1': 'foo', 'item_2': 'bar' }, ])
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 _resolve_body(self, info, definitions, parts): """Return an XSD element for the SOAP:Body. Each part is a parameter or a return value and appears inside a wrapper element within the body named identically to the operation name and its namespace is the value of the namespace attribute. """ if not info: return None namespace = info["namespace"] if self.type == "input": tag_name = etree.QName(namespace, self.operation.name) else: tag_name = etree.QName(namespace, self.abstract.name.localname) # Create the xsd element to create/parse the response. Each part # is a sub element of the root node (which uses the operation name) elements = [] for name, msg in parts.items(): if msg.element: elements.append(msg.element) else: elements.append(xsd.Element(name, msg.type)) return xsd.Element(tag_name, xsd.ComplexType(xsd.Sequence(elements)))
def test_build_max_occurs_unbounded(): 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()), ], max_occurs='unbounded') )) 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._value_1 == [ { 'item_1': 'foo', 'item_2': 'bar', } ]
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_build_group_min_occurs_2_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=2, max_occurs=2, ) ), ) with pytest.raises(TypeError): custom_type( _value_1=[ {"item_1": "foo", "item_2": "bar", "error": True}, {"item_1": "foo", "item_2": "bar"}, ] )
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_build_sequence_and_attributes(): custom_element = 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()), ]), [ xsd.Attribute( etree.QName('http://tests.python-zeep.org/', 'attr_1'), xsd.String()), xsd.Attribute('attr_2', xsd.String()), ] )) expected = load_xml(""" <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/" ns0:attr_1="x" attr_2="y"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2>bar</ns0:item_2> </ns0:authentication> """) obj = custom_element.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2 == 'bar' assert obj.attr_1 == 'x' assert obj.attr_2 == 'y'
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 = '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(node.getchildren()[0], None) assert obj.item == 'foo' assert obj.foo == 'x' assert obj.attr__item == 'bar'
def test_build_occurs_1(): 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()), ]) )) 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(expected[0], None) assert obj.item_1 == 'foo' assert obj.item_2 == 'bar'
def test_createMassApps(obj): f = obj.openXlsFile('D:\Python\SOAP\delivery_2.xlsx') v_count = 0 for t in f: if len(str(f[str(t)][0])) > 1: contragentId = str(f[str(t)][0]).split(".")[0].strip() shopId = str(f[str(t)][7]).split(".")[0].strip() cardId = int(str(f[str(t)][9]).split(".")[0]) objectTypeId = int(str(f[str(t)][12]).split(".")[0]) dateExp = datetime.fromordinal(datetime(1900, 1, 1).toordinal() + int(f[str(t)][16]) - 2).date() plannedDeliveryDate=datetime.fromordinal(datetime(1900, 1, 1).toordinal() + int(f[str(t)][19]) - 2).date() #listOfNecessaryDocuments = {"documentType": 1, "documentName": 'test', "documentDescription": 'test1'} #listOfNecessaryDocuments = (1, 'test', 'test1') xsd_type = xsd.ComplexType( xsd.Sequence([ xsd.Element('documentType', xsd.String()), xsd.Element('documentName', xsd.String()), xsd.Element('documentDescription', xsd.String()) ])) listOfNecessaryDocuments = xsd_type(1, 2, 3) res = obj.connectToWebService(wsdl).service.createApplication(contragentId=contragentId, taxId=f[str(t)][1], phoneNumber=f[str(t)][2], clientName=str(f[str(t)][3]), deliveryAddress=str(f[str(t)][4]), productName=str(f[str(t)][6]), shopId=shopId, cardId=cardId, cardMask=str(f[str(t)][10]), channelDelivery=str(f[str(t)][11]), objectTypeId=objectTypeId, comment=str(f[str(t)][13]), #str(f[str(t)][11]), messageIdPrimaryProcess=str(f[str(t)][14]), primaryProcessCode=str(f[str(t)][15]), dateExp=dateExp, externalSystem=str(f[str(t)][17]), user=str(f[str(t)][18]), plannedDeliveryDate=plannedDeliveryDate, listOfNecessaryDocuments=list(listOfNecessaryDocuments)) v_count += 1 print(res) print("Rows created: " + str(v_count))
def test_create_node(): 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.Attribute("attr", xsd.String())], ), ) # sequences obj = custom_type(username="******", password="******", attr="x") expected = """ <document> <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/" attr="x"> <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)
def test_choice_max_occurs(): fields = xsd.ComplexType( xsd.Sequence([ xsd.Choice([ xsd.Element('item_1', xsd.String()), xsd.Element('item_2', xsd.String()) ], max_occurs=3) ])) args = tuple([]) kwargs = { '_value_1': [{ 'item_1': 'foo' }, { 'item_2': 'bar' }, { 'item_1': 'bla' }] } result = valueobjects._process_signature(fields, args, kwargs) assert result == { '_value_1': [ { 'item_1': 'foo' }, { 'item_2': 'bar' }, { 'item_1': 'bla' }, ] }
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], }
def test_choice_max_occurs_on_choice(): xsd_type = xsd.ComplexType( xsd.Sequence([ xsd.Choice([ xsd.Element('item_1', xsd.String(), max_occurs=2), xsd.Element('item_2', xsd.String()) ], max_occurs=2) ])) args = tuple([]) kwargs = { '_value_1': [ { 'item_1': ['foo', 'bar'] }, { 'item_2': 'bla' }, ] } result = valueobjects._process_signature(xsd_type, args, kwargs) assert result == { '_value_1': [{ 'item_1': ['foo', 'bar'] }, { 'item_2': 'bla' }] }