]>
Commit | Line | Data |
---|---|---|
1 | #---------------------------------------------------------------------------- | |
2 | # Name: xmlmarshallertests.py | |
3 | # Purpose: | |
4 | # | |
5 | # Author: John Spurling | |
6 | # | |
7 | # Created: 8/16/04 | |
8 | # CVS-ID: $Id$ | |
9 | # Copyright: (c) 2004-2005 ActiveGrid, Inc. | |
10 | # License: wxWindows License | |
11 | #---------------------------------------------------------------------------- | |
12 | ||
13 | import unittest | |
14 | import xmlmarshaller | |
15 | from xmlprettyprinter import xmlprettyprint | |
16 | ||
17 | marshalledPersonObject = """ | |
18 | <person objtype="Person"> | |
19 | <firstName>Albert</firstName> | |
20 | <lastName>Camus</lastName> | |
21 | <address>23 Absurd St.</address> | |
22 | <city>Ennui</city> | |
23 | <state>MO</state> | |
24 | <zip>54321</zip> | |
25 | <_phoneNumber>808-303-2323</_phoneNumber> | |
26 | <favoriteWords objtype="list"> | |
27 | <item>angst</item> | |
28 | <item>ennui</item> | |
29 | <item>existence</item> | |
30 | </favoriteWords> | |
31 | <weight objtype="float">150</weight> | |
32 | </person> | |
33 | """ | |
34 | ||
35 | marshalledint = ''' | |
36 | <item objtype="int">23</item> | |
37 | ''' | |
38 | ||
39 | marshalledlist = ''' | |
40 | <mylist objtype="list"> | |
41 | <item>foo</item> | |
42 | <item>bar</item> | |
43 | </mylist> | |
44 | ''' | |
45 | ||
46 | ## a dummy class taken from the old XmlMarshaller module. | |
47 | ## class Person: | |
48 | ## def __init__(self): | |
49 | ## # These are not necessary but are nice if you want to tailor | |
50 | ## # the Python object <-> XML binding | |
51 | ||
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 | |
60 | ## self.city = None | |
61 | ## self.state = None | |
62 | ## self.zip = None | |
63 | ## self._phoneNumber = None | |
64 | ## self.favoriteWords = None | |
65 | ## self.weight = None | |
66 | class Person: | |
67 | __xmlflattensequence__ = {'asequence': ('the_earth_is_flat',)} | |
68 | ||
69 | class XmlMarshallerTestFunctions(unittest.TestCase): | |
70 | ||
71 | def setUp(self): | |
72 | '''common setup code goes here.''' | |
73 | pass | |
74 | ||
75 | def testInt(self): | |
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) | |
82 | ||
83 | def testDict(self): | |
84 | xml = xmlmarshaller.marshal({'one': 1, | |
85 | 'two': 2, | |
86 | 'three': 3}) | |
87 | print "\n#########################################" | |
88 | print "# testString test case #" | |
89 | print "#########################################" | |
90 | print "marshalled dict object:\n" | |
91 | print xmlprettyprint(xml) | |
92 | ||
93 | def testBool(self): | |
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 | |
107 | ||
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) | |
115 | ||
116 | def testEmptyElement(self): | |
117 | person = Person() | |
118 | person.firstName = "Albert" | |
119 | person.__xmlattributes__ = ('firstName',) | |
120 | xml = xmlmarshaller.marshal(person, 'person') | |
121 | print "\n#########################################" | |
122 | print "# testEmptyElement test case #" | |
123 | print "#########################################" | |
124 | print xml | |
125 | assert (xml == """<person objtype="__main__.Person" firstName="Albert"/>""") | |
126 | ||
127 | def testXMLFlattenSequence(self): | |
128 | person = Person() | |
129 | person.asequence = ('one', 'two') | |
130 | xml = xmlmarshaller.marshal(person, 'person') | |
131 | print "\n#########################################" | |
132 | print "# testXMLFlattenSequence test case #" | |
133 | print "#########################################" | |
134 | print xml | |
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) | |
139 | ||
140 | def testInstance(self): | |
141 | print "\n#########################################" | |
142 | print "# testInstance test case #" | |
143 | print "#########################################" | |
144 | class Foo: | |
145 | def __init__(self): | |
146 | self.alist = [1,2] | |
147 | self.astring = 'f00' | |
148 | f = Foo() | |
149 | xml = xmlmarshaller.marshal(f, 'foo') | |
150 | print xml | |
151 | ||
152 | def testPerson(self): | |
153 | person = Person() | |
154 | person.firstName = "Albert" | |
155 | person.lastName = "Camus" | |
156 | person.addressLine1 = "23 Absurd St." | |
157 | person.city = "Ennui" | |
158 | person.state = "MO" | |
159 | person.zip = "54321" | |
160 | person._phoneNumber = "808-303-2323" | |
161 | person.favoriteWords = ['angst', 'ennui', 'existence'] | |
162 | person.weight = 150 | |
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" | |
170 | print xml | |
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) | |
179 | ||
180 | ||
181 | ||
182 | if __name__ == "__main__": | |
183 | unittest.main() |