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_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_serialize_simple(): 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/", "name"), xsd.String(), ), xsd.Attribute( etree.QName("http://tests.python-zeep.org/", "attr"), xsd.String(), ), ])), ) obj = custom_type(name="foo", attr="x") assert obj.name == "foo" assert obj.attr == "x" result = serialize_object(obj) assert result == {"name": "foo", "attr": "x"}
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_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', 'item_2': None, 'item_3': None, }
def test_serialize_simple(): 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/', 'name'), xsd.String()), xsd.Attribute( etree.QName('http://tests.python-zeep.org/', 'attr'), xsd.String()), ]))) obj = custom_type(name='foo', attr='x') assert obj.name == 'foo' assert obj.attr == 'x' result = serialize_object(obj) assert result == { 'name': 'foo', 'attr': 'x', }
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 _resolve_header(self, info, definitions, parts): name = etree.QName(self.nsmap['soap-env'], 'Header') container = xsd.All(consume_other=True) if not info: return xsd.Element(name, xsd.ComplexType(container)) for item in info: message_name = item['message'].text part_name = item['part'] message = definitions.get('messages', message_name) if message == self.abstract: del parts[part_name] part = message.parts[part_name] if part.element: element = part.element.clone() element.attr_name = part_name else: element = xsd.Element(part_name, part.type) container.append(element) return xsd.Element(name, xsd.ComplexType(container))
def test_choice_sequences_simple(): 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([]) kwargs = {"item_1": "value-1", "item_2": "value-2"} result = valueobjects._process_signature(xsd_type, args, kwargs) assert result == { "item_1": "value-1", "item_2": "value-2", "item_3": None, "item_4": None, }
def _resolve_header(self, info, definitions, parts): if not info: return sequence = xsd.Sequence() for item in info: message_name = item['message'].text part_name = item['part'] message = definitions.get('messages', message_name) if message == self.abstract: del parts[part_name] sequence.append(message.parts[part_name].element) return xsd.Element(None, xsd.ComplexType(sequence))
def showtraindepart(station): WSDL = 'http://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2017-10-01' client = Client(wsdl=WSDL) header = xsd.Element( '{http://thalesgroup.com/RTTI/2013-11-28/Token/types}AccessToken', xsd.ComplexType([ xsd.Element( '{http://thalesgroup.com/RTTI/2013-11-28/Token/types}TokenValue', xsd.String()), ]) ) header_value = header(TokenValue=config.LDB_TOKEN) res = client.service.GetDepartureBoard( numRows=10, crs=station.upper(), _soapheaders=[header_value]) services = res.trainServices.service deps = [] for service in services: traindep = {} traindep['deptime'] = service.std traindep['dest'] = service.destination.location[0].locationName traindep['toc'] = service.operator traindep['platform'] = service.platform if service.etd == "On time": traindep['status'] = service.etd elif service.etd == "Cancelled": traindep['status'] = service.etd traindep['actualtime'] = "" traindep['platform'] = " " else: traindep['status'] = "Late" traindep['actdeptime'] = service.etd deps.append(traindep) html = render_template('train.depart.html',config=config, loc=res.locationName, deps=deps) return html
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_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 create_document_info(doc_name, doc_instructions): print "entering create_document_info" # ns1:DocumentInfo(childDocumentInfos: ns1:DocumentInfo[], clientIdentifier: xsd:string, dateRequested: ns1:Date, # instructions: xsd:string, metadata: ns1:Metadata[], name: xsd:string, projectTicket: xsd:string, # sourceLocale: xsd:string, submissionTicket: xsd:string, targetInfos: ns1:TargetInfo[], wordCount: xsd:int) tgt_infos = create_target_info() date_type = document_client.get_type('ns1:Date') date_wrap = xsd.Element('Date', date_type) date_value = date_wrap(critical=False, date=date_requested) docinfo_type = document_client.get_type('ns1:DocumentInfo') docinfo_wrap = xsd.Element('DocumentInfo', docinfo_type) if submission_ticket is not None: document_info = docinfo_wrap(childDocumentInfos=zeep.xsd.Nil, clientIdentifier=client_identifier, dateRequested=date_value, instructions=zeep.xsd.Nil, metadata=[], name=doc_name, projectTicket=project_ticket, sourceLocale=src_lang, submissionTicket=submission_ticket, targetInfos=tgt_infos, wordCount=zeep.xsd.Nil) else: document_info = docinfo_wrap(childDocumentInfos=zeep.xsd.Nil, clientIdentifier=client_identifier, dateRequested=date_value, instructions=zeep.xsd.Nil, metadata=[], name=doc_name, projectTicket=project_ticket, sourceLocale=src_lang, submissionTicket=zeep.xsd.Nil, targetInfos=tgt_infos, wordCount=zeep.xsd.Nil) return document_info
def test_sequence_parse_anytype_obj(): value_type = xsd.ComplexType( xsd.Sequence([ xsd.Element( '{http://tests.python-zeep.org/}value', xsd.Integer()), ]) ) schema = Schema( etree.Element( '{http://www.w3.org/2001/XMLSchema}Schema', targetNamespace='http://tests.python-zeep.org/')) root = list(schema._schemas.values())[0] root.register_type('{http://tests.python-zeep.org/}something', value_type) 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.AnyType()), ]) )) expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ns0:item_1 xsi:type="ns0:something"> <ns0:value>100</ns0:value> </ns0:item_1> </ns0:container> """) obj = custom_type.parse(expected, schema) assert obj.item_1.value == 100
def test_document_message_serializer(): wsdl = stub(schema=stub(_prefix_map={})) operation = stub(soapaction='my-actiGn') msg = messages.DocumentMessage(wsdl=wsdl, name=None, operation=operation, nsmap=soap.Soap11Binding.nsmap) namespace = 'http://test.python-zeep.org/tests/document' # Fake resolve() msg.body = xsd.Element( etree.QName(namespace, 'response'), xsd.ComplexType([ xsd.Element(etree.QName(namespace, 'arg1'), xsd.String()), xsd.Element(etree.QName(namespace, 'arg2'), xsd.String()), ])) msg.namespace = { 'body': 'http://test.python-zeep.org/tests/document', 'header': None, 'headerfault': None } serialized = msg.serialize(arg1='ah1', arg2='ah2') expected = """ <?xml version="1.0"?> <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Body> <ns0:response xmlns:ns0="http://test.python-zeep.org/tests/document"> <ns0:arg1>ah1</ns0:arg1> <ns0:arg2>ah2</ns0:arg2> </ns0:response> </soap-env:Body> </soap-env:Envelope> """ assert_nodes_equal(expected, serialized.content)
def resolve(self, definitions, abstract_message): """Override the default `SoapMessage.resolve()` since we need to wrap the parts in an element. """ self.abstract = abstract_message # If this message has no parts then we have nothing to do. This might # happen for output messages which don't return anything. if not self.abstract.parts and self.type != 'input': return parts = OrderedDict(self.abstract.parts) self.headers = self._resolve_header(self._info['header'], definitions, parts) self.headerfault = self._resolve_header(self._info['headerfault'], definitions, parts) # 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. body_info = self._info['body'] if body_info: namespace = self._info['body']['namespace'] if self.type == 'input': tag_name = etree.QName(namespace, self.operation.name) else: tag_name = etree.QName(namespace, self.abstract.name.localname) self.body = xsd.Element( tag_name, xsd.ComplexType( xsd.Sequence([ msg.element if msg.element else xsd.Element( name, msg.type) for name, msg in parts.items() ])))
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 __init__(self, client): self.client = client WDSL = "http://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2017-10-01" self.stations = {} with open("station_crs.txt", "r") as f: for line in f: self.stations[line.split(":")[0]] = ( line.split(":")[1]).replace("\n", "") history = HistoryPlugin() self.wdsl_client = Client(wsdl=WDSL, plugins=[history]) header = xsd.Element( '{http://thalesgroup.com/RTTI/2013-11-28/Token/types}AccessToken', xsd.ComplexType([ xsd.Element( '{http://thalesgroup.com/RTTI/2013-11-28/Token/types}TokenValue', xsd.String()), ])) self.header_value = header(TokenValue=os.environ.get("NRE_TOKEN")) self.tfl_api_token = json.loads(os.environ) self.line_id = { "bakerloo": 0, "central": 1, "circle": 2, "district": 3, "hammersmith-city": 4, "jubilee": 5, "metropolitan": 6, "northern": 7, "piccadilly": 8, "victoria": 9, "waterloo-city": 10 }
def test_default_soap_headers(): 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='******') 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') 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()) == 2
def test_signature_nested_sequences_multiple(): 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()), ]) ], min_occurs=2, max_occurs=3) ]))) assert custom_type.signature() == ( 'item_1: xsd:string, item_2: xsd:string, item_3: xsd:string, item_4: xsd:string, _value_1: ({item_5: xsd:string} | {item_6: xsd:string} | {item_5: xsd:string, item_6: xsd:string})[]' # noqa )
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_mime_content_serialize_xml(): wsdl = stub(types=stub(_prefix_map={})) operation = stub(location='my-action', name='foo') element_1 = xsd.Element( 'arg1', xsd.ComplexType([xsd.Element('arg1_1', xsd.String())])) element_2 = xsd.Element('arg2', xsd.String()) abstract_message = definitions.AbstractMessage( etree.QName('{http://test.python-zeep.org/tests/msg}Method')) abstract_message.parts = OrderedDict([ ('xarg1', definitions.MessagePart(element=element_1, type=None)), ('xarg2', definitions.MessagePart(element=element_2, type=None)), ]) msg = messages.MimeContent(wsdl=wsdl, name=None, operation=operation, content_type='text/xml', part_name=None) msg._info = { 'body': { 'namespace': 'http://test.python-zeep.org/tests/rpc' }, 'header': None, 'headerfault': None } msg.resolve(wsdl, abstract_message) serialized = msg.serialize(xarg1={'arg1_1': 'uh'}, xarg2='bla') assert serialized.headers == {'Content-Type': 'text/xml'} assert serialized.path == 'my-action' assert_nodes_equal( load_xml(serialized.content), load_xml( "<foo><xarg1><arg1_1>uh</arg1_1></xarg1><xarg2>bla</xarg2></foo>"))
def test_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/', 'item_1'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'item_2'), xsd.ComplexType( xsd.Sequence([ xsd.Element( '{http://tests.python-zeep.org/}item_2a', xsd.String()), xsd.Element( '{http://tests.python-zeep.org/}item_2b', xsd.String()), ]) ) ) ]) )) expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> <ns0:item_2> <ns0:item_2a>2a</ns0:item_2a> <ns0:item_2b>2b</ns0:item_2b> </ns0:item_2> </ns0:container> """) obj = custom_type.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2.item_2a == '2a' assert obj.item_2.item_2b == '2b'
def test_create_node(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType(children=[ 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(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)
def test_nested_choice_optional(): 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.Choice([ xsd.Element('{http://tests.python-zeep.org/}item_2', xsd.String()), xsd.Element('{http://tests.python-zeep.org/}item_3', xsd.String()), ], min_occurs=0, max_occurs=1), ]))) 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' expected = etree.fromstring(""" <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>foo</ns0:item_1> </ns0:container> """) obj = custom_type.parse(expected, None) assert obj.item_1 == 'foo' assert obj.item_2 is None assert obj.item_3 is None
def test_group_optional(): 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))) 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' assert not hasattr(obj, 'foobar')
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', }]