1 #----------------------------------------------------------------------------
2 # Name: xmlmarshallertests.py
5 # Author: John Spurling
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
15 from xmlprettyprinter
import xmlprettyprint
17 marshalledPersonObject
= """
18 <person objtype="Person">
19 <firstName>Albert</firstName>
20 <lastName>Camus</lastName>
21 <address>23 Absurd St.</address>
25 <_phoneNumber>808-303-2323</_phoneNumber>
26 <favoriteWords objtype="list">
29 <item>existence</item>
31 <weight objtype="float">150</weight>
36 <item objtype="int">23</item>
40 <mylist objtype="list">
46 ## a dummy class taken from the old XmlMarshaller module.
48 ## def __init__(self):
49 ## # These are not necessary but are nice if you want to tailor
50 ## # the Python object <-> XML binding
52 ## # The xml element name to use for this object, otherwise it
53 ## # will use a fully qualified Python name like __main__.Person
54 ## # which can be ugly.
55 ## self.__xmlname__ = "person"
56 ## self.firstName = None
57 ## self.lastName = None
58 ## self.addressLine1 = None
59 ## self.addressLine2 = None
63 ## self._phoneNumber = None
64 ## self.favoriteWords = None
67 __xmlflattensequence__
= {'asequence': ('the_earth_is_flat',)}
69 class XmlMarshallerTestFunctions(unittest
.TestCase
):
72 '''common setup code goes here.'''
76 xml
= xmlmarshaller
.marshal(1)
77 print "\n#########################################"
78 print "# testString test case #"
79 print "#########################################"
80 print "marshalled int object:\n"
81 print xmlprettyprint(xml
)
84 xml
= xmlmarshaller
.marshal({'one': 1,
87 print "\n#########################################"
88 print "# testString test case #"
89 print "#########################################"
90 print "marshalled dict object:\n"
91 print xmlprettyprint(xml
)
94 xmltrue
= xmlmarshaller
.marshal(True)
95 xmlfalse
= xmlmarshaller
.marshal(False)
96 print "\n#########################################"
97 print "# testBool test case #"
98 print "#########################################"
99 print "marshalled boolean true object:\n"
100 print xmlprettyprint(xmltrue
)
101 print "\nmarshalled boolean false object:\n"
102 print xmlprettyprint(xmlfalse
)
103 pytrue
= xmlmarshaller
.unmarshal(xmltrue
)
104 assert pytrue
is True
105 pyfalse
= xmlmarshaller
.unmarshal(xmlfalse
)
106 assert pyfalse
is False
108 def testString(self
):
109 xml
= xmlmarshaller
.marshal(
110 "all your marshalled objects are belong to us")
111 print "\n#########################################"
112 print "# testString test case #"
113 print "#########################################"
114 print xmlprettyprint(xml
)
116 def testEmptyElement(self
):
118 person
.firstName
= "Albert"
119 person
.__xmlattributes
__ = ('firstName',)
120 xml
= xmlmarshaller
.marshal(person
, 'person')
121 print "\n#########################################"
122 print "# testEmptyElement test case #"
123 print "#########################################"
125 assert (xml
== """<person objtype="__main__.Person" firstName="Albert"/>""")
127 def testXMLFlattenSequence(self
):
129 person
.asequence
= ('one', 'two')
130 xml
= xmlmarshaller
.marshal(person
, 'person')
131 print "\n#########################################"
132 print "# testXMLFlattenSequence test case #"
133 print "#########################################"
135 assert (xml
== """<person objtype="__main__.Person"><the_earth_is_flat>one</the_earth_is_flat><the_earth_is_flat>two</the_earth_is_flat></person>""")
136 unmarshalledperson
= xmlmarshaller
.unmarshal(xml
)
137 assert(hasattr(unmarshalledperson
, 'asequence'))
138 assert(len(unmarshalledperson
.asequence
) == 2)
140 def testInstance(self
):
141 print "\n#########################################"
142 print "# testInstance test case #"
143 print "#########################################"
149 xml
= xmlmarshaller
.marshal(f
, 'foo')
152 def testPerson(self
):
154 person
.firstName
= "Albert"
155 person
.lastName
= "Camus"
156 person
.addressLine1
= "23 Absurd St."
157 person
.city
= "Ennui"
160 person
._phoneNumber
= "808-303-2323"
161 person
.favoriteWords
= ['angst', 'ennui', 'existence']
163 # __xmlattributes__ = ('fabulousness',)
164 person
.fabulousness
= "tres tres"
165 xml
= xmlmarshaller
.marshal(person
)
166 print "\n#########################################"
167 print "# testPerson test case #"
168 print "#########################################"
169 print "Person object marshalled into XML:\n"
171 # When encountering a "person" element, use the Person class
172 ## elementMappings = { "person" : Person }
173 ## obj = unmarshal(xml, elementMappings = elementMappings)
174 ## print "Person object recreated from XML with attribute types indicated:"
175 ## print obj.person.__class__
176 ## for (attr, value) in obj.person.__dict__.items():
177 ## if not attr.startswith("__"):
178 ## print attr, "=", value, type(value)
182 if __name__
== "__main__":