예제 #1
0
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
예제 #2
0
def test_parse_anytype_obj():
    value_type = xsd.ComplexType(children=[
        xsd.Element('{http://tests.python-zeep.org/}value', xsd.Integer()),
    ])

    schema = xsd.Schema()
    schema.register_type('{http://tests.python-zeep.org/}something',
                         value_type)

    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'container'),
        xsd.ComplexType(children=[
            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
예제 #3
0
def guess_xsd_type(obj):
    """Return the XSD Type for the given object"""
    if isinstance(obj, bool):
        return xsd.Boolean()
    if isinstance(obj, int):
        return xsd.Integer()
    if isinstance(obj, float):
        return xsd.Float()
    if isinstance(obj, datetime.datetime):
        return xsd.DateTime()
    if isinstance(obj, datetime.date):
        return xsd.Date()
    return xsd.String()
예제 #4
0
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 = schema.root_document
    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
예제 #5
0
def test_sequence_parse_anytype_obj():
    value_type = xsd.ComplexType(
        xsd.Sequence([
            xsd.Element(
                '{http://tests.python-zeep.org/}value',
                xsd.Integer()),
        ])
    )

    parser_context = ParserContext()
    schema = SchemaDocument(
        etree.Element('{http://www.w3.org/2001/XMLSchema}Schema'),
        transport=None,
        location=None,
        parser_context=parser_context,
        base_url=None)

    schema._target_namespace = 'http://tests.python-zeep.org/'
    schema.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