| 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
9
10 from common_imports import etree, StringIO, HelperTestCase, fileInTestDir
11 from common_imports import SillyFileLike, canonicalize, doctest
12
13 from lxml import objectify
14
15 PYTYPE_NAMESPACE = "http://codespeak.net/lxml/objectify/pytype"
16 XML_SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"
17 XML_SCHEMA_INSTANCE_NS = "http://www.w3.org/2001/XMLSchema-instance"
18 XML_SCHEMA_INSTANCE_TYPE_ATTR = "{%s}type" % XML_SCHEMA_INSTANCE_NS
19 XML_SCHEMA_NIL_ATTR = "{%s}nil" % XML_SCHEMA_INSTANCE_NS
20 TREE_PYTYPE = "TREE"
21 DEFAULT_NSMAP = { "py" : PYTYPE_NAMESPACE,
22 "xsi" : XML_SCHEMA_INSTANCE_NS,
23 "xsd" : XML_SCHEMA_NS}
24
25 objectclass2xsitype = {
26 # objectify built-in
27 objectify.IntElement: ("int", "short", "byte", "unsignedShort",
28 "unsignedByte",),
29 objectify.LongElement: ("integer", "nonPositiveInteger", "negativeInteger",
30 "long", "nonNegativeInteger", "unsignedLong",
31 "unsignedInt", "positiveInteger",),
32 objectify.FloatElement: ("float", "double"),
33 objectify.BoolElement: ("boolean",),
34 objectify.StringElement: ("string", "normalizedString", "token", "language",
35 "Name", "NCName", "ID", "IDREF", "ENTITY",
36 "NMTOKEN", ),
37 # None: xsi:nil="true"
38 }
39
40 xsitype2objclass = dict(( (v, k) for k in objectclass2xsitype
41 for v in objectclass2xsitype[k] ))
42
43 objectclass2pytype = {
44 # objectify built-in
45 objectify.IntElement: "int",
46 objectify.LongElement: "long",
47 objectify.FloatElement: "float",
48 objectify.BoolElement: "bool",
49 objectify.StringElement: "str",
50 # None: xsi:nil="true"
51 }
52
53 pytype2objclass = dict(( (objectclass2pytype[k], k) for k in objectclass2pytype))
54
55 xml_str = '''\
56 <obj:root xmlns:obj="objectified" xmlns:other="otherNS">
57 <obj:c1 a1="A1" a2="A2" other:a3="A3">
58 <obj:c2>0</obj:c2>
59 <obj:c2>1</obj:c2>
60 <obj:c2>2</obj:c2>
61 <other:c2>3</other:c2>
62 <c2>3</c2>
63 </obj:c1>
64 </obj:root>'''
65
67 """Test cases for lxml.objectify
68 """
69 etree = etree
70
73
75 super(ObjectifyTestCase, self).setUp()
76 self.parser = self.etree.XMLParser(remove_blank_text=True)
77 self.lookup = etree.ElementNamespaceClassLookup(
78 objectify.ObjectifyElementClassLookup() )
79 self.parser.setElementClassLookup(self.lookup)
80
81 self.Element = self.parser.makeelement
82
83 ns = self.lookup.get_namespace("otherNS")
84 ns[None] = self.etree.ElementBase
85
87 self.lookup.get_namespace("otherNS").clear()
88 objectify.setPytypeAttributeTag()
89 del self.lookup
90 del self.parser
91 super(ObjectifyTestCase, self).tearDown()
92
96
98 nsmap = {}
99 elt = objectify.Element("test", nsmap=nsmap)
100 self.assertEquals(elt.nsmap.values(), [PYTYPE_NAMESPACE])
101
103 nsmap = {"mypy": PYTYPE_NAMESPACE,
104 "myxsi": XML_SCHEMA_INSTANCE_NS,
105 "myxsd": XML_SCHEMA_NS}
106 elt = objectify.Element("test", nsmap=nsmap)
107 self.assertEquals(elt.nsmap, nsmap)
108
110 nsmap = {"my": "someNS",
111 "myother": "someOtherNS",
112 "myxsd": XML_SCHEMA_NS}
113 elt = objectify.Element("test", nsmap=nsmap)
114 self.assert_(PYTYPE_NAMESPACE in elt.nsmap.values())
115 for prefix, ns in nsmap.items():
116 self.assert_(prefix in elt.nsmap)
117 self.assertEquals(nsmap[prefix], elt.nsmap[prefix])
118
120 root = objectify.Element("root")
121 root.sub = objectify.Element("test")
122 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
123
125 root = objectify.Element("root")
126 nsmap = {}
127 root.sub = objectify.Element("test", nsmap=nsmap)
128 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
129
131 root = objectify.Element("root")
132 nsmap = {"mypy": PYTYPE_NAMESPACE,
133 "myxsi": XML_SCHEMA_INSTANCE_NS,
134 "myxsd": XML_SCHEMA_NS}
135 root.sub = objectify.Element("test", nsmap=nsmap)
136 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
137
139 root = objectify.Element("root")
140 nsmap = {"my": "someNS",
141 "myother": "someOtherNS",
142 "myxsd": XML_SCHEMA_NS,}
143 root.sub = objectify.Element("test", nsmap=nsmap)
144 expected = nsmap.copy()
145 del expected["myxsd"]
146 expected.update(DEFAULT_NSMAP)
147 self.assertEquals(root.sub.nsmap, expected)
148
152
154 nsmap = {}
155 value = objectify.DataElement("test this", nsmap=nsmap)
156 self.assertEquals(value.nsmap.values(), [PYTYPE_NAMESPACE])
157
159 nsmap = {"mypy": PYTYPE_NAMESPACE,
160 "myxsi": XML_SCHEMA_INSTANCE_NS,
161 "myxsd": XML_SCHEMA_NS}
162 value = objectify.DataElement("test this", nsmap=nsmap)
163 self.assertEquals(value.nsmap, nsmap)
164
166 nsmap = {"my": "someNS",
167 "myother": "someOtherNS",
168 "myxsd": XML_SCHEMA_NS,}
169 value = objectify.DataElement("test", nsmap=nsmap)
170 self.assert_(PYTYPE_NAMESPACE in value.nsmap.values())
171 for prefix, ns in nsmap.items():
172 self.assert_(prefix in value.nsmap)
173 self.assertEquals(nsmap[prefix], value.nsmap[prefix])
174
176 root = objectify.Element("root")
177 root.value = objectify.DataElement("test this")
178 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
179
181 root = objectify.Element("root")
182 nsmap = {}
183 root.value = objectify.DataElement("test this", nsmap=nsmap)
184 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
185
187 root = objectify.Element("root")
188 nsmap = {"mypy": PYTYPE_NAMESPACE,
189 "myxsi": XML_SCHEMA_INSTANCE_NS,
190 "myxsd": XML_SCHEMA_NS}
191 root.value = objectify.DataElement("test this", nsmap=nsmap)
192 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
193
195 root = objectify.Element("root")
196 nsmap = {"my": "someNS",
197 "myother": "someOtherNS",
198 "myxsd": XML_SCHEMA_NS}
199 root.value = objectify.DataElement("test", nsmap=nsmap)
200 expected = nsmap.copy()
201 del expected["myxsd"]
202 expected.update(DEFAULT_NSMAP)
203 self.assertEquals(root.value.nsmap, expected)
204
206 # keyword arguments override attrib entries
207 value = objectify.DataElement(23, _pytype="str", _xsi="foobar",
208 attrib={"gnu": "muh", "cat": "meeow",
209 "dog": "wuff"},
210 bird="tchilp", dog="grrr")
211 self.assertEquals(value.get("gnu"), "muh")
212 self.assertEquals(value.get("cat"), "meeow")
213 self.assertEquals(value.get("dog"), "grrr")
214 self.assertEquals(value.get("bird"), "tchilp")
215
217 # Check that DataElement preserves all attributes ObjectifiedDataElement
218 # arguments
219 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
220 attrib={"gnu": "muh", "cat": "meeow",
221 "dog": "wuff"},
222 bird="tchilp", dog="grrr")
223 value = objectify.DataElement(arg)
224 self.assert_(isinstance(value, objectify.StringElement))
225 for attr in arg.attrib:
226 self.assertEquals(value.get(attr), arg.get(attr))
227
229 # Check that _pytype arg overrides original py:pytype of
230 # ObjectifiedDataElement
231 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
232 attrib={"gnu": "muh", "cat": "meeow",
233 "dog": "wuff"},
234 bird="tchilp", dog="grrr")
235 value = objectify.DataElement(arg, _pytype="NoneType")
236 self.assert_(isinstance(value, objectify.NoneElement))
237 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
238 self.assertEquals(value.text, None)
239 self.assertEquals(value.pyval, None)
240 for attr in arg.attrib:
241 #if not attr == objectify.PYTYPE_ATTRIBUTE:
242 self.assertEquals(value.get(attr), arg.get(attr))
243
245 # Check that _pytype arg overrides original py:pytype of
246 # ObjectifiedDataElement
247 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
248 attrib={"gnu": "muh", "cat": "meeow",
249 "dog": "wuff"},
250 bird="tchilp", dog="grrr")
251 value = objectify.DataElement(arg, _pytype="int")
252 self.assert_(isinstance(value, objectify.IntElement))
253 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
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 _xsi arg overrides original xsi:type of given
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, _xsi="xsd:int")
266 self.assert_(isinstance(value, objectify.IntElement))
267 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
268 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
269 for attr in arg.attrib:
270 if not attr in [objectify.PYTYPE_ATTRIBUTE,
271 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
272 self.assertEquals(value.get(attr), arg.get(attr))
273
275 # Check that _pytype and _xsi args override original py:pytype and
276 # xsi:type attributes of given ObjectifiedDataElement
277 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
278 attrib={"gnu": "muh", "cat": "meeow",
279 "dog": "wuff"},
280 bird="tchilp", dog="grrr")
281 value = objectify.DataElement(arg, _pytype="int", _xsi="xsd:int")
282 self.assert_(isinstance(value, objectify.IntElement))
283 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
284 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
285 for attr in arg.attrib:
286 if not attr in [objectify.PYTYPE_ATTRIBUTE,
287 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
288 self.assertEquals(value.get(attr), arg.get(attr))
289
293
297
299 arg = objectify.DataElement(3.1415)
300 self.assertRaises(ValueError, objectify.DataElement, arg,
301 _pytype="int")
302
304 arg = objectify.DataElement(3.1415)
305 self.assertRaises(ValueError, objectify.DataElement, arg,
306 _xsi="xsd:int")
307
311
315
319
321 root = self.XML(xml_str)
322 self.assertEquals(1, root.countchildren())
323 self.assertEquals(5, root.c1.countchildren())
324
326 root = self.XML(xml_str)
327 self.assertEquals("0", getattr(root.c1, "{objectified}c2").text)
328 self.assertEquals("3", getattr(root.c1, "{otherNS}c2").text)
329
331 root = self.XML(xml_str)
332 self.assertRaises(AttributeError, getattr, root.c1, "NOT_THERE")
333 self.assertRaises(AttributeError, getattr, root.c1, "{unknownNS}c2")
334
336 root = self.XML(xml_str)
337 self.assertEquals(1, len(root.c1))
338 root.addattr("c1", "test")
339 self.assertEquals(2, len(root.c1))
340 self.assertEquals("test", root.c1[1].text)
341
343 root = self.XML(xml_str)
344 self.assertEquals(1, len(root.c1))
345
346 new_el = self.Element("test", myattr="5")
347 root.addattr("c1", new_el)
348 self.assertEquals(2, len(root.c1))
349 self.assertEquals(None, root.c1[0].get("myattr"))
350 self.assertEquals("5", root.c1[1].get("myattr"))
351
353 root = self.XML(xml_str)
354 self.assertEquals(1, len(root.c1))
355
356 new_el = self.Element("test")
357 self.etree.SubElement(new_el, "a", myattr="A")
358 self.etree.SubElement(new_el, "a", myattr="B")
359
360 root.addattr("c1", list(new_el.a))
361 self.assertEquals(3, len(root.c1))
362 self.assertEquals(None, root.c1[0].get("myattr"))
363 self.assertEquals("A", root.c1[1].get("myattr"))
364 self.assertEquals("B", root.c1[2].get("myattr"))
365
367 root = self.XML(xml_str)
368 self.assertEquals(3, len(root.c1.c2))
369 root.c1.addattr("c2", 3)
370 self.assertEquals(4, len(root.c1.c2))
371 self.assertEquals("3", root.c1.c2[3].text)
372
374 root = self.XML(xml_str)
375 self.assertEquals("0", root.c1.c2[0].text)
376 self.assertEquals("1", root.c1.c2[1].text)
377 self.assertEquals("2", root.c1.c2[2].text)
378 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
379
381 root = self.XML(xml_str)
382 self.assertEquals("0", root.c1.c2[0].text)
383 self.assertEquals("0", root.c1.c2[-3].text)
384 self.assertEquals("1", root.c1.c2[-2].text)
385 self.assertEquals("2", root.c1.c2[-1].text)
386 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
387
389 root = self.XML(xml_str)
390 self.assertEquals(1, len(root))
391 self.assertEquals(1, len(root.c1))
392 self.assertEquals(3, len(root.c1.c2))
393
395 root = self.XML(xml_str)
396 self.assertEquals([root],
397 list(iter(root)))
398 self.assertEquals([root.c1],
399 list(iter(root.c1)))
400 self.assertEquals([root.c1.c2[0], root.c1.c2[1], root.c1.c2[2]],
401 list(iter((root.c1.c2))))
402
404 root = self.XML(xml_str)
405 self.assert_(isinstance(root.c1.c2, objectify.ObjectifiedElement))
406 self.assertFalse(isinstance(getattr(root.c1, "{otherNS}c2"),
407 objectify.ObjectifiedElement))
408
410 root = self.XML(xml_str)
411 dir_c1 = dir(objectify.ObjectifiedElement) + ['c1']
412 dir_c1.sort()
413 dir_c2 = dir(objectify.ObjectifiedElement) + ['c2']
414 dir_c2.sort()
415
416 self.assertEquals(dir_c1, dir(root))
417 self.assertEquals(dir_c2, dir(root.c1))
418
420 root = self.XML(xml_str)
421 self.assertEquals({'c1' : root.c1}, vars(root))
422 self.assertEquals({'c2' : root.c1.c2}, vars(root.c1))
423
425 root = self.XML(xml_str)
426 self.assertRaises(TypeError, setattr, root.c1.c2, 'text', "test")
427 self.assertRaises(TypeError, setattr, root.c1.c2, 'pyval', "test")
428
430 Element = self.Element
431 SubElement = self.etree.SubElement
432 root = Element("root")
433 root.c = ["c1", "c2"]
434
435 c1 = root.c[0]
436 c2 = root.c[1]
437
438 self.assertEquals([c1,c2], list(root.c))
439 self.assertEquals(["c1", "c2"],
440 [ c.text for c in root.c ])
441
442 root2 = Element("root2")
443 root2.el = [ "test", "test" ]
444 self.assertEquals(["test", "test"],
445 [ el.text for el in root2.el ])
446
447 root.c = [ root2.el, root2.el ]
448 self.assertEquals(["test", "test"],
449 [ c.text for c in root.c ])
450 self.assertEquals(["test", "test"],
451 [ el.text for el in root2.el ])
452
453 root.c[:] = [ c1, c2, c2, c1 ]
454 self.assertEquals(["c1", "c2", "c2", "c1"],
455 [ c.text for c in root.c ])
456
458 # make sure strings are not handled as sequences
459 Element = self.Element
460 SubElement = self.etree.SubElement
461 root = Element("root")
462 root.c = "TEST"
463 self.assertEquals(["TEST"],
464 [ c.text for c in root.c ])
465
467 # make sure strings are set as children
468 Element = self.Element
469 SubElement = self.etree.SubElement
470 root = Element("root")
471 root["c"] = "TEST"
472 self.assertEquals(["TEST"],
473 [ c.text for c in root.c ])
474
476 # make sure 'text' etc. are set as children
477 Element = self.Element
478 SubElement = self.etree.SubElement
479 root = Element("root")
480
481 root["text"] = "TEST"
482 self.assertEquals(["TEST"],
483 [ c.text for c in root["text"] ])
484
485 root["tail"] = "TEST"
486 self.assertEquals(["TEST"],
487 [ c.text for c in root["tail"] ])
488
489 root["pyval"] = "TEST"
490 self.assertEquals(["TEST"],
491 [ c.text for c in root["pyval"] ])
492
493 root["tag"] = "TEST"
494 self.assertEquals(["TEST"],
495 [ c.text for c in root["tag"] ])
496
498 XML = self.XML
499 root = XML('<a><b><c/></b><b/><c><b/></c></a>')
500 self.assertEquals(1, len(root.findall("c")))
501 self.assertEquals(2, len(root.findall(".//c")))
502 self.assertEquals(3, len(root.findall(".//b")))
503 self.assert_(root.findall(".//b")[1] is root.getchildren()[1])
504
506 XML = self.XML
507 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
508 self.assertEquals(2, len(root.findall(".//{X}b")))
509 self.assertEquals(3, len(root.findall(".//b")))
510 self.assertEquals(2, len(root.findall("b")))
511
513 root = self.Element('root')
514 root.a = 5
515 root.b = 6
516 self.assert_(isinstance(root, objectify.ObjectifiedElement))
517 self.assert_(isinstance(root.a, objectify.IntElement))
518 self.assert_(isinstance(root.b, objectify.IntElement))
519
521 Element = self.Element
522 SubElement = self.etree.SubElement
523
524 nil_attr = XML_SCHEMA_NIL_ATTR
525 root = Element("{objectified}root")
526 SubElement(root, "{objectified}none")
527 SubElement(root, "{objectified}none", {nil_attr : "true"})
528 self.assertFalse(isinstance(root.none, objectify.NoneElement))
529 self.assertFalse(isinstance(root.none[0], objectify.NoneElement))
530 self.assert_(isinstance(root.none[1], objectify.NoneElement))
531 self.assertEquals(root.none[1], None)
532 self.assertFalse(root.none[1])
533
535 value = objectify.DataElement(None)
536 self.assert_(isinstance(value, objectify.NoneElement))
537 self.assertEquals(value, None)
538 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
539
541 Element = self.Element
542 SubElement = self.etree.SubElement
543 root = Element("{objectified}root")
544 root.bool = True
545 self.assertEquals(root.bool, True)
546 self.assert_(isinstance(root.bool, objectify.BoolElement))
547
548 root.bool = False
549 self.assertEquals(root.bool, False)
550 self.assert_(isinstance(root.bool, objectify.BoolElement))
551
553 value = objectify.DataElement(True)
554 self.assert_(isinstance(value, objectify.BoolElement))
555 self.assertEquals(value, True)
556
557 value = objectify.DataElement(False)
558 self.assert_(isinstance(value, objectify.BoolElement))
559 self.assertEquals(value, False)
560
562 Element = self.Element
563 SubElement = self.etree.SubElement
564 root = Element("{objectified}root")
565 root.s = "test"
566 self.assert_(isinstance(root.s, objectify.StringElement))
567
569 Element = self.Element
570 SubElement = self.etree.SubElement
571 root = Element("{objectified}root")
572 root.s = "3"
573 self.assert_(isinstance(root.s, objectify.StringElement))
574
576 Element = self.Element
577 SubElement = self.etree.SubElement
578 root = Element("{objectified}root")
579 root.s = "3.72"
580 self.assert_(isinstance(root.s, objectify.StringElement))
581
583 Element = self.Element
584 SubElement = self.etree.SubElement
585 root = Element("{objectified}root")
586 root.s = "test"
587
588 self.assertEquals("test" * 5, root.s * 5)
589 self.assertEquals(5 * "test", 5 * root.s)
590
591 self.assertRaises(TypeError, operator.mul, root.s, "honk")
592 self.assertRaises(TypeError, operator.mul, "honk", root.s)
593
595 Element = self.Element
596 SubElement = self.etree.SubElement
597 root = Element("{objectified}root")
598 root.s = "test"
599
600 s = "toast"
601 self.assertEquals("test" + s, root.s + s)
602 self.assertEquals(s + "test", s + root.s)
603
605 s = "%d %f %s %r"
606 el = objectify.DataElement(s)
607 values = (1, 7.0, "abcd", None)
608 self.assertEquals(s % values, el % values)
609
610 s = "%d"
611 el = objectify.DataElement(s)
612 val = 5
613 self.assertEquals(s % val, el % val)
614
615 s = "%d %s"
616 el = objectify.DataElement(s)
617 val = 5
618 self.assertRaises(TypeError, el.__mod__, val)
619
620 s = ""
621 el = objectify.DataElement(s)
622 val = 5
623 self.assertRaises(TypeError, el.__mod__, val)
624
626 s = "%d %f %s %r"
627 el = objectify.DataElement(s)
628 values = (objectify.DataElement(1),
629 objectify.DataElement(7.0),
630 objectify.DataElement("abcd"),
631 objectify.DataElement(None))
632 self.assertEquals(s % values, el % values)
633
635 value = objectify.DataElement("test")
636 self.assert_(isinstance(value, objectify.StringElement))
637 self.assertEquals(value, "test")
638
640 value = objectify.DataElement("3")
641 self.assert_(isinstance(value, objectify.StringElement))
642 self.assertEquals(value, "3")
643
645 value = objectify.DataElement("3.20")
646 self.assert_(isinstance(value, objectify.StringElement))
647 self.assertEquals(value, "3.20")
648
650 Element = self.Element
651 SubElement = self.etree.SubElement
652 root = Element("{objectified}root")
653 root.s = u"test"
654 self.assert_(isinstance(root.s, objectify.StringElement))
655
657 Element = self.Element
658 SubElement = self.etree.SubElement
659 root = Element("{objectified}root")
660 root.s = u"3"
661 self.assert_(isinstance(root.s, objectify.StringElement))
662
664 Element = self.Element
665 SubElement = self.etree.SubElement
666 root = Element("{objectified}root")
667 root.s = u"3.72"
668 self.assert_(isinstance(root.s, objectify.StringElement))
669
671 Element = self.Element
672 SubElement = self.etree.SubElement
673 root = Element("{objectified}root")
674 root.s = u"test"
675
676 self.assertEquals(u"test" * 5, root.s * 5)
677 self.assertEquals(5 * u"test", 5 * root.s)
678
679 self.assertRaises(TypeError, operator.mul, root.s, u"honk")
680 self.assertRaises(TypeError, operator.mul, u"honk", root.s)
681
683 Element = self.Element
684 SubElement = self.etree.SubElement
685 root = Element("{objectified}root")
686 root.s = u"test"
687
688 s = u"toast"
689 self.assertEquals(u"test" + s, root.s + s)
690 self.assertEquals(s + u"test", s + root.s)
691
693 value = objectify.DataElement(u"test")
694 self.assert_(isinstance(value, objectify.StringElement))
695 self.assertEquals(value, u"test")
696
698 value = objectify.DataElement("3")
699 self.assert_(isinstance(value, objectify.StringElement))
700 self.assertEquals(value, u"3")
701
703 value = objectify.DataElement(u"3.20")
704 self.assert_(isinstance(value, objectify.StringElement))
705 self.assertEquals(value, u"3.20")
706
708 Element = self.Element
709 SubElement = self.etree.SubElement
710 root = Element("{objectified}root")
711 root.none = 5
712 self.assert_(isinstance(root.none, objectify.IntElement))
713
715 value = objectify.DataElement(5)
716 self.assert_(isinstance(value, objectify.IntElement))
717 self.assertEquals(value, 5)
718
720 Element = self.Element
721 SubElement = self.etree.SubElement
722 root = Element("{objectified}root")
723 root.none = 5.5
724 self.assert_(isinstance(root.none, objectify.FloatElement))
725
727 value = objectify.DataElement(5.5)
728 self.assert_(isinstance(value, objectify.FloatElement))
729 self.assertEquals(value, 5.5)
730
732 for xsi, objclass in xsitype2objclass.iteritems():
733 # 1 is a valid value for all ObjectifiedDataElement classes
734 pyval = 1
735 value = objectify.DataElement(pyval, _xsi=xsi)
736 self.assert_(isinstance(value, objclass),
737 "DataElement(%s, _xsi='%s') returns %s, expected %s"
738 % (pyval, xsi, type(value), objclass))
739
741 for xsi, objclass in xsitype2objclass.iteritems():
742 # 1 is a valid value for all ObjectifiedDataElement classes
743 pyval = 1
744 value = objectify.DataElement(pyval, _xsi="xsd:%s" % xsi)
745 self.assert_(isinstance(value, objclass),
746 "DataElement(%s, _xsi='%s') returns %s, expected %s"
747 % (pyval, xsi, type(value), objclass))
748
750 for xsi, objclass in xsitype2objclass.iteritems():
751 # 1 is a valid value for all ObjectifiedDataElement classes
752 self.assertRaises(ValueError, objectify.DataElement, 1,
753 _xsi="foo:%s" % xsi)
754
756 for pytype, objclass in pytype2objclass.iteritems():
757 # 1 is a valid value for all ObjectifiedDataElement classes
758 pyval = 1
759 value = objectify.DataElement(pyval, _pytype=pytype)
760 self.assert_(isinstance(value, objclass),
761 "DataElement(%s, _pytype='%s') returns %s, expected %s"
762 % (pyval, pytype, type(value), objclass))
763
765 pyval = 1
766 pytype = "NoneType"
767 objclass = objectify.NoneElement
768 value = objectify.DataElement(pyval, _pytype=pytype)
769 self.assert_(isinstance(value, objclass),
770 "DataElement(%s, _pytype='%s') returns %s, expected %s"
771 % (pyval, pytype, type(value), objclass))
772 self.assertEquals(value.text, None)
773 self.assertEquals(value.pyval, None)
774
776 # pre-2.0 lxml called NoneElement "none"
777 pyval = 1
778 pytype = "none"
779 objclass = objectify.NoneElement
780 value = objectify.DataElement(pyval, _pytype=pytype)
781 self.assert_(isinstance(value, objclass),
782 "DataElement(%s, _pytype='%s') returns %s, expected %s"
783 % (pyval, pytype, type(value), objclass))
784 self.assertEquals(value.text, None)
785 self.assertEquals(value.pyval, None)
786
788 Element = self.Element
789 SubElement = self.etree.SubElement
790 class MyFloat(float):
791 pass
792 root = Element("{objectified}root")
793 root.myfloat = MyFloat(5.5)
794 self.assert_(isinstance(root.myfloat, objectify.FloatElement))
795 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
796
800 value = objectify.DataElement(MyFloat(5.5))
801 self.assert_(isinstance(value, objectify.FloatElement))
802 self.assertEquals(value, 5.5)
803 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None)
804
806 XML = self.XML
807 root = XML('''\
808 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
809 <b xsi:type="boolean">true</b>
810 <b xsi:type="boolean">false</b>
811 <b xsi:type="boolean">1</b>
812 <b xsi:type="boolean">0</b>
813
814 <f xsi:type="float">5</f>
815 <f xsi:type="double">5</f>
816
817 <s xsi:type="string">5</s>
818 <s xsi:type="normalizedString">5</s>
819 <s xsi:type="token">5</s>
820 <s xsi:type="language">5</s>
821 <s xsi:type="Name">5</s>
822 <s xsi:type="NCName">5</s>
823 <s xsi:type="ID">5</s>
824 <s xsi:type="IDREF">5</s>
825 <s xsi:type="ENTITY">5</s>
826 <s xsi:type="NMTOKEN">5</s>
827
828 <l xsi:type="integer">5</l>
829 <l xsi:type="nonPositiveInteger">5</l>
830 <l xsi:type="negativeInteger">5</l>
831 <l xsi:type="long">5</l>
832 <l xsi:type="nonNegativeInteger">5</l>
833 <l xsi:type="unsignedLong">5</l>
834 <l xsi:type="unsignedInt">5</l>
835 <l xsi:type="positiveInteger">5</l>
836
837 <i xsi:type="int">5</i>
838 <i xsi:type="short">5</i>
839 <i xsi:type="byte">5</i>
840 <i xsi:type="unsignedShort">5</i>
841 <i xsi:type="unsignedByte">5</i>
842
843 <n xsi:nil="true"/>
844 </root>
845 ''')
846
847 for b in root.b:
848 self.assert_(isinstance(b, objectify.BoolElement))
849 self.assertEquals(True, root.b[0])
850 self.assertEquals(False, root.b[1])
851 self.assertEquals(True, root.b[2])
852 self.assertEquals(False, root.b[3])
853
854 for f in root.f:
855 self.assert_(isinstance(f, objectify.FloatElement))
856 self.assertEquals(5, f)
857
858 for s in root.s:
859 self.assert_(isinstance(s, objectify.StringElement))
860 self.assertEquals("5", s)
861
862 for l in root.l:
863 self.assert_(isinstance(l, objectify.LongElement))
864 self.assertEquals(5L, l)
865
866 for i in root.i:
867 self.assert_(isinstance(i, objectify.IntElement))
868 self.assertEquals(5, i)
869
870 self.assert_(isinstance(root.n, objectify.NoneElement))
871 self.assertEquals(None, root.n)
872
874 XML = self.XML
875 root = XML('''\
876 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
877 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
878 <b xsi:type="xsd:boolean">true</b>
879 <b xsi:type="xsd:boolean">false</b>
880 <b xsi:type="xsd:boolean">1</b>
881 <b xsi:type="xsd:boolean">0</b>
882
883 <f xsi:type="xsd:float">5</f>
884 <f xsi:type="xsd:double">5</f>
885
886 <s xsi:type="xsd:string">5</s>
887 <s xsi:type="xsd:normalizedString">5</s>
888 <s xsi:type="xsd:token">5</s>
889 <s xsi:type="xsd:language">5</s>
890 <s xsi:type="xsd:Name">5</s>
891 <s xsi:type="xsd:NCName">5</s>
892 <s xsi:type="xsd:ID">5</s>
893 <s xsi:type="xsd:IDREF">5</s>
894 <s xsi:type="xsd:ENTITY">5</s>
895 <s xsi:type="xsd:NMTOKEN">5</s>
896
897 <l xsi:type="xsd:integer">5</l>
898 <l xsi:type="xsd:nonPositiveInteger">5</l>
899 <l xsi:type="xsd:negativeInteger">5</l>
900 <l xsi:type="xsd:long">5</l>
901 <l xsi:type="xsd:nonNegativeInteger">5</l>
902 <l xsi:type="xsd:unsignedLong">5</l>
903 <l xsi:type="xsd:unsignedInt">5</l>
904 <l xsi:type="xsd:positiveInteger">5</l>
905
906 <i xsi:type="xsd:int">5</i>
907 <i xsi:type="xsd:short">5</i>
908 <i xsi:type="xsd:byte">5</i>
909 <i xsi:type="xsd:unsignedShort">5</i>
910 <i xsi:type="xsd:unsignedByte">5</i>
911
912 <n xsi:nil="true"/>
913 </root>
914 ''')
915
916 for b in root.b:
917 self.assert_(isinstance(b, objectify.BoolElement))
918 self.assertEquals(True, root.b[0])
919 self.assertEquals(False, root.b[1])
920 self.assertEquals(True, root.b[2])
921 self.assertEquals(False, root.b[3])
922
923 for f in root.f:
924 self.assert_(isinstance(f, objectify.FloatElement))
925 self.assertEquals(5, f)
926
927 for s in root.s:
928 self.assert_(isinstance(s, objectify.StringElement))
929 self.assertEquals("5", s)
930
931 for l in root.l:
932 self.assert_(isinstance(l, objectify.LongElement))
933 self.assertEquals(5L, l)
934
935 for i in root.i:
936 self.assert_(isinstance(i, objectify.IntElement))
937 self.assertEquals(5, i)
938
939 self.assert_(isinstance(root.n, objectify.NoneElement))
940 self.assertEquals(None, root.n)
941
943 XML = self.XML
944 root = XML(u'<root><b>why</b><b>try</b></root>')
945 strs = [ str(s) for s in root.b ]
946 self.assertEquals(["why", "try"],
947 strs)
948
950 XML = self.XML
951 root = XML(u'<root><b>test</b><b>taste</b><b></b><b/></root>')
952 self.assertFalse(root.b[0] < root.b[1])
953 self.assertFalse(root.b[0] <= root.b[1])
954 self.assertFalse(root.b[0] == root.b[1])
955
956 self.assert_(root.b[0] != root.b[1])
957 self.assert_(root.b[0] >= root.b[1])
958 self.assert_(root.b[0] > root.b[1])
959
960 self.assertEquals(root.b[0], "test")
961 self.assertEquals("test", root.b[0])
962 self.assert_(root.b[0] > 5)
963 self.assert_(5 < root.b[0])
964
965 self.assertEquals("", root.b[2])
966 self.assertEquals(root.b[2], "")
967 self.assertEquals("", root.b[3])
968 self.assertEquals(root.b[3], "")
969 self.assertEquals(root.b[2], root.b[3])
970
971 root.b = "test"
972 self.assert_(root.b)
973 root.b = ""
974 self.assertFalse(root.b)
975 self.assertEquals(root.b, "")
976 self.assertEquals("", root.b)
977
979 XML = self.XML
980 root = XML(u'<root><b>5</b><b>6</b></root>')
981 self.assert_(root.b[0] < root.b[1])
982 self.assert_(root.b[0] <= root.b[1])
983 self.assert_(root.b[0] != root.b[1])
984
985 self.assertFalse(root.b[0] == root.b[1])
986 self.assertFalse(root.b[0] >= root.b[1])
987 self.assertFalse(root.b[0] > root.b[1])
988
989 self.assertEquals(root.b[0], 5)
990 self.assertEquals(5, root.b[0])
991 self.assert_(root.b[0] < "5")
992 self.assert_("5" > root.b[0])
993
994 root.b = 5
995 self.assert_(root.b)
996 root.b = 0
997 self.assertFalse(root.b)
998
999 # float + long share the NumberElement implementation with int
1000
1002 XML = self.XML
1003 root = XML(u'<root><b>false</b><b>true</b></root>')
1004 self.assert_(root.b[0] < root.b[1])
1005 self.assert_(root.b[0] <= root.b[1])
1006 self.assert_(root.b[0] != root.b[1])
1007
1008 self.assertFalse(root.b[0] == root.b[1])
1009 self.assertFalse(root.b[0] >= root.b[1])
1010 self.assertFalse(root.b[0] > root.b[1])
1011
1012 self.assertFalse(root.b[0])
1013 self.assert_(root.b[1])
1014
1015 self.assertEquals(root.b[0], False)
1016 self.assertEquals(False, root.b[0])
1017 self.assert_(root.b[0] < 5)
1018 self.assert_(5 > root.b[0])
1019
1020 root.b = True
1021 self.assert_(root.b)
1022 root.b = False
1023 self.assertFalse(root.b)
1024
1026 XML = self.XML
1027 root = XML(u"""
1028 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1029 <b xsi:nil="true"></b><b xsi:nil="true"/>
1030 </root>""")
1031 self.assert_(root.b[0] == root.b[1])
1032 self.assertFalse(root.b[0])
1033 self.assertEquals(root.b[0], None)
1034 self.assertEquals(None, root.b[0])
1035
1036 for comparison in ["abc", 5, 7.3, True, [], ()]:
1037 none = root.b[1]
1038 self.assert_(none < comparison, "%s (%s) should be < %s" %
1039 (none, type(none), comparison) )
1040 self.assert_(comparison > none, "%s should be > %s (%s)" %
1041 (comparison, none, type(none)) )
1042
1044 el = objectify.DataElement(1, _xsi="string")
1045 self.assertEquals(
1046 el.get(XML_SCHEMA_INSTANCE_TYPE_ATTR),
1047 'xsd:string')
1048
1050 el = objectify.DataElement(1, _xsi="string",
1051 nsmap={'schema': XML_SCHEMA_NS})
1052 self.assertEquals(
1053 el.get(XML_SCHEMA_INSTANCE_TYPE_ATTR),
1054 'schema:string')
1055
1059
1061 XML = self.XML
1062 root = XML(u'''\
1063 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1064 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1065 <b>5</b>
1066 <b>test</b>
1067 <c>1.1</c>
1068 <c>\uF8D2</c>
1069 <x>true</x>
1070 <n xsi:nil="true" />
1071 <n></n>
1072 <b xsi:type="double">5</b>
1073 <b xsi:type="float">5</b>
1074 <s xsi:type="string">23</s>
1075 <s py:pytype="str">42</s>
1076 <f py:pytype="float">300</f>
1077 <l py:pytype="long">2</l>
1078 <t py:pytype="TREE"></t>
1079 </a>
1080 ''')
1081 objectify.annotate(root)
1082
1083 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1084 for c in root.iterchildren() ]
1085 self.assertEquals("int", child_types[ 0])
1086 self.assertEquals("str", child_types[ 1])
1087 self.assertEquals("float", child_types[ 2])
1088 self.assertEquals("str", child_types[ 3])
1089 self.assertEquals("bool", child_types[ 4])
1090 self.assertEquals("NoneType", child_types[ 5])
1091 self.assertEquals(None, child_types[ 6])
1092 self.assertEquals("float", child_types[ 7])
1093 self.assertEquals("float", child_types[ 8])
1094 self.assertEquals("str", child_types[ 9])
1095 self.assertEquals("int", child_types[10])
1096 self.assertEquals("int", child_types[11])
1097 self.assertEquals("int", child_types[12])
1098 self.assertEquals(None, child_types[13])
1099
1100 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1101
1103 XML = self.XML
1104 root = XML(u'''\
1105 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1106 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1107 <n></n>
1108 </a>
1109 ''')
1110 objectify.annotate(root)
1111
1112 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1113 for c in root.iterchildren() ]
1114 self.assertEquals(None, child_types[0])
1115
1116 objectify.annotate(root, empty_pytype="str")
1117
1118 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1119 for c in root.iterchildren() ]
1120 self.assertEquals("str", child_types[0])
1121
1123 XML = self.XML
1124 root = XML(u'''\
1125 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1126 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1127 <b>5</b>
1128 <b>test</b>
1129 <c>1.1</c>
1130 <c>\uF8D2</c>
1131 <x>true</x>
1132 <n xsi:nil="true" />
1133 <n></n>
1134 <b xsi:type="double">5</b>
1135 <b xsi:type="float">5</b>
1136 <s xsi:type="string">23</s>
1137 <s py:pytype="str">42</s>
1138 <f py:pytype="float">300</f>
1139 <l py:pytype="long">2</l>
1140 <t py:pytype="TREE"></t>
1141 </a>
1142 ''')
1143 objectify.annotate(root, ignore_old=False)
1144
1145 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1146 for c in root.iterchildren() ]
1147 self.assertEquals("int", child_types[ 0])
1148 self.assertEquals("str", child_types[ 1])
1149 self.assertEquals("float", child_types[ 2])
1150 self.assertEquals("str", child_types[ 3])
1151 self.assertEquals("bool", child_types[ 4])
1152 self.assertEquals("NoneType", child_types[ 5])
1153 self.assertEquals(None, child_types[ 6])
1154 self.assertEquals("float", child_types[ 7])
1155 self.assertEquals("float", child_types[ 8])
1156 self.assertEquals("str", child_types[ 9])
1157 self.assertEquals("str", child_types[10])
1158 self.assertEquals("float", child_types[11])
1159 self.assertEquals("long", child_types[12])
1160 self.assertEquals(TREE_PYTYPE, child_types[13])
1161
1162 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1163
1165 XML = self.XML
1166 root = XML(u'''\
1167 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1168 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1169 <b>5</b>
1170 <b>test</b>
1171 <c>1.1</c>
1172 <c>\uF8D2</c>
1173 <x>true</x>
1174 <n xsi:nil="true" />
1175 <n></n>
1176 <b xsi:type="double">5</b>
1177 <b xsi:type="float">5</b>
1178 <s xsi:type="string">23</s>
1179 <s py:pytype="str">42</s>
1180 <f py:pytype="float">300</f>
1181 <l py:pytype="long">2</l>
1182 <t py:pytype="TREE"></t>
1183 </a>
1184 ''')
1185 objectify.annotate(root, ignore_old=False, ignore_xsi=False,
1186 annotate_xsi=1, annotate_pytype=1)
1187
1188 # check py annotations
1189 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1190 for c in root.iterchildren() ]
1191 self.assertEquals("int", child_types[ 0])
1192 self.assertEquals("str", child_types[ 1])
1193 self.assertEquals("float", child_types[ 2])
1194 self.assertEquals("str", child_types[ 3])
1195 self.assertEquals("bool", child_types[ 4])
1196 self.assertEquals("NoneType", child_types[ 5])
1197 self.assertEquals(None, child_types[ 6])
1198 self.assertEquals("float", child_types[ 7])
1199 self.assertEquals("float", child_types[ 8])
1200 self.assertEquals("str", child_types[ 9])
1201 self.assertEquals("str", child_types[10])
1202 self.assertEquals("float", child_types[11])
1203 self.assertEquals("long", child_types[12])
1204 self.assertEquals(TREE_PYTYPE, child_types[13])
1205
1206 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1207
1208 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1209 for c in root.iterchildren() ]
1210
1211 # check xsi annotations
1212 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1213 for c in root.iterchildren() ]
1214 self.assertEquals("xsd:int", child_types[ 0])
1215 self.assertEquals("xsd:string", child_types[ 1])
1216 self.assertEquals("xsd:double", child_types[ 2])
1217 self.assertEquals("xsd:string", child_types[ 3])
1218 self.assertEquals("xsd:boolean", child_types[ 4])
1219 self.assertEquals(None, child_types[ 5])
1220 self.assertEquals(None, child_types[ 6])
1221 self.assertEquals("xsd:double", child_types[ 7])
1222 self.assertEquals("xsd:float", child_types[ 8])
1223 self.assertEquals("xsd:string", child_types[ 9])
1224 self.assertEquals("xsd:string", child_types[10])
1225 self.assertEquals("xsd:double", child_types[11])
1226 self.assertEquals("xsd:integer", child_types[12])
1227 self.assertEquals(None, child_types[13])
1228
1229 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1230
1232 XML = self.XML
1233 root = XML(u'''\
1234 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1235 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1236 <b>5</b>
1237 <b>test</b>
1238 <c>1.1</c>
1239 <c>\uF8D2</c>
1240 <x>true</x>
1241 <n xsi:nil="true" />
1242 <n></n>
1243 <b xsi:type="double">5</b>
1244 <b xsi:type="float">5</b>
1245 <s xsi:type="string">23</s>
1246 <s py:pytype="str">42</s>
1247 <f py:pytype="float">300</f>
1248 <l py:pytype="long">2</l>
1249 <t py:pytype="TREE"></t>
1250 </a>
1251 ''')
1252 objectify.xsiannotate(root, ignore_old=False)
1253
1254 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1255 for c in root.iterchildren() ]
1256 self.assertEquals("xsd:int", child_types[ 0])
1257 self.assertEquals("xsd:string", child_types[ 1])
1258 self.assertEquals("xsd:double", child_types[ 2])
1259 self.assertEquals("xsd:string", child_types[ 3])
1260 self.assertEquals("xsd:boolean", child_types[ 4])
1261 self.assertEquals(None, child_types[ 5])
1262 self.assertEquals(None, child_types[ 6])
1263 self.assertEquals("xsd:double", child_types[ 7])
1264 self.assertEquals("xsd:float", child_types[ 8])
1265 self.assertEquals("xsd:string", child_types[ 9])
1266 self.assertEquals("xsd:string", child_types[10])
1267 self.assertEquals("xsd:double", child_types[11])
1268 self.assertEquals("xsd:integer", child_types[12])
1269 self.assertEquals(None, child_types[13])
1270
1272 XML = self.XML
1273 root = XML(u'''\
1274 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1275 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1276 <b>5</b>
1277 <b>test</b>
1278 <c>1.1</c>
1279 <c>\uF8D2</c>
1280 <x>true</x>
1281 <n xsi:nil="true" />
1282 <n></n>
1283 <b xsi:type="double">5</b>
1284 <b xsi:type="float">5</b>
1285 <s xsi:type="string">23</s>
1286 <s py:pytype="str">42</s>
1287 <f py:pytype="float">300</f>
1288 <l py:pytype="long">2</l>
1289 <t py:pytype="TREE"></t>
1290 </a>
1291 ''')
1292 objectify.pyannotate(root, ignore_old=True)
1293
1294 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1295 for c in root.iterchildren() ]
1296 self.assertEquals("int", child_types[ 0])
1297 self.assertEquals("str", child_types[ 1])
1298 self.assertEquals("float", child_types[ 2])
1299 self.assertEquals("str", child_types[ 3])
1300 self.assertEquals("bool", child_types[ 4])
1301 self.assertEquals("NoneType", child_types[ 5])
1302 self.assertEquals(None, child_types[ 6])
1303 self.assertEquals("float", child_types[ 7])
1304 self.assertEquals("float", child_types[ 8])
1305 self.assertEquals("str", child_types[ 9])
1306 self.assertEquals("int", child_types[10])
1307 self.assertEquals("int", child_types[11])
1308 self.assertEquals("int", child_types[12])
1309 self.assertEquals(None, child_types[13])
1310
1311 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1312
1314 XML = self.XML
1315 root = XML(u'''\
1316 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1317 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1318 <n></n>
1319 </a>
1320 ''')
1321 objectify.pyannotate(root)
1322
1323 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1324 for c in root.iterchildren() ]
1325 self.assertEquals(None, child_types[0])
1326
1327 objectify.annotate(root, empty_pytype="str")
1328
1329 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1330 for c in root.iterchildren() ]
1331 self.assertEquals("str", child_types[0])
1332
1334 XML = self.XML
1335 root = XML(u'''\
1336 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1337 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1338 <b>5</b>
1339 <b>test</b>
1340 <c>1.1</c>
1341 <c>\uF8D2</c>
1342 <x>true</x>
1343 <n xsi:nil="true" />
1344 <n></n>
1345 <b xsi:type="double">5</b>
1346 <b xsi:type="float">5</b>
1347 <s xsi:type="string">23</s>
1348 <s py:pytype="str">42</s>
1349 <f py:pytype="float">300</f>
1350 <l py:pytype="long">2</l>
1351 <t py:pytype="TREE"></t>
1352 </a>
1353 ''')
1354 objectify.pyannotate(root)
1355
1356 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1357 for c in root.iterchildren() ]
1358 self.assertEquals("int", child_types[ 0])
1359 self.assertEquals("str", child_types[ 1])
1360 self.assertEquals("float", child_types[ 2])
1361 self.assertEquals("str", child_types[ 3])
1362 self.assertEquals("bool", child_types[ 4])
1363 self.assertEquals("NoneType", child_types[ 5])
1364 self.assertEquals(None, child_types[ 6])
1365 self.assertEquals("float", child_types[ 7])
1366 self.assertEquals("float", child_types[ 8])
1367 self.assertEquals("str", child_types[ 9])
1368 self.assertEquals("str", child_types[10])
1369 self.assertEquals("float", child_types[11])
1370 self.assertEquals("long", child_types[12])
1371 self.assertEquals(TREE_PYTYPE, child_types[13])
1372
1373 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1374
1376 XML = self.XML
1377 root = XML(u'''\
1378 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1379 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1380 <b>5</b>
1381 <b>test</b>
1382 <c>1.1</c>
1383 <c>\uF8D2</c>
1384 <x>true</x>
1385 <n xsi:nil="true" />
1386 <n></n>
1387 <b xsi:type="double">5</b>
1388 <b xsi:type="float">5</b>
1389 <s xsi:type="string">23</s>
1390 <s py:pytype="str">42</s>
1391 <f py:pytype="float">300</f>
1392 <l py:pytype="long">2</l>
1393 <t py:pytype="TREE"></t>
1394 </a>
1395 ''')
1396 objectify.xsiannotate(root, ignore_old=True)
1397
1398 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1399 for c in root.iterchildren() ]
1400 self.assertEquals("xsd:int", child_types[ 0])
1401 self.assertEquals("xsd:string", child_types[ 1])
1402 self.assertEquals("xsd:double", child_types[ 2])
1403 self.assertEquals("xsd:string", child_types[ 3])
1404 self.assertEquals("xsd:boolean", child_types[ 4])
1405 self.assertEquals(None, child_types[ 5])
1406 self.assertEquals(None, child_types[ 6])
1407 self.assertEquals("xsd:int", child_types[ 7])
1408 self.assertEquals("xsd:int", child_types[ 8])
1409 self.assertEquals("xsd:int", child_types[ 9])
1410 self.assertEquals("xsd:string", child_types[10])
1411 self.assertEquals("xsd:double", child_types[11])
1412 self.assertEquals("xsd:integer", child_types[12])
1413 self.assertEquals(None, child_types[13])
1414
1415 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1416
1418 XML = self.XML
1419 root = XML(u'''\
1420 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1421 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1422 <b>5</b>
1423 <b>test</b>
1424 <c>1.1</c>
1425 <c>\uF8D2</c>
1426 <x>true</x>
1427 <n xsi:nil="true" />
1428 <n></n>
1429 <b xsi:type="double">5</b>
1430 <b xsi:type="float">5</b>
1431 <s xsi:type="string">23</s>
1432 <s py:pytype="str">42</s>
1433 <f py:pytype="float">300</f>
1434 <l py:pytype="long">2</l>
1435 <t py:pytype="TREE"></t>
1436 </a>
1437 ''')
1438 objectify.deannotate(root)
1439
1440 for c in root.getiterator():
1441 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1442 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1443
1444 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1445
1447 XML = self.XML
1448 root = XML(u'''\
1449 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1450 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1451 <b>5</b>
1452 <b>test</b>
1453 <c>1.1</c>
1454 <c>\uF8D2</c>
1455 <x>true</x>
1456 <n xsi:nil="true" />
1457 <n></n>
1458 <b xsi:type="double">5</b>
1459 <b xsi:type="float">5</b>
1460 <s xsi:type="string">23</s>
1461 <s py:pytype="str">42</s>
1462 <f py:pytype="float">300</f>
1463 <l py:pytype="long">2</l>
1464 <t py:pytype="TREE"></t>
1465 </a>
1466 ''')
1467 objectify.xsiannotate(root)
1468 objectify.deannotate(root, xsi=False)
1469
1470 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1471 for c in root.iterchildren() ]
1472 self.assertEquals("xsd:int", child_types[ 0])
1473 self.assertEquals("xsd:string", child_types[ 1])
1474 self.assertEquals("xsd:double", child_types[ 2])
1475 self.assertEquals("xsd:string", child_types[ 3])
1476 self.assertEquals("xsd:boolean", child_types[ 4])
1477 self.assertEquals(None, child_types[ 5])
1478 self.assertEquals(None, child_types[ 6])
1479 self.assertEquals("xsd:int", child_types[ 7])
1480 self.assertEquals("xsd:int", child_types[ 8])
1481 self.assertEquals("xsd:int", child_types[ 9])
1482 self.assertEquals("xsd:string", child_types[10])
1483 self.assertEquals("xsd:double", child_types[11])
1484 self.assertEquals("xsd:integer", child_types[12])
1485 self.assertEquals(None, child_types[13])
1486
1487 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1488
1489 for c in root.getiterator():
1490 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1491
1493 XML = self.XML
1494 root = XML(u'''\
1495 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1496 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1497 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1498 <b>5</b>
1499 <b>test</b>
1500 <c>1.1</c>
1501 <c>\uF8D2</c>
1502 <x>true</x>
1503 <n xsi:nil="true" />
1504 <n></n>
1505 <b xsi:type="xsd:double">5</b>
1506 <b xsi:type="xsd:float">5</b>
1507 <s xsi:type="xsd:string">23</s>
1508 <s py:pytype="str">42</s>
1509 <f py:pytype="float">300</f>
1510 <l py:pytype="long">2</l>
1511 <t py:pytype="TREE"></t>
1512 </a>
1513 ''')
1514 objectify.annotate(root)
1515 objectify.deannotate(root, pytype=False)
1516
1517 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1518 for c in root.iterchildren() ]
1519 self.assertEquals("int", child_types[ 0])
1520 self.assertEquals("str", child_types[ 1])
1521 self.assertEquals("float", child_types[ 2])
1522 self.assertEquals("str", child_types[ 3])
1523 self.assertEquals("bool", child_types[ 4])
1524 self.assertEquals("NoneType", child_types[ 5])
1525 self.assertEquals(None, child_types[ 6])
1526 self.assertEquals("float", child_types[ 7])
1527 self.assertEquals("float", child_types[ 8])
1528 self.assertEquals("str", child_types[ 9])
1529 self.assertEquals("int", child_types[10])
1530 self.assertEquals("int", child_types[11])
1531 self.assertEquals("int", child_types[12])
1532 self.assertEquals(None, child_types[13])
1533
1534 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1535
1536 for c in root.getiterator():
1537 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1538
1540 XML = self.XML
1541 root = XML(u'''\
1542 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1543 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1544 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1545 <b xsi:type="xsd:int">5</b>
1546 <b xsi:type="xsd:string">test</b>
1547 <c xsi:type="xsd:float">1.1</c>
1548 <c xsi:type="xsd:string">\uF8D2</c>
1549 <x xsi:type="xsd:boolean">true</x>
1550 <n xsi:nil="true" />
1551 <n></n>
1552 <b xsi:type="xsd:double">5</b>
1553 <b xsi:type="xsd:float">5</b>
1554 <s xsi:type="xsd:string">23</s>
1555 <s xsi:type="xsd:string">42</s>
1556 <f xsi:type="xsd:float">300</f>
1557 <l xsi:type="xsd:long">2</l>
1558 <t py:pytype="TREE"></t>
1559 </a>
1560 ''')
1561 objectify.annotate(root)
1562 objectify.deannotate(root, xsi=False)
1563
1564 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1565 for c in root.iterchildren() ]
1566 self.assertEquals("xsd:int", child_types[ 0])
1567 self.assertEquals("xsd:string", child_types[ 1])
1568 self.assertEquals("xsd:float", child_types[ 2])
1569 self.assertEquals("xsd:string", child_types[ 3])
1570 self.assertEquals("xsd:boolean", child_types[ 4])
1571 self.assertEquals(None, child_types[ 5])
1572 self.assertEquals(None, child_types[ 6])
1573 self.assertEquals("xsd:double", child_types[ 7])
1574 self.assertEquals("xsd:float", child_types[ 8])
1575 self.assertEquals("xsd:string", child_types[ 9])
1576 self.assertEquals("xsd:string", child_types[10])
1577 self.assertEquals("xsd:float", child_types[11])
1578 self.assertEquals("xsd:long", child_types[12])
1579 self.assertEquals(None, child_types[13])
1580
1581 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1582
1583 for c in root.getiterator():
1584 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1585
1587 XML = self.XML
1588
1589 xml = u'''\
1590 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1591 <b>5</b>
1592 <b>test</b>
1593 <c>1.1</c>
1594 <c>\uF8D2</c>
1595 <x>true</x>
1596 <n xsi:nil="true" />
1597 <n></n>
1598 <b xsi:type="double">5</b>
1599 </a>
1600 '''
1601
1602 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1603 objectify.setPytypeAttributeTag("{TEST}test")
1604
1605 root = XML(xml)
1606 objectify.annotate(root)
1607
1608 attribs = root.xpath("//@py:%s" % pytype_name,
1609 namespaces={"py" : pytype_ns})
1610 self.assertEquals(0, len(attribs))
1611 attribs = root.xpath("//@py:test",
1612 namespaces={"py" : "TEST"})
1613 self.assertEquals(7, len(attribs))
1614
1615 objectify.setPytypeAttributeTag()
1616 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1617
1618 self.assertNotEqual("test", pytype_ns.lower())
1619 self.assertNotEqual("test", pytype_name.lower())
1620
1621 root = XML(xml)
1622 attribs = root.xpath("//@py:%s" % pytype_name,
1623 namespaces={"py" : pytype_ns})
1624 self.assertEquals(0, len(attribs))
1625
1626 objectify.annotate(root)
1627 attribs = root.xpath("//@py:%s" % pytype_name,
1628 namespaces={"py" : pytype_ns})
1629 self.assertEquals(7, len(attribs))
1630
1632 orig_types = objectify.getRegisteredTypes()
1633
1634 try:
1635 orig_types[0].unregister()
1636 self.assertEquals(orig_types[1:], objectify.getRegisteredTypes())
1637
1638 class NewType(objectify.ObjectifiedDataElement):
1639 pass
1640
1641 def checkMyType(s):
1642 return True
1643
1644 pytype = objectify.PyType("mytype", checkMyType, NewType)
1645 pytype.register()
1646 self.assert_(pytype in objectify.getRegisteredTypes())
1647 pytype.unregister()
1648
1649 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1650 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1651 pytype.unregister()
1652
1653 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1654 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1655 pytype.unregister()
1656
1657 self.assertRaises(ValueError, pytype.register,
1658 before = [objectify.getRegisteredTypes()[0].name],
1659 after = [objectify.getRegisteredTypes()[1].name])
1660
1661 finally:
1662 for pytype in objectify.getRegisteredTypes():
1663 pytype.unregister()
1664 for pytype in orig_types:
1665 pytype.register()
1666
1668 root = self.XML(xml_str)
1669 path = objectify.ObjectPath( "root.c1.c2" )
1670 self.assertEquals(root.c1.c2.text, path.find(root).text)
1671 self.assertEquals(root.c1.c2.text, path(root).text)
1672
1674 root = self.XML(xml_str)
1675 path = objectify.ObjectPath( ['root', 'c1', 'c2'] )
1676 self.assertEquals(root.c1.c2.text, path.find(root).text)
1677 self.assertEquals(root.c1.c2.text, path(root).text)
1678
1680 root = self.XML(xml_str)
1681 path = objectify.ObjectPath( "root.c1.c99" )
1682 self.assertRaises(AttributeError, path, root)
1683 self.assertEquals(None, path(root, None))
1684
1686 root = self.XML(xml_str)
1687 path = objectify.ObjectPath("root . {objectified}c1. c2")
1688 self.assertEquals(root.c1.c2.text, path(root).text)
1689
1690 path = objectify.ObjectPath(" root.{objectified} c1.c2 [ 0 ] ")
1691 self.assertEquals(root.c1.c2.text, path(root).text)
1692
1694 root = self.XML(xml_str)
1695 path = objectify.ObjectPath( "root" )
1696 self.assert_(path.hasattr(root))
1697 path = objectify.ObjectPath( "root.c1" )
1698 self.assert_(path.hasattr(root))
1699 path = objectify.ObjectPath( "root.c1.c2" )
1700 self.assert_(path.hasattr(root))
1701 path = objectify.ObjectPath( "root.c1.{otherNS}c2" )
1702 self.assert_(path.hasattr(root))
1703 path = objectify.ObjectPath( "root.c1.c2[1]" )
1704 self.assert_(path.hasattr(root))
1705 path = objectify.ObjectPath( "root.c1.c2[2]" )
1706 self.assert_(path.hasattr(root))
1707 path = objectify.ObjectPath( "root.c1.c2[3]" )
1708 self.assertFalse(path.hasattr(root))
1709 path = objectify.ObjectPath( "root.c1[1].c2" )
1710 self.assertFalse(path.hasattr(root))
1711
1713 root = self.XML(xml_str)
1714 path = objectify.ObjectPath( "." )
1715 self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
1716
1718 root = self.XML(xml_str)
1719 path = objectify.ObjectPath( [''] )
1720 self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
1721
1723 root = self.XML(xml_str)
1724 path = objectify.ObjectPath( ".c1.c2" )
1725 self.assertEquals(root.c1.c2.text, path(root).text)
1726
1728 root = self.XML(xml_str)
1729 path = objectify.ObjectPath( ['', 'c1', 'c2'] )
1730 self.assertEquals(root.c1.c2.text, path(root).text)
1731
1733 root = self.XML(xml_str)
1734 path = objectify.ObjectPath( "root.c1[0].c2[0]" )
1735 self.assertEquals(root.c1.c2.text, path(root).text)
1736
1737 path = objectify.ObjectPath( "root.c1[0].c2" )
1738 self.assertEquals(root.c1.c2.text, path(root).text)
1739
1740 path = objectify.ObjectPath( "root.c1[0].c2[1]" )
1741 self.assertEquals(root.c1.c2[1].text, path(root).text)
1742
1743 path = objectify.ObjectPath( "root.c1.c2[2]" )
1744 self.assertEquals(root.c1.c2[2].text, path(root).text)
1745
1746 path = objectify.ObjectPath( "root.c1.c2[-1]" )
1747 self.assertEquals(root.c1.c2[-1].text, path(root).text)
1748
1749 path = objectify.ObjectPath( "root.c1.c2[-3]" )
1750 self.assertEquals(root.c1.c2[-3].text, path(root).text)
1751
1753 root = self.XML(xml_str)
1754 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
1755 self.assertEquals(root.c1.c2.text, path(root).text)
1756
1757 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
1758 self.assertEquals(root.c1.c2[2].text, path(root).text)
1759
1760 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
1761 self.assertEquals(root.c1.c2[2].text, path(root).text)
1762
1763 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
1764 self.assertEquals(root.c1.c2[-1].text, path(root).text)
1765
1766 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
1767 self.assertEquals(root.c1.c2[-3].text, path(root).text)
1768
1770 self.assertRaises(ValueError, objectify.ObjectPath,
1771 "root.c1[0].c2[-1-2]")
1772 self.assertRaises(ValueError, objectify.ObjectPath,
1773 ['root', 'c1[0]', 'c2[-1-2]'])
1774
1775 self.assertRaises(ValueError, objectify.ObjectPath,
1776 "root[2].c1.c2")
1777 self.assertRaises(ValueError, objectify.ObjectPath,
1778 ['root[2]', 'c1', 'c2'])
1779
1780 self.assertRaises(ValueError, objectify.ObjectPath,
1781 [])
1782 self.assertRaises(ValueError, objectify.ObjectPath,
1783 ['', '', ''])
1784
1786 root = self.XML(xml_str)
1787 path = objectify.ObjectPath("root.c1[9999].c2")
1788 self.assertRaises(AttributeError, path, root)
1789
1790 path = objectify.ObjectPath("root.c1[0].c2[9999]")
1791 self.assertRaises(AttributeError, path, root)
1792
1793 path = objectify.ObjectPath(".c1[9999].c2[0]")
1794 self.assertRaises(AttributeError, path, root)
1795
1796 path = objectify.ObjectPath("root.c1[-2].c2")
1797 self.assertRaises(AttributeError, path, root)
1798
1799 path = objectify.ObjectPath("root.c1[0].c2[-4]")
1800 self.assertRaises(AttributeError, path, root)
1801
1803 root = self.XML(xml_str)
1804 path = objectify.ObjectPath( "{objectified}root.c1.c2" )
1805 self.assertEquals(root.c1.c2.text, path.find(root).text)
1806 path = objectify.ObjectPath( "{objectified}root.{objectified}c1.c2" )
1807 self.assertEquals(root.c1.c2.text, path.find(root).text)
1808 path = objectify.ObjectPath( "root.{objectified}c1.{objectified}c2" )
1809 self.assertEquals(root.c1.c2.text, path.find(root).text)
1810 path = objectify.ObjectPath( "root.c1.{objectified}c2" )
1811 self.assertEquals(root.c1.c2.text, path.find(root).text)
1812 path = objectify.ObjectPath( "root.c1.{otherNS}c2" )
1813 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
1814 path.find(root).text)
1815
1817 root = self.XML(xml_str)
1818 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
1819 self.assertEquals(root.c1.c2.text, path.find(root).text)
1820 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
1821 self.assertEquals(root.c1.c2.text, path.find(root).text)
1822 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
1823 self.assertEquals(root.c1.c2.text, path.find(root).text)
1824 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
1825 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
1826 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
1827 self.assertEquals(root.c1.c2.text, path.find(root).text)
1828 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
1829 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
1830 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
1831 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
1832 path.find(root).text)
1833
1835 root = self.XML(xml_str)
1836 path = objectify.ObjectPath( "root.c1.c2" )
1837 self.assertEquals(root.c1.c2.text, path.find(root).text)
1838 self.assertEquals("1", root.c1.c2[1].text)
1839
1840 new_value = "my new value"
1841 path.setattr(root, new_value)
1842
1843 self.assertEquals(new_value, root.c1.c2.text)
1844 self.assertEquals(new_value, path(root).text)
1845 self.assertEquals("1", root.c1.c2[1].text)
1846
1848 root = self.XML(xml_str)
1849 path = objectify.ObjectPath( "root.c1.c2" )
1850 self.assertEquals(root.c1.c2.text, path.find(root).text)
1851 self.assertEquals("1", root.c1.c2[1].text)
1852
1853 new_el = self.Element("{objectified}test")
1854 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST"
1855 path.setattr(root, new_el.sub)
1856
1857 self.assertEquals("ATTR", root.c1.c2.get("myattr"))
1858 self.assertEquals("TEST", root.c1.c2.a.text)
1859 self.assertEquals("TEST", path(root).a.text)
1860 self.assertEquals("1", root.c1.c2[1].text)
1861
1863 root = self.XML(xml_str)
1864 path = objectify.ObjectPath( "root.c1.c99" )
1865 self.assertRaises(AttributeError, path.find, root)
1866
1867 new_value = "my new value"
1868 path.setattr(root, new_value)
1869
1870 self.assertEquals(1, len(root.c1.c99))
1871 self.assertEquals(new_value, root.c1.c99.text)
1872 self.assertEquals(new_value, path(root).text)
1873
1875 root = self.XML(xml_str)
1876 path = objectify.ObjectPath( "root.c1.c99" )
1877 self.assertRaises(AttributeError, path.find, root)
1878
1879 new_el = self.Element("{objectified}test")
1880 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST"
1881 path.setattr(root, new_el.sub)
1882
1883 self.assertEquals(1, len(root.c1.c99))
1884 self.assertEquals("ATTR", root.c1.c99.get("myattr"))
1885 self.assertEquals("TEST", root.c1.c99.a.text)
1886 self.assertEquals("TEST", path(root).a.text)
1887
1889 root = self.XML(xml_str)
1890 path = objectify.ObjectPath( "root.c1.c99" )
1891 self.assertRaises(AttributeError, path.find, root)
1892
1893 new_el = self.Element("{objectified}test")
1894 new_el.a = ["TEST1", "TEST2"]
1895 new_el.a[0].set("myattr", "ATTR1")
1896 new_el.a[1].set("myattr", "ATTR2")
1897
1898 path.setattr(root, list(new_el.a))
1899
1900 self.assertEquals(2, len(root.c1.c99))
1901 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
1902 self.assertEquals("TEST1", root.c1.c99[0].text)
1903 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
1904 self.assertEquals("TEST2", root.c1.c99[1].text)
1905 self.assertEquals("TEST1", path(root).text)
1906
1908 root = self.XML(xml_str)
1909 path = objectify.ObjectPath( "root.c1.c2" )
1910 self.assertEquals(3, len(root.c1.c2))
1911 path.addattr(root, "test")
1912 self.assertEquals(4, len(root.c1.c2))
1913 self.assertEquals(["0", "1", "2", "test"],
1914 [el.text for el in root.c1.c2])
1915
1917 root = self.XML(xml_str)
1918 path = objectify.ObjectPath( "root.c1.c2" )
1919 self.assertEquals(3, len(root.c1.c2))
1920
1921 new_el = self.Element("{objectified}test")
1922 etree.SubElement(new_el, "{objectified}sub").a = "TEST"
1923
1924 path.addattr(root, new_el.sub)
1925 self.assertEquals(4, len(root.c1.c2))
1926 self.assertEquals("TEST", root.c1.c2[3].a.text)
1927 self.assertEquals(["0", "1", "2"],
1928 [el.text for el in root.c1.c2[:3]])
1929
1931 root = self.XML(xml_str)
1932 path = objectify.ObjectPath( "root.c1.c99" )
1933 self.assertRaises(AttributeError, path.find, root)
1934
1935 new_value = "my new value"
1936 path.addattr(root, new_value)
1937
1938 self.assertEquals(1, len(root.c1.c99))
1939 self.assertEquals(new_value, root.c1.c99.text)
1940 self.assertEquals(new_value, path(root).text)
1941
1943 root = self.XML(xml_str)
1944 path = objectify.ObjectPath( "root.c1.c99" )
1945 self.assertRaises(AttributeError, path.find, root)
1946
1947 new_el = self.Element("{objectified}test")
1948 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST"
1949
1950 path.addattr(root, new_el.sub)
1951 self.assertEquals(1, len(root.c1.c99))
1952 self.assertEquals("TEST", root.c1.c99.a.text)
1953 self.assertEquals("TEST", path(root).a.text)
1954 self.assertEquals("ATTR", root.c1.c99.get("myattr"))
1955
1957 root = self.XML(xml_str)
1958 path = objectify.ObjectPath( "root.c1.c99" )
1959 self.assertRaises(AttributeError, path.find, root)
1960
1961 new_el = self.Element("{objectified}test")
1962 new_el.a = ["TEST1", "TEST2"]
1963
1964 self.assertEquals(2, len(new_el.a))
1965
1966 path.addattr(root, list(new_el.a))
1967 self.assertEquals(2, len(root.c1.c99))
1968 self.assertEquals("TEST1", root.c1.c99.text)
1969 self.assertEquals("TEST2", path(root)[1].text)
1970
1972 root = self.XML(xml_str)
1973 self.assertEquals(
1974 ['{objectified}root', '{objectified}root.c1',
1975 '{objectified}root.c1.c2',
1976 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]',
1977 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'],
1978 root.descendantpaths())
1979
1981 root = self.XML(xml_str)
1982 self.assertEquals(
1983 ['{objectified}c1', '{objectified}c1.c2',
1984 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]',
1985 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'],
1986 root.c1.descendantpaths())
1987
1989 root = self.XML(xml_str)
1990 self.assertEquals(
1991 ['root.{objectified}c1', 'root.{objectified}c1.c2',
1992 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]',
1993 'root.{objectified}c1.{otherNS}c2',
1994 'root.{objectified}c1.{}c2'],
1995 root.c1.descendantpaths('root'))
1996
1998 import pickle
1999
2000 root = self.XML(xml_str)
2001 out = StringIO()
2002 pickle.dump(root, out)
2003
2004 new_root = pickle.loads(out.getvalue())
2005 self.assertEquals(
2006 etree.tostring(new_root),
2007 etree.tostring(root))
2008
2009 # E-Factory tests, need to use sub-elements as root element is always
2010 # type-looked-up as ObjectifiedElement (no annotations)
2012 E = objectify.E
2013 root = E.root(E.val(23))
2014 self.assert_(isinstance(root.val, objectify.IntElement))
2015
2017 E = objectify.E
2018 root = E.root(E.val(23L))
2019 self.assert_(isinstance(root.val, objectify.LongElement))
2020
2022 E = objectify.E
2023 root = E.root(E.val(233.23))
2024 self.assert_(isinstance(root.val, objectify.FloatElement))
2025
2027 E = objectify.E
2028 root = E.root(E.val("what?"))
2029 self.assert_(isinstance(root.val, objectify.StringElement))
2030
2032 E = objectify.E
2033 root = E.root(E.val(unicode("blöödy häll", encoding="ISO-8859-1")))
2034 self.assert_(isinstance(root.val, objectify.StringElement))
2035
2037 E = objectify.E
2038 root = E.root(E.val(True))
2039 self.assert_(isinstance(root.val, objectify.BoolElement))
2040
2042 E = objectify.E
2043 root = E.root(E.val(None))
2044 self.assert_(isinstance(root.val, objectify.NoneElement))
2045
2047 E = objectify.E
2048 root = E.root(E.val(1, "foo", 2.0, "bar ", True, None))
2049 self.assert_(isinstance(root.val, objectify.StringElement))
2050
2055
2057 E = objectify.E
2058 DataElement = objectify.DataElement
2059 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1),
2060 DataElement(2.0))
2061 self.assert_(isinstance(root, objectify.ObjectifiedElement))
2062 self.assertEquals(root.text, "text")
2063 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement))
2064 self.assertEquals(root.sub.tail, "tail")
2065 self.assert_(isinstance(root.sub.subsub, objectify.StringElement))
2066 self.assertEquals(len(root.value), 2)
2067 self.assert_(isinstance(root.value[0], objectify.IntElement))
2068 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2069
2071 suite = unittest.TestSuite()
2072 suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
2073 suite.addTests(
2074 [doctest.DocFileSuite('../../../doc/objectify.txt')])
2075 return suite
2076
2077 if __name__ == '__main__':
2078 print 'to test use test.py %s' % __file__
2079
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0beta1 on Fri Jan 11 16:02:49 2008 | http://epydoc.sourceforge.net |