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