| Home | Trees | Indices | Help |
|
|---|
|
|
1 # -*- coding: utf-8 -*-
2
3 """
4 Tests specific to the lxml.objectify API
5 """
6
7
8 import unittest, operator, sys, os.path
9
10 this_dir = os.path.dirname(__file__)
11 if this_dir not in sys.path:
12 sys.path.insert(0, this_dir) # needed for Py3
13
14 from common_imports import etree, HelperTestCase, fileInTestDir
15 from common_imports import SillyFileLike, canonicalize, doctest, make_doctest
16 from common_imports import _bytes, _str, StringIO, BytesIO
17
18 from lxml import objectify
19
20 PYTYPE_NAMESPACE = "http://codespeak.net/lxml/objectify/pytype"
21 XML_SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"
22 XML_SCHEMA_INSTANCE_NS = "http://www.w3.org/2001/XMLSchema-instance"
23 XML_SCHEMA_INSTANCE_TYPE_ATTR = "{%s}type" % XML_SCHEMA_INSTANCE_NS
24 XML_SCHEMA_NIL_ATTR = "{%s}nil" % XML_SCHEMA_INSTANCE_NS
25 TREE_PYTYPE = "TREE"
26 DEFAULT_NSMAP = { "py" : PYTYPE_NAMESPACE,
27 "xsi" : XML_SCHEMA_INSTANCE_NS,
28 "xsd" : XML_SCHEMA_NS}
29
30 objectclass2xsitype = {
31 # objectify built-in
32 objectify.IntElement: ("int", "short", "byte", "unsignedShort",
33 "unsignedByte", "integer", "nonPositiveInteger",
34 "negativeInteger", "long", "nonNegativeInteger",
35 "unsignedLong", "unsignedInt", "positiveInteger",),
36 objectify.FloatElement: ("float", "double"),
37 objectify.BoolElement: ("boolean",),
38 objectify.StringElement: ("string", "normalizedString", "token", "language",
39 "Name", "NCName", "ID", "IDREF", "ENTITY",
40 "NMTOKEN", ),
41 # None: xsi:nil="true"
42 }
43
44 xsitype2objclass = dict([ (v, k) for k in objectclass2xsitype
45 for v in objectclass2xsitype[k] ])
46
47 objectclass2pytype = {
48 # objectify built-in
49 objectify.IntElement: "int",
50 objectify.FloatElement: "float",
51 objectify.BoolElement: "bool",
52 objectify.StringElement: "str",
53 # None: xsi:nil="true"
54 }
55
56 pytype2objclass = dict([ (objectclass2pytype[k], k)
57 for k in objectclass2pytype])
58
59 xml_str = '''\
60 <obj:root xmlns:obj="objectified" xmlns:other="otherNS">
61 <obj:c1 a1="A1" a2="A2" other:a3="A3">
62 <obj:c2>0</obj:c2>
63 <obj:c2>1</obj:c2>
64 <obj:c2>2</obj:c2>
65 <other:c2>3</other:c2>
66 <c2>4</c2>
67 </obj:c1>
68 </obj:root>'''
69
71 """Test cases for lxml.objectify
72 """
73 etree = etree
74
77
79 super(ObjectifyTestCase, self).setUp()
80 self.parser = self.etree.XMLParser(remove_blank_text=True)
81 self.lookup = etree.ElementNamespaceClassLookup(
82 objectify.ObjectifyElementClassLookup() )
83 self.parser.set_element_class_lookup(self.lookup)
84
85 self.Element = self.parser.makeelement
86
87 ns = self.lookup.get_namespace("otherNS")
88 ns[None] = self.etree.ElementBase
89
90 self._orig_types = objectify.getRegisteredTypes()
91
93 self.lookup.get_namespace("otherNS").clear()
94 objectify.set_pytype_attribute_tag()
95 del self.lookup
96 del self.parser
97
98 for pytype in objectify.getRegisteredTypes():
99 pytype.unregister()
100 for pytype in self._orig_types:
101 pytype.register()
102 del self._orig_types
103
104 super(ObjectifyTestCase, self).tearDown()
105
106
110
112 nsmap = {}
113 elt = objectify.Element("test", nsmap=nsmap)
114 self.assertEquals(list(elt.nsmap.values()), [PYTYPE_NAMESPACE])
115
117 nsmap = {"mypy": PYTYPE_NAMESPACE,
118 "myxsi": XML_SCHEMA_INSTANCE_NS,
119 "myxsd": XML_SCHEMA_NS}
120 elt = objectify.Element("test", nsmap=nsmap)
121 self.assertEquals(elt.nsmap, nsmap)
122
124 nsmap = {"my": "someNS",
125 "myother": "someOtherNS",
126 "myxsd": XML_SCHEMA_NS}
127 elt = objectify.Element("test", nsmap=nsmap)
128 self.assert_(PYTYPE_NAMESPACE in elt.nsmap.values())
129 for prefix, ns in nsmap.items():
130 self.assert_(prefix in elt.nsmap)
131 self.assertEquals(nsmap[prefix], elt.nsmap[prefix])
132
134 root = objectify.Element("root")
135 root.sub = objectify.Element("test")
136 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
137
139 root = objectify.Element("root")
140 nsmap = {}
141 root.sub = objectify.Element("test", nsmap=nsmap)
142 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
143
145 root = objectify.Element("root")
146 nsmap = {"mypy": PYTYPE_NAMESPACE,
147 "myxsi": XML_SCHEMA_INSTANCE_NS,
148 "myxsd": XML_SCHEMA_NS}
149 root.sub = objectify.Element("test", nsmap=nsmap)
150 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
151
153 root = objectify.Element("root")
154 nsmap = {"my": "someNS",
155 "myother": "someOtherNS",
156 "myxsd": XML_SCHEMA_NS,}
157 root.sub = objectify.Element("test", nsmap=nsmap)
158 expected = nsmap.copy()
159 del expected["myxsd"]
160 expected.update(DEFAULT_NSMAP)
161 self.assertEquals(root.sub.nsmap, expected)
162
166
168 nsmap = {}
169 value = objectify.DataElement("test this", nsmap=nsmap)
170 self.assertEquals(list(value.nsmap.values()), [PYTYPE_NAMESPACE])
171
173 nsmap = {"mypy": PYTYPE_NAMESPACE,
174 "myxsi": XML_SCHEMA_INSTANCE_NS,
175 "myxsd": XML_SCHEMA_NS}
176 value = objectify.DataElement("test this", nsmap=nsmap)
177 self.assertEquals(value.nsmap, nsmap)
178
180 nsmap = {"my": "someNS",
181 "myother": "someOtherNS",
182 "myxsd": XML_SCHEMA_NS,}
183 value = objectify.DataElement("test", nsmap=nsmap)
184 self.assert_(PYTYPE_NAMESPACE in value.nsmap.values())
185 for prefix, ns in nsmap.items():
186 self.assert_(prefix in value.nsmap)
187 self.assertEquals(nsmap[prefix], value.nsmap[prefix])
188
190 root = objectify.Element("root")
191 root.value = objectify.DataElement("test this")
192 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
193
195 root = objectify.Element("root")
196 nsmap = {}
197 root.value = objectify.DataElement("test this", nsmap=nsmap)
198 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
199
201 root = objectify.Element("root")
202 nsmap = {"mypy": PYTYPE_NAMESPACE,
203 "myxsi": XML_SCHEMA_INSTANCE_NS,
204 "myxsd": XML_SCHEMA_NS}
205 root.value = objectify.DataElement("test this", nsmap=nsmap)
206 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
207
209 root = objectify.Element("root")
210 nsmap = {"my": "someNS",
211 "myother": "someOtherNS",
212 "myxsd": XML_SCHEMA_NS}
213 root.value = objectify.DataElement("test", nsmap=nsmap)
214 expected = nsmap.copy()
215 del expected["myxsd"]
216 expected.update(DEFAULT_NSMAP)
217 self.assertEquals(root.value.nsmap, expected)
218
220 # keyword arguments override attrib entries
221 value = objectify.DataElement(23, _pytype="str", _xsi="foobar",
222 attrib={"gnu": "muh", "cat": "meeow",
223 "dog": "wuff"},
224 bird="tchilp", dog="grrr")
225 self.assertEquals(value.get("gnu"), "muh")
226 self.assertEquals(value.get("cat"), "meeow")
227 self.assertEquals(value.get("dog"), "grrr")
228 self.assertEquals(value.get("bird"), "tchilp")
229
231 # Check that DataElement preserves all attributes ObjectifiedDataElement
232 # arguments
233 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
234 attrib={"gnu": "muh", "cat": "meeow",
235 "dog": "wuff"},
236 bird="tchilp", dog="grrr")
237 value = objectify.DataElement(arg)
238 self.assert_(isinstance(value, objectify.StringElement))
239 for attr in arg.attrib:
240 self.assertEquals(value.get(attr), arg.get(attr))
241
243 # Check that _pytype arg overrides original py:pytype of
244 # ObjectifiedDataElement
245 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
246 attrib={"gnu": "muh", "cat": "meeow",
247 "dog": "wuff"},
248 bird="tchilp", dog="grrr")
249 value = objectify.DataElement(arg, _pytype="NoneType")
250 self.assert_(isinstance(value, objectify.NoneElement))
251 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
252 self.assertEquals(value.text, None)
253 self.assertEquals(value.pyval, None)
254 for attr in arg.attrib:
255 #if not attr == objectify.PYTYPE_ATTRIBUTE:
256 self.assertEquals(value.get(attr), arg.get(attr))
257
259 # Check that _pytype arg overrides original py:pytype of
260 # ObjectifiedDataElement
261 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
262 attrib={"gnu": "muh", "cat": "meeow",
263 "dog": "wuff"},
264 bird="tchilp", dog="grrr")
265 value = objectify.DataElement(arg, _pytype="int")
266 self.assert_(isinstance(value, objectify.IntElement))
267 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
268 for attr in arg.attrib:
269 if not attr == objectify.PYTYPE_ATTRIBUTE:
270 self.assertEquals(value.get(attr), arg.get(attr))
271
273 # Check that _xsi arg overrides original xsi:type of given
274 # ObjectifiedDataElement
275 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
276 attrib={"gnu": "muh", "cat": "meeow",
277 "dog": "wuff"},
278 bird="tchilp", dog="grrr")
279 value = objectify.DataElement(arg, _xsi="xsd:int")
280 self.assert_(isinstance(value, objectify.IntElement))
281 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
282 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
283 for attr in arg.attrib:
284 if not attr in [objectify.PYTYPE_ATTRIBUTE,
285 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
286 self.assertEquals(value.get(attr), arg.get(attr))
287
289 # Check that _pytype and _xsi args override original py:pytype and
290 # xsi:type attributes of given ObjectifiedDataElement
291 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
292 attrib={"gnu": "muh", "cat": "meeow",
293 "dog": "wuff"},
294 bird="tchilp", dog="grrr")
295 value = objectify.DataElement(arg, _pytype="int", _xsi="xsd:int")
296 self.assert_(isinstance(value, objectify.IntElement))
297 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
298 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
299 for attr in arg.attrib:
300 if not attr in [objectify.PYTYPE_ATTRIBUTE,
301 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
302 self.assertEquals(value.get(attr), arg.get(attr))
303
307
311
313 arg = objectify.DataElement(3.1415)
314 self.assertRaises(ValueError, objectify.DataElement, arg,
315 _pytype="int")
316
318 arg = objectify.DataElement(3.1415)
319 self.assertRaises(ValueError, objectify.DataElement, arg,
320 _xsi="xsd:int")
321
323 arg = objectify.Element('arg')
324 value = objectify.DataElement(arg)
325 self.assert_(isinstance(value, objectify.ObjectifiedElement))
326 for attr in arg.attrib:
327 self.assertEquals(value.get(attr), arg.get(attr))
328
332
336
340
342 root = self.XML(xml_str)
343 self.assertEquals(1, root.countchildren())
344 self.assertEquals(5, root.c1.countchildren())
345
347 root = self.XML(xml_str)
348 self.assertEquals("0", getattr(root.c1, "{objectified}c2").text)
349 self.assertEquals("3", getattr(root.c1, "{otherNS}c2").text)
350
352 root = self.XML(xml_str)
353 self.assertRaises(AttributeError, getattr, root.c1, "NOT_THERE")
354 self.assertRaises(AttributeError, getattr, root.c1, "{unknownNS}c2")
355
357 root = self.XML(xml_str)
358 self.assertEquals("4", getattr(root.c1, "{}c2").text)
359 self.assertEquals("0", getattr(root.c1, "c2").text)
360
362 for val in [
363 2, 2**32, 1.2, "Won't get fooled again",
364 _str("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1'), True,
365 False, None]:
366 root = self.Element('root')
367 attrname = 'val'
368 setattr(root, attrname, val)
369 result = getattr(root, attrname)
370 self.assertEquals(val, result)
371 self.assertEquals(type(val), type(result.pyval))
372
374 root = self.Element('root')
375 attrname = 'val'
376 val = _bytes("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1')
377 self.assertRaises(ValueError, setattr, root, attrname, val)
378 self.assertRaises(AttributeError, getattr, root, attrname)
379
381 root = self.XML(xml_str)
382 self.assertEquals(1, len(root.c1))
383 root.addattr("c1", "test")
384 self.assertEquals(2, len(root.c1))
385 self.assertEquals("test", root.c1[1].text)
386
388 root = self.XML(xml_str)
389 self.assertEquals(1, len(root.c1))
390
391 new_el = self.Element("test", myattr="5")
392 root.addattr("c1", new_el)
393 self.assertEquals(2, len(root.c1))
394 self.assertEquals(None, root.c1[0].get("myattr"))
395 self.assertEquals("5", root.c1[1].get("myattr"))
396
398 root = self.XML(xml_str)
399 self.assertEquals(1, len(root.c1))
400
401 new_el = self.Element("test")
402 self.etree.SubElement(new_el, "a", myattr="A")
403 self.etree.SubElement(new_el, "a", myattr="B")
404
405 root.addattr("c1", list(new_el.a))
406 self.assertEquals(3, len(root.c1))
407 self.assertEquals(None, root.c1[0].get("myattr"))
408 self.assertEquals("A", root.c1[1].get("myattr"))
409 self.assertEquals("B", root.c1[2].get("myattr"))
410
412 root = self.XML(xml_str)
413 self.assertEquals(3, len(root.c1.c2))
414 root.c1.addattr("c2", 3)
415 self.assertEquals(4, len(root.c1.c2))
416 self.assertEquals("3", root.c1.c2[3].text)
417
419 root = self.XML(xml_str)
420 self.assertEquals("0", root.c1.c2[0].text)
421 self.assertEquals("1", root.c1.c2[1].text)
422 self.assertEquals("2", root.c1.c2[2].text)
423 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
424
426 root = self.XML(xml_str)
427 self.assertEquals("0", root.c1.c2[0].text)
428 self.assertEquals("0", root.c1.c2[-3].text)
429 self.assertEquals("1", root.c1.c2[-2].text)
430 self.assertEquals("2", root.c1.c2[-1].text)
431 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
432
434 root = self.XML(xml_str)
435 self.assertEquals(1, len(root))
436 self.assertEquals(1, len(root.c1))
437 self.assertEquals(3, len(root.c1.c2))
438
440 root = self.XML(xml_str)
441 self.assertEquals([root],
442 list(iter(root)))
443 self.assertEquals([root.c1],
444 list(iter(root.c1)))
445 self.assertEquals([root.c1.c2[0], root.c1.c2[1], root.c1.c2[2]],
446 list(iter((root.c1.c2))))
447
449 root = self.XML(xml_str)
450 self.assert_(isinstance(root.c1.c2, objectify.ObjectifiedElement))
451 self.assertFalse(isinstance(getattr(root.c1, "{otherNS}c2"),
452 objectify.ObjectifiedElement))
453
455 root = self.XML(xml_str)
456 dir_c1 = dir(objectify.ObjectifiedElement) + ['c1']
457 dir_c1.sort()
458 dir_c2 = dir(objectify.ObjectifiedElement) + ['c2']
459 dir_c2.sort()
460
461 self.assertEquals(dir_c1, dir(root))
462 self.assertEquals(dir_c2, dir(root.c1))
463
465 root = self.XML(xml_str)
466 self.assertEquals({'c1' : root.c1}, vars(root))
467 self.assertEquals({'c2' : root.c1.c2}, vars(root.c1))
468
470 root = self.XML(xml_str)
471 self.assertRaises(TypeError, setattr, root.c1.c2, 'text', "test")
472 self.assertRaises(TypeError, setattr, root.c1.c2, 'pyval', "test")
473
474 # slicing
475
477 root = self.XML("<root><c>c1</c><c>c2</c></root>")
478 self.assertEquals(["c1", "c2"],
479 [ c.text for c in root.c[:] ])
480
482 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
483 test_list = ["c1", "c2", "c3", "c4"]
484
485 self.assertEquals(test_list,
486 [ c.text for c in root.c[:] ])
487 self.assertEquals(test_list[1:2],
488 [ c.text for c in root.c[1:2] ])
489 self.assertEquals(test_list[-3:-1],
490 [ c.text for c in root.c[-3:-1] ])
491 self.assertEquals(test_list[-3:3],
492 [ c.text for c in root.c[-3:3] ])
493 self.assertEquals(test_list[-3000:3],
494 [ c.text for c in root.c[-3000:3] ])
495 self.assertEquals(test_list[-3:3000],
496 [ c.text for c in root.c[-3:3000] ])
497
499 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
500 test_list = ["c1", "c2", "c3", "c4"]
501
502 self.assertEquals(test_list,
503 [ c.text for c in root.c[:] ])
504 self.assertEquals(test_list[2:1:-1],
505 [ c.text for c in root.c[2:1:-1] ])
506 self.assertEquals(test_list[-1:-3:-1],
507 [ c.text for c in root.c[-1:-3:-1] ])
508 self.assertEquals(test_list[2:-3:-1],
509 [ c.text for c in root.c[2:-3:-1] ])
510 self.assertEquals(test_list[2:-3000:-1],
511 [ c.text for c in root.c[2:-3000:-1] ])
512
513 # slice assignment
514
516 Element = self.Element
517 root = Element("root")
518 root.c = ["c1", "c2"]
519
520 c1 = root.c[0]
521 c2 = root.c[1]
522
523 self.assertEquals([c1,c2], list(root.c))
524 self.assertEquals(["c1", "c2"],
525 [ c.text for c in root.c ])
526
528 Element = self.Element
529 root = Element("root")
530 root.c = ["c1", "c2"]
531
532 c1 = root.c[0]
533 c2 = root.c[1]
534
535 self.assertEquals([c1,c2], list(root.c))
536 self.assertEquals(["c1", "c2"],
537 [ c.text for c in root.c ])
538
539 root2 = Element("root2")
540 root2.el = [ "test", "test" ]
541 self.assertEquals(["test", "test"],
542 [ el.text for el in root2.el ])
543
544 root.c = [ root2.el, root2.el ]
545 self.assertEquals(["test", "test"],
546 [ c.text for c in root.c ])
547 self.assertEquals(["test", "test"],
548 [ el.text for el in root2.el ])
549
550 root.c[:] = [ c1, c2, c2, c1 ]
551 self.assertEquals(["c1", "c2", "c2", "c1"],
552 [ c.text for c in root.c ])
553
555 Element = self.Element
556 root = Element("root")
557 l = ["c1", "c2", "c3", "c4"]
558 root.c = l
559
560 self.assertEquals(["c1", "c2", "c3", "c4"],
561 [ c.text for c in root.c ])
562 self.assertEquals(l,
563 [ c.text for c in root.c ])
564
565 new_slice = ["cA", "cB"]
566 l[1:2] = new_slice
567 root.c[1:2] = new_slice
568
569 self.assertEquals(["c1", "cA", "cB", "c3", "c4"], l)
570 self.assertEquals(["c1", "cA", "cB", "c3", "c4"],
571 [ c.text for c in root.c ])
572 self.assertEquals(l,
573 [ c.text for c in root.c ])
574
576 Element = self.Element
577 root = Element("root")
578 l = ["c1", "c2", "c3", "c4"]
579 root.c = l
580
581 self.assertEquals(["c1", "c2", "c3", "c4"],
582 [ c.text for c in root.c ])
583 self.assertEquals(l,
584 [ c.text for c in root.c ])
585
586 new_slice = ["cA", "cB"]
587 l[1:1] = new_slice
588 root.c[1:1] = new_slice
589
590 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"], l)
591 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"],
592 [ c.text for c in root.c ])
593 self.assertEquals(l,
594 [ c.text for c in root.c ])
595
597 Element = self.Element
598 root = Element("root")
599 l = ["c1", "c2", "c3", "c4"]
600 root.c = l
601
602 self.assertEquals(["c1", "c2", "c3", "c4"],
603 [ c.text for c in root.c ])
604 self.assertEquals(l,
605 [ c.text for c in root.c ])
606
607 new_slice = ["cA", "cB"]
608 l[-2:-2] = new_slice
609 root.c[-2:-2] = new_slice
610
611 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"], l)
612 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"],
613 [ c.text for c in root.c ])
614 self.assertEquals(l,
615 [ c.text for c in root.c ])
616
618 Element = self.Element
619 root = Element("root")
620
621 root.c = []
622 self.assertRaises(
623 AttributeError, getattr, root, 'c')
624
626 Element = self.Element
627 root = Element("root")
628 l = ["c1", "c2", "c3", "c4"]
629 root.c = l
630
631 self.assertEquals(["c1", "c2", "c3", "c4"],
632 [ c.text for c in root.c ])
633 self.assertEquals(l,
634 [ c.text for c in root.c ])
635
636 new_slice = ["cA", "cB", "cC"]
637 self.assertRaises(
638 ValueError, operator.setitem,
639 l, slice(1,2,-1), new_slice)
640 self.assertRaises(
641 ValueError, operator.setitem,
642 root.c, slice(1,2,-1), new_slice)
643
645 Element = self.Element
646 root = Element("root")
647 l = ["c1", "c2", "c3", "c4"]
648 root.c = l
649
650 self.assertEquals(["c1", "c2", "c3", "c4"],
651 [ c.text for c in root.c ])
652 self.assertEquals(l,
653 [ c.text for c in root.c ])
654
655 new_slice = ["cA", "cB"]
656 l[-1:1:-1] = new_slice
657 root.c[-1:1:-1] = new_slice
658
659 self.assertEquals(["c1", "c2", "cB", "cA"], l)
660 self.assertEquals(["c1", "c2", "cB", "cA"],
661 [ c.text for c in root.c ])
662 self.assertEquals(l,
663 [ c.text for c in root.c ])
664
666 Element = self.Element
667 root = Element("root")
668 l = ["c1", "c2", "c3", "c4"]
669 root.c = l
670
671 self.assertEquals(["c1", "c2", "c3", "c4"],
672 [ c.text for c in root.c ])
673 self.assertEquals(l,
674 [ c.text for c in root.c ])
675
676 new_slice = ["cA", "cB"]
677 l[-1:-4:-2] = new_slice
678 root.c[-1:-4:-2] = new_slice
679
680 self.assertEquals(["c1", "cB", "c3", "cA"], l)
681 self.assertEquals(["c1", "cB", "c3", "cA"],
682 [ c.text for c in root.c ])
683 self.assertEquals(l,
684 [ c.text for c in root.c ])
685
686 # other stuff
687
689 # make sure strings are not handled as sequences
690 Element = self.Element
691 root = Element("root")
692 root.c = "TEST"
693 self.assertEquals(["TEST"],
694 [ c.text for c in root.c ])
695
697 # make sure strings are set as children
698 Element = self.Element
699 root = Element("root")
700 root["c"] = "TEST"
701 self.assertEquals(["TEST"],
702 [ c.text for c in root.c ])
703
705 # make sure 'text' etc. are set as children
706 Element = self.Element
707 root = Element("root")
708
709 root["text"] = "TEST"
710 self.assertEquals(["TEST"],
711 [ c.text for c in root["text"] ])
712
713 root["tail"] = "TEST"
714 self.assertEquals(["TEST"],
715 [ c.text for c in root["tail"] ])
716
717 root["pyval"] = "TEST"
718 self.assertEquals(["TEST"],
719 [ c.text for c in root["pyval"] ])
720
721 root["tag"] = "TEST"
722 self.assertEquals(["TEST"],
723 [ c.text for c in root["tag"] ])
724
726 XML = self.XML
727 root = XML('<a><b><c/></b><b/><c><b/></c></a>')
728 self.assertEquals(1, len(root.findall("c")))
729 self.assertEquals(2, len(root.findall(".//c")))
730 self.assertEquals(3, len(root.findall(".//b")))
731 self.assert_(root.findall(".//b")[1] is root.getchildren()[1])
732
734 XML = self.XML
735 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
736 self.assertEquals(2, len(root.findall(".//{X}b")))
737 self.assertEquals(3, len(root.findall(".//b")))
738 self.assertEquals(2, len(root.findall("b")))
739
741 root = self.Element('root')
742 root.a = 5
743 root.b = 6
744 self.assert_(isinstance(root, objectify.ObjectifiedElement))
745 self.assert_(isinstance(root.a, objectify.IntElement))
746 self.assert_(isinstance(root.b, objectify.IntElement))
747
749 Element = self.Element
750 SubElement = self.etree.SubElement
751
752 nil_attr = XML_SCHEMA_NIL_ATTR
753 root = Element("{objectified}root")
754 SubElement(root, "{objectified}none")
755 SubElement(root, "{objectified}none", {nil_attr : "true"})
756 self.assertFalse(isinstance(root.none, objectify.NoneElement))
757 self.assertFalse(isinstance(root.none[0], objectify.NoneElement))
758 self.assert_(isinstance(root.none[1], objectify.NoneElement))
759 self.assertEquals(hash(root.none[1]), hash(None))
760 self.assertEquals(root.none[1], None)
761 self.assertFalse(root.none[1])
762
764 value = objectify.DataElement(None)
765 self.assert_(isinstance(value, objectify.NoneElement))
766 self.assertEquals(value, None)
767 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
768
770 Element = self.Element
771 SubElement = self.etree.SubElement
772 root = Element("{objectified}root")
773 root.bool = True
774 self.assertEquals(root.bool, True)
775 self.assertEquals(root.bool + root.bool, True + True)
776 self.assertEquals(True + root.bool, True + root.bool)
777 self.assertEquals(root.bool * root.bool, True * True)
778 self.assertEquals(int(root.bool), int(True))
779 self.assertEquals(hash(root.bool), hash(True))
780 self.assertEquals(complex(root.bool), complex(True))
781 self.assert_(isinstance(root.bool, objectify.BoolElement))
782
783 root.bool = False
784 self.assertEquals(root.bool, False)
785 self.assertEquals(root.bool + root.bool, False + False)
786 self.assertEquals(False + root.bool, False + root.bool)
787 self.assertEquals(root.bool * root.bool, False * False)
788 self.assertEquals(int(root.bool), int(False))
789 self.assertEquals(hash(root.bool), hash(False))
790 self.assertEquals(complex(root.bool), complex(False))
791 self.assert_(isinstance(root.bool, objectify.BoolElement))
792
794 value = objectify.DataElement(True)
795 self.assert_(isinstance(value, objectify.BoolElement))
796 self.assertEquals(value, True)
797
798 value = objectify.DataElement(False)
799 self.assert_(isinstance(value, objectify.BoolElement))
800 self.assertEquals(value, False)
801
803 Element = self.Element
804 SubElement = self.etree.SubElement
805 root = Element("{objectified}root")
806 root.s = "test"
807 self.assert_(isinstance(root.s, objectify.StringElement))
808
810 Element = self.Element
811 SubElement = self.etree.SubElement
812 root = Element("{objectified}root")
813 root.s = "3"
814 self.assert_(isinstance(root.s, objectify.StringElement))
815
817 Element = self.Element
818 SubElement = self.etree.SubElement
819 root = Element("{objectified}root")
820 root.s = "3.72"
821 self.assert_(isinstance(root.s, objectify.StringElement))
822
824 Element = self.Element
825 SubElement = self.etree.SubElement
826 root = Element("{objectified}root")
827 root.s = "test"
828
829 self.assertEquals("test" * 5, root.s * 5)
830 self.assertEquals(5 * "test", 5 * root.s)
831
832 self.assertRaises(TypeError, operator.mul, root.s, "honk")
833 self.assertRaises(TypeError, operator.mul, "honk", root.s)
834
836 Element = self.Element
837 SubElement = self.etree.SubElement
838 root = Element("{objectified}root")
839 root.s = "test"
840
841 s = "toast"
842 self.assertEquals("test" + s, root.s + s)
843 self.assertEquals(s + "test", s + root.s)
844
846 s = "%d %f %s %r"
847 el = objectify.DataElement(s)
848 values = (1, 7.0, "abcd", None)
849 self.assertEquals(s % values, el % values)
850
851 s = "%d"
852 el = objectify.DataElement(s)
853 val = 5
854 self.assertEquals(s % val, el % val)
855
856 s = "%d %s"
857 el = objectify.DataElement(s)
858 val = 5
859 self.assertRaises(TypeError, el.__mod__, val)
860
861 s = ""
862 el = objectify.DataElement(s)
863 val = 5
864 self.assertRaises(TypeError, el.__mod__, val)
865
870
875
880
885
887 s = "%d %f %s %r"
888 el = objectify.DataElement(s)
889 values = (objectify.DataElement(1),
890 objectify.DataElement(7.0),
891 objectify.DataElement("abcd"),
892 objectify.DataElement(None))
893 self.assertEquals(s % values, el % values)
894
896 value = objectify.DataElement("test")
897 self.assert_(isinstance(value, objectify.StringElement))
898 self.assertEquals(value, "test")
899
901 value = objectify.DataElement("3")
902 self.assert_(isinstance(value, objectify.StringElement))
903 self.assertEquals(value, "3")
904
906 value = objectify.DataElement("3.20")
907 self.assert_(isinstance(value, objectify.StringElement))
908 self.assertEquals(value, "3.20")
909
911 Element = self.Element
912 SubElement = self.etree.SubElement
913 root = Element("{objectified}root")
914 root.s = _str("test")
915 self.assert_(isinstance(root.s, objectify.StringElement))
916
918 Element = self.Element
919 SubElement = self.etree.SubElement
920 root = Element("{objectified}root")
921 root.s = _str("3")
922 self.assert_(isinstance(root.s, objectify.StringElement))
923
925 Element = self.Element
926 SubElement = self.etree.SubElement
927 root = Element("{objectified}root")
928 root.s = _str("3.72")
929 self.assert_(isinstance(root.s, objectify.StringElement))
930
932 Element = self.Element
933 SubElement = self.etree.SubElement
934 root = Element("{objectified}root")
935 root.s = _str("test")
936
937 self.assertEquals(_str("test") * 5, root.s * 5)
938 self.assertEquals(5 * _str("test"), 5 * root.s)
939
940 self.assertRaises(TypeError, operator.mul, root.s, _str("honk"))
941 self.assertRaises(TypeError, operator.mul, _str("honk"), root.s)
942
944 Element = self.Element
945 SubElement = self.etree.SubElement
946 root = Element("{objectified}root")
947 root.s = _str("test")
948
949 s = _str("toast")
950 self.assertEquals(_str("test") + s, root.s + s)
951 self.assertEquals(s + _str("test"), s + root.s)
952
954 value = objectify.DataElement(_str("test"))
955 self.assert_(isinstance(value, objectify.StringElement))
956 self.assertEquals(value, _str("test"))
957
959 value = objectify.DataElement("3")
960 self.assert_(isinstance(value, objectify.StringElement))
961 self.assertEquals(value, _str("3"))
962
964 value = objectify.DataElement(_str("3.20"))
965 self.assert_(isinstance(value, objectify.StringElement))
966 self.assertEquals(value, _str("3.20"))
967
969 Element = self.Element
970 SubElement = self.etree.SubElement
971 root = Element("{objectified}root")
972 root.none = 5
973 self.assert_(isinstance(root.none, objectify.IntElement))
974
976 value = objectify.DataElement(5)
977 self.assert_(isinstance(value, objectify.IntElement))
978 self.assertEquals(value, 5)
979
983
985 Element = self.Element
986 SubElement = self.etree.SubElement
987 root = Element("{objectified}root")
988 root.none = 5.5
989 self.assert_(isinstance(root.none, objectify.FloatElement))
990
992 value = objectify.DataElement(5.5)
993 self.assert_(isinstance(value, objectify.FloatElement))
994 self.assertEquals(value, 5.5)
995
999
1001 for xsi, objclass in xsitype2objclass.items():
1002 # 1 is a valid value for all ObjectifiedDataElement classes
1003 pyval = 1
1004 value = objectify.DataElement(pyval, _xsi=xsi)
1005 self.assert_(isinstance(value, objclass),
1006 "DataElement(%s, _xsi='%s') returns %s, expected %s"
1007 % (pyval, xsi, type(value), objclass))
1008
1010 for xsi, objclass in xsitype2objclass.items():
1011 # 1 is a valid value for all ObjectifiedDataElement classes
1012 pyval = 1
1013 value = objectify.DataElement(pyval, _xsi="xsd:%s" % xsi)
1014 self.assert_(isinstance(value, objclass),
1015 "DataElement(%s, _xsi='%s') returns %s, expected %s"
1016 % (pyval, xsi, type(value), objclass))
1017
1019 for xsi, objclass in xsitype2objclass.items():
1020 # 1 is a valid value for all ObjectifiedDataElement classes
1021 self.assertRaises(ValueError, objectify.DataElement, 1,
1022 _xsi="foo:%s" % xsi)
1023
1025 for pytype, objclass in pytype2objclass.items():
1026 # 1 is a valid value for all ObjectifiedDataElement classes
1027 pyval = 1
1028 value = objectify.DataElement(pyval, _pytype=pytype)
1029 self.assert_(isinstance(value, objclass),
1030 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1031 % (pyval, pytype, type(value), objclass))
1032
1034 pyval = 1
1035 pytype = "NoneType"
1036 objclass = objectify.NoneElement
1037 value = objectify.DataElement(pyval, _pytype=pytype)
1038 self.assert_(isinstance(value, objclass),
1039 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1040 % (pyval, pytype, type(value), objclass))
1041 self.assertEquals(value.text, None)
1042 self.assertEquals(value.pyval, None)
1043
1045 # pre-2.0 lxml called NoneElement "none"
1046 pyval = 1
1047 pytype = "none"
1048 objclass = objectify.NoneElement
1049 value = objectify.DataElement(pyval, _pytype=pytype)
1050 self.assert_(isinstance(value, objclass),
1051 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1052 % (pyval, pytype, type(value), objclass))
1053 self.assertEquals(value.text, None)
1054 self.assertEquals(value.pyval, None)
1055
1057 Element = self.Element
1058 SubElement = self.etree.SubElement
1059 class MyFloat(float):
1060 pass
1061 root = Element("{objectified}root")
1062 root.myfloat = MyFloat(5.5)
1063 self.assert_(isinstance(root.myfloat, objectify.FloatElement))
1064 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
1065
1069 value = objectify.DataElement(MyFloat(5.5))
1070 self.assert_(isinstance(value, objectify.FloatElement))
1071 self.assertEquals(value, 5.5)
1072 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None)
1073
1075 XML = self.XML
1076 root = XML('''\
1077 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1078 <b xsi:type="boolean">true</b>
1079 <b xsi:type="boolean">false</b>
1080 <b xsi:type="boolean">1</b>
1081 <b xsi:type="boolean">0</b>
1082
1083 <f xsi:type="float">5</f>
1084 <f xsi:type="double">5</f>
1085
1086 <s xsi:type="string">5</s>
1087 <s xsi:type="normalizedString">5</s>
1088 <s xsi:type="token">5</s>
1089 <s xsi:type="language">5</s>
1090 <s xsi:type="Name">5</s>
1091 <s xsi:type="NCName">5</s>
1092 <s xsi:type="ID">5</s>
1093 <s xsi:type="IDREF">5</s>
1094 <s xsi:type="ENTITY">5</s>
1095 <s xsi:type="NMTOKEN">5</s>
1096
1097 <l xsi:type="integer">5</l>
1098 <l xsi:type="nonPositiveInteger">5</l>
1099 <l xsi:type="negativeInteger">5</l>
1100 <l xsi:type="long">5</l>
1101 <l xsi:type="nonNegativeInteger">5</l>
1102 <l xsi:type="unsignedLong">5</l>
1103 <l xsi:type="unsignedInt">5</l>
1104 <l xsi:type="positiveInteger">5</l>
1105
1106 <i xsi:type="int">5</i>
1107 <i xsi:type="short">5</i>
1108 <i xsi:type="byte">5</i>
1109 <i xsi:type="unsignedShort">5</i>
1110 <i xsi:type="unsignedByte">5</i>
1111
1112 <n xsi:nil="true"/>
1113 </root>
1114 ''')
1115
1116 for b in root.b:
1117 self.assert_(isinstance(b, objectify.BoolElement))
1118 self.assertEquals(True, root.b[0])
1119 self.assertEquals(False, root.b[1])
1120 self.assertEquals(True, root.b[2])
1121 self.assertEquals(False, root.b[3])
1122
1123 for f in root.f:
1124 self.assert_(isinstance(f, objectify.FloatElement))
1125 self.assertEquals(5, f)
1126
1127 for s in root.s:
1128 self.assert_(isinstance(s, objectify.StringElement))
1129 self.assertEquals("5", s)
1130
1131 for i in root.i:
1132 self.assert_(isinstance(i, objectify.IntElement))
1133 self.assertEquals(5, i)
1134
1135 for l in root.l:
1136 self.assert_(isinstance(l, objectify.IntElement))
1137 self.assertEquals(5, i)
1138
1139 self.assert_(isinstance(root.n, objectify.NoneElement))
1140 self.assertEquals(None, root.n)
1141
1143 XML = self.XML
1144 root = XML('''\
1145 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1146 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1147 <b xsi:type="xsd:boolean">true</b>
1148 <b xsi:type="xsd:boolean">false</b>
1149 <b xsi:type="xsd:boolean">1</b>
1150 <b xsi:type="xsd:boolean">0</b>
1151
1152 <f xsi:type="xsd:float">5</f>
1153 <f xsi:type="xsd:double">5</f>
1154
1155 <s xsi:type="xsd:string">5</s>
1156 <s xsi:type="xsd:normalizedString">5</s>
1157 <s xsi:type="xsd:token">5</s>
1158 <s xsi:type="xsd:language">5</s>
1159 <s xsi:type="xsd:Name">5</s>
1160 <s xsi:type="xsd:NCName">5</s>
1161 <s xsi:type="xsd:ID">5</s>
1162 <s xsi:type="xsd:IDREF">5</s>
1163 <s xsi:type="xsd:ENTITY">5</s>
1164 <s xsi:type="xsd:NMTOKEN">5</s>
1165
1166 <l xsi:type="xsd:integer">5</l>
1167 <l xsi:type="xsd:nonPositiveInteger">5</l>
1168 <l xsi:type="xsd:negativeInteger">5</l>
1169 <l xsi:type="xsd:long">5</l>
1170 <l xsi:type="xsd:nonNegativeInteger">5</l>
1171 <l xsi:type="xsd:unsignedLong">5</l>
1172 <l xsi:type="xsd:unsignedInt">5</l>
1173 <l xsi:type="xsd:positiveInteger">5</l>
1174
1175 <i xsi:type="xsd:int">5</i>
1176 <i xsi:type="xsd:short">5</i>
1177 <i xsi:type="xsd:byte">5</i>
1178 <i xsi:type="xsd:unsignedShort">5</i>
1179 <i xsi:type="xsd:unsignedByte">5</i>
1180
1181 <n xsi:nil="true"/>
1182 </root>
1183 ''')
1184
1185 for b in root.b:
1186 self.assert_(isinstance(b, objectify.BoolElement))
1187 self.assertEquals(True, root.b[0])
1188 self.assertEquals(False, root.b[1])
1189 self.assertEquals(True, root.b[2])
1190 self.assertEquals(False, root.b[3])
1191
1192 for f in root.f:
1193 self.assert_(isinstance(f, objectify.FloatElement))
1194 self.assertEquals(5, f)
1195
1196 for s in root.s:
1197 self.assert_(isinstance(s, objectify.StringElement))
1198 self.assertEquals("5", s)
1199
1200 for i in root.i:
1201 self.assert_(isinstance(i, objectify.IntElement))
1202 self.assertEquals(5, i)
1203
1204 for l in root.l:
1205 self.assert_(isinstance(l, objectify.IntElement))
1206 self.assertEquals(5, l)
1207
1208 self.assert_(isinstance(root.n, objectify.NoneElement))
1209 self.assertEquals(None, root.n)
1210
1212 XML = self.XML
1213 root = XML(_bytes('<root><b>why</b><b>try</b></root>'))
1214 strs = [ str(s) for s in root.b ]
1215 self.assertEquals(["why", "try"],
1216 strs)
1217
1219 XML = self.XML
1220 root = XML(_bytes('<root><b>test</b><b>taste</b><b></b><b/></root>'))
1221 self.assertFalse(root.b[0] < root.b[1])
1222 self.assertFalse(root.b[0] <= root.b[1])
1223 self.assertFalse(root.b[0] == root.b[1])
1224
1225 self.assert_(root.b[0] != root.b[1])
1226 self.assert_(root.b[0] >= root.b[1])
1227 self.assert_(root.b[0] > root.b[1])
1228
1229 self.assertEquals(root.b[0], "test")
1230 self.assertEquals("test", root.b[0])
1231
1232 self.assertEquals("", root.b[2])
1233 self.assertEquals(root.b[2], "")
1234 self.assertEquals("", root.b[3])
1235 self.assertEquals(root.b[3], "")
1236 self.assertEquals(root.b[2], root.b[3])
1237
1238 root.b = "test"
1239 self.assert_(root.b)
1240 root.b = ""
1241 self.assertFalse(root.b)
1242 self.assertEquals(root.b, "")
1243 self.assertEquals("", root.b)
1244
1246 XML = self.XML
1247 root = XML(_bytes('<root><b>5</b><b>6</b></root>'))
1248 self.assert_(root.b[0] < root.b[1])
1249 self.assert_(root.b[0] <= root.b[1])
1250 self.assert_(root.b[0] != root.b[1])
1251
1252 self.assertFalse(root.b[0] == root.b[1])
1253 self.assertFalse(root.b[0] >= root.b[1])
1254 self.assertFalse(root.b[0] > root.b[1])
1255
1256 self.assertEquals(root.b[0], 5)
1257 self.assertEquals(5, root.b[0])
1258 self.assertNotEquals(root.b[0], "5")
1259
1260 root.b = 5
1261 self.assert_(root.b)
1262 root.b = 0
1263 self.assertFalse(root.b)
1264
1265 # float + long share the NumberElement implementation with int
1266
1268 XML = self.XML
1269 root = XML(_bytes('<root><b>false</b><b>true</b></root>'))
1270 self.assert_(root.b[0] < root.b[1])
1271 self.assert_(root.b[0] <= root.b[1])
1272 self.assert_(root.b[0] != root.b[1])
1273
1274 self.assertFalse(root.b[0] == root.b[1])
1275 self.assertFalse(root.b[0] >= root.b[1])
1276 self.assertFalse(root.b[0] > root.b[1])
1277
1278 self.assertFalse(root.b[0])
1279 self.assert_(root.b[1])
1280
1281 self.assertEquals(root.b[0], False)
1282 self.assertEquals(False, root.b[0])
1283 self.assert_(root.b[0] < 5)
1284 self.assert_(5 > root.b[0])
1285
1286 root.b = True
1287 self.assert_(root.b)
1288 root.b = False
1289 self.assertFalse(root.b)
1290
1292 XML = self.XML
1293 root = XML(_bytes("""
1294 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1295 <b xsi:nil="true"></b><b xsi:nil="true"/>
1296 </root>"""))
1297 self.assert_(root.b[0] == root.b[1])
1298 self.assertFalse(root.b[0])
1299 self.assertEquals(root.b[0], None)
1300 self.assertEquals(None, root.b[0])
1301
1302 # doesn't work in Py3:
1303
1304 #for comparison in ["abc", 5, 7.3, True, [], ()]:
1305 # none = root.b[1]
1306 # self.assert_(none < comparison, "%s (%s) should be < %s" %
1307 # (none, type(none), comparison) )
1308 # self.assert_(comparison > none, "%s should be > %s (%s)" %
1309 # (comparison, none, type(none)) )
1310
1312 el = objectify.DataElement(1, _xsi="string")
1313 self.assertEquals(
1314 el.get(XML_SCHEMA_INSTANCE_TYPE_ATTR),
1315 'xsd:string')
1316
1318 el = objectify.DataElement(1, _xsi="string",
1319 nsmap={'schema': XML_SCHEMA_NS})
1320 self.assertEquals(
1321 el.get(XML_SCHEMA_INSTANCE_TYPE_ATTR),
1322 'schema:string')
1323
1327
1329 XML = self.XML
1330 root = XML(_bytes('''\
1331 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1332 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1333 <b>5</b>
1334 <b>test</b>
1335 <c>1.1</c>
1336 <c>\uF8D2</c>
1337 <x>true</x>
1338 <n xsi:nil="true" />
1339 <n></n>
1340 <b xsi:type="double">5</b>
1341 <b xsi:type="float">5</b>
1342 <s xsi:type="string">23</s>
1343 <s py:pytype="str">42</s>
1344 <f py:pytype="float">300</f>
1345 <l py:pytype="long">2</l>
1346 <t py:pytype="TREE"></t>
1347 </a>
1348 '''))
1349 objectify.annotate(root)
1350
1351 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1352 for c in root.iterchildren() ]
1353 self.assertEquals("int", child_types[ 0])
1354 self.assertEquals("str", child_types[ 1])
1355 self.assertEquals("float", child_types[ 2])
1356 self.assertEquals("str", child_types[ 3])
1357 self.assertEquals("bool", child_types[ 4])
1358 self.assertEquals("NoneType", child_types[ 5])
1359 self.assertEquals(None, child_types[ 6])
1360 self.assertEquals("float", child_types[ 7])
1361 self.assertEquals("float", child_types[ 8])
1362 self.assertEquals("str", child_types[ 9])
1363 self.assertEquals("int", child_types[10])
1364 self.assertEquals("int", child_types[11])
1365 self.assertEquals("int", child_types[12])
1366 self.assertEquals(None, child_types[13])
1367
1368 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1369
1371 XML = self.XML
1372 root = XML(_bytes('''\
1373 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1374 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1375 <n></n>
1376 </a>
1377 '''))
1378 objectify.annotate(root)
1379
1380 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1381 for c in root.iterchildren() ]
1382 self.assertEquals(None, child_types[0])
1383
1384 objectify.annotate(root, empty_pytype="str")
1385
1386 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1387 for c in root.iterchildren() ]
1388 self.assertEquals("str", child_types[0])
1389
1391 XML = self.XML
1392 root = XML(_bytes('''\
1393 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1394 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1395 <b>5</b>
1396 <b>test</b>
1397 <c>1.1</c>
1398 <c>\uF8D2</c>
1399 <x>true</x>
1400 <n xsi:nil="true" />
1401 <n></n>
1402 <b xsi:type="double">5</b>
1403 <b xsi:type="float">5</b>
1404 <s xsi:type="string">23</s>
1405 <s py:pytype="str">42</s>
1406 <f py:pytype="float">300</f>
1407 <l py:pytype="long">2</l>
1408 <t py:pytype="TREE"></t>
1409 </a>
1410 '''))
1411 objectify.annotate(root, ignore_old=False)
1412
1413 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1414 for c in root.iterchildren() ]
1415 self.assertEquals("int", child_types[ 0])
1416 self.assertEquals("str", child_types[ 1])
1417 self.assertEquals("float", child_types[ 2])
1418 self.assertEquals("str", child_types[ 3])
1419 self.assertEquals("bool", child_types[ 4])
1420 self.assertEquals("NoneType", child_types[ 5])
1421 self.assertEquals(None, child_types[ 6])
1422 self.assertEquals("float", child_types[ 7])
1423 self.assertEquals("float", child_types[ 8])
1424 self.assertEquals("str", child_types[ 9])
1425 self.assertEquals("str", child_types[10])
1426 self.assertEquals("float", child_types[11])
1427 self.assertEquals("int", child_types[12])
1428 self.assertEquals(TREE_PYTYPE, child_types[13])
1429
1430 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1431
1433 XML = self.XML
1434 root = XML(_bytes('''\
1435 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1436 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1437 <b>5</b>
1438 <b>test</b>
1439 <c>1.1</c>
1440 <c>\uF8D2</c>
1441 <x>true</x>
1442 <n xsi:nil="true" />
1443 <n></n>
1444 <b xsi:type="double">5</b>
1445 <b xsi:type="float">5</b>
1446 <s xsi:type="string">23</s>
1447 <s py:pytype="str">42</s>
1448 <f py:pytype="float">300</f>
1449 <l py:pytype="long">2</l>
1450 <t py:pytype="TREE"></t>
1451 </a>
1452 '''))
1453 objectify.annotate(root, ignore_old=False, ignore_xsi=False,
1454 annotate_xsi=1, annotate_pytype=1)
1455
1456 # check py annotations
1457 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1458 for c in root.iterchildren() ]
1459 self.assertEquals("int", child_types[ 0])
1460 self.assertEquals("str", child_types[ 1])
1461 self.assertEquals("float", child_types[ 2])
1462 self.assertEquals("str", child_types[ 3])
1463 self.assertEquals("bool", child_types[ 4])
1464 self.assertEquals("NoneType", child_types[ 5])
1465 self.assertEquals(None, child_types[ 6])
1466 self.assertEquals("float", child_types[ 7])
1467 self.assertEquals("float", child_types[ 8])
1468 self.assertEquals("str", child_types[ 9])
1469 self.assertEquals("str", child_types[10])
1470 self.assertEquals("float", child_types[11])
1471 self.assertEquals("int", child_types[12])
1472 self.assertEquals(TREE_PYTYPE, child_types[13])
1473
1474 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1475
1476 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1477 for c in root.iterchildren() ]
1478
1479 # check xsi annotations
1480 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1481 for c in root.iterchildren() ]
1482 self.assertEquals("xsd:integer", child_types[ 0])
1483 self.assertEquals("xsd:string", child_types[ 1])
1484 self.assertEquals("xsd:double", child_types[ 2])
1485 self.assertEquals("xsd:string", child_types[ 3])
1486 self.assertEquals("xsd:boolean", child_types[ 4])
1487 self.assertEquals(None, child_types[ 5])
1488 self.assertEquals(None, child_types[ 6])
1489 self.assertEquals("xsd:double", child_types[ 7])
1490 self.assertEquals("xsd:float", child_types[ 8])
1491 self.assertEquals("xsd:string", child_types[ 9])
1492 self.assertEquals("xsd:string", child_types[10])
1493 self.assertEquals("xsd:double", child_types[11])
1494 self.assertEquals("xsd:integer", child_types[12])
1495 self.assertEquals(None, child_types[13])
1496
1497 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1498
1500 XML = self.XML
1501 root = XML(_bytes('''\
1502 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1503 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1504 <b>5</b>
1505 <b>test</b>
1506 <c>1.1</c>
1507 <c>\uF8D2</c>
1508 <x>true</x>
1509 <n xsi:nil="true" />
1510 <n></n>
1511 <b xsi:type="double">5</b>
1512 <b xsi:type="float">5</b>
1513 <s xsi:type="string">23</s>
1514 <s py:pytype="str">42</s>
1515 <f py:pytype="float">300</f>
1516 <l py:pytype="long">2</l>
1517 <t py:pytype="TREE"></t>
1518 </a>
1519 '''))
1520 objectify.xsiannotate(root, ignore_old=False)
1521
1522 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1523 for c in root.iterchildren() ]
1524 self.assertEquals("xsd:integer", child_types[ 0])
1525 self.assertEquals("xsd:string", child_types[ 1])
1526 self.assertEquals("xsd:double", child_types[ 2])
1527 self.assertEquals("xsd:string", child_types[ 3])
1528 self.assertEquals("xsd:boolean", child_types[ 4])
1529 self.assertEquals(None, child_types[ 5])
1530 self.assertEquals(None, child_types[ 6])
1531 self.assertEquals("xsd:double", child_types[ 7])
1532 self.assertEquals("xsd:float", child_types[ 8])
1533 self.assertEquals("xsd:string", child_types[ 9])
1534 self.assertEquals("xsd:string", child_types[10])
1535 self.assertEquals("xsd:double", child_types[11])
1536 self.assertEquals("xsd:integer", child_types[12])
1537 self.assertEquals(None, child_types[13])
1538
1540 XML = self.XML
1541 root = XML(_bytes('''\
1542 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1543 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1544 <b>5</b>
1545 <b>test</b>
1546 <c>1.1</c>
1547 <c>\uF8D2</c>
1548 <x>true</x>
1549 <n xsi:nil="true" />
1550 <n></n>
1551 <b xsi:type="double">5</b>
1552 <b xsi:type="float">5</b>
1553 <s xsi:type="string">23</s>
1554 <s py:pytype="str">42</s>
1555 <f py:pytype="float">300</f>
1556 <l py:pytype="long">2</l>
1557 <t py:pytype="TREE"></t>
1558 </a>
1559 '''))
1560 objectify.pyannotate(root, ignore_old=True)
1561
1562 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1563 for c in root.iterchildren() ]
1564 self.assertEquals("int", child_types[ 0])
1565 self.assertEquals("str", child_types[ 1])
1566 self.assertEquals("float", child_types[ 2])
1567 self.assertEquals("str", child_types[ 3])
1568 self.assertEquals("bool", child_types[ 4])
1569 self.assertEquals("NoneType", child_types[ 5])
1570 self.assertEquals(None, child_types[ 6])
1571 self.assertEquals("float", child_types[ 7])
1572 self.assertEquals("float", child_types[ 8])
1573 self.assertEquals("str", child_types[ 9])
1574 self.assertEquals("int", child_types[10])
1575 self.assertEquals("int", child_types[11])
1576 self.assertEquals("int", child_types[12])
1577 self.assertEquals(None, child_types[13])
1578
1579 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1580
1582 XML = self.XML
1583 root = XML('''\
1584 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1585 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1586 <n></n>
1587 </a>
1588 ''')
1589 objectify.pyannotate(root)
1590
1591 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1592 for c in root.iterchildren() ]
1593 self.assertEquals(None, child_types[0])
1594
1595 objectify.annotate(root, empty_pytype="str")
1596
1597 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1598 for c in root.iterchildren() ]
1599 self.assertEquals("str", child_types[0])
1600
1602 XML = self.XML
1603 root = XML('''\
1604 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1605 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1606 <b>5</b>
1607 <b>test</b>
1608 <c>1.1</c>
1609 <c>\uF8D2</c>
1610 <x>true</x>
1611 <n xsi:nil="true" />
1612 <n></n>
1613 <b xsi:type="double">5</b>
1614 <b xsi:type="float">5</b>
1615 <s xsi:type="string">23</s>
1616 <s py:pytype="str">42</s>
1617 <f py:pytype="float">300</f>
1618 <l py:pytype="long">2</l>
1619 <t py:pytype="TREE"></t>
1620 </a>
1621 ''')
1622 objectify.pyannotate(root)
1623
1624 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1625 for c in root.iterchildren() ]
1626 self.assertEquals("int", child_types[ 0])
1627 self.assertEquals("str", child_types[ 1])
1628 self.assertEquals("float", child_types[ 2])
1629 self.assertEquals("str", child_types[ 3])
1630 self.assertEquals("bool", child_types[ 4])
1631 self.assertEquals("NoneType", child_types[ 5])
1632 self.assertEquals(None, child_types[ 6])
1633 self.assertEquals("float", child_types[ 7])
1634 self.assertEquals("float", child_types[ 8])
1635 self.assertEquals("str", child_types[ 9])
1636 self.assertEquals("str", child_types[10])
1637 self.assertEquals("float", child_types[11])
1638 self.assertEquals("int", child_types[12])
1639 self.assertEquals(TREE_PYTYPE, child_types[13])
1640
1641 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1642
1644 XML = self.XML
1645 root = XML(_bytes('''\
1646 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1647 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1648 <b>5</b>
1649 <b>test</b>
1650 <c>1.1</c>
1651 <c>\uF8D2</c>
1652 <x>true</x>
1653 <n xsi:nil="true" />
1654 <n></n>
1655 <b xsi:type="double">5</b>
1656 <b xsi:type="float">5</b>
1657 <s xsi:type="string">23</s>
1658 <s py:pytype="str">42</s>
1659 <f py:pytype="float">300</f>
1660 <l py:pytype="long">2</l>
1661 <t py:pytype="TREE"></t>
1662 </a>
1663 '''))
1664 objectify.xsiannotate(root, ignore_old=True)
1665
1666 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1667 for c in root.iterchildren() ]
1668 self.assertEquals("xsd:integer", child_types[ 0])
1669 self.assertEquals("xsd:string", child_types[ 1])
1670 self.assertEquals("xsd:double", child_types[ 2])
1671 self.assertEquals("xsd:string", child_types[ 3])
1672 self.assertEquals("xsd:boolean", child_types[ 4])
1673 self.assertEquals(None, child_types[ 5])
1674 self.assertEquals(None, child_types[ 6])
1675 self.assertEquals("xsd:integer", child_types[ 7])
1676 self.assertEquals("xsd:integer", child_types[ 8])
1677 self.assertEquals("xsd:integer", child_types[ 9])
1678 self.assertEquals("xsd:string", child_types[10])
1679 self.assertEquals("xsd:double", child_types[11])
1680 self.assertEquals("xsd:integer", child_types[12])
1681 self.assertEquals(None, child_types[13])
1682
1683 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1684
1686 XML = self.XML
1687 root = XML(_bytes('''\
1688 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1689 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1690 <b>5</b>
1691 <b>test</b>
1692 <c>1.1</c>
1693 <c>\uF8D2</c>
1694 <x>true</x>
1695 <n xsi:nil="true" />
1696 <n></n>
1697 <b xsi:type="double">5</b>
1698 <b xsi:type="float">5</b>
1699 <s xsi:type="string">23</s>
1700 <s py:pytype="str">42</s>
1701 <f py:pytype="float">300</f>
1702 <l py:pytype="long">2</l>
1703 <t py:pytype="TREE"></t>
1704 </a>
1705 '''))
1706 objectify.deannotate(root)
1707
1708 for c in root.getiterator():
1709 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1710 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1711
1712 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1713
1715 XML = self.XML
1716 root = XML(_bytes('''\
1717 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1718 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1719 <b>5</b>
1720 <b>test</b>
1721 <c>1.1</c>
1722 <c>\uF8D2</c>
1723 <x>true</x>
1724 <n xsi:nil="true" />
1725 <n></n>
1726 <b xsi:type="double">5</b>
1727 <b xsi:type="float">5</b>
1728 <s xsi:type="string">23</s>
1729 <s py:pytype="str">42</s>
1730 <f py:pytype="float">300</f>
1731 <l py:pytype="long">2</l>
1732 <t py:pytype="TREE"></t>
1733 </a>
1734 '''))
1735 objectify.annotate(
1736 root, ignore_old=False, ignore_xsi=False, annotate_xsi=True,
1737 empty_pytype='str', empty_type='string')
1738 objectify.deannotate(root, pytype=False, xsi=False, xsi_nil=True)
1739
1740 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1741 for c in root.iterchildren() ]
1742 self.assertEquals("xsd:integer", child_types[ 0])
1743 self.assertEquals("xsd:string", child_types[ 1])
1744 self.assertEquals("xsd:double", child_types[ 2])
1745 self.assertEquals("xsd:string", child_types[ 3])
1746 self.assertEquals("xsd:boolean", child_types[ 4])
1747 self.assertEquals(None, child_types[ 5])
1748 self.assertEquals("xsd:string", child_types[ 6])
1749 self.assertEquals("xsd:double", child_types[ 7])
1750 self.assertEquals("xsd:float", child_types[ 8])
1751 self.assertEquals("xsd:string", child_types[ 9])
1752 self.assertEquals("xsd:string", child_types[10])
1753 self.assertEquals("xsd:double", child_types[11])
1754 self.assertEquals("xsd:integer", child_types[12])
1755 self.assertEquals(None, child_types[13])
1756
1757 self.assertEquals(None, root.n.get(XML_SCHEMA_NIL_ATTR))
1758
1759 for c in root.iterchildren():
1760 self.assertNotEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1761 # these have no equivalent in xsi:type
1762 if (c.get(objectify.PYTYPE_ATTRIBUTE) not in [TREE_PYTYPE,
1763 "NoneType"]):
1764 self.assertNotEquals(
1765 None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1766
1768 XML = self.XML
1769 root = XML(_bytes('''\
1770 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1771 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1772 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1773 <b>5</b>
1774 <b>test</b>
1775 <c>1.1</c>
1776 <c>\uF8D2</c>
1777 <x>true</x>
1778 <n xsi:nil="true" />
1779 <n></n>
1780 <b xsi:type="xsd:double">5</b>
1781 <b xsi:type="xsd:float">5</b>
1782 <s xsi:type="xsd:string">23</s>
1783 <s py:pytype="str">42</s>
1784 <f py:pytype="float">300</f>
1785 <l py:pytype="long">2</l>
1786 <t py:pytype="TREE"></t>
1787 </a>
1788 '''))
1789 objectify.annotate(root)
1790 objectify.deannotate(root, pytype=False)
1791
1792 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1793 for c in root.iterchildren() ]
1794 self.assertEquals("int", child_types[ 0])
1795 self.assertEquals("str", child_types[ 1])
1796 self.assertEquals("float", child_types[ 2])
1797 self.assertEquals("str", child_types[ 3])
1798 self.assertEquals("bool", child_types[ 4])
1799 self.assertEquals("NoneType", child_types[ 5])
1800 self.assertEquals(None, child_types[ 6])
1801 self.assertEquals("float", child_types[ 7])
1802 self.assertEquals("float", child_types[ 8])
1803 self.assertEquals("str", child_types[ 9])
1804 self.assertEquals("int", child_types[10])
1805 self.assertEquals("int", child_types[11])
1806 self.assertEquals("int", child_types[12])
1807 self.assertEquals(None, child_types[13])
1808
1809 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1810
1811 for c in root.getiterator():
1812 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1813
1815 XML = self.XML
1816 root = XML(_bytes('''\
1817 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1818 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1819 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1820 <b xsi:type="xsd:int">5</b>
1821 <b xsi:type="xsd:string">test</b>
1822 <c xsi:type="xsd:float">1.1</c>
1823 <c xsi:type="xsd:string">\uF8D2</c>
1824 <x xsi:type="xsd:boolean">true</x>
1825 <n xsi:nil="true" />
1826 <n></n>
1827 <b xsi:type="xsd:double">5</b>
1828 <b xsi:type="xsd:float">5</b>
1829 <s xsi:type="xsd:string">23</s>
1830 <s xsi:type="xsd:string">42</s>
1831 <f xsi:type="xsd:float">300</f>
1832 <l xsi:type="xsd:long">2</l>
1833 <t py:pytype="TREE"></t>
1834 </a>
1835 '''))
1836 objectify.annotate(root)
1837 objectify.deannotate(root, xsi=False)
1838
1839 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1840 for c in root.iterchildren() ]
1841 self.assertEquals("xsd:int", child_types[ 0])
1842 self.assertEquals("xsd:string", child_types[ 1])
1843 self.assertEquals("xsd:float", child_types[ 2])
1844 self.assertEquals("xsd:string", child_types[ 3])
1845 self.assertEquals("xsd:boolean", child_types[ 4])
1846 self.assertEquals(None, child_types[ 5])
1847 self.assertEquals(None, child_types[ 6])
1848 self.assertEquals("xsd:double", child_types[ 7])
1849 self.assertEquals("xsd:float", child_types[ 8])
1850 self.assertEquals("xsd:string", child_types[ 9])
1851 self.assertEquals("xsd:string", child_types[10])
1852 self.assertEquals("xsd:float", child_types[11])
1853 self.assertEquals("xsd:long", child_types[12])
1854 self.assertEquals(None, child_types[13])
1855
1856 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1857
1858 for c in root.getiterator():
1859 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1860
1862 XML = self.XML
1863
1864 xml = _bytes('''\
1865 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1866 <b>5</b>
1867 <b>test</b>
1868 <c>1.1</c>
1869 <c>\uF8D2</c>
1870 <x>true</x>
1871 <n xsi:nil="true" />
1872 <n></n>
1873 <b xsi:type="double">5</b>
1874 </a>
1875 ''')
1876
1877 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1878 objectify.set_pytype_attribute_tag("{TEST}test")
1879
1880 root = XML(xml)
1881 objectify.annotate(root)
1882
1883 attribs = root.xpath("//@py:%s" % pytype_name,
1884 namespaces={"py" : pytype_ns})
1885 self.assertEquals(0, len(attribs))
1886 attribs = root.xpath("//@py:test",
1887 namespaces={"py" : "TEST"})
1888 self.assertEquals(7, len(attribs))
1889
1890 objectify.set_pytype_attribute_tag()
1891 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1892
1893 self.assertNotEqual("test", pytype_ns.lower())
1894 self.assertNotEqual("test", pytype_name.lower())
1895
1896 root = XML(xml)
1897 attribs = root.xpath("//@py:%s" % pytype_name,
1898 namespaces={"py" : pytype_ns})
1899 self.assertEquals(0, len(attribs))
1900
1901 objectify.annotate(root)
1902 attribs = root.xpath("//@py:%s" % pytype_name,
1903 namespaces={"py" : pytype_ns})
1904 self.assertEquals(7, len(attribs))
1905
1907 orig_types = objectify.getRegisteredTypes()
1908 orig_types[0].unregister()
1909 self.assertEquals(orig_types[1:], objectify.getRegisteredTypes())
1910
1911 class NewType(objectify.ObjectifiedDataElement):
1912 pass
1913
1914 def checkMyType(s):
1915 return True
1916
1917 pytype = objectify.PyType("mytype", checkMyType, NewType)
1918 self.assert_(pytype not in objectify.getRegisteredTypes())
1919 pytype.register()
1920 self.assert_(pytype in objectify.getRegisteredTypes())
1921 pytype.unregister()
1922 self.assert_(pytype not in objectify.getRegisteredTypes())
1923
1924 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1925 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1926 pytype.unregister()
1927
1928 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1929 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1930 pytype.unregister()
1931
1932 self.assertRaises(ValueError, pytype.register,
1933 before = [objectify.getRegisteredTypes()[0].name],
1934 after = [objectify.getRegisteredTypes()[1].name])
1935
1937 from datetime import datetime
1938 def parse_date(value):
1939 if len(value) != 14:
1940 raise ValueError(value)
1941 Y = int(value[0:4])
1942 M = int(value[4:6])
1943 D = int(value[6:8])
1944 h = int(value[8:10])
1945 m = int(value[10:12])
1946 s = int(value[12:14])
1947 return datetime(Y, M, D, h, m, s)
1948
1949 def stringify_date(date):
1950 return date.strftime("%Y%m%d%H%M%S")
1951
1952 class DatetimeElement(objectify.ObjectifiedDataElement):
1953 def pyval(self):
1954 return parse_date(self.text)
1955 pyval = property(pyval)
1956
1957 datetime_type = objectify.PyType(
1958 "datetime", parse_date, DatetimeElement, stringify_date)
1959 datetime_type.xmlSchemaTypes = "dateTime"
1960 datetime_type.register()
1961
1962 NAMESPACE = "http://foo.net/xmlns"
1963 NAMESPACE_MAP = {'ns': NAMESPACE}
1964
1965 r = objectify.Element("{%s}root" % NAMESPACE, nsmap=NAMESPACE_MAP)
1966 time = datetime.now()
1967 r.date = time
1968
1969 self.assert_(isinstance(r.date, DatetimeElement))
1970 self.assert_(isinstance(r.date.pyval, datetime))
1971
1972 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1973 self.assertEquals(r.date.text, stringify_date(time))
1974
1975 r.date = objectify.E.date(time)
1976
1977 self.assert_(isinstance(r.date, DatetimeElement))
1978 self.assert_(isinstance(r.date.pyval, datetime))
1979
1980 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1981 self.assertEquals(r.date.text, stringify_date(time))
1982
1983 date = objectify.DataElement(time)
1984
1985 self.assert_(isinstance(date, DatetimeElement))
1986 self.assert_(isinstance(date.pyval, datetime))
1987
1988 self.assertEquals(date.pyval, parse_date(stringify_date(time)))
1989 self.assertEquals(date.text, stringify_date(time))
1990
1992 root = self.XML(xml_str)
1993 path = objectify.ObjectPath( "root.c1.c2" )
1994 self.assertEquals(root.c1.c2.text, path.find(root).text)
1995 self.assertEquals(root.c1.c2.text, path(root).text)
1996
1998 root = self.XML(xml_str)
1999 path = objectify.ObjectPath( ['root', 'c1', 'c2'] )
2000 self.assertEquals(root.c1.c2.text, path.find(root).text)
2001 self.assertEquals(root.c1.c2.text, path(root).text)
2002
2004 root = self.XML(xml_str)
2005 path = objectify.ObjectPath( "root.c1.c99" )
2006 self.assertRaises(AttributeError, path, root)
2007
2009 root = self.XML(xml_str)
2010 path = objectify.ObjectPath( "root.c1.c99" )
2011 self.assertEquals(None, path(root, None))
2012 path = objectify.ObjectPath( "root.c99.c2" )
2013 self.assertEquals(None, path(root, None))
2014 path = objectify.ObjectPath( "notroot.c99.c2" )
2015 self.assertEquals(None, path(root, None))
2016
2018 root = self.XML(xml_str)
2019 path = objectify.ObjectPath( ".c1.c99" )
2020 self.assertEquals(None, path(root, None))
2021 path = objectify.ObjectPath( ".c99.c2" )
2022 self.assertEquals(None, path(root, None))
2023
2025 root = self.XML(xml_str)
2026 path = objectify.ObjectPath("root . {objectified}c1. c2")
2027 self.assertEquals(root.c1.c2.text, path(root).text)
2028
2029 path = objectify.ObjectPath(" root.{objectified} c1.c2 [ 0 ] ")
2030 self.assertEquals(root.c1.c2.text, path(root).text)
2031
2034
2037
2039 root = self.XML(xml_str)
2040 path = objectify.ObjectPath( "root" )
2041 self.assert_(path.hasattr(root))
2042 path = objectify.ObjectPath( "root.c1" )
2043 self.assert_(path.hasattr(root))
2044 path = objectify.ObjectPath( "root.c1.c2" )
2045 self.assert_(path.hasattr(root))
2046 path = objectify.ObjectPath( "root.c1.{otherNS}c2" )
2047 self.assert_(path.hasattr(root))
2048 path = objectify.ObjectPath( "root.c1.c2[1]" )
2049 self.assert_(path.hasattr(root))
2050 path = objectify.ObjectPath( "root.c1.c2[2]" )
2051 self.assert_(path.hasattr(root))
2052 path = objectify.ObjectPath( "root.c1.c2[3]" )
2053 self.assertFalse(path.hasattr(root))
2054 path = objectify.ObjectPath( "root.c1[1].c2" )
2055 self.assertFalse(path.hasattr(root))
2056
2058 root = self.XML(xml_str)
2059 path = objectify.ObjectPath( "." )
2060 self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
2061
2063 root = self.XML(xml_str)
2064 path = objectify.ObjectPath( [''] )
2065 self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
2066
2068 root = self.XML(xml_str)
2069 path = objectify.ObjectPath( ".c1.c2" )
2070 self.assertEquals(root.c1.c2.text, path(root).text)
2071
2073 root = self.XML(xml_str)
2074 path = objectify.ObjectPath( ['', 'c1', 'c2'] )
2075 self.assertEquals(root.c1.c2.text, path(root).text)
2076
2078 root = self.XML(xml_str)
2079 path = objectify.ObjectPath( "root.c1[0].c2[0]" )
2080 self.assertEquals(root.c1.c2.text, path(root).text)
2081
2082 path = objectify.ObjectPath( "root.c1[0].c2" )
2083 self.assertEquals(root.c1.c2.text, path(root).text)
2084
2085 path = objectify.ObjectPath( "root.c1[0].c2[1]" )
2086 self.assertEquals(root.c1.c2[1].text, path(root).text)
2087
2088 path = objectify.ObjectPath( "root.c1.c2[2]" )
2089 self.assertEquals(root.c1.c2[2].text, path(root).text)
2090
2091 path = objectify.ObjectPath( "root.c1.c2[-1]" )
2092 self.assertEquals(root.c1.c2[-1].text, path(root).text)
2093
2094 path = objectify.ObjectPath( "root.c1.c2[-3]" )
2095 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2096
2098 root = self.XML(xml_str)
2099 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
2100 self.assertEquals(root.c1.c2.text, path(root).text)
2101
2102 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
2103 self.assertEquals(root.c1.c2[2].text, path(root).text)
2104
2105 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
2106 self.assertEquals(root.c1.c2[2].text, path(root).text)
2107
2108 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
2109 self.assertEquals(root.c1.c2[-1].text, path(root).text)
2110
2111 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
2112 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2113
2115 self.assertRaises(ValueError, objectify.ObjectPath,
2116 "root.c1[0].c2[-1-2]")
2117 self.assertRaises(ValueError, objectify.ObjectPath,
2118 ['root', 'c1[0]', 'c2[-1-2]'])
2119
2120 self.assertRaises(ValueError, objectify.ObjectPath,
2121 "root[2].c1.c2")
2122 self.assertRaises(ValueError, objectify.ObjectPath,
2123 ['root[2]', 'c1', 'c2'])
2124
2125 self.assertRaises(ValueError, objectify.ObjectPath,
2126 [])
2127 self.assertRaises(ValueError, objectify.ObjectPath,
2128 ['', '', ''])
2129
2131 root = self.XML(xml_str)
2132 path = objectify.ObjectPath("root.c1[9999].c2")
2133 self.assertRaises(AttributeError, path, root)
2134
2135 path = objectify.ObjectPath("root.c1[0].c2[9999]")
2136 self.assertRaises(AttributeError, path, root)
2137
2138 path = objectify.ObjectPath(".c1[9999].c2[0]")
2139 self.assertRaises(AttributeError, path, root)
2140
2141 path = objectify.ObjectPath("root.c1[-2].c2")
2142 self.assertRaises(AttributeError, path, root)
2143
2144 path = objectify.ObjectPath("root.c1[0].c2[-4]")
2145 self.assertRaises(AttributeError, path, root)
2146
2148 root = self.XML(xml_str)
2149 path = objectify.ObjectPath( "{objectified}root.c1.c2" )
2150 self.assertEquals(root.c1.c2.text, path.find(root).text)
2151 path = objectify.ObjectPath( "{objectified}root.{objectified}c1.c2" )
2152 self.assertEquals(root.c1.c2.text, path.find(root).text)
2153 path = objectify.ObjectPath( "root.{objectified}c1.{objectified}c2" )
2154 self.assertEquals(root.c1.c2.text, path.find(root).text)
2155 path = objectify.ObjectPath( "root.c1.{objectified}c2" )
2156 self.assertEquals(root.c1.c2.text, path.find(root).text)
2157 path = objectify.ObjectPath( "root.c1.{otherNS}c2" )
2158 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2159 path.find(root).text)
2160
2162 root = self.XML(xml_str)
2163 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
2164 self.assertEquals(root.c1.c2.text, path.find(root).text)
2165 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
2166 self.assertEquals(root.c1.c2.text, path.find(root).text)
2167 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
2168 self.assertEquals(root.c1.c2.text, path.find(root).text)
2169 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
2170 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2171 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
2172 self.assertEquals(root.c1.c2.text, path.find(root).text)
2173 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
2174 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2175 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
2176 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2177 path.find(root).text)
2178
2180 root = self.XML(xml_str)
2181 path = objectify.ObjectPath( "root.c1.c2" )
2182 self.assertEquals(root.c1.c2.text, path.find(root).text)
2183 self.assertEquals("1", root.c1.c2[1].text)
2184
2185 new_value = "my new value"
2186 path.setattr(root, new_value)
2187
2188 self.assertEquals(new_value, root.c1.c2.text)
2189 self.assertEquals(new_value, path(root).text)
2190 self.assertEquals("1", root.c1.c2[1].text)
2191
2193 root = self.XML(xml_str)
2194 path = objectify.ObjectPath( "root.c1.c2" )
2195 self.assertEquals(root.c1.c2.text, path.find(root).text)
2196 self.assertEquals("1", root.c1.c2[1].text)
2197
2198 new_el = self.Element("{objectified}test")
2199 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST"
2200 path.setattr(root, new_el.sub)
2201
2202 self.assertEquals("ATTR", root.c1.c2.get("myattr"))
2203 self.assertEquals("TEST", root.c1.c2.a.text)
2204 self.assertEquals("TEST", path(root).a.text)
2205 self.assertEquals("1", root.c1.c2[1].text)
2206
2208 root = self.XML(xml_str)
2209 path = objectify.ObjectPath( "root.c1.c99" )
2210 self.assertRaises(AttributeError, path.find, root)
2211
2212 new_value = "my new value"
2213 path.setattr(root, new_value)
2214
2215 self.assertEquals(1, len(root.c1.c99))
2216 self.assertEquals(new_value, root.c1.c99.text)
2217 self.assertEquals(new_value, path(root).text)
2218
2220 root = self.XML(xml_str)
2221 path = objectify.ObjectPath( "root.c1.c99" )
2222 self.assertRaises(AttributeError, path.find, root)
2223
2224 new_el = self.Element("{objectified}test")
2225 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST"
2226 path.setattr(root, new_el.sub)
2227
2228 self.assertEquals(1, len(root.c1.c99))
2229 self.assertEquals("ATTR", root.c1.c99.get("myattr"))
2230 self.assertEquals("TEST", root.c1.c99.a.text)
2231 self.assertEquals("TEST", path(root).a.text)
2232
2234 root = self.XML(xml_str)
2235 path = objectify.ObjectPath( "root.c1.c99" )
2236 self.assertRaises(AttributeError, path.find, root)
2237
2238 new_el = self.Element("{objectified}test")
2239 new_el.a = ["TEST1", "TEST2"]
2240 new_el.a[0].set("myattr", "ATTR1")
2241 new_el.a[1].set("myattr", "ATTR2")
2242
2243 path.setattr(root, list(new_el.a))
2244
2245 self.assertEquals(2, len(root.c1.c99))
2246 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
2247 self.assertEquals("TEST1", root.c1.c99[0].text)
2248 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
2249 self.assertEquals("TEST2", root.c1.c99[1].text)
2250 self.assertEquals("TEST1", path(root).text)
2251
2253 root = self.XML(xml_str)
2254 path = objectify.ObjectPath( "root.c1.c2" )
2255 self.assertEquals(3, len(root.c1.c2))
2256 path.addattr(root, "test")
2257 self.assertEquals(4, len(root.c1.c2))
2258 self.assertEquals(["0", "1", "2", "test"],
2259 [el.text for el in root.c1.c2])
2260
2262 root = self.XML(xml_str)
2263 path = objectify.ObjectPath( "root.c1.c2" )
2264 self.assertEquals(3, len(root.c1.c2))
2265
2266 new_el = self.Element("{objectified}test")
2267 etree.SubElement(new_el, "{objectified}sub").a = "TEST"
2268
2269 path.addattr(root, new_el.sub)
2270 self.assertEquals(4, len(root.c1.c2))
2271 self.assertEquals("TEST", root.c1.c2[3].a.text)
2272 self.assertEquals(["0", "1", "2"],
2273 [el.text for el in root.c1.c2[:3]])
2274
2276 root = self.XML(xml_str)
2277 path = objectify.ObjectPath( "root.c1.c99" )
2278 self.assertRaises(AttributeError, path.find, root)
2279
2280 new_value = "my new value"
2281 path.addattr(root, new_value)
2282
2283 self.assertEquals(1, len(root.c1.c99))
2284 self.assertEquals(new_value, root.c1.c99.text)
2285 self.assertEquals(new_value, path(root).text)
2286
2288 root = self.XML(xml_str)
2289 path = objectify.ObjectPath( "root.c1.c99" )
2290 self.assertRaises(AttributeError, path.find, root)
2291
2292 new_el = self.Element("{objectified}test")
2293 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST"
2294
2295 path.addattr(root, new_el.sub)
2296 self.assertEquals(1, len(root.c1.c99))
2297 self.assertEquals("TEST", root.c1.c99.a.text)
2298 self.assertEquals("TEST", path(root).a.text)
2299 self.assertEquals("ATTR", root.c1.c99.get("myattr"))
2300
2302 root = self.XML(xml_str)
2303 path = objectify.ObjectPath( "root.c1.c99" )
2304 self.assertRaises(AttributeError, path.find, root)
2305
2306 new_el = self.Element("{objectified}test")
2307 new_el.a = ["TEST1", "TEST2"]
2308
2309 self.assertEquals(2, len(new_el.a))
2310
2311 path.addattr(root, list(new_el.a))
2312 self.assertEquals(2, len(root.c1.c99))
2313 self.assertEquals("TEST1", root.c1.c99.text)
2314 self.assertEquals("TEST2", path(root)[1].text)
2315
2317 root = self.XML(xml_str)
2318 self.assertEquals(
2319 ['{objectified}root', '{objectified}root.c1',
2320 '{objectified}root.c1.c2',
2321 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]',
2322 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'],
2323 root.descendantpaths())
2324
2326 root = self.XML(xml_str)
2327 self.assertEquals(
2328 ['{objectified}c1', '{objectified}c1.c2',
2329 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]',
2330 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'],
2331 root.c1.descendantpaths())
2332
2334 root = self.XML(xml_str)
2335 self.assertEquals(
2336 ['root.{objectified}c1', 'root.{objectified}c1.c2',
2337 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]',
2338 'root.{objectified}c1.{otherNS}c2',
2339 'root.{objectified}c1.{}c2'],
2340 root.c1.descendantpaths('root'))
2341
2343 import pickle
2344
2345 root = self.XML(xml_str)
2346 out = BytesIO()
2347 pickle.dump(root, out)
2348
2349 new_root = pickle.loads(out.getvalue())
2350 self.assertEquals(
2351 etree.tostring(new_root),
2352 etree.tostring(root))
2353
2355 import pickle
2356
2357 tree = etree.ElementTree(self.XML(xml_str + "<?my pi?>"))
2358 out = BytesIO()
2359 pickle.dump(tree, out)
2360
2361 new_tree = pickle.loads(out.getvalue())
2362 self.assert_(isinstance(new_tree, etree._ElementTree))
2363 self.assertEquals(
2364 etree.tostring(new_tree),
2365 etree.tostring(tree))
2366
2370
2374
2376 self._test_pickle('<x>Pickle me!</x>')
2377 self._test_pickle(objectify.DataElement('Pickle me!'))
2378
2380 self._test_pickle('<x>true</x>')
2381 self._test_pickle('<x>false</x>')
2382 self._test_pickle(objectify.DataElement(True))
2383 self._test_pickle(objectify.DataElement(False))
2384
2386 self._test_pickle('''
2387 <x xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>''')
2388 self._test_pickle(objectify.DataElement(None))
2389
2391 import pickle
2392 if isinstance(stringOrElt, (etree._Element, etree._ElementTree)):
2393 elt = stringOrElt
2394 else:
2395 elt = self.XML(stringOrElt)
2396 out = BytesIO()
2397 pickle.dump(elt, out)
2398
2399 new_elt = pickle.loads(out.getvalue())
2400 self.assertEquals(
2401 etree.tostring(new_elt),
2402 etree.tostring(elt))
2403
2404 # E-Factory tests, need to use sub-elements as root element is always
2405 # type-looked-up as ObjectifiedElement (no annotations)
2407 E = objectify.E
2408 root = E.root(E.val(23))
2409 self.assert_(isinstance(root.val, objectify.IntElement))
2410
2412 E = objectify.E
2413 root = E.root(E.val(233.23))
2414 self.assert_(isinstance(root.val, objectify.FloatElement))
2415
2417 E = objectify.E
2418 root = E.root(E.val("what?"))
2419 self.assert_(isinstance(root.val, objectify.StringElement))
2420
2422 E = objectify.E
2423 root = E.root(E.val(_str("blöödy häll", encoding="ISO-8859-1")))
2424 self.assert_(isinstance(root.val, objectify.StringElement))
2425
2427 E = objectify.E
2428 root = E.root(E.val(True))
2429 self.assert_(isinstance(root.val, objectify.BoolElement))
2430
2432 E = objectify.E
2433 root = E.root(E.val(None))
2434 self.assert_(isinstance(root.val, objectify.NoneElement))
2435
2437 E = objectify.E
2438 root = E.root(E.val(1, "foo", 2.0, "bar ", True, None))
2439 self.assert_(isinstance(root.val, objectify.StringElement))
2440
2445
2447 E = objectify.E
2448 DataElement = objectify.DataElement
2449 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1),
2450 DataElement(2.0))
2451 self.assert_(isinstance(root, objectify.ObjectifiedElement))
2452 self.assertEquals(root.text, "text")
2453 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement))
2454 self.assertEquals(root.sub.tail, "tail")
2455 self.assert_(isinstance(root.sub.subsub, objectify.StringElement))
2456 self.assertEquals(len(root.value), 2)
2457 self.assert_(isinstance(root.value[0], objectify.IntElement))
2458 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2459
2461 class Attribute(objectify.ObjectifiedDataElement):
2462 def __init__(self):
2463 objectify.ObjectifiedDataElement.__init__(self)
2464 self.set("datatype", "TYPE")
2465 self.set("range", "0.,1.")
2466
2467 attr = Attribute()
2468 self.assertEquals(attr.text, None)
2469 self.assertEquals(attr.get("datatype"), "TYPE")
2470 self.assertEquals(attr.get("range"), "0.,1.")
2471
2473 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2474 docinfo = root.getroottree().docinfo
2475 self.assertEquals(docinfo.URL, "http://no/such/url")
2476
2478 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2479 docinfo = root.getroottree().docinfo
2480 self.assertEquals(docinfo.URL, "http://no/such/url")
2481 docinfo.URL = "https://secret/url"
2482 self.assertEquals(docinfo.URL, "https://secret/url")
2483
2485 tree = objectify.parse(BytesIO("<root/>"), base_url="http://no/such/url")
2486 docinfo = tree.docinfo
2487 self.assertEquals(docinfo.URL, "http://no/such/url")
2488
2490 tree = objectify.parse(fileInTestDir('include/test_xinclude.xml'),
2491 base_url="http://no/such/url")
2492 docinfo = tree.docinfo
2493 self.assertEquals(docinfo.URL, "http://no/such/url")
2494
2496 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2497 self.assertEquals(root.base, "http://no/such/url")
2498 self.assertEquals(
2499 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2500 root.base = "https://secret/url"
2501 self.assertEquals(root.base, "https://secret/url")
2502 self.assertEquals(
2503 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2504 "https://secret/url")
2505
2507 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2508 self.assertEquals(root.base, "http://no/such/url")
2509 self.assertEquals(
2510 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2511 root.set('{http://www.w3.org/XML/1998/namespace}base',
2512 "https://secret/url")
2513 self.assertEquals(root.base, "https://secret/url")
2514 self.assertEquals(
2515 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2516 "https://secret/url")
2517
2519 XML = self.XML
2520
2521 xml = _bytes('''\
2522 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2523 <i>5</i>
2524 <i>-5</i>
2525 <l>4294967296</l>
2526 <l>-4294967296</l>
2527 <f>1.1</f>
2528 <b>true</b>
2529 <b>false</b>
2530 <s>Strange things happen, where strings collide</s>
2531 <s>True</s>
2532 <s>False</s>
2533 <s>t</s>
2534 <s>f</s>
2535 <s></s>
2536 <s>None</s>
2537 <n xsi:nil="true" />
2538 </root>
2539 ''')
2540 root = XML(xml)
2541
2542 for i in root.i:
2543 self.assert_(isinstance(i, objectify.IntElement))
2544 for l in root.l:
2545 self.assert_(isinstance(l, objectify.IntElement))
2546 for f in root.f:
2547 self.assert_(isinstance(f, objectify.FloatElement))
2548 for b in root.b:
2549 self.assert_(isinstance(b, objectify.BoolElement))
2550 self.assertEquals(True, root.b[0])
2551 self.assertEquals(False, root.b[1])
2552 for s in root.s:
2553 self.assert_(isinstance(s, objectify.StringElement))
2554 self.assert_(isinstance(root.n, objectify.NoneElement))
2555 self.assertEquals(None, root.n)
2556
2558 suite = unittest.TestSuite()
2559 suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
2560 suite.addTests(doctest.DocTestSuite(objectify))
2561 if sys.version_info >= (2,4):
2562 suite.addTests(
2563 [make_doctest('../../../doc/objectify.txt')])
2564 return suite
2565
2566 if __name__ == '__main__':
2567 print('to test use test.py %s' % __file__)
2568
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Fri Nov 11 18:38:33 2011 | http://epydoc.sourceforge.net |