| 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>3</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 for val in [
358 2, 2**32, 1.2, "Won't get fooled again",
359 _str("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1'), True,
360 False, None]:
361 root = self.Element('root')
362 attrname = 'val'
363 setattr(root, attrname, val)
364 result = getattr(root, attrname)
365 self.assertEquals(val, result)
366 self.assertEquals(type(val), type(result.pyval))
367
369 root = self.Element('root')
370 attrname = 'val'
371 val = _bytes("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1')
372 self.assertRaises(ValueError, setattr, root, attrname, val)
373 self.assertRaises(AttributeError, getattr, root, attrname)
374
376 root = self.XML(xml_str)
377 self.assertEquals(1, len(root.c1))
378 root.addattr("c1", "test")
379 self.assertEquals(2, len(root.c1))
380 self.assertEquals("test", root.c1[1].text)
381
383 root = self.XML(xml_str)
384 self.assertEquals(1, len(root.c1))
385
386 new_el = self.Element("test", myattr="5")
387 root.addattr("c1", new_el)
388 self.assertEquals(2, len(root.c1))
389 self.assertEquals(None, root.c1[0].get("myattr"))
390 self.assertEquals("5", root.c1[1].get("myattr"))
391
393 root = self.XML(xml_str)
394 self.assertEquals(1, len(root.c1))
395
396 new_el = self.Element("test")
397 self.etree.SubElement(new_el, "a", myattr="A")
398 self.etree.SubElement(new_el, "a", myattr="B")
399
400 root.addattr("c1", list(new_el.a))
401 self.assertEquals(3, len(root.c1))
402 self.assertEquals(None, root.c1[0].get("myattr"))
403 self.assertEquals("A", root.c1[1].get("myattr"))
404 self.assertEquals("B", root.c1[2].get("myattr"))
405
407 root = self.XML(xml_str)
408 self.assertEquals(3, len(root.c1.c2))
409 root.c1.addattr("c2", 3)
410 self.assertEquals(4, len(root.c1.c2))
411 self.assertEquals("3", root.c1.c2[3].text)
412
414 root = self.XML(xml_str)
415 self.assertEquals("0", root.c1.c2[0].text)
416 self.assertEquals("1", root.c1.c2[1].text)
417 self.assertEquals("2", root.c1.c2[2].text)
418 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
419
421 root = self.XML(xml_str)
422 self.assertEquals("0", root.c1.c2[0].text)
423 self.assertEquals("0", root.c1.c2[-3].text)
424 self.assertEquals("1", root.c1.c2[-2].text)
425 self.assertEquals("2", root.c1.c2[-1].text)
426 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
427
429 root = self.XML(xml_str)
430 self.assertEquals(1, len(root))
431 self.assertEquals(1, len(root.c1))
432 self.assertEquals(3, len(root.c1.c2))
433
435 root = self.XML(xml_str)
436 self.assertEquals([root],
437 list(iter(root)))
438 self.assertEquals([root.c1],
439 list(iter(root.c1)))
440 self.assertEquals([root.c1.c2[0], root.c1.c2[1], root.c1.c2[2]],
441 list(iter((root.c1.c2))))
442
444 root = self.XML(xml_str)
445 self.assert_(isinstance(root.c1.c2, objectify.ObjectifiedElement))
446 self.assertFalse(isinstance(getattr(root.c1, "{otherNS}c2"),
447 objectify.ObjectifiedElement))
448
450 root = self.XML(xml_str)
451 dir_c1 = dir(objectify.ObjectifiedElement) + ['c1']
452 dir_c1.sort()
453 dir_c2 = dir(objectify.ObjectifiedElement) + ['c2']
454 dir_c2.sort()
455
456 self.assertEquals(dir_c1, dir(root))
457 self.assertEquals(dir_c2, dir(root.c1))
458
460 root = self.XML(xml_str)
461 self.assertEquals({'c1' : root.c1}, vars(root))
462 self.assertEquals({'c2' : root.c1.c2}, vars(root.c1))
463
465 root = self.XML(xml_str)
466 self.assertRaises(TypeError, setattr, root.c1.c2, 'text', "test")
467 self.assertRaises(TypeError, setattr, root.c1.c2, 'pyval', "test")
468
469 # slicing
470
472 root = self.XML("<root><c>c1</c><c>c2</c></root>")
473 self.assertEquals(["c1", "c2"],
474 [ c.text for c in root.c[:] ])
475
477 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
478 test_list = ["c1", "c2", "c3", "c4"]
479
480 self.assertEquals(test_list,
481 [ c.text for c in root.c[:] ])
482 self.assertEquals(test_list[1:2],
483 [ c.text for c in root.c[1:2] ])
484 self.assertEquals(test_list[-3:-1],
485 [ c.text for c in root.c[-3:-1] ])
486 self.assertEquals(test_list[-3:3],
487 [ c.text for c in root.c[-3:3] ])
488 self.assertEquals(test_list[-3000:3],
489 [ c.text for c in root.c[-3000:3] ])
490 self.assertEquals(test_list[-3:3000],
491 [ c.text for c in root.c[-3:3000] ])
492
494 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
495 test_list = ["c1", "c2", "c3", "c4"]
496
497 self.assertEquals(test_list,
498 [ c.text for c in root.c[:] ])
499 self.assertEquals(test_list[2:1:-1],
500 [ c.text for c in root.c[2:1:-1] ])
501 self.assertEquals(test_list[-1:-3:-1],
502 [ c.text for c in root.c[-1:-3:-1] ])
503 self.assertEquals(test_list[2:-3:-1],
504 [ c.text for c in root.c[2:-3:-1] ])
505 self.assertEquals(test_list[2:-3000:-1],
506 [ c.text for c in root.c[2:-3000:-1] ])
507
508 # slice assignment
509
511 Element = self.Element
512 root = Element("root")
513 root.c = ["c1", "c2"]
514
515 c1 = root.c[0]
516 c2 = root.c[1]
517
518 self.assertEquals([c1,c2], list(root.c))
519 self.assertEquals(["c1", "c2"],
520 [ c.text for c in root.c ])
521
523 Element = self.Element
524 root = Element("root")
525 root.c = ["c1", "c2"]
526
527 c1 = root.c[0]
528 c2 = root.c[1]
529
530 self.assertEquals([c1,c2], list(root.c))
531 self.assertEquals(["c1", "c2"],
532 [ c.text for c in root.c ])
533
534 root2 = Element("root2")
535 root2.el = [ "test", "test" ]
536 self.assertEquals(["test", "test"],
537 [ el.text for el in root2.el ])
538
539 root.c = [ root2.el, root2.el ]
540 self.assertEquals(["test", "test"],
541 [ c.text for c in root.c ])
542 self.assertEquals(["test", "test"],
543 [ el.text for el in root2.el ])
544
545 root.c[:] = [ c1, c2, c2, c1 ]
546 self.assertEquals(["c1", "c2", "c2", "c1"],
547 [ c.text for c in root.c ])
548
550 Element = self.Element
551 root = Element("root")
552 l = ["c1", "c2", "c3", "c4"]
553 root.c = l
554
555 self.assertEquals(["c1", "c2", "c3", "c4"],
556 [ c.text for c in root.c ])
557 self.assertEquals(l,
558 [ c.text for c in root.c ])
559
560 new_slice = ["cA", "cB"]
561 l[1:2] = new_slice
562 root.c[1:2] = new_slice
563
564 self.assertEquals(["c1", "cA", "cB", "c3", "c4"], l)
565 self.assertEquals(["c1", "cA", "cB", "c3", "c4"],
566 [ c.text for c in root.c ])
567 self.assertEquals(l,
568 [ c.text for c in root.c ])
569
571 Element = self.Element
572 root = Element("root")
573 l = ["c1", "c2", "c3", "c4"]
574 root.c = l
575
576 self.assertEquals(["c1", "c2", "c3", "c4"],
577 [ c.text for c in root.c ])
578 self.assertEquals(l,
579 [ c.text for c in root.c ])
580
581 new_slice = ["cA", "cB"]
582 l[1:1] = new_slice
583 root.c[1:1] = new_slice
584
585 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"], l)
586 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"],
587 [ c.text for c in root.c ])
588 self.assertEquals(l,
589 [ c.text for c in root.c ])
590
592 Element = self.Element
593 root = Element("root")
594 l = ["c1", "c2", "c3", "c4"]
595 root.c = l
596
597 self.assertEquals(["c1", "c2", "c3", "c4"],
598 [ c.text for c in root.c ])
599 self.assertEquals(l,
600 [ c.text for c in root.c ])
601
602 new_slice = ["cA", "cB"]
603 l[-2:-2] = new_slice
604 root.c[-2:-2] = new_slice
605
606 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"], l)
607 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"],
608 [ c.text for c in root.c ])
609 self.assertEquals(l,
610 [ c.text for c in root.c ])
611
613 Element = self.Element
614 root = Element("root")
615
616 root.c = []
617 self.assertRaises(
618 AttributeError, getattr, root, 'c')
619
621 Element = self.Element
622 root = Element("root")
623 l = ["c1", "c2", "c3", "c4"]
624 root.c = l
625
626 self.assertEquals(["c1", "c2", "c3", "c4"],
627 [ c.text for c in root.c ])
628 self.assertEquals(l,
629 [ c.text for c in root.c ])
630
631 new_slice = ["cA", "cB", "cC"]
632 self.assertRaises(
633 ValueError, operator.setitem,
634 l, slice(1,2,-1), new_slice)
635 self.assertRaises(
636 ValueError, operator.setitem,
637 root.c, slice(1,2,-1), new_slice)
638
640 Element = self.Element
641 root = Element("root")
642 l = ["c1", "c2", "c3", "c4"]
643 root.c = l
644
645 self.assertEquals(["c1", "c2", "c3", "c4"],
646 [ c.text for c in root.c ])
647 self.assertEquals(l,
648 [ c.text for c in root.c ])
649
650 new_slice = ["cA", "cB"]
651 l[-1:1:-1] = new_slice
652 root.c[-1:1:-1] = new_slice
653
654 self.assertEquals(["c1", "c2", "cB", "cA"], l)
655 self.assertEquals(["c1", "c2", "cB", "cA"],
656 [ c.text for c in root.c ])
657 self.assertEquals(l,
658 [ c.text for c in root.c ])
659
661 Element = self.Element
662 root = Element("root")
663 l = ["c1", "c2", "c3", "c4"]
664 root.c = l
665
666 self.assertEquals(["c1", "c2", "c3", "c4"],
667 [ c.text for c in root.c ])
668 self.assertEquals(l,
669 [ c.text for c in root.c ])
670
671 new_slice = ["cA", "cB"]
672 l[-1:-4:-2] = new_slice
673 root.c[-1:-4:-2] = new_slice
674
675 self.assertEquals(["c1", "cB", "c3", "cA"], l)
676 self.assertEquals(["c1", "cB", "c3", "cA"],
677 [ c.text for c in root.c ])
678 self.assertEquals(l,
679 [ c.text for c in root.c ])
680
681 # other stuff
682
684 # make sure strings are not handled as sequences
685 Element = self.Element
686 root = Element("root")
687 root.c = "TEST"
688 self.assertEquals(["TEST"],
689 [ c.text for c in root.c ])
690
692 # make sure strings are set as children
693 Element = self.Element
694 root = Element("root")
695 root["c"] = "TEST"
696 self.assertEquals(["TEST"],
697 [ c.text for c in root.c ])
698
700 # make sure 'text' etc. are set as children
701 Element = self.Element
702 root = Element("root")
703
704 root["text"] = "TEST"
705 self.assertEquals(["TEST"],
706 [ c.text for c in root["text"] ])
707
708 root["tail"] = "TEST"
709 self.assertEquals(["TEST"],
710 [ c.text for c in root["tail"] ])
711
712 root["pyval"] = "TEST"
713 self.assertEquals(["TEST"],
714 [ c.text for c in root["pyval"] ])
715
716 root["tag"] = "TEST"
717 self.assertEquals(["TEST"],
718 [ c.text for c in root["tag"] ])
719
721 XML = self.XML
722 root = XML('<a><b><c/></b><b/><c><b/></c></a>')
723 self.assertEquals(1, len(root.findall("c")))
724 self.assertEquals(2, len(root.findall(".//c")))
725 self.assertEquals(3, len(root.findall(".//b")))
726 self.assert_(root.findall(".//b")[1] is root.getchildren()[1])
727
729 XML = self.XML
730 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
731 self.assertEquals(2, len(root.findall(".//{X}b")))
732 self.assertEquals(3, len(root.findall(".//b")))
733 self.assertEquals(2, len(root.findall("b")))
734
736 root = self.Element('root')
737 root.a = 5
738 root.b = 6
739 self.assert_(isinstance(root, objectify.ObjectifiedElement))
740 self.assert_(isinstance(root.a, objectify.IntElement))
741 self.assert_(isinstance(root.b, objectify.IntElement))
742
744 Element = self.Element
745 SubElement = self.etree.SubElement
746
747 nil_attr = XML_SCHEMA_NIL_ATTR
748 root = Element("{objectified}root")
749 SubElement(root, "{objectified}none")
750 SubElement(root, "{objectified}none", {nil_attr : "true"})
751 self.assertFalse(isinstance(root.none, objectify.NoneElement))
752 self.assertFalse(isinstance(root.none[0], objectify.NoneElement))
753 self.assert_(isinstance(root.none[1], objectify.NoneElement))
754 self.assertEquals(hash(root.none[1]), hash(None))
755 self.assertEquals(root.none[1], None)
756 self.assertFalse(root.none[1])
757
759 value = objectify.DataElement(None)
760 self.assert_(isinstance(value, objectify.NoneElement))
761 self.assertEquals(value, None)
762 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
763
765 Element = self.Element
766 SubElement = self.etree.SubElement
767 root = Element("{objectified}root")
768 root.bool = True
769 self.assertEquals(root.bool, True)
770 self.assertEquals(root.bool + root.bool, True + True)
771 self.assertEquals(True + root.bool, True + root.bool)
772 self.assertEquals(root.bool * root.bool, True * True)
773 self.assertEquals(int(root.bool), int(True))
774 self.assertEquals(hash(root.bool), hash(True))
775 self.assertEquals(complex(root.bool), complex(True))
776 self.assert_(isinstance(root.bool, objectify.BoolElement))
777
778 root.bool = False
779 self.assertEquals(root.bool, False)
780 self.assertEquals(root.bool + root.bool, False + False)
781 self.assertEquals(False + root.bool, False + root.bool)
782 self.assertEquals(root.bool * root.bool, False * False)
783 self.assertEquals(int(root.bool), int(False))
784 self.assertEquals(hash(root.bool), hash(False))
785 self.assertEquals(complex(root.bool), complex(False))
786 self.assert_(isinstance(root.bool, objectify.BoolElement))
787
789 value = objectify.DataElement(True)
790 self.assert_(isinstance(value, objectify.BoolElement))
791 self.assertEquals(value, True)
792
793 value = objectify.DataElement(False)
794 self.assert_(isinstance(value, objectify.BoolElement))
795 self.assertEquals(value, False)
796
798 Element = self.Element
799 SubElement = self.etree.SubElement
800 root = Element("{objectified}root")
801 root.s = "test"
802 self.assert_(isinstance(root.s, objectify.StringElement))
803
805 Element = self.Element
806 SubElement = self.etree.SubElement
807 root = Element("{objectified}root")
808 root.s = "3"
809 self.assert_(isinstance(root.s, objectify.StringElement))
810
812 Element = self.Element
813 SubElement = self.etree.SubElement
814 root = Element("{objectified}root")
815 root.s = "3.72"
816 self.assert_(isinstance(root.s, objectify.StringElement))
817
819 Element = self.Element
820 SubElement = self.etree.SubElement
821 root = Element("{objectified}root")
822 root.s = "test"
823
824 self.assertEquals("test" * 5, root.s * 5)
825 self.assertEquals(5 * "test", 5 * root.s)
826
827 self.assertRaises(TypeError, operator.mul, root.s, "honk")
828 self.assertRaises(TypeError, operator.mul, "honk", root.s)
829
831 Element = self.Element
832 SubElement = self.etree.SubElement
833 root = Element("{objectified}root")
834 root.s = "test"
835
836 s = "toast"
837 self.assertEquals("test" + s, root.s + s)
838 self.assertEquals(s + "test", s + root.s)
839
841 s = "%d %f %s %r"
842 el = objectify.DataElement(s)
843 values = (1, 7.0, "abcd", None)
844 self.assertEquals(s % values, el % values)
845
846 s = "%d"
847 el = objectify.DataElement(s)
848 val = 5
849 self.assertEquals(s % val, el % val)
850
851 s = "%d %s"
852 el = objectify.DataElement(s)
853 val = 5
854 self.assertRaises(TypeError, el.__mod__, val)
855
856 s = ""
857 el = objectify.DataElement(s)
858 val = 5
859 self.assertRaises(TypeError, el.__mod__, val)
860
865
870
875
880
882 s = "%d %f %s %r"
883 el = objectify.DataElement(s)
884 values = (objectify.DataElement(1),
885 objectify.DataElement(7.0),
886 objectify.DataElement("abcd"),
887 objectify.DataElement(None))
888 self.assertEquals(s % values, el % values)
889
891 value = objectify.DataElement("test")
892 self.assert_(isinstance(value, objectify.StringElement))
893 self.assertEquals(value, "test")
894
896 value = objectify.DataElement("3")
897 self.assert_(isinstance(value, objectify.StringElement))
898 self.assertEquals(value, "3")
899
901 value = objectify.DataElement("3.20")
902 self.assert_(isinstance(value, objectify.StringElement))
903 self.assertEquals(value, "3.20")
904
906 Element = self.Element
907 SubElement = self.etree.SubElement
908 root = Element("{objectified}root")
909 root.s = _str("test")
910 self.assert_(isinstance(root.s, objectify.StringElement))
911
913 Element = self.Element
914 SubElement = self.etree.SubElement
915 root = Element("{objectified}root")
916 root.s = _str("3")
917 self.assert_(isinstance(root.s, objectify.StringElement))
918
920 Element = self.Element
921 SubElement = self.etree.SubElement
922 root = Element("{objectified}root")
923 root.s = _str("3.72")
924 self.assert_(isinstance(root.s, objectify.StringElement))
925
927 Element = self.Element
928 SubElement = self.etree.SubElement
929 root = Element("{objectified}root")
930 root.s = _str("test")
931
932 self.assertEquals(_str("test") * 5, root.s * 5)
933 self.assertEquals(5 * _str("test"), 5 * root.s)
934
935 self.assertRaises(TypeError, operator.mul, root.s, _str("honk"))
936 self.assertRaises(TypeError, operator.mul, _str("honk"), root.s)
937
939 Element = self.Element
940 SubElement = self.etree.SubElement
941 root = Element("{objectified}root")
942 root.s = _str("test")
943
944 s = _str("toast")
945 self.assertEquals(_str("test") + s, root.s + s)
946 self.assertEquals(s + _str("test"), s + root.s)
947
949 value = objectify.DataElement(_str("test"))
950 self.assert_(isinstance(value, objectify.StringElement))
951 self.assertEquals(value, _str("test"))
952
954 value = objectify.DataElement("3")
955 self.assert_(isinstance(value, objectify.StringElement))
956 self.assertEquals(value, _str("3"))
957
959 value = objectify.DataElement(_str("3.20"))
960 self.assert_(isinstance(value, objectify.StringElement))
961 self.assertEquals(value, _str("3.20"))
962
964 Element = self.Element
965 SubElement = self.etree.SubElement
966 root = Element("{objectified}root")
967 root.none = 5
968 self.assert_(isinstance(root.none, objectify.IntElement))
969
971 value = objectify.DataElement(5)
972 self.assert_(isinstance(value, objectify.IntElement))
973 self.assertEquals(value, 5)
974
978
980 Element = self.Element
981 SubElement = self.etree.SubElement
982 root = Element("{objectified}root")
983 root.none = 5.5
984 self.assert_(isinstance(root.none, objectify.FloatElement))
985
987 value = objectify.DataElement(5.5)
988 self.assert_(isinstance(value, objectify.FloatElement))
989 self.assertEquals(value, 5.5)
990
994
996 for xsi, objclass in xsitype2objclass.items():
997 # 1 is a valid value for all ObjectifiedDataElement classes
998 pyval = 1
999 value = objectify.DataElement(pyval, _xsi=xsi)
1000 self.assert_(isinstance(value, objclass),
1001 "DataElement(%s, _xsi='%s') returns %s, expected %s"
1002 % (pyval, xsi, type(value), objclass))
1003
1005 for xsi, objclass in xsitype2objclass.items():
1006 # 1 is a valid value for all ObjectifiedDataElement classes
1007 pyval = 1
1008 value = objectify.DataElement(pyval, _xsi="xsd:%s" % xsi)
1009 self.assert_(isinstance(value, objclass),
1010 "DataElement(%s, _xsi='%s') returns %s, expected %s"
1011 % (pyval, xsi, type(value), objclass))
1012
1014 for xsi, objclass in xsitype2objclass.items():
1015 # 1 is a valid value for all ObjectifiedDataElement classes
1016 self.assertRaises(ValueError, objectify.DataElement, 1,
1017 _xsi="foo:%s" % xsi)
1018
1020 for pytype, objclass in pytype2objclass.items():
1021 # 1 is a valid value for all ObjectifiedDataElement classes
1022 pyval = 1
1023 value = objectify.DataElement(pyval, _pytype=pytype)
1024 self.assert_(isinstance(value, objclass),
1025 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1026 % (pyval, pytype, type(value), objclass))
1027
1029 pyval = 1
1030 pytype = "NoneType"
1031 objclass = objectify.NoneElement
1032 value = objectify.DataElement(pyval, _pytype=pytype)
1033 self.assert_(isinstance(value, objclass),
1034 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1035 % (pyval, pytype, type(value), objclass))
1036 self.assertEquals(value.text, None)
1037 self.assertEquals(value.pyval, None)
1038
1040 # pre-2.0 lxml called NoneElement "none"
1041 pyval = 1
1042 pytype = "none"
1043 objclass = objectify.NoneElement
1044 value = objectify.DataElement(pyval, _pytype=pytype)
1045 self.assert_(isinstance(value, objclass),
1046 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1047 % (pyval, pytype, type(value), objclass))
1048 self.assertEquals(value.text, None)
1049 self.assertEquals(value.pyval, None)
1050
1052 Element = self.Element
1053 SubElement = self.etree.SubElement
1054 class MyFloat(float):
1055 pass
1056 root = Element("{objectified}root")
1057 root.myfloat = MyFloat(5.5)
1058 self.assert_(isinstance(root.myfloat, objectify.FloatElement))
1059 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
1060
1064 value = objectify.DataElement(MyFloat(5.5))
1065 self.assert_(isinstance(value, objectify.FloatElement))
1066 self.assertEquals(value, 5.5)
1067 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None)
1068
1070 XML = self.XML
1071 root = XML('''\
1072 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1073 <b xsi:type="boolean">true</b>
1074 <b xsi:type="boolean">false</b>
1075 <b xsi:type="boolean">1</b>
1076 <b xsi:type="boolean">0</b>
1077
1078 <f xsi:type="float">5</f>
1079 <f xsi:type="double">5</f>
1080
1081 <s xsi:type="string">5</s>
1082 <s xsi:type="normalizedString">5</s>
1083 <s xsi:type="token">5</s>
1084 <s xsi:type="language">5</s>
1085 <s xsi:type="Name">5</s>
1086 <s xsi:type="NCName">5</s>
1087 <s xsi:type="ID">5</s>
1088 <s xsi:type="IDREF">5</s>
1089 <s xsi:type="ENTITY">5</s>
1090 <s xsi:type="NMTOKEN">5</s>
1091
1092 <l xsi:type="integer">5</l>
1093 <l xsi:type="nonPositiveInteger">5</l>
1094 <l xsi:type="negativeInteger">5</l>
1095 <l xsi:type="long">5</l>
1096 <l xsi:type="nonNegativeInteger">5</l>
1097 <l xsi:type="unsignedLong">5</l>
1098 <l xsi:type="unsignedInt">5</l>
1099 <l xsi:type="positiveInteger">5</l>
1100
1101 <i xsi:type="int">5</i>
1102 <i xsi:type="short">5</i>
1103 <i xsi:type="byte">5</i>
1104 <i xsi:type="unsignedShort">5</i>
1105 <i xsi:type="unsignedByte">5</i>
1106
1107 <n xsi:nil="true"/>
1108 </root>
1109 ''')
1110
1111 for b in root.b:
1112 self.assert_(isinstance(b, objectify.BoolElement))
1113 self.assertEquals(True, root.b[0])
1114 self.assertEquals(False, root.b[1])
1115 self.assertEquals(True, root.b[2])
1116 self.assertEquals(False, root.b[3])
1117
1118 for f in root.f:
1119 self.assert_(isinstance(f, objectify.FloatElement))
1120 self.assertEquals(5, f)
1121
1122 for s in root.s:
1123 self.assert_(isinstance(s, objectify.StringElement))
1124 self.assertEquals("5", s)
1125
1126 for i in root.i:
1127 self.assert_(isinstance(i, objectify.IntElement))
1128 self.assertEquals(5, i)
1129
1130 for l in root.l:
1131 self.assert_(isinstance(l, objectify.IntElement))
1132 self.assertEquals(5, i)
1133
1134 self.assert_(isinstance(root.n, objectify.NoneElement))
1135 self.assertEquals(None, root.n)
1136
1138 XML = self.XML
1139 root = XML('''\
1140 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1141 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1142 <b xsi:type="xsd:boolean">true</b>
1143 <b xsi:type="xsd:boolean">false</b>
1144 <b xsi:type="xsd:boolean">1</b>
1145 <b xsi:type="xsd:boolean">0</b>
1146
1147 <f xsi:type="xsd:float">5</f>
1148 <f xsi:type="xsd:double">5</f>
1149
1150 <s xsi:type="xsd:string">5</s>
1151 <s xsi:type="xsd:normalizedString">5</s>
1152 <s xsi:type="xsd:token">5</s>
1153 <s xsi:type="xsd:language">5</s>
1154 <s xsi:type="xsd:Name">5</s>
1155 <s xsi:type="xsd:NCName">5</s>
1156 <s xsi:type="xsd:ID">5</s>
1157 <s xsi:type="xsd:IDREF">5</s>
1158 <s xsi:type="xsd:ENTITY">5</s>
1159 <s xsi:type="xsd:NMTOKEN">5</s>
1160
1161 <l xsi:type="xsd:integer">5</l>
1162 <l xsi:type="xsd:nonPositiveInteger">5</l>
1163 <l xsi:type="xsd:negativeInteger">5</l>
1164 <l xsi:type="xsd:long">5</l>
1165 <l xsi:type="xsd:nonNegativeInteger">5</l>
1166 <l xsi:type="xsd:unsignedLong">5</l>
1167 <l xsi:type="xsd:unsignedInt">5</l>
1168 <l xsi:type="xsd:positiveInteger">5</l>
1169
1170 <i xsi:type="xsd:int">5</i>
1171 <i xsi:type="xsd:short">5</i>
1172 <i xsi:type="xsd:byte">5</i>
1173 <i xsi:type="xsd:unsignedShort">5</i>
1174 <i xsi:type="xsd:unsignedByte">5</i>
1175
1176 <n xsi:nil="true"/>
1177 </root>
1178 ''')
1179
1180 for b in root.b:
1181 self.assert_(isinstance(b, objectify.BoolElement))
1182 self.assertEquals(True, root.b[0])
1183 self.assertEquals(False, root.b[1])
1184 self.assertEquals(True, root.b[2])
1185 self.assertEquals(False, root.b[3])
1186
1187 for f in root.f:
1188 self.assert_(isinstance(f, objectify.FloatElement))
1189 self.assertEquals(5, f)
1190
1191 for s in root.s:
1192 self.assert_(isinstance(s, objectify.StringElement))
1193 self.assertEquals("5", s)
1194
1195 for i in root.i:
1196 self.assert_(isinstance(i, objectify.IntElement))
1197 self.assertEquals(5, i)
1198
1199 for l in root.l:
1200 self.assert_(isinstance(l, objectify.IntElement))
1201 self.assertEquals(5, l)
1202
1203 self.assert_(isinstance(root.n, objectify.NoneElement))
1204 self.assertEquals(None, root.n)
1205
1207 XML = self.XML
1208 root = XML(_bytes('<root><b>why</b><b>try</b></root>'))
1209 strs = [ str(s) for s in root.b ]
1210 self.assertEquals(["why", "try"],
1211 strs)
1212
1214 XML = self.XML
1215 root = XML(_bytes('<root><b>test</b><b>taste</b><b></b><b/></root>'))
1216 self.assertFalse(root.b[0] < root.b[1])
1217 self.assertFalse(root.b[0] <= root.b[1])
1218 self.assertFalse(root.b[0] == root.b[1])
1219
1220 self.assert_(root.b[0] != root.b[1])
1221 self.assert_(root.b[0] >= root.b[1])
1222 self.assert_(root.b[0] > root.b[1])
1223
1224 self.assertEquals(root.b[0], "test")
1225 self.assertEquals("test", root.b[0])
1226
1227 self.assertEquals("", root.b[2])
1228 self.assertEquals(root.b[2], "")
1229 self.assertEquals("", root.b[3])
1230 self.assertEquals(root.b[3], "")
1231 self.assertEquals(root.b[2], root.b[3])
1232
1233 root.b = "test"
1234 self.assert_(root.b)
1235 root.b = ""
1236 self.assertFalse(root.b)
1237 self.assertEquals(root.b, "")
1238 self.assertEquals("", root.b)
1239
1241 XML = self.XML
1242 root = XML(_bytes('<root><b>5</b><b>6</b></root>'))
1243 self.assert_(root.b[0] < root.b[1])
1244 self.assert_(root.b[0] <= root.b[1])
1245 self.assert_(root.b[0] != root.b[1])
1246
1247 self.assertFalse(root.b[0] == root.b[1])
1248 self.assertFalse(root.b[0] >= root.b[1])
1249 self.assertFalse(root.b[0] > root.b[1])
1250
1251 self.assertEquals(root.b[0], 5)
1252 self.assertEquals(5, root.b[0])
1253 self.assertNotEquals(root.b[0], "5")
1254
1255 root.b = 5
1256 self.assert_(root.b)
1257 root.b = 0
1258 self.assertFalse(root.b)
1259
1260 # float + long share the NumberElement implementation with int
1261
1263 XML = self.XML
1264 root = XML(_bytes('<root><b>false</b><b>true</b></root>'))
1265 self.assert_(root.b[0] < root.b[1])
1266 self.assert_(root.b[0] <= root.b[1])
1267 self.assert_(root.b[0] != root.b[1])
1268
1269 self.assertFalse(root.b[0] == root.b[1])
1270 self.assertFalse(root.b[0] >= root.b[1])
1271 self.assertFalse(root.b[0] > root.b[1])
1272
1273 self.assertFalse(root.b[0])
1274 self.assert_(root.b[1])
1275
1276 self.assertEquals(root.b[0], False)
1277 self.assertEquals(False, root.b[0])
1278 self.assert_(root.b[0] < 5)
1279 self.assert_(5 > root.b[0])
1280
1281 root.b = True
1282 self.assert_(root.b)
1283 root.b = False
1284 self.assertFalse(root.b)
1285
1287 XML = self.XML
1288 root = XML(_bytes("""
1289 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1290 <b xsi:nil="true"></b><b xsi:nil="true"/>
1291 </root>"""))
1292 self.assert_(root.b[0] == root.b[1])
1293 self.assertFalse(root.b[0])
1294 self.assertEquals(root.b[0], None)
1295 self.assertEquals(None, root.b[0])
1296
1297 # doesn't work in Py3:
1298
1299 #for comparison in ["abc", 5, 7.3, True, [], ()]:
1300 # none = root.b[1]
1301 # self.assert_(none < comparison, "%s (%s) should be < %s" %
1302 # (none, type(none), comparison) )
1303 # self.assert_(comparison > none, "%s should be > %s (%s)" %
1304 # (comparison, none, type(none)) )
1305
1307 el = objectify.DataElement(1, _xsi="string")
1308 self.assertEquals(
1309 el.get(XML_SCHEMA_INSTANCE_TYPE_ATTR),
1310 'xsd:string')
1311
1313 el = objectify.DataElement(1, _xsi="string",
1314 nsmap={'schema': XML_SCHEMA_NS})
1315 self.assertEquals(
1316 el.get(XML_SCHEMA_INSTANCE_TYPE_ATTR),
1317 'schema:string')
1318
1322
1324 XML = self.XML
1325 root = XML(_bytes('''\
1326 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1327 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1328 <b>5</b>
1329 <b>test</b>
1330 <c>1.1</c>
1331 <c>\uF8D2</c>
1332 <x>true</x>
1333 <n xsi:nil="true" />
1334 <n></n>
1335 <b xsi:type="double">5</b>
1336 <b xsi:type="float">5</b>
1337 <s xsi:type="string">23</s>
1338 <s py:pytype="str">42</s>
1339 <f py:pytype="float">300</f>
1340 <l py:pytype="long">2</l>
1341 <t py:pytype="TREE"></t>
1342 </a>
1343 '''))
1344 objectify.annotate(root)
1345
1346 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1347 for c in root.iterchildren() ]
1348 self.assertEquals("int", child_types[ 0])
1349 self.assertEquals("str", child_types[ 1])
1350 self.assertEquals("float", child_types[ 2])
1351 self.assertEquals("str", child_types[ 3])
1352 self.assertEquals("bool", child_types[ 4])
1353 self.assertEquals("NoneType", child_types[ 5])
1354 self.assertEquals(None, child_types[ 6])
1355 self.assertEquals("float", child_types[ 7])
1356 self.assertEquals("float", child_types[ 8])
1357 self.assertEquals("str", child_types[ 9])
1358 self.assertEquals("int", child_types[10])
1359 self.assertEquals("int", child_types[11])
1360 self.assertEquals("int", child_types[12])
1361 self.assertEquals(None, child_types[13])
1362
1363 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1364
1366 XML = self.XML
1367 root = XML(_bytes('''\
1368 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1369 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1370 <n></n>
1371 </a>
1372 '''))
1373 objectify.annotate(root)
1374
1375 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1376 for c in root.iterchildren() ]
1377 self.assertEquals(None, child_types[0])
1378
1379 objectify.annotate(root, empty_pytype="str")
1380
1381 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1382 for c in root.iterchildren() ]
1383 self.assertEquals("str", child_types[0])
1384
1386 XML = self.XML
1387 root = XML(_bytes('''\
1388 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1389 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1390 <b>5</b>
1391 <b>test</b>
1392 <c>1.1</c>
1393 <c>\uF8D2</c>
1394 <x>true</x>
1395 <n xsi:nil="true" />
1396 <n></n>
1397 <b xsi:type="double">5</b>
1398 <b xsi:type="float">5</b>
1399 <s xsi:type="string">23</s>
1400 <s py:pytype="str">42</s>
1401 <f py:pytype="float">300</f>
1402 <l py:pytype="long">2</l>
1403 <t py:pytype="TREE"></t>
1404 </a>
1405 '''))
1406 objectify.annotate(root, ignore_old=False)
1407
1408 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1409 for c in root.iterchildren() ]
1410 self.assertEquals("int", child_types[ 0])
1411 self.assertEquals("str", child_types[ 1])
1412 self.assertEquals("float", child_types[ 2])
1413 self.assertEquals("str", child_types[ 3])
1414 self.assertEquals("bool", child_types[ 4])
1415 self.assertEquals("NoneType", child_types[ 5])
1416 self.assertEquals(None, child_types[ 6])
1417 self.assertEquals("float", child_types[ 7])
1418 self.assertEquals("float", child_types[ 8])
1419 self.assertEquals("str", child_types[ 9])
1420 self.assertEquals("str", child_types[10])
1421 self.assertEquals("float", child_types[11])
1422 self.assertEquals("int", child_types[12])
1423 self.assertEquals(TREE_PYTYPE, child_types[13])
1424
1425 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1426
1428 XML = self.XML
1429 root = XML(_bytes('''\
1430 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1431 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1432 <b>5</b>
1433 <b>test</b>
1434 <c>1.1</c>
1435 <c>\uF8D2</c>
1436 <x>true</x>
1437 <n xsi:nil="true" />
1438 <n></n>
1439 <b xsi:type="double">5</b>
1440 <b xsi:type="float">5</b>
1441 <s xsi:type="string">23</s>
1442 <s py:pytype="str">42</s>
1443 <f py:pytype="float">300</f>
1444 <l py:pytype="long">2</l>
1445 <t py:pytype="TREE"></t>
1446 </a>
1447 '''))
1448 objectify.annotate(root, ignore_old=False, ignore_xsi=False,
1449 annotate_xsi=1, annotate_pytype=1)
1450
1451 # check py annotations
1452 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1453 for c in root.iterchildren() ]
1454 self.assertEquals("int", child_types[ 0])
1455 self.assertEquals("str", child_types[ 1])
1456 self.assertEquals("float", child_types[ 2])
1457 self.assertEquals("str", child_types[ 3])
1458 self.assertEquals("bool", child_types[ 4])
1459 self.assertEquals("NoneType", child_types[ 5])
1460 self.assertEquals(None, child_types[ 6])
1461 self.assertEquals("float", child_types[ 7])
1462 self.assertEquals("float", child_types[ 8])
1463 self.assertEquals("str", child_types[ 9])
1464 self.assertEquals("str", child_types[10])
1465 self.assertEquals("float", child_types[11])
1466 self.assertEquals("int", child_types[12])
1467 self.assertEquals(TREE_PYTYPE, child_types[13])
1468
1469 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1470
1471 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1472 for c in root.iterchildren() ]
1473
1474 # check xsi annotations
1475 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1476 for c in root.iterchildren() ]
1477 self.assertEquals("xsd:integer", child_types[ 0])
1478 self.assertEquals("xsd:string", child_types[ 1])
1479 self.assertEquals("xsd:double", child_types[ 2])
1480 self.assertEquals("xsd:string", child_types[ 3])
1481 self.assertEquals("xsd:boolean", child_types[ 4])
1482 self.assertEquals(None, child_types[ 5])
1483 self.assertEquals(None, child_types[ 6])
1484 self.assertEquals("xsd:double", child_types[ 7])
1485 self.assertEquals("xsd:float", child_types[ 8])
1486 self.assertEquals("xsd:string", child_types[ 9])
1487 self.assertEquals("xsd:string", child_types[10])
1488 self.assertEquals("xsd:double", child_types[11])
1489 self.assertEquals("xsd:integer", child_types[12])
1490 self.assertEquals(None, child_types[13])
1491
1492 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1493
1495 XML = self.XML
1496 root = XML(_bytes('''\
1497 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1498 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1499 <b>5</b>
1500 <b>test</b>
1501 <c>1.1</c>
1502 <c>\uF8D2</c>
1503 <x>true</x>
1504 <n xsi:nil="true" />
1505 <n></n>
1506 <b xsi:type="double">5</b>
1507 <b xsi:type="float">5</b>
1508 <s xsi:type="string">23</s>
1509 <s py:pytype="str">42</s>
1510 <f py:pytype="float">300</f>
1511 <l py:pytype="long">2</l>
1512 <t py:pytype="TREE"></t>
1513 </a>
1514 '''))
1515 objectify.xsiannotate(root, ignore_old=False)
1516
1517 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1518 for c in root.iterchildren() ]
1519 self.assertEquals("xsd:integer", child_types[ 0])
1520 self.assertEquals("xsd:string", child_types[ 1])
1521 self.assertEquals("xsd:double", child_types[ 2])
1522 self.assertEquals("xsd:string", child_types[ 3])
1523 self.assertEquals("xsd:boolean", child_types[ 4])
1524 self.assertEquals(None, child_types[ 5])
1525 self.assertEquals(None, child_types[ 6])
1526 self.assertEquals("xsd:double", child_types[ 7])
1527 self.assertEquals("xsd:float", child_types[ 8])
1528 self.assertEquals("xsd:string", child_types[ 9])
1529 self.assertEquals("xsd:string", child_types[10])
1530 self.assertEquals("xsd:double", child_types[11])
1531 self.assertEquals("xsd:integer", child_types[12])
1532 self.assertEquals(None, child_types[13])
1533
1535 XML = self.XML
1536 root = XML(_bytes('''\
1537 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1538 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1539 <b>5</b>
1540 <b>test</b>
1541 <c>1.1</c>
1542 <c>\uF8D2</c>
1543 <x>true</x>
1544 <n xsi:nil="true" />
1545 <n></n>
1546 <b xsi:type="double">5</b>
1547 <b xsi:type="float">5</b>
1548 <s xsi:type="string">23</s>
1549 <s py:pytype="str">42</s>
1550 <f py:pytype="float">300</f>
1551 <l py:pytype="long">2</l>
1552 <t py:pytype="TREE"></t>
1553 </a>
1554 '''))
1555 objectify.pyannotate(root, ignore_old=True)
1556
1557 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1558 for c in root.iterchildren() ]
1559 self.assertEquals("int", child_types[ 0])
1560 self.assertEquals("str", child_types[ 1])
1561 self.assertEquals("float", child_types[ 2])
1562 self.assertEquals("str", child_types[ 3])
1563 self.assertEquals("bool", child_types[ 4])
1564 self.assertEquals("NoneType", child_types[ 5])
1565 self.assertEquals(None, child_types[ 6])
1566 self.assertEquals("float", child_types[ 7])
1567 self.assertEquals("float", child_types[ 8])
1568 self.assertEquals("str", child_types[ 9])
1569 self.assertEquals("int", child_types[10])
1570 self.assertEquals("int", child_types[11])
1571 self.assertEquals("int", child_types[12])
1572 self.assertEquals(None, child_types[13])
1573
1574 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1575
1577 XML = self.XML
1578 root = XML('''\
1579 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1580 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1581 <n></n>
1582 </a>
1583 ''')
1584 objectify.pyannotate(root)
1585
1586 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1587 for c in root.iterchildren() ]
1588 self.assertEquals(None, child_types[0])
1589
1590 objectify.annotate(root, empty_pytype="str")
1591
1592 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1593 for c in root.iterchildren() ]
1594 self.assertEquals("str", child_types[0])
1595
1597 XML = self.XML
1598 root = XML('''\
1599 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1600 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1601 <b>5</b>
1602 <b>test</b>
1603 <c>1.1</c>
1604 <c>\uF8D2</c>
1605 <x>true</x>
1606 <n xsi:nil="true" />
1607 <n></n>
1608 <b xsi:type="double">5</b>
1609 <b xsi:type="float">5</b>
1610 <s xsi:type="string">23</s>
1611 <s py:pytype="str">42</s>
1612 <f py:pytype="float">300</f>
1613 <l py:pytype="long">2</l>
1614 <t py:pytype="TREE"></t>
1615 </a>
1616 ''')
1617 objectify.pyannotate(root)
1618
1619 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1620 for c in root.iterchildren() ]
1621 self.assertEquals("int", child_types[ 0])
1622 self.assertEquals("str", child_types[ 1])
1623 self.assertEquals("float", child_types[ 2])
1624 self.assertEquals("str", child_types[ 3])
1625 self.assertEquals("bool", child_types[ 4])
1626 self.assertEquals("NoneType", child_types[ 5])
1627 self.assertEquals(None, child_types[ 6])
1628 self.assertEquals("float", child_types[ 7])
1629 self.assertEquals("float", child_types[ 8])
1630 self.assertEquals("str", child_types[ 9])
1631 self.assertEquals("str", child_types[10])
1632 self.assertEquals("float", child_types[11])
1633 self.assertEquals("int", child_types[12])
1634 self.assertEquals(TREE_PYTYPE, child_types[13])
1635
1636 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1637
1639 XML = self.XML
1640 root = XML(_bytes('''\
1641 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1642 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1643 <b>5</b>
1644 <b>test</b>
1645 <c>1.1</c>
1646 <c>\uF8D2</c>
1647 <x>true</x>
1648 <n xsi:nil="true" />
1649 <n></n>
1650 <b xsi:type="double">5</b>
1651 <b xsi:type="float">5</b>
1652 <s xsi:type="string">23</s>
1653 <s py:pytype="str">42</s>
1654 <f py:pytype="float">300</f>
1655 <l py:pytype="long">2</l>
1656 <t py:pytype="TREE"></t>
1657 </a>
1658 '''))
1659 objectify.xsiannotate(root, ignore_old=True)
1660
1661 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1662 for c in root.iterchildren() ]
1663 self.assertEquals("xsd:integer", child_types[ 0])
1664 self.assertEquals("xsd:string", child_types[ 1])
1665 self.assertEquals("xsd:double", child_types[ 2])
1666 self.assertEquals("xsd:string", child_types[ 3])
1667 self.assertEquals("xsd:boolean", child_types[ 4])
1668 self.assertEquals(None, child_types[ 5])
1669 self.assertEquals(None, child_types[ 6])
1670 self.assertEquals("xsd:integer", child_types[ 7])
1671 self.assertEquals("xsd:integer", child_types[ 8])
1672 self.assertEquals("xsd:integer", child_types[ 9])
1673 self.assertEquals("xsd:string", child_types[10])
1674 self.assertEquals("xsd:double", child_types[11])
1675 self.assertEquals("xsd:integer", child_types[12])
1676 self.assertEquals(None, child_types[13])
1677
1678 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1679
1681 XML = self.XML
1682 root = XML(_bytes('''\
1683 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1684 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1685 <b>5</b>
1686 <b>test</b>
1687 <c>1.1</c>
1688 <c>\uF8D2</c>
1689 <x>true</x>
1690 <n xsi:nil="true" />
1691 <n></n>
1692 <b xsi:type="double">5</b>
1693 <b xsi:type="float">5</b>
1694 <s xsi:type="string">23</s>
1695 <s py:pytype="str">42</s>
1696 <f py:pytype="float">300</f>
1697 <l py:pytype="long">2</l>
1698 <t py:pytype="TREE"></t>
1699 </a>
1700 '''))
1701 objectify.deannotate(root)
1702
1703 for c in root.getiterator():
1704 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1705 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1706
1707 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1708
1710 XML = self.XML
1711 root = XML(_bytes('''\
1712 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1713 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1714 <b>5</b>
1715 <b>test</b>
1716 <c>1.1</c>
1717 <c>\uF8D2</c>
1718 <x>true</x>
1719 <n xsi:nil="true" />
1720 <n></n>
1721 <b xsi:type="double">5</b>
1722 <b xsi:type="float">5</b>
1723 <s xsi:type="string">23</s>
1724 <s py:pytype="str">42</s>
1725 <f py:pytype="float">300</f>
1726 <l py:pytype="long">2</l>
1727 <t py:pytype="TREE"></t>
1728 </a>
1729 '''))
1730 objectify.annotate(
1731 root, ignore_old=False, ignore_xsi=False, annotate_xsi=True,
1732 empty_pytype='str', empty_type='string')
1733 objectify.deannotate(root, pytype=False, xsi=False, xsi_nil=True)
1734
1735 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1736 for c in root.iterchildren() ]
1737 self.assertEquals("xsd:integer", child_types[ 0])
1738 self.assertEquals("xsd:string", child_types[ 1])
1739 self.assertEquals("xsd:double", child_types[ 2])
1740 self.assertEquals("xsd:string", child_types[ 3])
1741 self.assertEquals("xsd:boolean", child_types[ 4])
1742 self.assertEquals(None, child_types[ 5])
1743 self.assertEquals("xsd:string", child_types[ 6])
1744 self.assertEquals("xsd:double", child_types[ 7])
1745 self.assertEquals("xsd:float", child_types[ 8])
1746 self.assertEquals("xsd:string", child_types[ 9])
1747 self.assertEquals("xsd:string", child_types[10])
1748 self.assertEquals("xsd:double", child_types[11])
1749 self.assertEquals("xsd:integer", child_types[12])
1750 self.assertEquals(None, child_types[13])
1751
1752 self.assertEquals(None, root.n.get(XML_SCHEMA_NIL_ATTR))
1753
1754 for c in root.iterchildren():
1755 self.assertNotEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1756 # these have no equivalent in xsi:type
1757 if (c.get(objectify.PYTYPE_ATTRIBUTE) not in [TREE_PYTYPE,
1758 "NoneType"]):
1759 self.assertNotEquals(
1760 None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1761
1763 XML = self.XML
1764 root = XML(_bytes('''\
1765 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1766 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1767 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1768 <b>5</b>
1769 <b>test</b>
1770 <c>1.1</c>
1771 <c>\uF8D2</c>
1772 <x>true</x>
1773 <n xsi:nil="true" />
1774 <n></n>
1775 <b xsi:type="xsd:double">5</b>
1776 <b xsi:type="xsd:float">5</b>
1777 <s xsi:type="xsd:string">23</s>
1778 <s py:pytype="str">42</s>
1779 <f py:pytype="float">300</f>
1780 <l py:pytype="long">2</l>
1781 <t py:pytype="TREE"></t>
1782 </a>
1783 '''))
1784 objectify.annotate(root)
1785 objectify.deannotate(root, pytype=False)
1786
1787 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1788 for c in root.iterchildren() ]
1789 self.assertEquals("int", child_types[ 0])
1790 self.assertEquals("str", child_types[ 1])
1791 self.assertEquals("float", child_types[ 2])
1792 self.assertEquals("str", child_types[ 3])
1793 self.assertEquals("bool", child_types[ 4])
1794 self.assertEquals("NoneType", child_types[ 5])
1795 self.assertEquals(None, child_types[ 6])
1796 self.assertEquals("float", child_types[ 7])
1797 self.assertEquals("float", child_types[ 8])
1798 self.assertEquals("str", child_types[ 9])
1799 self.assertEquals("int", child_types[10])
1800 self.assertEquals("int", child_types[11])
1801 self.assertEquals("int", child_types[12])
1802 self.assertEquals(None, child_types[13])
1803
1804 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1805
1806 for c in root.getiterator():
1807 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1808
1810 XML = self.XML
1811 root = XML(_bytes('''\
1812 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1813 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1814 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1815 <b xsi:type="xsd:int">5</b>
1816 <b xsi:type="xsd:string">test</b>
1817 <c xsi:type="xsd:float">1.1</c>
1818 <c xsi:type="xsd:string">\uF8D2</c>
1819 <x xsi:type="xsd:boolean">true</x>
1820 <n xsi:nil="true" />
1821 <n></n>
1822 <b xsi:type="xsd:double">5</b>
1823 <b xsi:type="xsd:float">5</b>
1824 <s xsi:type="xsd:string">23</s>
1825 <s xsi:type="xsd:string">42</s>
1826 <f xsi:type="xsd:float">300</f>
1827 <l xsi:type="xsd:long">2</l>
1828 <t py:pytype="TREE"></t>
1829 </a>
1830 '''))
1831 objectify.annotate(root)
1832 objectify.deannotate(root, xsi=False)
1833
1834 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1835 for c in root.iterchildren() ]
1836 self.assertEquals("xsd:int", child_types[ 0])
1837 self.assertEquals("xsd:string", child_types[ 1])
1838 self.assertEquals("xsd:float", child_types[ 2])
1839 self.assertEquals("xsd:string", child_types[ 3])
1840 self.assertEquals("xsd:boolean", child_types[ 4])
1841 self.assertEquals(None, child_types[ 5])
1842 self.assertEquals(None, child_types[ 6])
1843 self.assertEquals("xsd:double", child_types[ 7])
1844 self.assertEquals("xsd:float", child_types[ 8])
1845 self.assertEquals("xsd:string", child_types[ 9])
1846 self.assertEquals("xsd:string", child_types[10])
1847 self.assertEquals("xsd:float", child_types[11])
1848 self.assertEquals("xsd:long", child_types[12])
1849 self.assertEquals(None, child_types[13])
1850
1851 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1852
1853 for c in root.getiterator():
1854 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1855
1857 XML = self.XML
1858
1859 xml = _bytes('''\
1860 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1861 <b>5</b>
1862 <b>test</b>
1863 <c>1.1</c>
1864 <c>\uF8D2</c>
1865 <x>true</x>
1866 <n xsi:nil="true" />
1867 <n></n>
1868 <b xsi:type="double">5</b>
1869 </a>
1870 ''')
1871
1872 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1873 objectify.set_pytype_attribute_tag("{TEST}test")
1874
1875 root = XML(xml)
1876 objectify.annotate(root)
1877
1878 attribs = root.xpath("//@py:%s" % pytype_name,
1879 namespaces={"py" : pytype_ns})
1880 self.assertEquals(0, len(attribs))
1881 attribs = root.xpath("//@py:test",
1882 namespaces={"py" : "TEST"})
1883 self.assertEquals(7, len(attribs))
1884
1885 objectify.set_pytype_attribute_tag()
1886 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1887
1888 self.assertNotEqual("test", pytype_ns.lower())
1889 self.assertNotEqual("test", pytype_name.lower())
1890
1891 root = XML(xml)
1892 attribs = root.xpath("//@py:%s" % pytype_name,
1893 namespaces={"py" : pytype_ns})
1894 self.assertEquals(0, len(attribs))
1895
1896 objectify.annotate(root)
1897 attribs = root.xpath("//@py:%s" % pytype_name,
1898 namespaces={"py" : pytype_ns})
1899 self.assertEquals(7, len(attribs))
1900
1902 orig_types = objectify.getRegisteredTypes()
1903 orig_types[0].unregister()
1904 self.assertEquals(orig_types[1:], objectify.getRegisteredTypes())
1905
1906 class NewType(objectify.ObjectifiedDataElement):
1907 pass
1908
1909 def checkMyType(s):
1910 return True
1911
1912 pytype = objectify.PyType("mytype", checkMyType, NewType)
1913 self.assert_(pytype not in objectify.getRegisteredTypes())
1914 pytype.register()
1915 self.assert_(pytype in objectify.getRegisteredTypes())
1916 pytype.unregister()
1917 self.assert_(pytype not in objectify.getRegisteredTypes())
1918
1919 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1920 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1921 pytype.unregister()
1922
1923 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1924 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1925 pytype.unregister()
1926
1927 self.assertRaises(ValueError, pytype.register,
1928 before = [objectify.getRegisteredTypes()[0].name],
1929 after = [objectify.getRegisteredTypes()[1].name])
1930
1932 from datetime import datetime
1933 def parse_date(value):
1934 if len(value) != 14:
1935 raise ValueError(value)
1936 Y = int(value[0:4])
1937 M = int(value[4:6])
1938 D = int(value[6:8])
1939 h = int(value[8:10])
1940 m = int(value[10:12])
1941 s = int(value[12:14])
1942 return datetime(Y, M, D, h, m, s)
1943
1944 def stringify_date(date):
1945 return date.strftime("%Y%m%d%H%M%S")
1946
1947 class DatetimeElement(objectify.ObjectifiedDataElement):
1948 def pyval(self):
1949 return parse_date(self.text)
1950 pyval = property(pyval)
1951
1952 datetime_type = objectify.PyType(
1953 "datetime", parse_date, DatetimeElement, stringify_date)
1954 datetime_type.xmlSchemaTypes = "dateTime"
1955 datetime_type.register()
1956
1957 NAMESPACE = "http://foo.net/xmlns"
1958 NAMESPACE_MAP = {'ns': NAMESPACE}
1959
1960 r = objectify.Element("{%s}root" % NAMESPACE, nsmap=NAMESPACE_MAP)
1961 time = datetime.now()
1962 r.date = time
1963
1964 self.assert_(isinstance(r.date, DatetimeElement))
1965 self.assert_(isinstance(r.date.pyval, datetime))
1966
1967 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1968 self.assertEquals(r.date.text, stringify_date(time))
1969
1970 r.date = objectify.E.date(time)
1971
1972 self.assert_(isinstance(r.date, DatetimeElement))
1973 self.assert_(isinstance(r.date.pyval, datetime))
1974
1975 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1976 self.assertEquals(r.date.text, stringify_date(time))
1977
1978 date = objectify.DataElement(time)
1979
1980 self.assert_(isinstance(date, DatetimeElement))
1981 self.assert_(isinstance(date.pyval, datetime))
1982
1983 self.assertEquals(date.pyval, parse_date(stringify_date(time)))
1984 self.assertEquals(date.text, stringify_date(time))
1985
1987 root = self.XML(xml_str)
1988 path = objectify.ObjectPath( "root.c1.c2" )
1989 self.assertEquals(root.c1.c2.text, path.find(root).text)
1990 self.assertEquals(root.c1.c2.text, path(root).text)
1991
1993 root = self.XML(xml_str)
1994 path = objectify.ObjectPath( ['root', 'c1', 'c2'] )
1995 self.assertEquals(root.c1.c2.text, path.find(root).text)
1996 self.assertEquals(root.c1.c2.text, path(root).text)
1997
1999 root = self.XML(xml_str)
2000 path = objectify.ObjectPath( "root.c1.c99" )
2001 self.assertRaises(AttributeError, path, root)
2002
2004 root = self.XML(xml_str)
2005 path = objectify.ObjectPath( "root.c1.c99" )
2006 self.assertEquals(None, path(root, None))
2007 path = objectify.ObjectPath( "root.c99.c2" )
2008 self.assertEquals(None, path(root, None))
2009 path = objectify.ObjectPath( "notroot.c99.c2" )
2010 self.assertEquals(None, path(root, None))
2011
2013 root = self.XML(xml_str)
2014 path = objectify.ObjectPath( ".c1.c99" )
2015 self.assertEquals(None, path(root, None))
2016 path = objectify.ObjectPath( ".c99.c2" )
2017 self.assertEquals(None, path(root, None))
2018
2020 root = self.XML(xml_str)
2021 path = objectify.ObjectPath("root . {objectified}c1. c2")
2022 self.assertEquals(root.c1.c2.text, path(root).text)
2023
2024 path = objectify.ObjectPath(" root.{objectified} c1.c2 [ 0 ] ")
2025 self.assertEquals(root.c1.c2.text, path(root).text)
2026
2029
2032
2034 root = self.XML(xml_str)
2035 path = objectify.ObjectPath( "root" )
2036 self.assert_(path.hasattr(root))
2037 path = objectify.ObjectPath( "root.c1" )
2038 self.assert_(path.hasattr(root))
2039 path = objectify.ObjectPath( "root.c1.c2" )
2040 self.assert_(path.hasattr(root))
2041 path = objectify.ObjectPath( "root.c1.{otherNS}c2" )
2042 self.assert_(path.hasattr(root))
2043 path = objectify.ObjectPath( "root.c1.c2[1]" )
2044 self.assert_(path.hasattr(root))
2045 path = objectify.ObjectPath( "root.c1.c2[2]" )
2046 self.assert_(path.hasattr(root))
2047 path = objectify.ObjectPath( "root.c1.c2[3]" )
2048 self.assertFalse(path.hasattr(root))
2049 path = objectify.ObjectPath( "root.c1[1].c2" )
2050 self.assertFalse(path.hasattr(root))
2051
2053 root = self.XML(xml_str)
2054 path = objectify.ObjectPath( "." )
2055 self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
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( ".c1.c2" )
2065 self.assertEquals(root.c1.c2.text, path(root).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( "root.c1[0].c2[0]" )
2075 self.assertEquals(root.c1.c2.text, path(root).text)
2076
2077 path = objectify.ObjectPath( "root.c1[0].c2" )
2078 self.assertEquals(root.c1.c2.text, path(root).text)
2079
2080 path = objectify.ObjectPath( "root.c1[0].c2[1]" )
2081 self.assertEquals(root.c1.c2[1].text, path(root).text)
2082
2083 path = objectify.ObjectPath( "root.c1.c2[2]" )
2084 self.assertEquals(root.c1.c2[2].text, path(root).text)
2085
2086 path = objectify.ObjectPath( "root.c1.c2[-1]" )
2087 self.assertEquals(root.c1.c2[-1].text, path(root).text)
2088
2089 path = objectify.ObjectPath( "root.c1.c2[-3]" )
2090 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2091
2093 root = self.XML(xml_str)
2094 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
2095 self.assertEquals(root.c1.c2.text, path(root).text)
2096
2097 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
2098 self.assertEquals(root.c1.c2[2].text, path(root).text)
2099
2100 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
2101 self.assertEquals(root.c1.c2[2].text, path(root).text)
2102
2103 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
2104 self.assertEquals(root.c1.c2[-1].text, path(root).text)
2105
2106 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
2107 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2108
2110 self.assertRaises(ValueError, objectify.ObjectPath,
2111 "root.c1[0].c2[-1-2]")
2112 self.assertRaises(ValueError, objectify.ObjectPath,
2113 ['root', 'c1[0]', 'c2[-1-2]'])
2114
2115 self.assertRaises(ValueError, objectify.ObjectPath,
2116 "root[2].c1.c2")
2117 self.assertRaises(ValueError, objectify.ObjectPath,
2118 ['root[2]', 'c1', 'c2'])
2119
2120 self.assertRaises(ValueError, objectify.ObjectPath,
2121 [])
2122 self.assertRaises(ValueError, objectify.ObjectPath,
2123 ['', '', ''])
2124
2126 root = self.XML(xml_str)
2127 path = objectify.ObjectPath("root.c1[9999].c2")
2128 self.assertRaises(AttributeError, path, root)
2129
2130 path = objectify.ObjectPath("root.c1[0].c2[9999]")
2131 self.assertRaises(AttributeError, path, root)
2132
2133 path = objectify.ObjectPath(".c1[9999].c2[0]")
2134 self.assertRaises(AttributeError, path, root)
2135
2136 path = objectify.ObjectPath("root.c1[-2].c2")
2137 self.assertRaises(AttributeError, path, root)
2138
2139 path = objectify.ObjectPath("root.c1[0].c2[-4]")
2140 self.assertRaises(AttributeError, path, root)
2141
2143 root = self.XML(xml_str)
2144 path = objectify.ObjectPath( "{objectified}root.c1.c2" )
2145 self.assertEquals(root.c1.c2.text, path.find(root).text)
2146 path = objectify.ObjectPath( "{objectified}root.{objectified}c1.c2" )
2147 self.assertEquals(root.c1.c2.text, path.find(root).text)
2148 path = objectify.ObjectPath( "root.{objectified}c1.{objectified}c2" )
2149 self.assertEquals(root.c1.c2.text, path.find(root).text)
2150 path = objectify.ObjectPath( "root.c1.{objectified}c2" )
2151 self.assertEquals(root.c1.c2.text, path.find(root).text)
2152 path = objectify.ObjectPath( "root.c1.{otherNS}c2" )
2153 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2154 path.find(root).text)
2155
2157 root = self.XML(xml_str)
2158 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
2159 self.assertEquals(root.c1.c2.text, path.find(root).text)
2160 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
2161 self.assertEquals(root.c1.c2.text, path.find(root).text)
2162 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
2163 self.assertEquals(root.c1.c2.text, path.find(root).text)
2164 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
2165 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2166 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
2167 self.assertEquals(root.c1.c2.text, path.find(root).text)
2168 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
2169 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2170 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
2171 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2172 path.find(root).text)
2173
2175 root = self.XML(xml_str)
2176 path = objectify.ObjectPath( "root.c1.c2" )
2177 self.assertEquals(root.c1.c2.text, path.find(root).text)
2178 self.assertEquals("1", root.c1.c2[1].text)
2179
2180 new_value = "my new value"
2181 path.setattr(root, new_value)
2182
2183 self.assertEquals(new_value, root.c1.c2.text)
2184 self.assertEquals(new_value, path(root).text)
2185 self.assertEquals("1", root.c1.c2[1].text)
2186
2188 root = self.XML(xml_str)
2189 path = objectify.ObjectPath( "root.c1.c2" )
2190 self.assertEquals(root.c1.c2.text, path.find(root).text)
2191 self.assertEquals("1", root.c1.c2[1].text)
2192
2193 new_el = self.Element("{objectified}test")
2194 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST"
2195 path.setattr(root, new_el.sub)
2196
2197 self.assertEquals("ATTR", root.c1.c2.get("myattr"))
2198 self.assertEquals("TEST", root.c1.c2.a.text)
2199 self.assertEquals("TEST", path(root).a.text)
2200 self.assertEquals("1", root.c1.c2[1].text)
2201
2203 root = self.XML(xml_str)
2204 path = objectify.ObjectPath( "root.c1.c99" )
2205 self.assertRaises(AttributeError, path.find, root)
2206
2207 new_value = "my new value"
2208 path.setattr(root, new_value)
2209
2210 self.assertEquals(1, len(root.c1.c99))
2211 self.assertEquals(new_value, root.c1.c99.text)
2212 self.assertEquals(new_value, path(root).text)
2213
2215 root = self.XML(xml_str)
2216 path = objectify.ObjectPath( "root.c1.c99" )
2217 self.assertRaises(AttributeError, path.find, root)
2218
2219 new_el = self.Element("{objectified}test")
2220 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST"
2221 path.setattr(root, new_el.sub)
2222
2223 self.assertEquals(1, len(root.c1.c99))
2224 self.assertEquals("ATTR", root.c1.c99.get("myattr"))
2225 self.assertEquals("TEST", root.c1.c99.a.text)
2226 self.assertEquals("TEST", path(root).a.text)
2227
2229 root = self.XML(xml_str)
2230 path = objectify.ObjectPath( "root.c1.c99" )
2231 self.assertRaises(AttributeError, path.find, root)
2232
2233 new_el = self.Element("{objectified}test")
2234 new_el.a = ["TEST1", "TEST2"]
2235 new_el.a[0].set("myattr", "ATTR1")
2236 new_el.a[1].set("myattr", "ATTR2")
2237
2238 path.setattr(root, list(new_el.a))
2239
2240 self.assertEquals(2, len(root.c1.c99))
2241 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
2242 self.assertEquals("TEST1", root.c1.c99[0].text)
2243 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
2244 self.assertEquals("TEST2", root.c1.c99[1].text)
2245 self.assertEquals("TEST1", path(root).text)
2246
2248 root = self.XML(xml_str)
2249 path = objectify.ObjectPath( "root.c1.c2" )
2250 self.assertEquals(3, len(root.c1.c2))
2251 path.addattr(root, "test")
2252 self.assertEquals(4, len(root.c1.c2))
2253 self.assertEquals(["0", "1", "2", "test"],
2254 [el.text for el in root.c1.c2])
2255
2257 root = self.XML(xml_str)
2258 path = objectify.ObjectPath( "root.c1.c2" )
2259 self.assertEquals(3, len(root.c1.c2))
2260
2261 new_el = self.Element("{objectified}test")
2262 etree.SubElement(new_el, "{objectified}sub").a = "TEST"
2263
2264 path.addattr(root, new_el.sub)
2265 self.assertEquals(4, len(root.c1.c2))
2266 self.assertEquals("TEST", root.c1.c2[3].a.text)
2267 self.assertEquals(["0", "1", "2"],
2268 [el.text for el in root.c1.c2[:3]])
2269
2271 root = self.XML(xml_str)
2272 path = objectify.ObjectPath( "root.c1.c99" )
2273 self.assertRaises(AttributeError, path.find, root)
2274
2275 new_value = "my new value"
2276 path.addattr(root, new_value)
2277
2278 self.assertEquals(1, len(root.c1.c99))
2279 self.assertEquals(new_value, root.c1.c99.text)
2280 self.assertEquals(new_value, path(root).text)
2281
2283 root = self.XML(xml_str)
2284 path = objectify.ObjectPath( "root.c1.c99" )
2285 self.assertRaises(AttributeError, path.find, root)
2286
2287 new_el = self.Element("{objectified}test")
2288 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST"
2289
2290 path.addattr(root, new_el.sub)
2291 self.assertEquals(1, len(root.c1.c99))
2292 self.assertEquals("TEST", root.c1.c99.a.text)
2293 self.assertEquals("TEST", path(root).a.text)
2294 self.assertEquals("ATTR", root.c1.c99.get("myattr"))
2295
2297 root = self.XML(xml_str)
2298 path = objectify.ObjectPath( "root.c1.c99" )
2299 self.assertRaises(AttributeError, path.find, root)
2300
2301 new_el = self.Element("{objectified}test")
2302 new_el.a = ["TEST1", "TEST2"]
2303
2304 self.assertEquals(2, len(new_el.a))
2305
2306 path.addattr(root, list(new_el.a))
2307 self.assertEquals(2, len(root.c1.c99))
2308 self.assertEquals("TEST1", root.c1.c99.text)
2309 self.assertEquals("TEST2", path(root)[1].text)
2310
2312 root = self.XML(xml_str)
2313 self.assertEquals(
2314 ['{objectified}root', '{objectified}root.c1',
2315 '{objectified}root.c1.c2',
2316 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]',
2317 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'],
2318 root.descendantpaths())
2319
2321 root = self.XML(xml_str)
2322 self.assertEquals(
2323 ['{objectified}c1', '{objectified}c1.c2',
2324 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]',
2325 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'],
2326 root.c1.descendantpaths())
2327
2329 root = self.XML(xml_str)
2330 self.assertEquals(
2331 ['root.{objectified}c1', 'root.{objectified}c1.c2',
2332 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]',
2333 'root.{objectified}c1.{otherNS}c2',
2334 'root.{objectified}c1.{}c2'],
2335 root.c1.descendantpaths('root'))
2336
2338 import pickle
2339
2340 root = self.XML(xml_str)
2341 out = BytesIO()
2342 pickle.dump(root, out)
2343
2344 new_root = pickle.loads(out.getvalue())
2345 self.assertEquals(
2346 etree.tostring(new_root),
2347 etree.tostring(root))
2348
2350 import pickle
2351
2352 tree = etree.ElementTree(self.XML(xml_str + "<?my pi?>"))
2353 out = BytesIO()
2354 pickle.dump(tree, out)
2355
2356 new_tree = pickle.loads(out.getvalue())
2357 self.assert_(isinstance(new_tree, etree._ElementTree))
2358 self.assertEquals(
2359 etree.tostring(new_tree),
2360 etree.tostring(tree))
2361
2365
2369
2371 self._test_pickle('<x>Pickle me!</x>')
2372 self._test_pickle(objectify.DataElement('Pickle me!'))
2373
2375 self._test_pickle('<x>true</x>')
2376 self._test_pickle('<x>false</x>')
2377 self._test_pickle(objectify.DataElement(True))
2378 self._test_pickle(objectify.DataElement(False))
2379
2381 self._test_pickle('''
2382 <x xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>''')
2383 self._test_pickle(objectify.DataElement(None))
2384
2386 import pickle
2387 if isinstance(stringOrElt, (etree._Element, etree._ElementTree)):
2388 elt = stringOrElt
2389 else:
2390 elt = self.XML(stringOrElt)
2391 out = BytesIO()
2392 pickle.dump(elt, out)
2393
2394 new_elt = pickle.loads(out.getvalue())
2395 self.assertEquals(
2396 etree.tostring(new_elt),
2397 etree.tostring(elt))
2398
2399 # E-Factory tests, need to use sub-elements as root element is always
2400 # type-looked-up as ObjectifiedElement (no annotations)
2402 E = objectify.E
2403 root = E.root(E.val(23))
2404 self.assert_(isinstance(root.val, objectify.IntElement))
2405
2407 E = objectify.E
2408 root = E.root(E.val(233.23))
2409 self.assert_(isinstance(root.val, objectify.FloatElement))
2410
2412 E = objectify.E
2413 root = E.root(E.val("what?"))
2414 self.assert_(isinstance(root.val, objectify.StringElement))
2415
2417 E = objectify.E
2418 root = E.root(E.val(_str("blöödy häll", encoding="ISO-8859-1")))
2419 self.assert_(isinstance(root.val, objectify.StringElement))
2420
2422 E = objectify.E
2423 root = E.root(E.val(True))
2424 self.assert_(isinstance(root.val, objectify.BoolElement))
2425
2427 E = objectify.E
2428 root = E.root(E.val(None))
2429 self.assert_(isinstance(root.val, objectify.NoneElement))
2430
2432 E = objectify.E
2433 root = E.root(E.val(1, "foo", 2.0, "bar ", True, None))
2434 self.assert_(isinstance(root.val, objectify.StringElement))
2435
2440
2442 E = objectify.E
2443 DataElement = objectify.DataElement
2444 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1),
2445 DataElement(2.0))
2446 self.assert_(isinstance(root, objectify.ObjectifiedElement))
2447 self.assertEquals(root.text, "text")
2448 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement))
2449 self.assertEquals(root.sub.tail, "tail")
2450 self.assert_(isinstance(root.sub.subsub, objectify.StringElement))
2451 self.assertEquals(len(root.value), 2)
2452 self.assert_(isinstance(root.value[0], objectify.IntElement))
2453 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2454
2456 class Attribute(objectify.ObjectifiedDataElement):
2457 def __init__(self):
2458 objectify.ObjectifiedDataElement.__init__(self)
2459 self.set("datatype", "TYPE")
2460 self.set("range", "0.,1.")
2461
2462 attr = Attribute()
2463 self.assertEquals(attr.text, None)
2464 self.assertEquals(attr.get("datatype"), "TYPE")
2465 self.assertEquals(attr.get("range"), "0.,1.")
2466
2468 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2469 docinfo = root.getroottree().docinfo
2470 self.assertEquals(docinfo.URL, "http://no/such/url")
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 docinfo.URL = "https://secret/url"
2477 self.assertEquals(docinfo.URL, "https://secret/url")
2478
2480 tree = objectify.parse(BytesIO("<root/>"), base_url="http://no/such/url")
2481 docinfo = tree.docinfo
2482 self.assertEquals(docinfo.URL, "http://no/such/url")
2483
2485 tree = objectify.parse(fileInTestDir('include/test_xinclude.xml'),
2486 base_url="http://no/such/url")
2487 docinfo = tree.docinfo
2488 self.assertEquals(docinfo.URL, "http://no/such/url")
2489
2491 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2492 self.assertEquals(root.base, "http://no/such/url")
2493 self.assertEquals(
2494 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2495 root.base = "https://secret/url"
2496 self.assertEquals(root.base, "https://secret/url")
2497 self.assertEquals(
2498 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2499 "https://secret/url")
2500
2502 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2503 self.assertEquals(root.base, "http://no/such/url")
2504 self.assertEquals(
2505 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2506 root.set('{http://www.w3.org/XML/1998/namespace}base',
2507 "https://secret/url")
2508 self.assertEquals(root.base, "https://secret/url")
2509 self.assertEquals(
2510 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2511 "https://secret/url")
2512
2514 XML = self.XML
2515
2516 xml = _bytes('''\
2517 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2518 <i>5</i>
2519 <i>-5</i>
2520 <l>4294967296</l>
2521 <l>-4294967296</l>
2522 <f>1.1</f>
2523 <b>true</b>
2524 <b>false</b>
2525 <s>Strange things happen, where strings collide</s>
2526 <s>True</s>
2527 <s>False</s>
2528 <s>t</s>
2529 <s>f</s>
2530 <s></s>
2531 <s>None</s>
2532 <n xsi:nil="true" />
2533 </root>
2534 ''')
2535 root = XML(xml)
2536
2537 for i in root.i:
2538 self.assert_(isinstance(i, objectify.IntElement))
2539 for l in root.l:
2540 self.assert_(isinstance(l, objectify.IntElement))
2541 for f in root.f:
2542 self.assert_(isinstance(f, objectify.FloatElement))
2543 for b in root.b:
2544 self.assert_(isinstance(b, objectify.BoolElement))
2545 self.assertEquals(True, root.b[0])
2546 self.assertEquals(False, root.b[1])
2547 for s in root.s:
2548 self.assert_(isinstance(s, objectify.StringElement))
2549 self.assert_(isinstance(root.n, objectify.NoneElement))
2550 self.assertEquals(None, root.n)
2551
2553 suite = unittest.TestSuite()
2554 suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
2555 suite.addTests(doctest.DocTestSuite(objectify))
2556 if sys.version_info >= (2,4):
2557 suite.addTests(
2558 [make_doctest('../../../doc/objectify.txt')])
2559 return suite
2560
2561 if __name__ == '__main__':
2562 print('to test use test.py %s' % __file__)
2563
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Mon Sep 6 10:55:22 2010 | http://epydoc.sourceforge.net |