def test_deserialize(self): class Punk(ComplexModel): __namespace__ = 'some_namespace' a = Unicode b = Integer c = Decimal d = DateTime class Foo(ComplexModel): __namespace__ = 'some_other_namespace' a = Unicode b = Integer c = Decimal d = DateTime e = XmlAttribute(Integer) def __eq__(self, other): # remember that this is a test object assert ( self.a == other.a and self.b == other.b and self.c == other.c and self.d == other.d and self.e == other.e ) return True docs = get_schema_documents([Punk, Foo]) pprint(docs) assert docs['s0'].tag == '{http://www.w3.org/2001/XMLSchema}schema' assert docs['tns'].tag == '{http://www.w3.org/2001/XMLSchema}schema' print() print("the other namespace %r:" % docs['tns'].attrib['targetNamespace']) assert docs['tns'].attrib['targetNamespace'] == 'some_namespace' print(etree.tostring(docs['tns'], pretty_print=True)) print() print("the other namespace %r:" % docs['s0'].attrib['targetNamespace']) assert docs['s0'].attrib['targetNamespace'] == 'some_other_namespace' print(etree.tostring(docs['s0'], pretty_print=True)) print() foo = Foo(a=u'a', b=1, c=decimal.Decimal('3.4'), d=datetime(2011,02,20,tzinfo=pytz.utc), e=5) doc = get_object_as_xml(foo, Foo) print(etree.tostring(doc, pretty_print=True)) foo_back = get_xml_as_object(doc, Foo) assert foo_back == foo # as long as it doesn't fail, it's ok. get_validation_schema([Punk, Foo])
def test_deserialize(self): class Punk(ComplexModel): __namespace__ = 'some_namespace' a = Unicode b = Integer c = Decimal d = DateTime class Foo(ComplexModel): __namespace__ = 'some_other_namespace' a = Unicode b = Integer c = Decimal d = DateTime e = XmlAttribute(Integer) def __eq__(self, other): # remember that this is a test object assert ( self.a == other.a and self.b == other.b and self.c == other.c and self.d == other.d and self.e == other.e ) return True docs = get_schema_documents([Punk, Foo]) pprint(docs) assert docs['s0'].tag == '{http://www.w3.org/2001/XMLSchema}schema' assert docs['tns'].tag == '{http://www.w3.org/2001/XMLSchema}schema' print() print("the other namespace %r:" % docs['tns'].attrib['targetNamespace']) assert docs['tns'].attrib['targetNamespace'] == 'some_namespace' print(etree.tostring(docs['tns'], pretty_print=True)) print() print("the other namespace %r:" % docs['s0'].attrib['targetNamespace']) assert docs['s0'].attrib['targetNamespace'] == 'some_other_namespace' print(etree.tostring(docs['s0'], pretty_print=True)) print() foo = Foo(a=u'a', b=1, c=decimal.Decimal('3.4'), d=datetime(2011,2,20,tzinfo=pytz.utc), e=5) doc = get_object_as_xml(foo, Foo) print(etree.tostring(doc, pretty_print=True)) foo_back = get_xml_as_object(doc, Foo) assert foo_back == foo # as long as it doesn't fail, it's ok. get_validation_schema([Punk, Foo])
# the default ns prefix is always tns print("the default namespace %r:" % docs['tns'].attrib['targetNamespace']) print(etree.tostring(docs['tns'], pretty_print=True)) print() # Namespace prefixes are assigned like s0, s1, s2, etc... print("the other namespace %r:" % docs['s0'].attrib['targetNamespace']) print(etree.tostring(docs['s0'], pretty_print=True)) print() print("the other namespace %r:" % docs['s2'].attrib['targetNamespace']) print(etree.tostring(docs['s2'], pretty_print=True)) print() # Object serialization and deserialization foo = Foo(a='a', b=1, c=3.4, d=datetime(2011, 02, 20), e=5, f='f') doc = get_object_as_xml(foo, Foo) print(etree.tostring(doc, pretty_print=True)) print(get_xml_as_object(doc, Foo)) print() # XmlData example. print("Product output (illustrates XmlData):") product = Product(id=uuid.uuid4(), edition=ProductEdition(id=uuid.uuid4(), name='My edition')) print(etree.tostring(get_object_as_xml(product, Product), pretty_print=True)) # See http://lxml.de/validation.html to see what this could be used for. print(get_validation_schema([Punk, Foo]))