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.DataElement(23, _pytype="str", _xsi="foobar",
222 attrib={"gnu": "muh", "cat": "meeow",
223 "dog": "wuff"},
224 bird="tchilp", dog="grrr")
225 self.assertEquals(value.get("gnu"), "muh")
226 self.assertEquals(value.get("cat"), "meeow")
227 self.assertEquals(value.get("dog"), "grrr")
228 self.assertEquals(value.get("bird"), "tchilp")
229
241
243
244
245 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
246 attrib={"gnu": "muh", "cat": "meeow",
247 "dog": "wuff"},
248 bird="tchilp", dog="grrr")
249 value = objectify.DataElement(arg, _pytype="NoneType")
250 self.assert_(isinstance(value, objectify.NoneElement))
251 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
252 self.assertEquals(value.text, None)
253 self.assertEquals(value.pyval, None)
254 for attr in arg.attrib:
255
256 self.assertEquals(value.get(attr), arg.get(attr))
257
259
260
261 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
262 attrib={"gnu": "muh", "cat": "meeow",
263 "dog": "wuff"},
264 bird="tchilp", dog="grrr")
265 value = objectify.DataElement(arg, _pytype="int")
266 self.assert_(isinstance(value, objectify.IntElement))
267 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
268 for attr in arg.attrib:
269 if not attr == objectify.PYTYPE_ATTRIBUTE:
270 self.assertEquals(value.get(attr), arg.get(attr))
271
273
274
275 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
276 attrib={"gnu": "muh", "cat": "meeow",
277 "dog": "wuff"},
278 bird="tchilp", dog="grrr")
279 value = objectify.DataElement(arg, _xsi="xsd:int")
280 self.assert_(isinstance(value, objectify.IntElement))
281 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
282 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
283 for attr in arg.attrib:
284 if not attr in [objectify.PYTYPE_ATTRIBUTE,
285 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
286 self.assertEquals(value.get(attr), arg.get(attr))
287
289
290
291 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
292 attrib={"gnu": "muh", "cat": "meeow",
293 "dog": "wuff"},
294 bird="tchilp", dog="grrr")
295 value = objectify.DataElement(arg, _pytype="int", _xsi="xsd:int")
296 self.assert_(isinstance(value, objectify.IntElement))
297 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
298 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
299 for attr in arg.attrib:
300 if not attr in [objectify.PYTYPE_ATTRIBUTE,
301 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
302 self.assertEquals(value.get(attr), arg.get(attr))
303
307
311
316
321
328
332
336
340
345
347 root = self.XML(xml_str)
348 self.assertEquals("0", getattr(root.c1, "{objectified}c2").text)
349 self.assertEquals("3", getattr(root.c1, "{otherNS}c2").text)
350
352 root = self.XML(xml_str)
353 self.assertRaises(AttributeError, getattr, root.c1, "NOT_THERE")
354 self.assertRaises(AttributeError, getattr, root.c1, "{unknownNS}c2")
355
360
362 for val in [
363 2, 2**32, 1.2, "Won't get fooled again",
364 _str("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1'), True,
365 False, None]:
366 root = self.Element('root')
367 attrname = 'val'
368 setattr(root, attrname, val)
369 result = getattr(root, attrname)
370 self.assertEquals(val, result)
371 self.assertEquals(type(val), type(result.pyval))
372
379
386
388 root = self.XML(xml_str)
389 self.assertEquals(1, len(root.c1))
390
391 new_el = self.Element("test", myattr="5")
392 root.addattr("c1", new_el)
393 self.assertEquals(2, len(root.c1))
394 self.assertEquals(None, root.c1[0].get("myattr"))
395 self.assertEquals("5", root.c1[1].get("myattr"))
396
398 root = self.XML(xml_str)
399 self.assertEquals(1, len(root.c1))
400
401 new_el = self.Element("test")
402 self.etree.SubElement(new_el, "a", myattr="A")
403 self.etree.SubElement(new_el, "a", myattr="B")
404
405 root.addattr("c1", list(new_el.a))
406 self.assertEquals(3, len(root.c1))
407 self.assertEquals(None, root.c1[0].get("myattr"))
408 self.assertEquals("A", root.c1[1].get("myattr"))
409 self.assertEquals("B", root.c1[2].get("myattr"))
410
417
419 root = self.XML(xml_str)
420 self.assertEquals("0", root.c1.c2[0].text)
421 self.assertEquals("1", root.c1.c2[1].text)
422 self.assertEquals("2", root.c1.c2[2].text)
423 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
424
426 root = self.XML(xml_str)
427 self.assertEquals("0", root.c1.c2[0].text)
428 self.assertEquals("0", root.c1.c2[-3].text)
429 self.assertEquals("1", root.c1.c2[-2].text)
430 self.assertEquals("2", root.c1.c2[-1].text)
431 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
432
434 root = self.XML(xml_str)
435 self.assertEquals(1, len(root))
436 self.assertEquals(1, len(root.c1))
437 self.assertEquals(3, len(root.c1.c2))
438
447
453
463
468
473
474
475
477 root = self.XML("<root><c>c1</c><c>c2</c></root>")
478 self.assertEquals(["c1", "c2"],
479 [ c.text for c in root.c[:] ])
480
482 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
483 test_list = ["c1", "c2", "c3", "c4"]
484
485 self.assertEquals(test_list,
486 [ c.text for c in root.c[:] ])
487 self.assertEquals(test_list[1:2],
488 [ c.text for c in root.c[1:2] ])
489 self.assertEquals(test_list[-3:-1],
490 [ c.text for c in root.c[-3:-1] ])
491 self.assertEquals(test_list[-3:3],
492 [ c.text for c in root.c[-3:3] ])
493 self.assertEquals(test_list[-3000:3],
494 [ c.text for c in root.c[-3000:3] ])
495 self.assertEquals(test_list[-3:3000],
496 [ c.text for c in root.c[-3:3000] ])
497
499 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
500 test_list = ["c1", "c2", "c3", "c4"]
501
502 self.assertEquals(test_list,
503 [ c.text for c in root.c[:] ])
504 self.assertEquals(test_list[2:1:-1],
505 [ c.text for c in root.c[2:1:-1] ])
506 self.assertEquals(test_list[-1:-3:-1],
507 [ c.text for c in root.c[-1:-3:-1] ])
508 self.assertEquals(test_list[2:-3:-1],
509 [ c.text for c in root.c[2:-3:-1] ])
510 self.assertEquals(test_list[2:-3000:-1],
511 [ c.text for c in root.c[2:-3000:-1] ])
512
513
514
526
528 Element = self.Element
529 root = Element("root")
530 root.c = ["c1", "c2"]
531
532 c1 = root.c[0]
533 c2 = root.c[1]
534
535 self.assertEquals([c1,c2], list(root.c))
536 self.assertEquals(["c1", "c2"],
537 [ c.text for c in root.c ])
538
539 root2 = Element("root2")
540 root2.el = [ "test", "test" ]
541 self.assertEquals(["test", "test"],
542 [ el.text for el in root2.el ])
543
544 root.c = [ root2.el, root2.el ]
545 self.assertEquals(["test", "test"],
546 [ c.text for c in root.c ])
547 self.assertEquals(["test", "test"],
548 [ el.text for el in root2.el ])
549
550 root.c[:] = [ c1, c2, c2, c1 ]
551 self.assertEquals(["c1", "c2", "c2", "c1"],
552 [ c.text for c in root.c ])
553
555 Element = self.Element
556 root = Element("root")
557 l = ["c1", "c2", "c3", "c4"]
558 root.c = l
559
560 self.assertEquals(["c1", "c2", "c3", "c4"],
561 [ c.text for c in root.c ])
562 self.assertEquals(l,
563 [ c.text for c in root.c ])
564
565 new_slice = ["cA", "cB"]
566 l[1:2] = new_slice
567 root.c[1:2] = new_slice
568
569 self.assertEquals(["c1", "cA", "cB", "c3", "c4"], l)
570 self.assertEquals(["c1", "cA", "cB", "c3", "c4"],
571 [ c.text for c in root.c ])
572 self.assertEquals(l,
573 [ c.text for c in root.c ])
574
576 Element = self.Element
577 root = Element("root")
578 l = ["c1", "c2", "c3", "c4"]
579 root.c = l
580
581 self.assertEquals(["c1", "c2", "c3", "c4"],
582 [ c.text for c in root.c ])
583 self.assertEquals(l,
584 [ c.text for c in root.c ])
585
586 new_slice = ["cA", "cB"]
587 l[1:1] = new_slice
588 root.c[1:1] = new_slice
589
590 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"], l)
591 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"],
592 [ c.text for c in root.c ])
593 self.assertEquals(l,
594 [ c.text for c in root.c ])
595
597 Element = self.Element
598 root = Element("root")
599 l = ["c1", "c2", "c3", "c4"]
600 root.c = l
601
602 self.assertEquals(["c1", "c2", "c3", "c4"],
603 [ c.text for c in root.c ])
604 self.assertEquals(l,
605 [ c.text for c in root.c ])
606
607 new_slice = ["cA", "cB"]
608 l[-2:-2] = new_slice
609 root.c[-2:-2] = new_slice
610
611 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"], l)
612 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"],
613 [ c.text for c in root.c ])
614 self.assertEquals(l,
615 [ c.text for c in root.c ])
616
624
626 Element = self.Element
627 root = Element("root")
628 l = ["c1", "c2", "c3", "c4"]
629 root.c = l
630
631 self.assertEquals(["c1", "c2", "c3", "c4"],
632 [ c.text for c in root.c ])
633 self.assertEquals(l,
634 [ c.text for c in root.c ])
635
636 new_slice = ["cA", "cB", "cC"]
637 self.assertRaises(
638 ValueError, operator.setitem,
639 l, slice(1,2,-1), new_slice)
640 self.assertRaises(
641 ValueError, operator.setitem,
642 root.c, slice(1,2,-1), new_slice)
643
645 Element = self.Element
646 root = Element("root")
647 l = ["c1", "c2", "c3", "c4"]
648 root.c = l
649
650 self.assertEquals(["c1", "c2", "c3", "c4"],
651 [ c.text for c in root.c ])
652 self.assertEquals(l,
653 [ c.text for c in root.c ])
654
655 new_slice = ["cA", "cB"]
656 l[-1:1:-1] = new_slice
657 root.c[-1:1:-1] = new_slice
658
659 self.assertEquals(["c1", "c2", "cB", "cA"], l)
660 self.assertEquals(["c1", "c2", "cB", "cA"],
661 [ c.text for c in root.c ])
662 self.assertEquals(l,
663 [ c.text for c in root.c ])
664
666 Element = self.Element
667 root = Element("root")
668 l = ["c1", "c2", "c3", "c4"]
669 root.c = l
670
671 self.assertEquals(["c1", "c2", "c3", "c4"],
672 [ c.text for c in root.c ])
673 self.assertEquals(l,
674 [ c.text for c in root.c ])
675
676 new_slice = ["cA", "cB"]
677 l[-1:-4:-2] = new_slice
678 root.c[-1:-4:-2] = new_slice
679
680 self.assertEquals(["c1", "cB", "c3", "cA"], l)
681 self.assertEquals(["c1", "cB", "c3", "cA"],
682 [ c.text for c in root.c ])
683 self.assertEquals(l,
684 [ c.text for c in root.c ])
685
686
687
695
703
705
706 Element = self.Element
707 root = Element("root")
708
709 root["text"] = "TEST"
710 self.assertEquals(["TEST"],
711 [ c.text for c in root["text"] ])
712
713 root["tail"] = "TEST"
714 self.assertEquals(["TEST"],
715 [ c.text for c in root["tail"] ])
716
717 root["pyval"] = "TEST"
718 self.assertEquals(["TEST"],
719 [ c.text for c in root["pyval"] ])
720
721 root["tag"] = "TEST"
722 self.assertEquals(["TEST"],
723 [ c.text for c in root["tag"] ])
724
732
734 XML = self.XML
735 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
736 self.assertEquals(2, len(root.findall(".//{X}b")))
737 self.assertEquals(3, len(root.findall(".//b")))
738 self.assertEquals(2, len(root.findall("b")))
739
747
762
768
770 Element = self.Element
771 SubElement = self.etree.SubElement
772 root = Element("{objectified}root")
773 root.bool = True
774 self.assertEquals(root.bool, True)
775 self.assertEquals(root.bool + root.bool, True + True)
776 self.assertEquals(True + root.bool, True + root.bool)
777 self.assertEquals(root.bool * root.bool, True * True)
778 self.assertEquals(int(root.bool), int(True))
779 self.assertEquals(hash(root.bool), hash(True))
780 self.assertEquals(complex(root.bool), complex(True))
781 self.assert_(isinstance(root.bool, objectify.BoolElement))
782
783 root.bool = False
784 self.assertEquals(root.bool, False)
785 self.assertEquals(root.bool + root.bool, False + False)
786 self.assertEquals(False + root.bool, False + root.bool)
787 self.assertEquals(root.bool * root.bool, False * False)
788 self.assertEquals(int(root.bool), int(False))
789 self.assertEquals(hash(root.bool), hash(False))
790 self.assertEquals(complex(root.bool), complex(False))
791 self.assert_(isinstance(root.bool, objectify.BoolElement))
792
801
808
815
822
824 Element = self.Element
825 SubElement = self.etree.SubElement
826 root = Element("{objectified}root")
827 root.s = "test"
828
829 self.assertEquals("test" * 5, root.s * 5)
830 self.assertEquals(5 * "test", 5 * root.s)
831
832 self.assertRaises(TypeError, operator.mul, root.s, "honk")
833 self.assertRaises(TypeError, operator.mul, "honk", root.s)
834
844
865
870
875
880
885
894
899
904
909
916
923
930
942
952
957
962
967
974
979
983
990
995
999
1008
1017
1023
1032
1034 pyval = 1
1035 pytype = "NoneType"
1036 objclass = objectify.NoneElement
1037 value = objectify.DataElement(pyval, _pytype=pytype)
1038 self.assert_(isinstance(value, objclass),
1039 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1040 % (pyval, pytype, type(value), objclass))
1041 self.assertEquals(value.text, None)
1042 self.assertEquals(value.pyval, None)
1043
1045
1046 pyval = 1
1047 pytype = "none"
1048 objclass = objectify.NoneElement
1049 value = objectify.DataElement(pyval, _pytype=pytype)
1050 self.assert_(isinstance(value, objclass),
1051 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1052 % (pyval, pytype, type(value), objclass))
1053 self.assertEquals(value.text, None)
1054 self.assertEquals(value.pyval, None)
1055
1061 root = Element("{objectified}root")
1062 root.myfloat = MyFloat(5.5)
1063 self.assert_(isinstance(root.myfloat, objectify.FloatElement))
1064 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
1065
1067 class MyFloat(float):
1068 pass
1069 value = objectify.DataElement(MyFloat(5.5))
1070 self.assert_(isinstance(value, objectify.FloatElement))
1071 self.assertEquals(value, 5.5)
1072 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None)
1073
1075 XML = self.XML
1076 root = XML('''\
1077 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1078 <b xsi:type="boolean">true</b>
1079 <b xsi:type="boolean">false</b>
1080 <b xsi:type="boolean">1</b>
1081 <b xsi:type="boolean">0</b>
1082
1083 <f xsi:type="float">5</f>
1084 <f xsi:type="double">5</f>
1085
1086 <s xsi:type="string">5</s>
1087 <s xsi:type="normalizedString">5</s>
1088 <s xsi:type="token">5</s>
1089 <s xsi:type="language">5</s>
1090 <s xsi:type="Name">5</s>
1091 <s xsi:type="NCName">5</s>
1092 <s xsi:type="ID">5</s>
1093 <s xsi:type="IDREF">5</s>
1094 <s xsi:type="ENTITY">5</s>
1095 <s xsi:type="NMTOKEN">5</s>
1096
1097 <l xsi:type="integer">5</l>
1098 <l xsi:type="nonPositiveInteger">5</l>
1099 <l xsi:type="negativeInteger">5</l>
1100 <l xsi:type="long">5</l>
1101 <l xsi:type="nonNegativeInteger">5</l>
1102 <l xsi:type="unsignedLong">5</l>
1103 <l xsi:type="unsignedInt">5</l>
1104 <l xsi:type="positiveInteger">5</l>
1105
1106 <i xsi:type="int">5</i>
1107 <i xsi:type="short">5</i>
1108 <i xsi:type="byte">5</i>
1109 <i xsi:type="unsignedShort">5</i>
1110 <i xsi:type="unsignedByte">5</i>
1111
1112 <n xsi:nil="true"/>
1113 </root>
1114 ''')
1115
1116 for b in root.b:
1117 self.assert_(isinstance(b, objectify.BoolElement))
1118 self.assertEquals(True, root.b[0])
1119 self.assertEquals(False, root.b[1])
1120 self.assertEquals(True, root.b[2])
1121 self.assertEquals(False, root.b[3])
1122
1123 for f in root.f:
1124 self.assert_(isinstance(f, objectify.FloatElement))
1125 self.assertEquals(5, f)
1126
1127 for s in root.s:
1128 self.assert_(isinstance(s, objectify.StringElement))
1129 self.assertEquals("5", s)
1130
1131 for i in root.i:
1132 self.assert_(isinstance(i, objectify.IntElement))
1133 self.assertEquals(5, i)
1134
1135 for l in root.l:
1136 self.assert_(isinstance(l, objectify.IntElement))
1137 self.assertEquals(5, i)
1138
1139 self.assert_(isinstance(root.n, objectify.NoneElement))
1140 self.assertEquals(None, root.n)
1141
1143 XML = self.XML
1144 root = XML('''\
1145 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1146 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1147 <b xsi:type="xsd:boolean">true</b>
1148 <b xsi:type="xsd:boolean">false</b>
1149 <b xsi:type="xsd:boolean">1</b>
1150 <b xsi:type="xsd:boolean">0</b>
1151
1152 <f xsi:type="xsd:float">5</f>
1153 <f xsi:type="xsd:double">5</f>
1154
1155 <s xsi:type="xsd:string">5</s>
1156 <s xsi:type="xsd:normalizedString">5</s>
1157 <s xsi:type="xsd:token">5</s>
1158 <s xsi:type="xsd:language">5</s>
1159 <s xsi:type="xsd:Name">5</s>
1160 <s xsi:type="xsd:NCName">5</s>
1161 <s xsi:type="xsd:ID">5</s>
1162 <s xsi:type="xsd:IDREF">5</s>
1163 <s xsi:type="xsd:ENTITY">5</s>
1164 <s xsi:type="xsd:NMTOKEN">5</s>
1165
1166 <l xsi:type="xsd:integer">5</l>
1167 <l xsi:type="xsd:nonPositiveInteger">5</l>
1168 <l xsi:type="xsd:negativeInteger">5</l>
1169 <l xsi:type="xsd:long">5</l>
1170 <l xsi:type="xsd:nonNegativeInteger">5</l>
1171 <l xsi:type="xsd:unsignedLong">5</l>
1172 <l xsi:type="xsd:unsignedInt">5</l>
1173 <l xsi:type="xsd:positiveInteger">5</l>
1174
1175 <i xsi:type="xsd:int">5</i>
1176 <i xsi:type="xsd:short">5</i>
1177 <i xsi:type="xsd:byte">5</i>
1178 <i xsi:type="xsd:unsignedShort">5</i>
1179 <i xsi:type="xsd:unsignedByte">5</i>
1180
1181 <n xsi:nil="true"/>
1182 </root>
1183 ''')
1184
1185 for b in root.b:
1186 self.assert_(isinstance(b, objectify.BoolElement))
1187 self.assertEquals(True, root.b[0])
1188 self.assertEquals(False, root.b[1])
1189 self.assertEquals(True, root.b[2])
1190 self.assertEquals(False, root.b[3])
1191
1192 for f in root.f:
1193 self.assert_(isinstance(f, objectify.FloatElement))
1194 self.assertEquals(5, f)
1195
1196 for s in root.s:
1197 self.assert_(isinstance(s, objectify.StringElement))
1198 self.assertEquals("5", s)
1199
1200 for i in root.i:
1201 self.assert_(isinstance(i, objectify.IntElement))
1202 self.assertEquals(5, i)
1203
1204 for l in root.l:
1205 self.assert_(isinstance(l, objectify.IntElement))
1206 self.assertEquals(5, l)
1207
1208 self.assert_(isinstance(root.n, objectify.NoneElement))
1209 self.assertEquals(None, root.n)
1210
1212 XML = self.XML
1213 root = XML(_bytes('<root><b>why</b><b>try</b></root>'))
1214 strs = [ str(s) for s in root.b ]
1215 self.assertEquals(["why", "try"],
1216 strs)
1217
1244
1264
1265
1266
1290
1292 XML = self.XML
1293 root = XML(_bytes("""
1294 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1295 <b xsi:nil="true"></b><b xsi:nil="true"/>
1296 </root>"""))
1297 self.assert_(root.b[0] == root.b[1])
1298 self.assertFalse(root.b[0])
1299 self.assertEquals(root.b[0], None)
1300 self.assertEquals(None, root.b[0])
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1316
1323
1327
1329 XML = self.XML
1330 root = XML(_bytes('''\
1331 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1332 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1333 <b>5</b>
1334 <b>test</b>
1335 <c>1.1</c>
1336 <c>\uF8D2</c>
1337 <x>true</x>
1338 <n xsi:nil="true" />
1339 <n></n>
1340 <b xsi:type="double">5</b>
1341 <b xsi:type="float">5</b>
1342 <s xsi:type="string">23</s>
1343 <s py:pytype="str">42</s>
1344 <f py:pytype="float">300</f>
1345 <l py:pytype="long">2</l>
1346 <t py:pytype="TREE"></t>
1347 </a>
1348 '''))
1349 objectify.annotate(root)
1350
1351 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1352 for c in root.iterchildren() ]
1353 self.assertEquals("int", child_types[ 0])
1354 self.assertEquals("str", child_types[ 1])
1355 self.assertEquals("float", child_types[ 2])
1356 self.assertEquals("str", child_types[ 3])
1357 self.assertEquals("bool", child_types[ 4])
1358 self.assertEquals("NoneType", child_types[ 5])
1359 self.assertEquals(None, child_types[ 6])
1360 self.assertEquals("float", child_types[ 7])
1361 self.assertEquals("float", child_types[ 8])
1362 self.assertEquals("str", child_types[ 9])
1363 self.assertEquals("int", child_types[10])
1364 self.assertEquals("int", child_types[11])
1365 self.assertEquals("int", child_types[12])
1366 self.assertEquals(None, child_types[13])
1367
1368 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1369
1389
1391 XML = self.XML
1392 root = XML(_bytes('''\
1393 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1394 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1395 <b>5</b>
1396 <b>test</b>
1397 <c>1.1</c>
1398 <c>\uF8D2</c>
1399 <x>true</x>
1400 <n xsi:nil="true" />
1401 <n></n>
1402 <b xsi:type="double">5</b>
1403 <b xsi:type="float">5</b>
1404 <s xsi:type="string">23</s>
1405 <s py:pytype="str">42</s>
1406 <f py:pytype="float">300</f>
1407 <l py:pytype="long">2</l>
1408 <t py:pytype="TREE"></t>
1409 </a>
1410 '''))
1411 objectify.annotate(root, ignore_old=False)
1412
1413 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1414 for c in root.iterchildren() ]
1415 self.assertEquals("int", child_types[ 0])
1416 self.assertEquals("str", child_types[ 1])
1417 self.assertEquals("float", child_types[ 2])
1418 self.assertEquals("str", child_types[ 3])
1419 self.assertEquals("bool", child_types[ 4])
1420 self.assertEquals("NoneType", child_types[ 5])
1421 self.assertEquals(None, child_types[ 6])
1422 self.assertEquals("float", child_types[ 7])
1423 self.assertEquals("float", child_types[ 8])
1424 self.assertEquals("str", child_types[ 9])
1425 self.assertEquals("str", child_types[10])
1426 self.assertEquals("float", child_types[11])
1427 self.assertEquals("int", child_types[12])
1428 self.assertEquals(TREE_PYTYPE, child_types[13])
1429
1430 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1431
1433 XML = self.XML
1434 root = XML(_bytes('''\
1435 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1436 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1437 <b>5</b>
1438 <b>test</b>
1439 <c>1.1</c>
1440 <c>\uF8D2</c>
1441 <x>true</x>
1442 <n xsi:nil="true" />
1443 <n></n>
1444 <b xsi:type="double">5</b>
1445 <b xsi:type="float">5</b>
1446 <s xsi:type="string">23</s>
1447 <s py:pytype="str">42</s>
1448 <f py:pytype="float">300</f>
1449 <l py:pytype="long">2</l>
1450 <t py:pytype="TREE"></t>
1451 </a>
1452 '''))
1453 objectify.annotate(root, ignore_old=False, ignore_xsi=False,
1454 annotate_xsi=1, annotate_pytype=1)
1455
1456
1457 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1458 for c in root.iterchildren() ]
1459 self.assertEquals("int", child_types[ 0])
1460 self.assertEquals("str", child_types[ 1])
1461 self.assertEquals("float", child_types[ 2])
1462 self.assertEquals("str", child_types[ 3])
1463 self.assertEquals("bool", child_types[ 4])
1464 self.assertEquals("NoneType", child_types[ 5])
1465 self.assertEquals(None, child_types[ 6])
1466 self.assertEquals("float", child_types[ 7])
1467 self.assertEquals("float", child_types[ 8])
1468 self.assertEquals("str", child_types[ 9])
1469 self.assertEquals("str", child_types[10])
1470 self.assertEquals("float", child_types[11])
1471 self.assertEquals("int", child_types[12])
1472 self.assertEquals(TREE_PYTYPE, child_types[13])
1473
1474 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1475
1476 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1477 for c in root.iterchildren() ]
1478
1479
1480 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1481 for c in root.iterchildren() ]
1482 self.assertEquals("xsd:integer", child_types[ 0])
1483 self.assertEquals("xsd:string", child_types[ 1])
1484 self.assertEquals("xsd:double", child_types[ 2])
1485 self.assertEquals("xsd:string", child_types[ 3])
1486 self.assertEquals("xsd:boolean", child_types[ 4])
1487 self.assertEquals(None, child_types[ 5])
1488 self.assertEquals(None, child_types[ 6])
1489 self.assertEquals("xsd:double", child_types[ 7])
1490 self.assertEquals("xsd:float", child_types[ 8])
1491 self.assertEquals("xsd:string", child_types[ 9])
1492 self.assertEquals("xsd:string", child_types[10])
1493 self.assertEquals("xsd:double", child_types[11])
1494 self.assertEquals("xsd:integer", child_types[12])
1495 self.assertEquals(None, child_types[13])
1496
1497 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1498
1500 XML = self.XML
1501 root = XML(_bytes('''\
1502 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1503 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1504 <b>5</b>
1505 <b>test</b>
1506 <c>1.1</c>
1507 <c>\uF8D2</c>
1508 <x>true</x>
1509 <n xsi:nil="true" />
1510 <n></n>
1511 <b xsi:type="double">5</b>
1512 <b xsi:type="float">5</b>
1513 <s xsi:type="string">23</s>
1514 <s py:pytype="str">42</s>
1515 <f py:pytype="float">300</f>
1516 <l py:pytype="long">2</l>
1517 <t py:pytype="TREE"></t>
1518 </a>
1519 '''))
1520 objectify.xsiannotate(root, ignore_old=False)
1521
1522 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1523 for c in root.iterchildren() ]
1524 self.assertEquals("xsd:integer", child_types[ 0])
1525 self.assertEquals("xsd:string", child_types[ 1])
1526 self.assertEquals("xsd:double", child_types[ 2])
1527 self.assertEquals("xsd:string", child_types[ 3])
1528 self.assertEquals("xsd:boolean", child_types[ 4])
1529 self.assertEquals(None, child_types[ 5])
1530 self.assertEquals(None, child_types[ 6])
1531 self.assertEquals("xsd:double", child_types[ 7])
1532 self.assertEquals("xsd:float", child_types[ 8])
1533 self.assertEquals("xsd:string", child_types[ 9])
1534 self.assertEquals("xsd:string", child_types[10])
1535 self.assertEquals("xsd:double", child_types[11])
1536 self.assertEquals("xsd:integer", child_types[12])
1537 self.assertEquals(None, child_types[13])
1538
1540 XML = self.XML
1541 root = XML(_bytes('''\
1542 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1543 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1544 <b>5</b>
1545 <b>test</b>
1546 <c>1.1</c>
1547 <c>\uF8D2</c>
1548 <x>true</x>
1549 <n xsi:nil="true" />
1550 <n></n>
1551 <b xsi:type="double">5</b>
1552 <b xsi:type="float">5</b>
1553 <s xsi:type="string">23</s>
1554 <s py:pytype="str">42</s>
1555 <f py:pytype="float">300</f>
1556 <l py:pytype="long">2</l>
1557 <t py:pytype="TREE"></t>
1558 </a>
1559 '''))
1560 objectify.pyannotate(root, ignore_old=True)
1561
1562 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1563 for c in root.iterchildren() ]
1564 self.assertEquals("int", child_types[ 0])
1565 self.assertEquals("str", child_types[ 1])
1566 self.assertEquals("float", child_types[ 2])
1567 self.assertEquals("str", child_types[ 3])
1568 self.assertEquals("bool", child_types[ 4])
1569 self.assertEquals("NoneType", child_types[ 5])
1570 self.assertEquals(None, child_types[ 6])
1571 self.assertEquals("float", child_types[ 7])
1572 self.assertEquals("float", child_types[ 8])
1573 self.assertEquals("str", child_types[ 9])
1574 self.assertEquals("int", child_types[10])
1575 self.assertEquals("int", child_types[11])
1576 self.assertEquals("int", child_types[12])
1577 self.assertEquals(None, child_types[13])
1578
1579 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1580
1600
1602 XML = self.XML
1603 root = XML('''\
1604 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1605 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1606 <b>5</b>
1607 <b>test</b>
1608 <c>1.1</c>
1609 <c>\uF8D2</c>
1610 <x>true</x>
1611 <n xsi:nil="true" />
1612 <n></n>
1613 <b xsi:type="double">5</b>
1614 <b xsi:type="float">5</b>
1615 <s xsi:type="string">23</s>
1616 <s py:pytype="str">42</s>
1617 <f py:pytype="float">300</f>
1618 <l py:pytype="long">2</l>
1619 <t py:pytype="TREE"></t>
1620 </a>
1621 ''')
1622 objectify.pyannotate(root)
1623
1624 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1625 for c in root.iterchildren() ]
1626 self.assertEquals("int", child_types[ 0])
1627 self.assertEquals("str", child_types[ 1])
1628 self.assertEquals("float", child_types[ 2])
1629 self.assertEquals("str", child_types[ 3])
1630 self.assertEquals("bool", child_types[ 4])
1631 self.assertEquals("NoneType", child_types[ 5])
1632 self.assertEquals(None, child_types[ 6])
1633 self.assertEquals("float", child_types[ 7])
1634 self.assertEquals("float", child_types[ 8])
1635 self.assertEquals("str", child_types[ 9])
1636 self.assertEquals("str", child_types[10])
1637 self.assertEquals("float", child_types[11])
1638 self.assertEquals("int", child_types[12])
1639 self.assertEquals(TREE_PYTYPE, child_types[13])
1640
1641 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1642
1644 XML = self.XML
1645 root = XML(_bytes('''\
1646 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1647 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1648 <b>5</b>
1649 <b>test</b>
1650 <c>1.1</c>
1651 <c>\uF8D2</c>
1652 <x>true</x>
1653 <n xsi:nil="true" />
1654 <n></n>
1655 <b xsi:type="double">5</b>
1656 <b xsi:type="float">5</b>
1657 <s xsi:type="string">23</s>
1658 <s py:pytype="str">42</s>
1659 <f py:pytype="float">300</f>
1660 <l py:pytype="long">2</l>
1661 <t py:pytype="TREE"></t>
1662 </a>
1663 '''))
1664 objectify.xsiannotate(root, ignore_old=True)
1665
1666 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1667 for c in root.iterchildren() ]
1668 self.assertEquals("xsd:integer", child_types[ 0])
1669 self.assertEquals("xsd:string", child_types[ 1])
1670 self.assertEquals("xsd:double", child_types[ 2])
1671 self.assertEquals("xsd:string", child_types[ 3])
1672 self.assertEquals("xsd:boolean", child_types[ 4])
1673 self.assertEquals(None, child_types[ 5])
1674 self.assertEquals(None, child_types[ 6])
1675 self.assertEquals("xsd:integer", child_types[ 7])
1676 self.assertEquals("xsd:integer", child_types[ 8])
1677 self.assertEquals("xsd:integer", child_types[ 9])
1678 self.assertEquals("xsd:string", child_types[10])
1679 self.assertEquals("xsd:double", child_types[11])
1680 self.assertEquals("xsd:integer", child_types[12])
1681 self.assertEquals(None, child_types[13])
1682
1683 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1684
1686 XML = self.XML
1687 root = XML(_bytes('''\
1688 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1689 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1690 <b>5</b>
1691 <b>test</b>
1692 <c>1.1</c>
1693 <c>\uF8D2</c>
1694 <x>true</x>
1695 <n xsi:nil="true" />
1696 <n></n>
1697 <b xsi:type="double">5</b>
1698 <b xsi:type="float">5</b>
1699 <s xsi:type="string">23</s>
1700 <s py:pytype="str">42</s>
1701 <f py:pytype="float">300</f>
1702 <l py:pytype="long">2</l>
1703 <t py:pytype="TREE"></t>
1704 </a>
1705 '''))
1706 objectify.deannotate(root)
1707
1708 for c in root.getiterator():
1709 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1710 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1711
1712 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1713
1715 XML = self.XML
1716 root = XML(_bytes('''\
1717 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1718 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1719 <b>5</b>
1720 <b>test</b>
1721 <c>1.1</c>
1722 <c>\uF8D2</c>
1723 <x>true</x>
1724 <n xsi:nil="true" />
1725 <n></n>
1726 <b xsi:type="double">5</b>
1727 <b xsi:type="float">5</b>
1728 <s xsi:type="string">23</s>
1729 <s py:pytype="str">42</s>
1730 <f py:pytype="float">300</f>
1731 <l py:pytype="long">2</l>
1732 <t py:pytype="TREE"></t>
1733 </a>
1734 '''))
1735 objectify.annotate(
1736 root, ignore_old=False, ignore_xsi=False, annotate_xsi=True,
1737 empty_pytype='str', empty_type='string')
1738 objectify.deannotate(root, pytype=False, xsi=False, xsi_nil=True)
1739
1740 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1741 for c in root.iterchildren() ]
1742 self.assertEquals("xsd:integer", child_types[ 0])
1743 self.assertEquals("xsd:string", child_types[ 1])
1744 self.assertEquals("xsd:double", child_types[ 2])
1745 self.assertEquals("xsd:string", child_types[ 3])
1746 self.assertEquals("xsd:boolean", child_types[ 4])
1747 self.assertEquals(None, child_types[ 5])
1748 self.assertEquals("xsd:string", child_types[ 6])
1749 self.assertEquals("xsd:double", child_types[ 7])
1750 self.assertEquals("xsd:float", child_types[ 8])
1751 self.assertEquals("xsd:string", child_types[ 9])
1752 self.assertEquals("xsd:string", child_types[10])
1753 self.assertEquals("xsd:double", child_types[11])
1754 self.assertEquals("xsd:integer", child_types[12])
1755 self.assertEquals(None, child_types[13])
1756
1757 self.assertEquals(None, root.n.get(XML_SCHEMA_NIL_ATTR))
1758
1759 for c in root.iterchildren():
1760 self.assertNotEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1761
1762 if (c.get(objectify.PYTYPE_ATTRIBUTE) not in [TREE_PYTYPE,
1763 "NoneType"]):
1764 self.assertNotEquals(
1765 None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1766
1768 XML = self.XML
1769 root = XML(_bytes('''\
1770 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1771 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1772 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1773 <b>5</b>
1774 <b>test</b>
1775 <c>1.1</c>
1776 <c>\uF8D2</c>
1777 <x>true</x>
1778 <n xsi:nil="true" />
1779 <n></n>
1780 <b xsi:type="xsd:double">5</b>
1781 <b xsi:type="xsd:float">5</b>
1782 <s xsi:type="xsd:string">23</s>
1783 <s py:pytype="str">42</s>
1784 <f py:pytype="float">300</f>
1785 <l py:pytype="long">2</l>
1786 <t py:pytype="TREE"></t>
1787 </a>
1788 '''))
1789 objectify.annotate(root)
1790 objectify.deannotate(root, pytype=False)
1791
1792 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1793 for c in root.iterchildren() ]
1794 self.assertEquals("int", child_types[ 0])
1795 self.assertEquals("str", child_types[ 1])
1796 self.assertEquals("float", child_types[ 2])
1797 self.assertEquals("str", child_types[ 3])
1798 self.assertEquals("bool", child_types[ 4])
1799 self.assertEquals("NoneType", child_types[ 5])
1800 self.assertEquals(None, child_types[ 6])
1801 self.assertEquals("float", child_types[ 7])
1802 self.assertEquals("float", child_types[ 8])
1803 self.assertEquals("str", child_types[ 9])
1804 self.assertEquals("int", child_types[10])
1805 self.assertEquals("int", child_types[11])
1806 self.assertEquals("int", child_types[12])
1807 self.assertEquals(None, child_types[13])
1808
1809 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1810
1811 for c in root.getiterator():
1812 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1813
1815 XML = self.XML
1816 root = XML(_bytes('''\
1817 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1818 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1819 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1820 <b xsi:type="xsd:int">5</b>
1821 <b xsi:type="xsd:string">test</b>
1822 <c xsi:type="xsd:float">1.1</c>
1823 <c xsi:type="xsd:string">\uF8D2</c>
1824 <x xsi:type="xsd:boolean">true</x>
1825 <n xsi:nil="true" />
1826 <n></n>
1827 <b xsi:type="xsd:double">5</b>
1828 <b xsi:type="xsd:float">5</b>
1829 <s xsi:type="xsd:string">23</s>
1830 <s xsi:type="xsd:string">42</s>
1831 <f xsi:type="xsd:float">300</f>
1832 <l xsi:type="xsd:long">2</l>
1833 <t py:pytype="TREE"></t>
1834 </a>
1835 '''))
1836 objectify.annotate(root)
1837 objectify.deannotate(root, xsi=False)
1838
1839 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1840 for c in root.iterchildren() ]
1841 self.assertEquals("xsd:int", child_types[ 0])
1842 self.assertEquals("xsd:string", child_types[ 1])
1843 self.assertEquals("xsd:float", child_types[ 2])
1844 self.assertEquals("xsd:string", child_types[ 3])
1845 self.assertEquals("xsd:boolean", child_types[ 4])
1846 self.assertEquals(None, child_types[ 5])
1847 self.assertEquals(None, child_types[ 6])
1848 self.assertEquals("xsd:double", child_types[ 7])
1849 self.assertEquals("xsd:float", child_types[ 8])
1850 self.assertEquals("xsd:string", child_types[ 9])
1851 self.assertEquals("xsd:string", child_types[10])
1852 self.assertEquals("xsd:float", child_types[11])
1853 self.assertEquals("xsd:long", child_types[12])
1854 self.assertEquals(None, child_types[13])
1855
1856 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1857
1858 for c in root.getiterator():
1859 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1860
1862 XML = self.XML
1863
1864 xml = _bytes('''\
1865 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1866 <b>5</b>
1867 <b>test</b>
1868 <c>1.1</c>
1869 <c>\uF8D2</c>
1870 <x>true</x>
1871 <n xsi:nil="true" />
1872 <n></n>
1873 <b xsi:type="double">5</b>
1874 </a>
1875 ''')
1876
1877 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1878 objectify.set_pytype_attribute_tag("{TEST}test")
1879
1880 root = XML(xml)
1881 objectify.annotate(root)
1882
1883 attribs = root.xpath("//@py:%s" % pytype_name,
1884 namespaces={"py" : pytype_ns})
1885 self.assertEquals(0, len(attribs))
1886 attribs = root.xpath("//@py:test",
1887 namespaces={"py" : "TEST"})
1888 self.assertEquals(7, len(attribs))
1889
1890 objectify.set_pytype_attribute_tag()
1891 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1892
1893 self.assertNotEqual("test", pytype_ns.lower())
1894 self.assertNotEqual("test", pytype_name.lower())
1895
1896 root = XML(xml)
1897 attribs = root.xpath("//@py:%s" % pytype_name,
1898 namespaces={"py" : pytype_ns})
1899 self.assertEquals(0, len(attribs))
1900
1901 objectify.annotate(root)
1902 attribs = root.xpath("//@py:%s" % pytype_name,
1903 namespaces={"py" : pytype_ns})
1904 self.assertEquals(7, len(attribs))
1905
1913
1914 def checkMyType(s):
1915 return True
1916
1917 pytype = objectify.PyType("mytype", checkMyType, NewType)
1918 self.assert_(pytype not in objectify.getRegisteredTypes())
1919 pytype.register()
1920 self.assert_(pytype in objectify.getRegisteredTypes())
1921 pytype.unregister()
1922 self.assert_(pytype not in objectify.getRegisteredTypes())
1923
1924 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1925 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1926 pytype.unregister()
1927
1928 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1929 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1930 pytype.unregister()
1931
1932 self.assertRaises(ValueError, pytype.register,
1933 before = [objectify.getRegisteredTypes()[0].name],
1934 after = [objectify.getRegisteredTypes()[1].name])
1935
1937 from datetime import datetime
1938 def parse_date(value):
1939 if len(value) != 14:
1940 raise ValueError(value)
1941 Y = int(value[0:4])
1942 M = int(value[4:6])
1943 D = int(value[6:8])
1944 h = int(value[8:10])
1945 m = int(value[10:12])
1946 s = int(value[12:14])
1947 return datetime(Y, M, D, h, m, s)
1948
1949 def stringify_date(date):
1950 return date.strftime("%Y%m%d%H%M%S")
1951
1952 class DatetimeElement(objectify.ObjectifiedDataElement):
1953 def pyval(self):
1954 return parse_date(self.text)
1955 pyval = property(pyval)
1956
1957 datetime_type = objectify.PyType(
1958 "datetime", parse_date, DatetimeElement, stringify_date)
1959 datetime_type.xmlSchemaTypes = "dateTime"
1960 datetime_type.register()
1961
1962 NAMESPACE = "http://foo.net/xmlns"
1963 NAMESPACE_MAP = {'ns': NAMESPACE}
1964
1965 r = objectify.Element("{%s}root" % NAMESPACE, nsmap=NAMESPACE_MAP)
1966 time = datetime.now()
1967 r.date = time
1968
1969 self.assert_(isinstance(r.date, DatetimeElement))
1970 self.assert_(isinstance(r.date.pyval, datetime))
1971
1972 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1973 self.assertEquals(r.date.text, stringify_date(time))
1974
1975 r.date = objectify.E.date(time)
1976
1977 self.assert_(isinstance(r.date, DatetimeElement))
1978 self.assert_(isinstance(r.date.pyval, datetime))
1979
1980 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1981 self.assertEquals(r.date.text, stringify_date(time))
1982
1983 date = objectify.DataElement(time)
1984
1985 self.assert_(isinstance(date, DatetimeElement))
1986 self.assert_(isinstance(date.pyval, datetime))
1987
1988 self.assertEquals(date.pyval, parse_date(stringify_date(time)))
1989 self.assertEquals(date.text, stringify_date(time))
1990
1996
2002
2007
2016
2023
2031
2034
2037
2056
2061
2066
2071
2076
2096
2098 root = self.XML(xml_str)
2099 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
2100 self.assertEquals(root.c1.c2.text, path(root).text)
2101
2102 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
2103 self.assertEquals(root.c1.c2[2].text, path(root).text)
2104
2105 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
2106 self.assertEquals(root.c1.c2[2].text, path(root).text)
2107
2108 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
2109 self.assertEquals(root.c1.c2[-1].text, path(root).text)
2110
2111 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
2112 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2113
2115 self.assertRaises(ValueError, objectify.ObjectPath,
2116 "root.c1[0].c2[-1-2]")
2117 self.assertRaises(ValueError, objectify.ObjectPath,
2118 ['root', 'c1[0]', 'c2[-1-2]'])
2119
2120 self.assertRaises(ValueError, objectify.ObjectPath,
2121 "root[2].c1.c2")
2122 self.assertRaises(ValueError, objectify.ObjectPath,
2123 ['root[2]', 'c1', 'c2'])
2124
2125 self.assertRaises(ValueError, objectify.ObjectPath,
2126 [])
2127 self.assertRaises(ValueError, objectify.ObjectPath,
2128 ['', '', ''])
2129
2131 root = self.XML(xml_str)
2132 path = objectify.ObjectPath("root.c1[9999].c2")
2133 self.assertRaises(AttributeError, path, root)
2134
2135 path = objectify.ObjectPath("root.c1[0].c2[9999]")
2136 self.assertRaises(AttributeError, path, root)
2137
2138 path = objectify.ObjectPath(".c1[9999].c2[0]")
2139 self.assertRaises(AttributeError, path, root)
2140
2141 path = objectify.ObjectPath("root.c1[-2].c2")
2142 self.assertRaises(AttributeError, path, root)
2143
2144 path = objectify.ObjectPath("root.c1[0].c2[-4]")
2145 self.assertRaises(AttributeError, path, root)
2146
2160
2162 root = self.XML(xml_str)
2163 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
2164 self.assertEquals(root.c1.c2.text, path.find(root).text)
2165 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
2166 self.assertEquals(root.c1.c2.text, path.find(root).text)
2167 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
2168 self.assertEquals(root.c1.c2.text, path.find(root).text)
2169 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
2170 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2171 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
2172 self.assertEquals(root.c1.c2.text, path.find(root).text)
2173 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
2174 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2175 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
2176 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2177 path.find(root).text)
2178
2191
2206
2218
2232
2234 root = self.XML(xml_str)
2235 path = objectify.ObjectPath( "root.c1.c99" )
2236 self.assertRaises(AttributeError, path.find, root)
2237
2238 new_el = self.Element("{objectified}test")
2239 new_el.a = ["TEST1", "TEST2"]
2240 new_el.a[0].set("myattr", "ATTR1")
2241 new_el.a[1].set("myattr", "ATTR2")
2242
2243 path.setattr(root, list(new_el.a))
2244
2245 self.assertEquals(2, len(root.c1.c99))
2246 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
2247 self.assertEquals("TEST1", root.c1.c99[0].text)
2248 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
2249 self.assertEquals("TEST2", root.c1.c99[1].text)
2250 self.assertEquals("TEST1", path(root).text)
2251
2260
2274
2286
2300
2315
2317 root = self.XML(xml_str)
2318 self.assertEquals(
2319 ['{objectified}root', '{objectified}root.c1',
2320 '{objectified}root.c1.c2',
2321 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]',
2322 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'],
2323 root.descendantpaths())
2324
2326 root = self.XML(xml_str)
2327 self.assertEquals(
2328 ['{objectified}c1', '{objectified}c1.c2',
2329 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]',
2330 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'],
2331 root.c1.descendantpaths())
2332
2334 root = self.XML(xml_str)
2335 self.assertEquals(
2336 ['root.{objectified}c1', 'root.{objectified}c1.c2',
2337 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]',
2338 'root.{objectified}c1.{otherNS}c2',
2339 'root.{objectified}c1.{}c2'],
2340 root.c1.descendantpaths('root'))
2341
2353
2366
2370
2374
2378
2384
2389
2391 import pickle
2392 if isinstance(stringOrElt, (etree._Element, etree._ElementTree)):
2393 elt = stringOrElt
2394 else:
2395 elt = self.XML(stringOrElt)
2396 out = BytesIO()
2397 pickle.dump(elt, out)
2398
2399 new_elt = pickle.loads(out.getvalue())
2400 self.assertEquals(
2401 etree.tostring(new_elt),
2402 etree.tostring(elt))
2403
2404
2405
2410
2415
2420
2425
2430
2435
2440
2445
2447 E = objectify.E
2448 DataElement = objectify.DataElement
2449 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1),
2450 DataElement(2.0))
2451 self.assert_(isinstance(root, objectify.ObjectifiedElement))
2452 self.assertEquals(root.text, "text")
2453 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement))
2454 self.assertEquals(root.sub.tail, "tail")
2455 self.assert_(isinstance(root.sub.subsub, objectify.StringElement))
2456 self.assertEquals(len(root.value), 2)
2457 self.assert_(isinstance(root.value[0], objectify.IntElement))
2458 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2459
2466
2467 attr = Attribute()
2468 self.assertEquals(attr.text, None)
2469 self.assertEquals(attr.get("datatype"), "TYPE")
2470 self.assertEquals(attr.get("range"), "0.,1.")
2471
2476
2483
2488
2494
2496 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2497 self.assertEquals(root.base, "http://no/such/url")
2498 self.assertEquals(
2499 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2500 root.base = "https://secret/url"
2501 self.assertEquals(root.base, "https://secret/url")
2502 self.assertEquals(
2503 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2504 "https://secret/url")
2505
2507 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2508 self.assertEquals(root.base, "http://no/such/url")
2509 self.assertEquals(
2510 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2511 root.set('{http://www.w3.org/XML/1998/namespace}base',
2512 "https://secret/url")
2513 self.assertEquals(root.base, "https://secret/url")
2514 self.assertEquals(
2515 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2516 "https://secret/url")
2517
2519 XML = self.XML
2520
2521 xml = _bytes('''\
2522 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2523 <i>5</i>
2524 <i>-5</i>
2525 <l>4294967296</l>
2526 <l>-4294967296</l>
2527 <f>1.1</f>
2528 <b>true</b>
2529 <b>false</b>
2530 <s>Strange things happen, where strings collide</s>
2531 <s>True</s>
2532 <s>False</s>
2533 <s>t</s>
2534 <s>f</s>
2535 <s></s>
2536 <s>None</s>
2537 <n xsi:nil="true" />
2538 </root>
2539 ''')
2540 root = XML(xml)
2541
2542 for i in root.i:
2543 self.assert_(isinstance(i, objectify.IntElement))
2544 for l in root.l:
2545 self.assert_(isinstance(l, objectify.IntElement))
2546 for f in root.f:
2547 self.assert_(isinstance(f, objectify.FloatElement))
2548 for b in root.b:
2549 self.assert_(isinstance(b, objectify.BoolElement))
2550 self.assertEquals(True, root.b[0])
2551 self.assertEquals(False, root.b[1])
2552 for s in root.s:
2553 self.assert_(isinstance(s, objectify.StringElement))
2554 self.assert_(isinstance(root.n, objectify.NoneElement))
2555 self.assertEquals(None, root.n)
2556
2558 suite = unittest.TestSuite()
2559 suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
2560 suite.addTests(doctest.DocTestSuite(objectify))
2561 if sys.version_info >= (2,4):
2562 suite.addTests(
2563 [make_doctest('../../../doc/objectify.txt')])
2564 return suite
2565
2566 if __name__ == '__main__':
2567 print('to test use test.py %s' % __file__)
2568