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_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 = "{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_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_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_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_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 visit_attribute(self, node, parent, namespace): node_type = node.get('type') if node_type: xml_type = parse_qname(node_type, node.nsmap) else: assert NotImplementedError() name = node.get('name') try: xsd_type = self.get_type(node_type)() except KeyError: xsd_type = xsd.UnresolvedType(xml_type) attr = xsd.Attribute(name, type_=xsd_type) self.elm_instances.append(attr) return attr
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_parse_basic_with_attrs(): custom_element = 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()), xsd.Attribute( etree.QName('http://tests.python-zeep.org/', 'attr_1'), xsd.String()), ])) expected = etree.fromstring(""" <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/" attr_1="x"> <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'
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_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_serialize(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType(children=[ xsd.Element(etree.QName('http://tests.python-zeep.org/', 'name'), xsd.String()), xsd.Attribute(etree.QName('http://tests.python-zeep.org/', 'attr'), xsd.String()), xsd.ListElement( etree.QName('http://tests.python-zeep.org/', 'items'), xsd.ComplexType(children=[ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'x'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'y'), xsd.ComplexType(children=[ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'x'), xsd.String()), ])) ]), max_occurs=2) ])) obj = custom_type(name='foo', attr='x', items=[ { 'x': 'bla', 'y': { 'x': 'deep' } }, { 'x': 'foo', 'y': { 'x': 'deeper' } }, ]) result = serialize_object(obj) assert result == { 'name': 'foo', 'attr': 'x', 'items': [ { 'x': 'bla', 'y': { 'x': 'deep' } }, { 'x': 'foo', 'y': { 'x': 'deeper' } }, ] }