/
XMLegant_Tests.py
291 lines (202 loc) · 8.94 KB
/
XMLegant_Tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from XMLegant import XMLegant
class XMLegant_Tests(XMLegant):
def test_simple(self):
x = XMLegant();
x.a.b.c = 'd';
assert x.toXML(False) == '<a><b><c>d</c></b></a>', x.toXML(False)
x.a.b.c = 'e'
assert x.toXML(False) == '<a><b><c>e</c></b></a>', x.toXML(False)
x.a.b = 'c'
assert x.toXML(False) == '<a><b>c</b></a>', x.toXML(False)
x.a = 'b'
assert x.toXML(False) == '<a>b</a>', x.toXML(False)
x.a.b = 'c'
assert x.toXML(False) == '<a><b>c</b></a>', x.toXML(False)
def test_multipleChildren(self):
x = XMLegant()
x.a.b = 'c'
assert x.toXML(False) == '<a><b>c</b></a>', x.toXML(False)
x.a.b = 'e'
assert x.toXML(False) == '<a><b>e</b></a>', x.toXML(False)
x.a.b().c = 'e'
assert x.toXML(False) == '<a><b>e</b><b><c>e</c></b></a>', x.toXML(False)
x.a.b('f')
assert x.toXML(False) == '<a><b>e</b><b><c>e</c></b><b>f</b></a>', x.toXML(False)
x.a.b = 'g'
assert x.toXML(False) == '<a><b>e</b><b><c>e</c></b><b>g</b></a>', x.toXML(False)
x.a.deleteChildren()
assert x.toXML(False) == '<a />', x.toXML(False)
x.a.b().c = 'd';
assert x.toXML(False) == '<a><b><c>d</c></b></a>', x.toXML(False)
x.a.b().c = 'e';
assert x.toXML(False) == '<a><b><c>d</c></b><b><c>e</c></b></a>', x.toXML(False)
x.a.deleteChildren();
assert x.toXML(False) == '<a />', x.toXML(False)
x.a.b.c = 'd';
assert x.toXML(False) == '<a><b><c>d</c></b></a>', x.toXML(False)
x.a.b().c = 'e';
assert x.toXML(False) == '<a><b><c>d</c></b><b><c>e</c></b></a>', x.toXML(False)
def test_attributes(self):
x = XMLegant();
x.a = {'b':'c', 'd':'e'};
assert x.toXML(False) == '<a b="c" d="e" />', x.toXML(False)
x.a['f'] = 'g';
assert x.toXML(False) == '<a b="c" d="e" f="g" />', x.toXML(False)
assert x.a['f'] == 'g', x.a['f']
def test_vars(self):
x = XMLegant();
x.a.b.c = 'z';
c_obj = x.a.b.c;
assert x.toXML(False) == '<a><b><c>z</c></b></a>', x.toXML(False)
# Note: This adds an additional 'c' child to c_obj's parent
c_obj('d');
assert x.toXML(False) == '<a><b><c>z</c><c>d</c></b></a>', x.toXML(False)
c_obj.e = 'f';
assert x.toXML(False) == '<a><b><c><e>f</e></c><c>d</c></b></a>', x.toXML(False)
"""
Note: The following may not work as expected. The value is:
<a><b><c>x</c><c>y</c><c>z</c></b></a>
instead of:
<a><b><c></c><c>x</c><c>y</c><c>z</c></b></a>
(missing the fisrt <c></c> element)
because () will overwrite an initial element if is it empty. This is
done so:
x = new XMLegant();
x.a.b('c');
works correctly. Without this adjustment, the above would equal:
<a><b><b><b>c</b></a>
...because the call to 'x.a.b' would create one element
and the call to '()' would create an additional one.
"""
x = XMLegant()
c_obj = x.a.b.c
c_obj('x')
c_obj('y')
c_obj('z')
assert x.toXML(False) == '<a><b><c>x</c><c>y</c><c>z</c></b></a>', x.toXML(False)
"""
If you need to get around this, simply initialize the element.
"""
x = XMLegant()
x.a.b.c = ''
c_obj = x.a.b.c
c_obj('x')
c_obj('y')
c_obj('z')
assert x.toXML(False) == '<a><b><c /><c>x</c><c>y</c><c>z</c></b></a>', x.toXML(False)
def test_childObjs(self):
x1 = XMLegant()
x1.d.e = 'f'
assert x1.toXML(False) == '<d><e>f</e></d>', x1.toXML(False)
x2 = XMLegant()
x2.a.b
assert x2.toXML(False) == '<a><b /></a>', x2.toXML(False)
x2.a.b = x1
assert x2.toXML(False) == '<a><b><d><e>f</e></d></b></a>', x2.toXML(False)
x2.a.b(x1)
assert x2.toXML(False) == '<a><b><d><e>f</e></d></b><b><d><e>f</e></d></b></a>', x2.toXML(False)
def test_childObjRef(self):
x = XMLegant()
x.a.b.c = 'd';
assert x.toXML(False) == '<a><b><c>d</c></b></a>', x.toXML(False)
x.a.b().e = 'f';
assert x.toXML(False) == '<a><b><c>d</c></b><b><e>f</e></b></a>', x.toXML(False)
x.a.b().g = 'h';
assert x.toXML(False) == '<a><b><c>d</c></b><b><e>f</e></b><b><g>h</g></b></a>', x.toXML(False)
x.a.b[1] = 'i';
assert x.toXML(False) == '<a><b><c>d</c></b><b>i</b><b><g>h</g></b></a>', x.toXML(False)
def test_addingBulkChildren(self):
"""
Suppose you want to easily create the structure
<books>
<book>
<title>Title 1</title>
<author>Author 1</author>
<isbn>isbn1</isbn>
</book>
<book>
<title>Title 2</title>
<author>Author 2</author>
<isbn>isbn2</isbn>
</book>
<book>
<title>Title 2</title>
<author>Author 2</author>
<isbn>isbn2</isbn>
</book>
...
</books>
"""
book_data = XMLegant()
x = XMLegant()
for i in xrange(5):
book_data.title = "Title "+str(i)
book_data.author = "Author "+str(i)
book_data.isbn = "isbn "+str(i)
x.books.book(book_data)
assert x.toXML(False) == '<books><book><title>Title 0</title><author>Author 0</author><isbn>isbn 0</isbn></book><book><title>Title 1</title><author>Author 1</author><isbn>isbn 1</isbn></book><book><title>Title 2</title><author>Author 2</author><isbn>isbn 2</isbn></book><book><title>Title 3</title><author>Author 3</author><isbn>isbn 3</isbn></book><book><title>Title 4</title><author>Author 4</author><isbn>isbn 4</isbn></book></books>', x.toXML(False)
def test_multRootChildren(self):
# XXX ElementTree doesn't appear to support multiple root elements
# (probably because it's invalid XML)
pass
def test_functional(self):
x = XMLegant()
x.media('type', 'documentary') \
.media \
.movie.title('PHP2: More Parser Stories') \
.plot('This is all about the people who make it work.') \
.characters() \
.character() \
.name('Mr. Parser') \
.actor('John Doe') \
.getParent() \
.character() \
.name('Mr. Parser2') \
.actor('John Doe2') \
.getParent() \
.getParent() \
.rating(5)
"""
<media type="documentary">
<movie>
<title>PHP2: More Parser Stories</title>
<plot>This is all about the people who make it work.</plot>
<characters>
<character>
<name>Mr. Parser</name>
<actor>John Doe</actor>
</character>
<character>
<name>Mr. Parser2</name>
<actor>John Doe2</actor>
</character>
</characters>
<rating>5</rating>
</movie>
</media>
"""
def test_underscores(self):
x = XMLegant()
x.foo_bar
assert x.toXML(False) == '<foo:bar />', x.toXML(False)
x.foo_bar['a_b'] = 'c'
assert x.toXML(False) == '<foo:bar a:b="c" />', x.toXML(False)
x.SetReplaceUnderscores(False)
assert x.toXML(False) == '<foo_bar a_b="c" />', x.toXML(False)
def test_bookFunc(self):
x = XMLegant()
for i in xrange(5):
x.books.book().title("Title %d " % i) \
.author("Author %d " % i) \
.isbn("isbn %d " % i)
#print x.toXML()
def test_encoding(self):
x = XMLegant()
x.a
assert x.toXML() == '<a />', x.toXML()
x['encoding'] = 'UTF-8';
assert x.toXML() == "<?xml version='1.0' encoding='UTF-8'?>\n<a />", x.toXML()
xmlTests = XMLegant_Tests()
for method in dir(xmlTests):
if method.startswith('test_'):
getattr(xmlTests, method)()