1
2
3 """
4 Tests specific to the lxml.objectify API
5 """
6
7
8 import unittest, operator, sys, os.path
9
10 this_dir = os.path.dirname(__file__)
11 if this_dir not in sys.path:
12 sys.path.insert(0, this_dir)
13
14 from common_imports import etree, HelperTestCase, fileInTestDir
15 from common_imports import SillyFileLike, canonicalize, doctest, make_doctest
16 from common_imports import _bytes, _str, StringIO, BytesIO
17
18 from lxml import objectify
19
20 PYTYPE_NAMESPACE = "http://codespeak.net/lxml/objectify/pytype"
21 XML_SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"
22 XML_SCHEMA_INSTANCE_NS = "http://www.w3.org/2001/XMLSchema-instance"
23 XML_SCHEMA_INSTANCE_TYPE_ATTR = "{%s}type" % XML_SCHEMA_INSTANCE_NS
24 XML_SCHEMA_NIL_ATTR = "{%s}nil" % XML_SCHEMA_INSTANCE_NS
25 TREE_PYTYPE = "TREE"
26 DEFAULT_NSMAP = { "py" : PYTYPE_NAMESPACE,
27 "xsi" : XML_SCHEMA_INSTANCE_NS,
28 "xsd" : XML_SCHEMA_NS}
29
30 objectclass2xsitype = {
31
32 objectify.IntElement: ("int", "short", "byte", "unsignedShort",
33 "unsignedByte", "integer", "nonPositiveInteger",
34 "negativeInteger", "long", "nonNegativeInteger",
35 "unsignedLong", "unsignedInt", "positiveInteger",),
36 objectify.FloatElement: ("float", "double"),
37 objectify.BoolElement: ("boolean",),
38 objectify.StringElement: ("string", "normalizedString", "token", "language",
39 "Name", "NCName", "ID", "IDREF", "ENTITY",
40 "NMTOKEN", ),
41
42 }
43
44 xsitype2objclass = dict([ (v, k) for k in objectclass2xsitype
45 for v in objectclass2xsitype[k] ])
46
47 objectclass2pytype = {
48
49 objectify.IntElement: "int",
50 objectify.FloatElement: "float",
51 objectify.BoolElement: "bool",
52 objectify.StringElement: "str",
53
54 }
55
56 pytype2objclass = dict([ (objectclass2pytype[k], k)
57 for k in objectclass2pytype])
58
59 xml_str = '''\
60 <obj:root xmlns:obj="objectified" xmlns:other="otherNS">
61 <obj:c1 a1="A1" a2="A2" other:a3="A3">
62 <obj:c2>0</obj:c2>
63 <obj:c2>1</obj:c2>
64 <obj:c2>2</obj:c2>
65 <other:c2>3</other:c2>
66 <c2>4</c2>
67 </obj:c1>
68 </obj:root>'''
69
71 """Test cases for lxml.objectify
72 """
73 etree = etree
74
77
91
105
106
110
115
122
132
137
143
151
162
166
171
178
188
193
199
207
218
220
221 value = objectify.ObjectifiedDataElement('test', 'toast')
222 self.assertEquals(value.text, 'testtoast')
223
228
230
231 value = objectify.DataElement(23, _pytype="str", _xsi="foobar",
232 attrib={"gnu": "muh", "cat": "meeow",
233 "dog": "wuff"},
234 bird="tchilp", dog="grrr")
235 self.assertEquals(value.get("gnu"), "muh")
236 self.assertEquals(value.get("cat"), "meeow")
237 self.assertEquals(value.get("dog"), "grrr")
238 self.assertEquals(value.get("bird"), "tchilp")
239
251
253
254
255 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
256 attrib={"gnu": "muh", "cat": "meeow",
257 "dog": "wuff"},
258 bird="tchilp", dog="grrr")
259 value = objectify.DataElement(arg, _pytype="NoneType")
260 self.assert_(isinstance(value, objectify.NoneElement))
261 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
262 self.assertEquals(value.text, None)
263 self.assertEquals(value.pyval, None)
264 for attr in arg.attrib:
265
266 self.assertEquals(value.get(attr), arg.get(attr))
267
269
270
271 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
272 attrib={"gnu": "muh", "cat": "meeow",
273 "dog": "wuff"},
274 bird="tchilp", dog="grrr")
275 value = objectify.DataElement(arg, _pytype="int")
276 self.assert_(isinstance(value, objectify.IntElement))
277 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
278 for attr in arg.attrib:
279 if not attr == objectify.PYTYPE_ATTRIBUTE:
280 self.assertEquals(value.get(attr), arg.get(attr))
281
283
284
285 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
286 attrib={"gnu": "muh", "cat": "meeow",
287 "dog": "wuff"},
288 bird="tchilp", dog="grrr")
289 value = objectify.DataElement(arg, _xsi="xsd:int")
290 self.assert_(isinstance(value, objectify.IntElement))
291 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
292 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
293 for attr in arg.attrib:
294 if not attr in [objectify.PYTYPE_ATTRIBUTE,
295 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
296 self.assertEquals(value.get(attr), arg.get(attr))
297
299
300
301 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
302 attrib={"gnu": "muh", "cat": "meeow",
303 "dog": "wuff"},
304 bird="tchilp", dog="grrr")
305 value = objectify.DataElement(arg, _pytype="int", _xsi="xsd:int")
306 self.assert_(isinstance(value, objectify.IntElement))
307 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
308 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
309 for attr in arg.attrib:
310 if not attr in [objectify.PYTYPE_ATTRIBUTE,
311 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
312 self.assertEquals(value.get(attr), arg.get(attr))
313
317
321
326
331
338
342
346
350
355
357 root = self.XML(xml_str)
358 self.assertEquals("0", getattr(root.c1, "{objectified}c2").text)
359 self.assertEquals("3", getattr(root.c1, "{otherNS}c2").text)
360
362 root = self.XML(xml_str)
363 self.assertRaises(AttributeError, getattr, root.c1, "NOT_THERE")
364 self.assertRaises(AttributeError, getattr, root.c1, "{unknownNS}c2")
365
370
372 for val in [
373 2, 2**32, 1.2, "Won't get fooled again",
374 _str("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1'), True,
375 False, None]:
376 root = self.Element('root')
377 attrname = 'val'
378 setattr(root, attrname, val)
379 result = getattr(root, attrname)
380 self.assertEquals(val, result)
381 self.assertEquals(type(val), type(result.pyval))
382
389
396
398 root = self.XML(xml_str)
399 self.assertEquals(1, len(root.c1))
400
401 new_el = self.Element("test", myattr="5")
402 root.addattr("c1", new_el)
403 self.assertEquals(2, len(root.c1))
404 self.assertEquals(None, root.c1[0].get("myattr"))
405 self.assertEquals("5", root.c1[1].get("myattr"))
406
408 root = self.XML(xml_str)
409 self.assertEquals(1, len(root.c1))
410
411 new_el = self.Element("test")
412 self.etree.SubElement(new_el, "a", myattr="A")
413 self.etree.SubElement(new_el, "a", myattr="B")
414
415 root.addattr("c1", list(new_el.a))
416 self.assertEquals(3, len(root.c1))
417 self.assertEquals(None, root.c1[0].get("myattr"))
418 self.assertEquals("A", root.c1[1].get("myattr"))
419 self.assertEquals("B", root.c1[2].get("myattr"))
420
427
429 root = self.XML(xml_str)
430 self.assertEquals("0", root.c1.c2[0].text)
431 self.assertEquals("1", root.c1.c2[1].text)
432 self.assertEquals("2", root.c1.c2[2].text)
433 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
434
436 root = self.XML(xml_str)
437 self.assertEquals("0", root.c1.c2[0].text)
438 self.assertEquals("0", root.c1.c2[-3].text)
439 self.assertEquals("1", root.c1.c2[-2].text)
440 self.assertEquals("2", root.c1.c2[-1].text)
441 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
442
444 root = self.XML(xml_str)
445 self.assertEquals(1, len(root))
446 self.assertEquals(1, len(root.c1))
447 self.assertEquals(3, len(root.c1.c2))
448
457
463
473
478
483
484
485
487 root = self.XML("<root><c>c1</c><c>c2</c></root>")
488 self.assertEquals(["c1", "c2"],
489 [ c.text for c in root.c[:] ])
490
492 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
493 test_list = ["c1", "c2", "c3", "c4"]
494
495 self.assertEquals(test_list,
496 [ c.text for c in root.c[:] ])
497 self.assertEquals(test_list[1:2],
498 [ c.text for c in root.c[1:2] ])
499 self.assertEquals(test_list[-3:-1],
500 [ c.text for c in root.c[-3:-1] ])
501 self.assertEquals(test_list[-3:3],
502 [ c.text for c in root.c[-3:3] ])
503 self.assertEquals(test_list[-3000:3],
504 [ c.text for c in root.c[-3000:3] ])
505 self.assertEquals(test_list[-3:3000],
506 [ c.text for c in root.c[-3:3000] ])
507
509 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
510 test_list = ["c1", "c2", "c3", "c4"]
511
512 self.assertEquals(test_list,
513 [ c.text for c in root.c[:] ])
514 self.assertEquals(test_list[2:1:-1],
515 [ c.text for c in root.c[2:1:-1] ])
516 self.assertEquals(test_list[-1:-3:-1],
517 [ c.text for c in root.c[-1:-3:-1] ])
518 self.assertEquals(test_list[2:-3:-1],
519 [ c.text for c in root.c[2:-3:-1] ])
520 self.assertEquals(test_list[2:-3000:-1],
521 [ c.text for c in root.c[2:-3000:-1] ])
522
523
524
536
538 Element = self.Element
539 root = Element("root")
540 root.c = ["c1", "c2"]
541
542 c1 = root.c[0]
543 c2 = root.c[1]
544
545 self.assertEquals([c1,c2], list(root.c))
546 self.assertEquals(["c1", "c2"],
547 [ c.text for c in root.c ])
548
549 root2 = Element("root2")
550 root2.el = [ "test", "test" ]
551 self.assertEquals(["test", "test"],
552 [ el.text for el in root2.el ])
553
554 root.c = [ root2.el, root2.el ]
555 self.assertEquals(["test", "test"],
556 [ c.text for c in root.c ])
557 self.assertEquals(["test", "test"],
558 [ el.text for el in root2.el ])
559
560 root.c[:] = [ c1, c2, c2, c1 ]
561 self.assertEquals(["c1", "c2", "c2", "c1"],
562 [ c.text for c in root.c ])
563
565 Element = self.Element
566 root = Element("root")
567 l = ["c1", "c2", "c3", "c4"]
568 root.c = l
569
570 self.assertEquals(["c1", "c2", "c3", "c4"],
571 [ c.text for c in root.c ])
572 self.assertEquals(l,
573 [ c.text for c in root.c ])
574
575 new_slice = ["cA", "cB"]
576 l[1:2] = new_slice
577 root.c[1:2] = new_slice
578
579 self.assertEquals(["c1", "cA", "cB", "c3", "c4"], l)
580 self.assertEquals(["c1", "cA", "cB", "c3", "c4"],
581 [ c.text for c in root.c ])
582 self.assertEquals(l,
583 [ c.text for c in root.c ])
584
586 Element = self.Element
587 root = Element("root")
588 l = ["c1", "c2", "c3", "c4"]
589 root.c = l
590
591 self.assertEquals(["c1", "c2", "c3", "c4"],
592 [ c.text for c in root.c ])
593 self.assertEquals(l,
594 [ c.text for c in root.c ])
595
596 new_slice = ["cA", "cB"]
597 l[1:1] = new_slice
598 root.c[1:1] = new_slice
599
600 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"], l)
601 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"],
602 [ c.text for c in root.c ])
603 self.assertEquals(l,
604 [ c.text for c in root.c ])
605
607 Element = self.Element
608 root = Element("root")
609 l = ["c1", "c2", "c3", "c4"]
610 root.c = l
611
612 self.assertEquals(["c1", "c2", "c3", "c4"],
613 [ c.text for c in root.c ])
614 self.assertEquals(l,
615 [ c.text for c in root.c ])
616
617 new_slice = ["cA", "cB"]
618 l[-2:-2] = new_slice
619 root.c[-2:-2] = new_slice
620
621 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"], l)
622 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"],
623 [ c.text for c in root.c ])
624 self.assertEquals(l,
625 [ c.text for c in root.c ])
626
634
636 Element = self.Element
637 root = Element("root")
638 l = ["c1", "c2", "c3", "c4"]
639 root.c = l
640
641 self.assertEquals(["c1", "c2", "c3", "c4"],
642 [ c.text for c in root.c ])
643 self.assertEquals(l,
644 [ c.text for c in root.c ])
645
646 new_slice = ["cA", "cB", "cC"]
647 self.assertRaises(
648 ValueError, operator.setitem,
649 l, slice(1,2,-1), new_slice)
650 self.assertRaises(
651 ValueError, operator.setitem,
652 root.c, slice(1,2,-1), new_slice)
653
655 Element = self.Element
656 root = Element("root")
657 l = ["c1", "c2", "c3", "c4"]
658 root.c = l
659
660 self.assertEquals(["c1", "c2", "c3", "c4"],
661 [ c.text for c in root.c ])
662 self.assertEquals(l,
663 [ c.text for c in root.c ])
664
665 new_slice = ["cA", "cB"]
666 l[-1:1:-1] = new_slice
667 root.c[-1:1:-1] = new_slice
668
669 self.assertEquals(["c1", "c2", "cB", "cA"], l)
670 self.assertEquals(["c1", "c2", "cB", "cA"],
671 [ c.text for c in root.c ])
672 self.assertEquals(l,
673 [ c.text for c in root.c ])
674
676 Element = self.Element
677 root = Element("root")
678 l = ["c1", "c2", "c3", "c4"]
679 root.c = l
680
681 self.assertEquals(["c1", "c2", "c3", "c4"],
682 [ c.text for c in root.c ])
683 self.assertEquals(l,
684 [ c.text for c in root.c ])
685
686 new_slice = ["cA", "cB"]
687 l[-1:-4:-2] = new_slice
688 root.c[-1:-4:-2] = new_slice
689
690 self.assertEquals(["c1", "cB", "c3", "cA"], l)
691 self.assertEquals(["c1", "cB", "c3", "cA"],
692 [ c.text for c in root.c ])
693 self.assertEquals(l,
694 [ c.text for c in root.c ])
695
696
697
705
713
715
716 Element = self.Element
717 root = Element("root")
718
719 root["text"] = "TEST"
720 self.assertEquals(["TEST"],
721 [ c.text for c in root["text"] ])
722
723 root["tail"] = "TEST"
724 self.assertEquals(["TEST"],
725 [ c.text for c in root["tail"] ])
726
727 root["pyval"] = "TEST"
728 self.assertEquals(["TEST"],
729 [ c.text for c in root["pyval"] ])
730
731 root["tag"] = "TEST"
732 self.assertEquals(["TEST"],
733 [ c.text for c in root["tag"] ])
734
742
744 XML = self.XML
745 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
746 self.assertEquals(2, len(root.findall(".//{X}b")))
747 self.assertEquals(3, len(root.findall(".//b")))
748 self.assertEquals(2, len(root.findall("b")))
749
757
772
778
780 Element = self.Element
781 SubElement = self.etree.SubElement
782 root = Element("{objectified}root")
783 root.bool = True
784 self.assertEquals(root.bool, True)
785 self.assertEquals(root.bool + root.bool, True + True)
786 self.assertEquals(True + root.bool, True + root.bool)
787 self.assertEquals(root.bool * root.bool, True * True)
788 self.assertEquals(int(root.bool), int(True))
789 self.assertEquals(hash(root.bool), hash(True))
790 self.assertEquals(complex(root.bool), complex(True))
791 self.assert_(isinstance(root.bool, objectify.BoolElement))
792
793 root.bool = False
794 self.assertEquals(root.bool, False)
795 self.assertEquals(root.bool + root.bool, False + False)
796 self.assertEquals(False + root.bool, False + root.bool)
797 self.assertEquals(root.bool * root.bool, False * False)
798 self.assertEquals(int(root.bool), int(False))
799 self.assertEquals(hash(root.bool), hash(False))
800 self.assertEquals(complex(root.bool), complex(False))
801 self.assert_(isinstance(root.bool, objectify.BoolElement))
802
811
818
825
832
834 Element = self.Element
835 SubElement = self.etree.SubElement
836 root = Element("{objectified}root")
837 root.s = "test"
838
839 self.assertEquals("test" * 5, root.s * 5)
840 self.assertEquals(5 * "test", 5 * root.s)
841
842 self.assertRaises(TypeError, operator.mul, root.s, "honk")
843 self.assertRaises(TypeError, operator.mul, "honk", root.s)
844
854
875
880
885
890
895
904
909
914
919
926
933
940
952
962
967
972
977
984
989
993
1000
1005
1009
1018
1027
1033
1042
1044 pyval = 1
1045 pytype = "NoneType"
1046 objclass = objectify.NoneElement
1047 value = objectify.DataElement(pyval, _pytype=pytype)
1048 self.assert_(isinstance(value, objclass),
1049 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1050 % (pyval, pytype, type(value), objclass))
1051 self.assertEquals(value.text, None)
1052 self.assertEquals(value.pyval, None)
1053
1055
1056 pyval = 1
1057 pytype = "none"
1058 objclass = objectify.NoneElement
1059 value = objectify.DataElement(pyval, _pytype=pytype)
1060 self.assert_(isinstance(value, objclass),
1061 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1062 % (pyval, pytype, type(value), objclass))
1063 self.assertEquals(value.text, None)
1064 self.assertEquals(value.pyval, None)
1065
1071 root = Element("{objectified}root")
1072 root.myfloat = MyFloat(5.5)
1073 self.assert_(isinstance(root.myfloat, objectify.FloatElement))
1074 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
1075
1077 class MyFloat(float):
1078 pass
1079 value = objectify.DataElement(MyFloat(5.5))
1080 self.assert_(isinstance(value, objectify.FloatElement))
1081 self.assertEquals(value, 5.5)
1082 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None)
1083
1085 XML = self.XML
1086 root = XML('''\
1087 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1088 <b xsi:type="boolean">true</b>
1089 <b xsi:type="boolean">false</b>
1090 <b xsi:type="boolean">1</b>
1091 <b xsi:type="boolean">0</b>
1092
1093 <f xsi:type="float">5</f>
1094 <f xsi:type="double">5</f>
1095
1096 <s xsi:type="string">5</s>
1097 <s xsi:type="normalizedString">5</s>
1098 <s xsi:type="token">5</s>
1099 <s xsi:type="language">5</s>
1100 <s xsi:type="Name">5</s>
1101 <s xsi:type="NCName">5</s>
1102 <s xsi:type="ID">5</s>
1103 <s xsi:type="IDREF">5</s>
1104 <s xsi:type="ENTITY">5</s>
1105 <s xsi:type="NMTOKEN">5</s>
1106
1107 <l xsi:type="integer">5</l>
1108 <l xsi:type="nonPositiveInteger">5</l>
1109 <l xsi:type="negativeInteger">5</l>
1110 <l xsi:type="long">5</l>
1111 <l xsi:type="nonNegativeInteger">5</l>
1112 <l xsi:type="unsignedLong">5</l>
1113 <l xsi:type="unsignedInt">5</l>
1114 <l xsi:type="positiveInteger">5</l>
1115
1116 <i xsi:type="int">5</i>
1117 <i xsi:type="short">5</i>
1118 <i xsi:type="byte">5</i>
1119 <i xsi:type="unsignedShort">5</i>
1120 <i xsi:type="unsignedByte">5</i>
1121
1122 <n xsi:nil="true"/>
1123 </root>
1124 ''')
1125
1126 for b in root.b:
1127 self.assert_(isinstance(b, objectify.BoolElement))
1128 self.assertEquals(True, root.b[0])
1129 self.assertEquals(False, root.b[1])
1130 self.assertEquals(True, root.b[2])
1131 self.assertEquals(False, root.b[3])
1132
1133 for f in root.f:
1134 self.assert_(isinstance(f, objectify.FloatElement))
1135 self.assertEquals(5, f)
1136
1137 for s in root.s:
1138 self.assert_(isinstance(s, objectify.StringElement))
1139 self.assertEquals("5", s)
1140
1141 for i in root.i:
1142 self.assert_(isinstance(i, objectify.IntElement))
1143 self.assertEquals(5, i)
1144
1145 for l in root.l:
1146 self.assert_(isinstance(l, objectify.IntElement))
1147 self.assertEquals(5, i)
1148
1149 self.assert_(isinstance(root.n, objectify.NoneElement))
1150 self.assertEquals(None, root.n)
1151
1153 XML = self.XML
1154 root = XML('''\
1155 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1156 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1157 <b xsi:type="xsd:boolean">true</b>
1158 <b xsi:type="xsd:boolean">false</b>
1159 <b xsi:type="xsd:boolean">1</b>
1160 <b xsi:type="xsd:boolean">0</b>
1161
1162 <f xsi:type="xsd:float">5</f>
1163 <f xsi:type="xsd:double">5</f>
1164
1165 <s xsi:type="xsd:string">5</s>
1166 <s xsi:type="xsd:normalizedString">5</s>
1167 <s xsi:type="xsd:token">5</s>
1168 <s xsi:type="xsd:language">5</s>
1169 <s xsi:type="xsd:Name">5</s>
1170 <s xsi:type="xsd:NCName">5</s>
1171 <s xsi:type="xsd:ID">5</s>
1172 <s xsi:type="xsd:IDREF">5</s>
1173 <s xsi:type="xsd:ENTITY">5</s>
1174 <s xsi:type="xsd:NMTOKEN">5</s>
1175
1176 <l xsi:type="xsd:integer">5</l>
1177 <l xsi:type="xsd:nonPositiveInteger">5</l>
1178 <l xsi:type="xsd:negativeInteger">5</l>
1179 <l xsi:type="xsd:long">5</l>
1180 <l xsi:type="xsd:nonNegativeInteger">5</l>
1181 <l xsi:type="xsd:unsignedLong">5</l>
1182 <l xsi:type="xsd:unsignedInt">5</l>
1183 <l xsi:type="xsd:positiveInteger">5</l>
1184
1185 <i xsi:type="xsd:int">5</i>
1186 <i xsi:type="xsd:short">5</i>
1187 <i xsi:type="xsd:byte">5</i>
1188 <i xsi:type="xsd:unsignedShort">5</i>
1189 <i xsi:type="xsd:unsignedByte">5</i>
1190
1191 <n xsi:nil="true"/>
1192 </root>
1193 ''')
1194
1195 for b in root.b:
1196 self.assert_(isinstance(b, objectify.BoolElement))
1197 self.assertEquals(True, root.b[0])
1198 self.assertEquals(False, root.b[1])
1199 self.assertEquals(True, root.b[2])
1200 self.assertEquals(False, root.b[3])
1201
1202 for f in root.f:
1203 self.assert_(isinstance(f, objectify.FloatElement))
1204 self.assertEquals(5, f)
1205
1206 for s in root.s:
1207 self.assert_(isinstance(s, objectify.StringElement))
1208 self.assertEquals("5", s)
1209
1210 for i in root.i:
1211 self.assert_(isinstance(i, objectify.IntElement))
1212 self.assertEquals(5, i)
1213
1214 for l in root.l:
1215 self.assert_(isinstance(l, objectify.IntElement))
1216 self.assertEquals(5, l)
1217
1218 self.assert_(isinstance(root.n, objectify.NoneElement))
1219 self.assertEquals(None, root.n)
1220
1222 XML = self.XML
1223 root = XML(_bytes('<root><b>why</b><b>try</b></root>'))
1224 strs = [ str(s) for s in root.b ]
1225 self.assertEquals(["why", "try"],
1226 strs)
1227
1254
1274
1275
1276
1300
1302 XML = self.XML
1303 root = XML(_bytes("""
1304 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1305 <b xsi:nil="true"></b><b xsi:nil="true"/>
1306 </root>"""))
1307 self.assert_(root.b[0] == root.b[1])
1308 self.assertFalse(root.b[0])
1309 self.assertEquals(root.b[0], None)
1310 self.assertEquals(None, root.b[0])
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1326
1333
1337
1339 XML = self.XML
1340 root = XML(_bytes('''\
1341 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1342 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1343 <b>5</b>
1344 <b>test</b>
1345 <c>1.1</c>
1346 <c>\uF8D2</c>
1347 <x>true</x>
1348 <n xsi:nil="true" />
1349 <n></n>
1350 <b xsi:type="double">5</b>
1351 <b xsi:type="float">5</b>
1352 <s xsi:type="string">23</s>
1353 <s py:pytype="str">42</s>
1354 <f py:pytype="float">300</f>
1355 <l py:pytype="long">2</l>
1356 <t py:pytype="TREE"></t>
1357 </a>
1358 '''))
1359 objectify.annotate(root)
1360
1361 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1362 for c in root.iterchildren() ]
1363 self.assertEquals("int", child_types[ 0])
1364 self.assertEquals("str", child_types[ 1])
1365 self.assertEquals("float", child_types[ 2])
1366 self.assertEquals("str", child_types[ 3])
1367 self.assertEquals("bool", child_types[ 4])
1368 self.assertEquals("NoneType", child_types[ 5])
1369 self.assertEquals(None, child_types[ 6])
1370 self.assertEquals("float", child_types[ 7])
1371 self.assertEquals("float", child_types[ 8])
1372 self.assertEquals("str", child_types[ 9])
1373 self.assertEquals("int", child_types[10])
1374 self.assertEquals("int", child_types[11])
1375 self.assertEquals("int", child_types[12])
1376 self.assertEquals(None, child_types[13])
1377
1378 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1379
1399
1401 XML = self.XML
1402 root = XML(_bytes('''\
1403 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1404 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1405 <b>5</b>
1406 <b>test</b>
1407 <c>1.1</c>
1408 <c>\uF8D2</c>
1409 <x>true</x>
1410 <n xsi:nil="true" />
1411 <n></n>
1412 <b xsi:type="double">5</b>
1413 <b xsi:type="float">5</b>
1414 <s xsi:type="string">23</s>
1415 <s py:pytype="str">42</s>
1416 <f py:pytype="float">300</f>
1417 <l py:pytype="long">2</l>
1418 <t py:pytype="TREE"></t>
1419 </a>
1420 '''))
1421 objectify.annotate(root, ignore_old=False)
1422
1423 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1424 for c in root.iterchildren() ]
1425 self.assertEquals("int", child_types[ 0])
1426 self.assertEquals("str", child_types[ 1])
1427 self.assertEquals("float", child_types[ 2])
1428 self.assertEquals("str", child_types[ 3])
1429 self.assertEquals("bool", child_types[ 4])
1430 self.assertEquals("NoneType", child_types[ 5])
1431 self.assertEquals(None, child_types[ 6])
1432 self.assertEquals("float", child_types[ 7])
1433 self.assertEquals("float", child_types[ 8])
1434 self.assertEquals("str", child_types[ 9])
1435 self.assertEquals("str", child_types[10])
1436 self.assertEquals("float", child_types[11])
1437 self.assertEquals("int", child_types[12])
1438 self.assertEquals(TREE_PYTYPE, child_types[13])
1439
1440 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1441
1443 XML = self.XML
1444 root = XML(_bytes('''\
1445 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1446 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1447 <b>5</b>
1448 <b>test</b>
1449 <c>1.1</c>
1450 <c>\uF8D2</c>
1451 <x>true</x>
1452 <n xsi:nil="true" />
1453 <n></n>
1454 <b xsi:type="double">5</b>
1455 <b xsi:type="float">5</b>
1456 <s xsi:type="string">23</s>
1457 <s py:pytype="str">42</s>
1458 <f py:pytype="float">300</f>
1459 <l py:pytype="long">2</l>
1460 <t py:pytype="TREE"></t>
1461 </a>
1462 '''))
1463 objectify.annotate(root, ignore_old=False, ignore_xsi=False,
1464 annotate_xsi=1, annotate_pytype=1)
1465
1466
1467 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1468 for c in root.iterchildren() ]
1469 self.assertEquals("int", child_types[ 0])
1470 self.assertEquals("str", child_types[ 1])
1471 self.assertEquals("float", child_types[ 2])
1472 self.assertEquals("str", child_types[ 3])
1473 self.assertEquals("bool", child_types[ 4])
1474 self.assertEquals("NoneType", child_types[ 5])
1475 self.assertEquals(None, child_types[ 6])
1476 self.assertEquals("float", child_types[ 7])
1477 self.assertEquals("float", child_types[ 8])
1478 self.assertEquals("str", child_types[ 9])
1479 self.assertEquals("str", child_types[10])
1480 self.assertEquals("float", child_types[11])
1481 self.assertEquals("int", child_types[12])
1482 self.assertEquals(TREE_PYTYPE, child_types[13])
1483
1484 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1485
1486 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1487 for c in root.iterchildren() ]
1488
1489
1490 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1491 for c in root.iterchildren() ]
1492 self.assertEquals("xsd:integer", child_types[ 0])
1493 self.assertEquals("xsd:string", child_types[ 1])
1494 self.assertEquals("xsd:double", child_types[ 2])
1495 self.assertEquals("xsd:string", child_types[ 3])
1496 self.assertEquals("xsd:boolean", child_types[ 4])
1497 self.assertEquals(None, child_types[ 5])
1498 self.assertEquals(None, child_types[ 6])
1499 self.assertEquals("xsd:double", child_types[ 7])
1500 self.assertEquals("xsd:float", child_types[ 8])
1501 self.assertEquals("xsd:string", child_types[ 9])
1502 self.assertEquals("xsd:string", child_types[10])
1503 self.assertEquals("xsd:double", child_types[11])
1504 self.assertEquals("xsd:integer", child_types[12])
1505 self.assertEquals(None, child_types[13])
1506
1507 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1508
1510 XML = self.XML
1511 root = XML(_bytes('''\
1512 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1513 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1514 <b>5</b>
1515 <b>test</b>
1516 <c>1.1</c>
1517 <c>\uF8D2</c>
1518 <x>true</x>
1519 <n xsi:nil="true" />
1520 <n></n>
1521 <b xsi:type="double">5</b>
1522 <b xsi:type="float">5</b>
1523 <s xsi:type="string">23</s>
1524 <s py:pytype="str">42</s>
1525 <f py:pytype="float">300</f>
1526 <l py:pytype="long">2</l>
1527 <t py:pytype="TREE"></t>
1528 </a>
1529 '''))
1530 objectify.xsiannotate(root, ignore_old=False)
1531
1532 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1533 for c in root.iterchildren() ]
1534 self.assertEquals("xsd:integer", child_types[ 0])
1535 self.assertEquals("xsd:string", child_types[ 1])
1536 self.assertEquals("xsd:double", child_types[ 2])
1537 self.assertEquals("xsd:string", child_types[ 3])
1538 self.assertEquals("xsd:boolean", child_types[ 4])
1539 self.assertEquals(None, child_types[ 5])
1540 self.assertEquals(None, child_types[ 6])
1541 self.assertEquals("xsd:double", child_types[ 7])
1542 self.assertEquals("xsd:float", child_types[ 8])
1543 self.assertEquals("xsd:string", child_types[ 9])
1544 self.assertEquals("xsd:string", child_types[10])
1545 self.assertEquals("xsd:double", child_types[11])
1546 self.assertEquals("xsd:integer", child_types[12])
1547 self.assertEquals(None, child_types[13])
1548
1550 XML = self.XML
1551 root = XML(_bytes('''\
1552 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1553 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1554 <b>5</b>
1555 <b>test</b>
1556 <c>1.1</c>
1557 <c>\uF8D2</c>
1558 <x>true</x>
1559 <n xsi:nil="true" />
1560 <n></n>
1561 <b xsi:type="double">5</b>
1562 <b xsi:type="float">5</b>
1563 <s xsi:type="string">23</s>
1564 <s py:pytype="str">42</s>
1565 <f py:pytype="float">300</f>
1566 <l py:pytype="long">2</l>
1567 <t py:pytype="TREE"></t>
1568 </a>
1569 '''))
1570 objectify.pyannotate(root, ignore_old=True)
1571
1572 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1573 for c in root.iterchildren() ]
1574 self.assertEquals("int", child_types[ 0])
1575 self.assertEquals("str", child_types[ 1])
1576 self.assertEquals("float", child_types[ 2])
1577 self.assertEquals("str", child_types[ 3])
1578 self.assertEquals("bool", child_types[ 4])
1579 self.assertEquals("NoneType", child_types[ 5])
1580 self.assertEquals(None, child_types[ 6])
1581 self.assertEquals("float", child_types[ 7])
1582 self.assertEquals("float", child_types[ 8])
1583 self.assertEquals("str", child_types[ 9])
1584 self.assertEquals("int", child_types[10])
1585 self.assertEquals("int", child_types[11])
1586 self.assertEquals("int", child_types[12])
1587 self.assertEquals(None, child_types[13])
1588
1589 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1590
1610
1612 XML = self.XML
1613 root = XML('''\
1614 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1615 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1616 <b>5</b>
1617 <b>test</b>
1618 <c>1.1</c>
1619 <c>\uF8D2</c>
1620 <x>true</x>
1621 <n xsi:nil="true" />
1622 <n></n>
1623 <b xsi:type="double">5</b>
1624 <b xsi:type="float">5</b>
1625 <s xsi:type="string">23</s>
1626 <s py:pytype="str">42</s>
1627 <f py:pytype="float">300</f>
1628 <l py:pytype="long">2</l>
1629 <t py:pytype="TREE"></t>
1630 </a>
1631 ''')
1632 objectify.pyannotate(root)
1633
1634 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1635 for c in root.iterchildren() ]
1636 self.assertEquals("int", child_types[ 0])
1637 self.assertEquals("str", child_types[ 1])
1638 self.assertEquals("float", child_types[ 2])
1639 self.assertEquals("str", child_types[ 3])
1640 self.assertEquals("bool", child_types[ 4])
1641 self.assertEquals("NoneType", child_types[ 5])
1642 self.assertEquals(None, child_types[ 6])
1643 self.assertEquals("float", child_types[ 7])
1644 self.assertEquals("float", child_types[ 8])
1645 self.assertEquals("str", child_types[ 9])
1646 self.assertEquals("str", child_types[10])
1647 self.assertEquals("float", child_types[11])
1648 self.assertEquals("int", child_types[12])
1649 self.assertEquals(TREE_PYTYPE, child_types[13])
1650
1651 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1652
1654 XML = self.XML
1655 root = XML(_bytes('''\
1656 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1657 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1658 <b>5</b>
1659 <b>test</b>
1660 <c>1.1</c>
1661 <c>\uF8D2</c>
1662 <x>true</x>
1663 <n xsi:nil="true" />
1664 <n></n>
1665 <b xsi:type="double">5</b>
1666 <b xsi:type="float">5</b>
1667 <s xsi:type="string">23</s>
1668 <s py:pytype="str">42</s>
1669 <f py:pytype="float">300</f>
1670 <l py:pytype="long">2</l>
1671 <t py:pytype="TREE"></t>
1672 </a>
1673 '''))
1674 objectify.xsiannotate(root, ignore_old=True)
1675
1676 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1677 for c in root.iterchildren() ]
1678 self.assertEquals("xsd:integer", child_types[ 0])
1679 self.assertEquals("xsd:string", child_types[ 1])
1680 self.assertEquals("xsd:double", child_types[ 2])
1681 self.assertEquals("xsd:string", child_types[ 3])
1682 self.assertEquals("xsd:boolean", child_types[ 4])
1683 self.assertEquals(None, child_types[ 5])
1684 self.assertEquals(None, child_types[ 6])
1685 self.assertEquals("xsd:integer", child_types[ 7])
1686 self.assertEquals("xsd:integer", child_types[ 8])
1687 self.assertEquals("xsd:integer", child_types[ 9])
1688 self.assertEquals("xsd:string", child_types[10])
1689 self.assertEquals("xsd:double", child_types[11])
1690 self.assertEquals("xsd:integer", child_types[12])
1691 self.assertEquals(None, child_types[13])
1692
1693 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1694
1696 XML = self.XML
1697 root = XML(_bytes('''\
1698 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1699 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1700 <b>5</b>
1701 <b>test</b>
1702 <c>1.1</c>
1703 <c>\uF8D2</c>
1704 <x>true</x>
1705 <n xsi:nil="true" />
1706 <n></n>
1707 <b xsi:type="double">5</b>
1708 <b xsi:type="float">5</b>
1709 <s xsi:type="string">23</s>
1710 <s py:pytype="str">42</s>
1711 <f py:pytype="float">300</f>
1712 <l py:pytype="long">2</l>
1713 <t py:pytype="TREE"></t>
1714 </a>
1715 '''))
1716 objectify.deannotate(root)
1717
1718 for c in root.getiterator():
1719 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1720 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1721
1722 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1723
1725 XML = self.XML
1726 root = XML(_bytes('''\
1727 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1728 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1729 <b>5</b>
1730 <b>test</b>
1731 <c>1.1</c>
1732 <c>\uF8D2</c>
1733 <x>true</x>
1734 <n xsi:nil="true" />
1735 <n></n>
1736 <b xsi:type="double">5</b>
1737 <b xsi:type="float">5</b>
1738 <s xsi:type="string">23</s>
1739 <s py:pytype="str">42</s>
1740 <f py:pytype="float">300</f>
1741 <l py:pytype="long">2</l>
1742 <t py:pytype="TREE"></t>
1743 </a>
1744 '''))
1745 objectify.annotate(
1746 root, ignore_old=False, ignore_xsi=False, annotate_xsi=True,
1747 empty_pytype='str', empty_type='string')
1748 objectify.deannotate(root, pytype=False, xsi=False, xsi_nil=True)
1749
1750 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1751 for c in root.iterchildren() ]
1752 self.assertEquals("xsd:integer", child_types[ 0])
1753 self.assertEquals("xsd:string", child_types[ 1])
1754 self.assertEquals("xsd:double", child_types[ 2])
1755 self.assertEquals("xsd:string", child_types[ 3])
1756 self.assertEquals("xsd:boolean", child_types[ 4])
1757 self.assertEquals(None, child_types[ 5])
1758 self.assertEquals("xsd:string", child_types[ 6])
1759 self.assertEquals("xsd:double", child_types[ 7])
1760 self.assertEquals("xsd:float", child_types[ 8])
1761 self.assertEquals("xsd:string", child_types[ 9])
1762 self.assertEquals("xsd:string", child_types[10])
1763 self.assertEquals("xsd:double", child_types[11])
1764 self.assertEquals("xsd:integer", child_types[12])
1765 self.assertEquals(None, child_types[13])
1766
1767 self.assertEquals(None, root.n.get(XML_SCHEMA_NIL_ATTR))
1768
1769 for c in root.iterchildren():
1770 self.assertNotEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1771
1772 if (c.get(objectify.PYTYPE_ATTRIBUTE) not in [TREE_PYTYPE,
1773 "NoneType"]):
1774 self.assertNotEquals(
1775 None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1776
1778 XML = self.XML
1779 root = XML(_bytes('''\
1780 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1781 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1782 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1783 <b>5</b>
1784 <b>test</b>
1785 <c>1.1</c>
1786 <c>\uF8D2</c>
1787 <x>true</x>
1788 <n xsi:nil="true" />
1789 <n></n>
1790 <b xsi:type="xsd:double">5</b>
1791 <b xsi:type="xsd:float">5</b>
1792 <s xsi:type="xsd:string">23</s>
1793 <s py:pytype="str">42</s>
1794 <f py:pytype="float">300</f>
1795 <l py:pytype="long">2</l>
1796 <t py:pytype="TREE"></t>
1797 </a>
1798 '''))
1799 objectify.annotate(root)
1800 objectify.deannotate(root, pytype=False)
1801
1802 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1803 for c in root.iterchildren() ]
1804 self.assertEquals("int", child_types[ 0])
1805 self.assertEquals("str", child_types[ 1])
1806 self.assertEquals("float", child_types[ 2])
1807 self.assertEquals("str", child_types[ 3])
1808 self.assertEquals("bool", child_types[ 4])
1809 self.assertEquals("NoneType", child_types[ 5])
1810 self.assertEquals(None, child_types[ 6])
1811 self.assertEquals("float", child_types[ 7])
1812 self.assertEquals("float", child_types[ 8])
1813 self.assertEquals("str", child_types[ 9])
1814 self.assertEquals("int", child_types[10])
1815 self.assertEquals("int", child_types[11])
1816 self.assertEquals("int", child_types[12])
1817 self.assertEquals(None, child_types[13])
1818
1819 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1820
1821 for c in root.getiterator():
1822 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1823
1825 XML = self.XML
1826 root = XML(_bytes('''\
1827 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1828 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1829 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1830 <b xsi:type="xsd:int">5</b>
1831 <b xsi:type="xsd:string">test</b>
1832 <c xsi:type="xsd:float">1.1</c>
1833 <c xsi:type="xsd:string">\uF8D2</c>
1834 <x xsi:type="xsd:boolean">true</x>
1835 <n xsi:nil="true" />
1836 <n></n>
1837 <b xsi:type="xsd:double">5</b>
1838 <b xsi:type="xsd:float">5</b>
1839 <s xsi:type="xsd:string">23</s>
1840 <s xsi:type="xsd:string">42</s>
1841 <f xsi:type="xsd:float">300</f>
1842 <l xsi:type="xsd:long">2</l>
1843 <t py:pytype="TREE"></t>
1844 </a>
1845 '''))
1846 objectify.annotate(root)
1847 objectify.deannotate(root, xsi=False)
1848
1849 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1850 for c in root.iterchildren() ]
1851 self.assertEquals("xsd:int", child_types[ 0])
1852 self.assertEquals("xsd:string", child_types[ 1])
1853 self.assertEquals("xsd:float", child_types[ 2])
1854 self.assertEquals("xsd:string", child_types[ 3])
1855 self.assertEquals("xsd:boolean", child_types[ 4])
1856 self.assertEquals(None, child_types[ 5])
1857 self.assertEquals(None, child_types[ 6])
1858 self.assertEquals("xsd:double", child_types[ 7])
1859 self.assertEquals("xsd:float", child_types[ 8])
1860 self.assertEquals("xsd:string", child_types[ 9])
1861 self.assertEquals("xsd:string", child_types[10])
1862 self.assertEquals("xsd:float", child_types[11])
1863 self.assertEquals("xsd:long", child_types[12])
1864 self.assertEquals(None, child_types[13])
1865
1866 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1867
1868 for c in root.getiterator():
1869 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1870
1872 XML = self.XML
1873
1874 xml = _bytes('''\
1875 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1876 <b>5</b>
1877 <b>test</b>
1878 <c>1.1</c>
1879 <c>\uF8D2</c>
1880 <x>true</x>
1881 <n xsi:nil="true" />
1882 <n></n>
1883 <b xsi:type="double">5</b>
1884 </a>
1885 ''')
1886
1887 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1888 objectify.set_pytype_attribute_tag("{TEST}test")
1889
1890 root = XML(xml)
1891 objectify.annotate(root)
1892
1893 attribs = root.xpath("//@py:%s" % pytype_name,
1894 namespaces={"py" : pytype_ns})
1895 self.assertEquals(0, len(attribs))
1896 attribs = root.xpath("//@py:test",
1897 namespaces={"py" : "TEST"})
1898 self.assertEquals(7, len(attribs))
1899
1900 objectify.set_pytype_attribute_tag()
1901 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1902
1903 self.assertNotEqual("test", pytype_ns.lower())
1904 self.assertNotEqual("test", pytype_name.lower())
1905
1906 root = XML(xml)
1907 attribs = root.xpath("//@py:%s" % pytype_name,
1908 namespaces={"py" : pytype_ns})
1909 self.assertEquals(0, len(attribs))
1910
1911 objectify.annotate(root)
1912 attribs = root.xpath("//@py:%s" % pytype_name,
1913 namespaces={"py" : pytype_ns})
1914 self.assertEquals(7, len(attribs))
1915
1923
1924 def checkMyType(s):
1925 return True
1926
1927 pytype = objectify.PyType("mytype", checkMyType, NewType)
1928 self.assert_(pytype not in objectify.getRegisteredTypes())
1929 pytype.register()
1930 self.assert_(pytype in objectify.getRegisteredTypes())
1931 pytype.unregister()
1932 self.assert_(pytype not in objectify.getRegisteredTypes())
1933
1934 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1935 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1936 pytype.unregister()
1937
1938 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1939 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1940 pytype.unregister()
1941
1942 self.assertRaises(ValueError, pytype.register,
1943 before = [objectify.getRegisteredTypes()[0].name],
1944 after = [objectify.getRegisteredTypes()[1].name])
1945
1947 from datetime import datetime
1948 def parse_date(value):
1949 if len(value) != 14:
1950 raise ValueError(value)
1951 Y = int(value[0:4])
1952 M = int(value[4:6])
1953 D = int(value[6:8])
1954 h = int(value[8:10])
1955 m = int(value[10:12])
1956 s = int(value[12:14])
1957 return datetime(Y, M, D, h, m, s)
1958
1959 def stringify_date(date):
1960 return date.strftime("%Y%m%d%H%M%S")
1961
1962 class DatetimeElement(objectify.ObjectifiedDataElement):
1963 def pyval(self):
1964 return parse_date(self.text)
1965 pyval = property(pyval)
1966
1967 datetime_type = objectify.PyType(
1968 "datetime", parse_date, DatetimeElement, stringify_date)
1969 datetime_type.xmlSchemaTypes = "dateTime"
1970 datetime_type.register()
1971
1972 NAMESPACE = "http://foo.net/xmlns"
1973 NAMESPACE_MAP = {'ns': NAMESPACE}
1974
1975 r = objectify.Element("{%s}root" % NAMESPACE, nsmap=NAMESPACE_MAP)
1976 time = datetime.now()
1977 r.date = time
1978
1979 self.assert_(isinstance(r.date, DatetimeElement))
1980 self.assert_(isinstance(r.date.pyval, datetime))
1981
1982 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1983 self.assertEquals(r.date.text, stringify_date(time))
1984
1985 r.date = objectify.E.date(time)
1986
1987 self.assert_(isinstance(r.date, DatetimeElement))
1988 self.assert_(isinstance(r.date.pyval, datetime))
1989
1990 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1991 self.assertEquals(r.date.text, stringify_date(time))
1992
1993 date = objectify.DataElement(time)
1994
1995 self.assert_(isinstance(date, DatetimeElement))
1996 self.assert_(isinstance(date.pyval, datetime))
1997
1998 self.assertEquals(date.pyval, parse_date(stringify_date(time)))
1999 self.assertEquals(date.text, stringify_date(time))
2000
2006
2012
2017
2026
2033
2041
2044
2047
2066
2071
2076
2081
2086
2106
2108 root = self.XML(xml_str)
2109 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
2110 self.assertEquals(root.c1.c2.text, path(root).text)
2111
2112 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
2113 self.assertEquals(root.c1.c2[2].text, path(root).text)
2114
2115 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
2116 self.assertEquals(root.c1.c2[2].text, path(root).text)
2117
2118 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
2119 self.assertEquals(root.c1.c2[-1].text, path(root).text)
2120
2121 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
2122 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2123
2125 self.assertRaises(ValueError, objectify.ObjectPath,
2126 "root.c1[0].c2[-1-2]")
2127 self.assertRaises(ValueError, objectify.ObjectPath,
2128 ['root', 'c1[0]', 'c2[-1-2]'])
2129
2130 self.assertRaises(ValueError, objectify.ObjectPath,
2131 "root[2].c1.c2")
2132 self.assertRaises(ValueError, objectify.ObjectPath,
2133 ['root[2]', 'c1', 'c2'])
2134
2135 self.assertRaises(ValueError, objectify.ObjectPath,
2136 [])
2137 self.assertRaises(ValueError, objectify.ObjectPath,
2138 ['', '', ''])
2139
2141 root = self.XML(xml_str)
2142 path = objectify.ObjectPath("root.c1[9999].c2")
2143 self.assertRaises(AttributeError, path, root)
2144
2145 path = objectify.ObjectPath("root.c1[0].c2[9999]")
2146 self.assertRaises(AttributeError, path, root)
2147
2148 path = objectify.ObjectPath(".c1[9999].c2[0]")
2149 self.assertRaises(AttributeError, path, root)
2150
2151 path = objectify.ObjectPath("root.c1[-2].c2")
2152 self.assertRaises(AttributeError, path, root)
2153
2154 path = objectify.ObjectPath("root.c1[0].c2[-4]")
2155 self.assertRaises(AttributeError, path, root)
2156
2170
2172 root = self.XML(xml_str)
2173 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
2174 self.assertEquals(root.c1.c2.text, path.find(root).text)
2175 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
2176 self.assertEquals(root.c1.c2.text, path.find(root).text)
2177 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
2178 self.assertEquals(root.c1.c2.text, path.find(root).text)
2179 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
2180 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2181 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
2182 self.assertEquals(root.c1.c2.text, path.find(root).text)
2183 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
2184 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2185 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
2186 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2187 path.find(root).text)
2188
2201
2216
2228
2242
2244 root = self.XML(xml_str)
2245 path = objectify.ObjectPath( "root.c1.c99" )
2246 self.assertRaises(AttributeError, path.find, root)
2247
2248 new_el = self.Element("{objectified}test")
2249 new_el.a = ["TEST1", "TEST2"]
2250 new_el.a[0].set("myattr", "ATTR1")
2251 new_el.a[1].set("myattr", "ATTR2")
2252
2253 path.setattr(root, list(new_el.a))
2254
2255 self.assertEquals(2, len(root.c1.c99))
2256 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
2257 self.assertEquals("TEST1", root.c1.c99[0].text)
2258 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
2259 self.assertEquals("TEST2", root.c1.c99[1].text)
2260 self.assertEquals("TEST1", path(root).text)
2261
2270
2284
2296
2310
2325
2327 root = self.XML(xml_str)
2328 self.assertEquals(
2329 ['{objectified}root', '{objectified}root.c1',
2330 '{objectified}root.c1.c2',
2331 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]',
2332 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'],
2333 root.descendantpaths())
2334
2336 root = self.XML(xml_str)
2337 self.assertEquals(
2338 ['{objectified}c1', '{objectified}c1.c2',
2339 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]',
2340 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'],
2341 root.c1.descendantpaths())
2342
2344 root = self.XML(xml_str)
2345 self.assertEquals(
2346 ['root.{objectified}c1', 'root.{objectified}c1.c2',
2347 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]',
2348 'root.{objectified}c1.{otherNS}c2',
2349 'root.{objectified}c1.{}c2'],
2350 root.c1.descendantpaths('root'))
2351
2363
2376
2380
2384
2388
2394
2399
2401 import pickle
2402 if isinstance(stringOrElt, (etree._Element, etree._ElementTree)):
2403 elt = stringOrElt
2404 else:
2405 elt = self.XML(stringOrElt)
2406 out = BytesIO()
2407 pickle.dump(elt, out)
2408
2409 new_elt = pickle.loads(out.getvalue())
2410 self.assertEquals(
2411 etree.tostring(new_elt),
2412 etree.tostring(elt))
2413
2414
2415
2420
2425
2430
2435
2440
2445
2450
2455
2457 E = objectify.E
2458 DataElement = objectify.DataElement
2459 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1),
2460 DataElement(2.0))
2461 self.assert_(isinstance(root, objectify.ObjectifiedElement))
2462 self.assertEquals(root.text, "text")
2463 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement))
2464 self.assertEquals(root.sub.tail, "tail")
2465 self.assert_(isinstance(root.sub.subsub, objectify.StringElement))
2466 self.assertEquals(len(root.value), 2)
2467 self.assert_(isinstance(root.value[0], objectify.IntElement))
2468 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2469
2476
2477 attr = Attribute()
2478 self.assertEquals(attr.text, None)
2479 self.assertEquals(attr.get("datatype"), "TYPE")
2480 self.assertEquals(attr.get("range"), "0.,1.")
2481
2486
2493
2498
2504
2506 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2507 self.assertEquals(root.base, "http://no/such/url")
2508 self.assertEquals(
2509 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2510 root.base = "https://secret/url"
2511 self.assertEquals(root.base, "https://secret/url")
2512 self.assertEquals(
2513 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2514 "https://secret/url")
2515
2517 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2518 self.assertEquals(root.base, "http://no/such/url")
2519 self.assertEquals(
2520 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2521 root.set('{http://www.w3.org/XML/1998/namespace}base',
2522 "https://secret/url")
2523 self.assertEquals(root.base, "https://secret/url")
2524 self.assertEquals(
2525 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2526 "https://secret/url")
2527
2529 XML = self.XML
2530
2531 xml = _bytes('''\
2532 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2533 <i>5</i>
2534 <i>-5</i>
2535 <l>4294967296</l>
2536 <l>-4294967296</l>
2537 <f>1.1</f>
2538 <b>true</b>
2539 <b>false</b>
2540 <s>Strange things happen, where strings collide</s>
2541 <s>True</s>
2542 <s>False</s>
2543 <s>t</s>
2544 <s>f</s>
2545 <s></s>
2546 <s>None</s>
2547 <n xsi:nil="true" />
2548 </root>
2549 ''')
2550 root = XML(xml)
2551
2552 for i in root.i:
2553 self.assert_(isinstance(i, objectify.IntElement))
2554 for l in root.l:
2555 self.assert_(isinstance(l, objectify.IntElement))
2556 for f in root.f:
2557 self.assert_(isinstance(f, objectify.FloatElement))
2558 for b in root.b:
2559 self.assert_(isinstance(b, objectify.BoolElement))
2560 self.assertEquals(True, root.b[0])
2561 self.assertEquals(False, root.b[1])
2562 for s in root.s:
2563 self.assert_(isinstance(s, objectify.StringElement))
2564 self.assert_(isinstance(root.n, objectify.NoneElement))
2565 self.assertEquals(None, root.n)
2566
2568 suite = unittest.TestSuite()
2569 suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
2570 suite.addTests(doctest.DocTestSuite(objectify))
2571 if sys.version_info >= (2,4):
2572 suite.addTests(
2573 [make_doctest('../../../doc/objectify.txt')])
2574 return suite
2575
2576 if __name__ == '__main__':
2577 print('to test use test.py %s' % __file__)
2578