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>3</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
357 for val in [
358 2, 2**32, 1.2, "Won't get fooled again",
359 _str("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1'), True,
360 False, None]:
361 root = self.Element('root')
362 attrname = 'val'
363 setattr(root, attrname, val)
364 result = getattr(root, attrname)
365 self.assertEquals(val, result)
366 self.assertEquals(type(val), type(result.pyval))
367
374
381
383 root = self.XML(xml_str)
384 self.assertEquals(1, len(root.c1))
385
386 new_el = self.Element("test", myattr="5")
387 root.addattr("c1", new_el)
388 self.assertEquals(2, len(root.c1))
389 self.assertEquals(None, root.c1[0].get("myattr"))
390 self.assertEquals("5", root.c1[1].get("myattr"))
391
393 root = self.XML(xml_str)
394 self.assertEquals(1, len(root.c1))
395
396 new_el = self.Element("test")
397 self.etree.SubElement(new_el, "a", myattr="A")
398 self.etree.SubElement(new_el, "a", myattr="B")
399
400 root.addattr("c1", list(new_el.a))
401 self.assertEquals(3, len(root.c1))
402 self.assertEquals(None, root.c1[0].get("myattr"))
403 self.assertEquals("A", root.c1[1].get("myattr"))
404 self.assertEquals("B", root.c1[2].get("myattr"))
405
412
414 root = self.XML(xml_str)
415 self.assertEquals("0", root.c1.c2[0].text)
416 self.assertEquals("1", root.c1.c2[1].text)
417 self.assertEquals("2", root.c1.c2[2].text)
418 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
419
421 root = self.XML(xml_str)
422 self.assertEquals("0", root.c1.c2[0].text)
423 self.assertEquals("0", root.c1.c2[-3].text)
424 self.assertEquals("1", root.c1.c2[-2].text)
425 self.assertEquals("2", root.c1.c2[-1].text)
426 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
427
429 root = self.XML(xml_str)
430 self.assertEquals(1, len(root))
431 self.assertEquals(1, len(root.c1))
432 self.assertEquals(3, len(root.c1.c2))
433
442
448
458
463
468
469
470
472 root = self.XML("<root><c>c1</c><c>c2</c></root>")
473 self.assertEquals(["c1", "c2"],
474 [ c.text for c in root.c[:] ])
475
477 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
478 test_list = ["c1", "c2", "c3", "c4"]
479
480 self.assertEquals(test_list,
481 [ c.text for c in root.c[:] ])
482 self.assertEquals(test_list[1:2],
483 [ c.text for c in root.c[1:2] ])
484 self.assertEquals(test_list[-3:-1],
485 [ c.text for c in root.c[-3:-1] ])
486 self.assertEquals(test_list[-3:3],
487 [ c.text for c in root.c[-3:3] ])
488 self.assertEquals(test_list[-3000:3],
489 [ c.text for c in root.c[-3000:3] ])
490 self.assertEquals(test_list[-3:3000],
491 [ c.text for c in root.c[-3:3000] ])
492
494 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
495 test_list = ["c1", "c2", "c3", "c4"]
496
497 self.assertEquals(test_list,
498 [ c.text for c in root.c[:] ])
499 self.assertEquals(test_list[2:1:-1],
500 [ c.text for c in root.c[2:1:-1] ])
501 self.assertEquals(test_list[-1:-3:-1],
502 [ c.text for c in root.c[-1:-3:-1] ])
503 self.assertEquals(test_list[2:-3:-1],
504 [ c.text for c in root.c[2:-3:-1] ])
505 self.assertEquals(test_list[2:-3000:-1],
506 [ c.text for c in root.c[2:-3000:-1] ])
507
508
509
521
523 Element = self.Element
524 root = Element("root")
525 root.c = ["c1", "c2"]
526
527 c1 = root.c[0]
528 c2 = root.c[1]
529
530 self.assertEquals([c1,c2], list(root.c))
531 self.assertEquals(["c1", "c2"],
532 [ c.text for c in root.c ])
533
534 root2 = Element("root2")
535 root2.el = [ "test", "test" ]
536 self.assertEquals(["test", "test"],
537 [ el.text for el in root2.el ])
538
539 root.c = [ root2.el, root2.el ]
540 self.assertEquals(["test", "test"],
541 [ c.text for c in root.c ])
542 self.assertEquals(["test", "test"],
543 [ el.text for el in root2.el ])
544
545 root.c[:] = [ c1, c2, c2, c1 ]
546 self.assertEquals(["c1", "c2", "c2", "c1"],
547 [ c.text for c in root.c ])
548
550 Element = self.Element
551 root = Element("root")
552 l = ["c1", "c2", "c3", "c4"]
553 root.c = l
554
555 self.assertEquals(["c1", "c2", "c3", "c4"],
556 [ c.text for c in root.c ])
557 self.assertEquals(l,
558 [ c.text for c in root.c ])
559
560 new_slice = ["cA", "cB"]
561 l[1:2] = new_slice
562 root.c[1:2] = new_slice
563
564 self.assertEquals(["c1", "cA", "cB", "c3", "c4"], l)
565 self.assertEquals(["c1", "cA", "cB", "c3", "c4"],
566 [ c.text for c in root.c ])
567 self.assertEquals(l,
568 [ c.text for c in root.c ])
569
571 Element = self.Element
572 root = Element("root")
573 l = ["c1", "c2", "c3", "c4"]
574 root.c = l
575
576 self.assertEquals(["c1", "c2", "c3", "c4"],
577 [ c.text for c in root.c ])
578 self.assertEquals(l,
579 [ c.text for c in root.c ])
580
581 new_slice = ["cA", "cB"]
582 l[1:1] = new_slice
583 root.c[1:1] = new_slice
584
585 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"], l)
586 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"],
587 [ c.text for c in root.c ])
588 self.assertEquals(l,
589 [ c.text for c in root.c ])
590
592 Element = self.Element
593 root = Element("root")
594 l = ["c1", "c2", "c3", "c4"]
595 root.c = l
596
597 self.assertEquals(["c1", "c2", "c3", "c4"],
598 [ c.text for c in root.c ])
599 self.assertEquals(l,
600 [ c.text for c in root.c ])
601
602 new_slice = ["cA", "cB"]
603 l[-2:-2] = new_slice
604 root.c[-2:-2] = new_slice
605
606 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"], l)
607 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"],
608 [ c.text for c in root.c ])
609 self.assertEquals(l,
610 [ c.text for c in root.c ])
611
619
621 Element = self.Element
622 root = Element("root")
623 l = ["c1", "c2", "c3", "c4"]
624 root.c = l
625
626 self.assertEquals(["c1", "c2", "c3", "c4"],
627 [ c.text for c in root.c ])
628 self.assertEquals(l,
629 [ c.text for c in root.c ])
630
631 new_slice = ["cA", "cB", "cC"]
632 self.assertRaises(
633 ValueError, operator.setitem,
634 l, slice(1,2,-1), new_slice)
635 self.assertRaises(
636 ValueError, operator.setitem,
637 root.c, slice(1,2,-1), new_slice)
638
640 Element = self.Element
641 root = Element("root")
642 l = ["c1", "c2", "c3", "c4"]
643 root.c = l
644
645 self.assertEquals(["c1", "c2", "c3", "c4"],
646 [ c.text for c in root.c ])
647 self.assertEquals(l,
648 [ c.text for c in root.c ])
649
650 new_slice = ["cA", "cB"]
651 l[-1:1:-1] = new_slice
652 root.c[-1:1:-1] = new_slice
653
654 self.assertEquals(["c1", "c2", "cB", "cA"], l)
655 self.assertEquals(["c1", "c2", "cB", "cA"],
656 [ c.text for c in root.c ])
657 self.assertEquals(l,
658 [ c.text for c in root.c ])
659
661 Element = self.Element
662 root = Element("root")
663 l = ["c1", "c2", "c3", "c4"]
664 root.c = l
665
666 self.assertEquals(["c1", "c2", "c3", "c4"],
667 [ c.text for c in root.c ])
668 self.assertEquals(l,
669 [ c.text for c in root.c ])
670
671 new_slice = ["cA", "cB"]
672 l[-1:-4:-2] = new_slice
673 root.c[-1:-4:-2] = new_slice
674
675 self.assertEquals(["c1", "cB", "c3", "cA"], l)
676 self.assertEquals(["c1", "cB", "c3", "cA"],
677 [ c.text for c in root.c ])
678 self.assertEquals(l,
679 [ c.text for c in root.c ])
680
681
682
690
698
700
701 Element = self.Element
702 root = Element("root")
703
704 root["text"] = "TEST"
705 self.assertEquals(["TEST"],
706 [ c.text for c in root["text"] ])
707
708 root["tail"] = "TEST"
709 self.assertEquals(["TEST"],
710 [ c.text for c in root["tail"] ])
711
712 root["pyval"] = "TEST"
713 self.assertEquals(["TEST"],
714 [ c.text for c in root["pyval"] ])
715
716 root["tag"] = "TEST"
717 self.assertEquals(["TEST"],
718 [ c.text for c in root["tag"] ])
719
727
729 XML = self.XML
730 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
731 self.assertEquals(2, len(root.findall(".//{X}b")))
732 self.assertEquals(3, len(root.findall(".//b")))
733 self.assertEquals(2, len(root.findall("b")))
734
742
757
763
765 Element = self.Element
766 SubElement = self.etree.SubElement
767 root = Element("{objectified}root")
768 root.bool = True
769 self.assertEquals(root.bool, True)
770 self.assertEquals(root.bool + root.bool, True + True)
771 self.assertEquals(True + root.bool, True + root.bool)
772 self.assertEquals(root.bool * root.bool, True * True)
773 self.assertEquals(int(root.bool), int(True))
774 self.assertEquals(hash(root.bool), hash(True))
775 self.assertEquals(complex(root.bool), complex(True))
776 self.assert_(isinstance(root.bool, objectify.BoolElement))
777
778 root.bool = False
779 self.assertEquals(root.bool, False)
780 self.assertEquals(root.bool + root.bool, False + False)
781 self.assertEquals(False + root.bool, False + root.bool)
782 self.assertEquals(root.bool * root.bool, False * False)
783 self.assertEquals(int(root.bool), int(False))
784 self.assertEquals(hash(root.bool), hash(False))
785 self.assertEquals(complex(root.bool), complex(False))
786 self.assert_(isinstance(root.bool, objectify.BoolElement))
787
796
803
810
817
819 Element = self.Element
820 SubElement = self.etree.SubElement
821 root = Element("{objectified}root")
822 root.s = "test"
823
824 self.assertEquals("test" * 5, root.s * 5)
825 self.assertEquals(5 * "test", 5 * root.s)
826
827 self.assertRaises(TypeError, operator.mul, root.s, "honk")
828 self.assertRaises(TypeError, operator.mul, "honk", root.s)
829
839
860
865
870
875
880
889
894
899
904
911
918
925
937
947
952
957
962
969
974
978
985
990
994
1003
1012
1018
1027
1029 pyval = 1
1030 pytype = "NoneType"
1031 objclass = objectify.NoneElement
1032 value = objectify.DataElement(pyval, _pytype=pytype)
1033 self.assert_(isinstance(value, objclass),
1034 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1035 % (pyval, pytype, type(value), objclass))
1036 self.assertEquals(value.text, None)
1037 self.assertEquals(value.pyval, None)
1038
1040
1041 pyval = 1
1042 pytype = "none"
1043 objclass = objectify.NoneElement
1044 value = objectify.DataElement(pyval, _pytype=pytype)
1045 self.assert_(isinstance(value, objclass),
1046 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1047 % (pyval, pytype, type(value), objclass))
1048 self.assertEquals(value.text, None)
1049 self.assertEquals(value.pyval, None)
1050
1056 root = Element("{objectified}root")
1057 root.myfloat = MyFloat(5.5)
1058 self.assert_(isinstance(root.myfloat, objectify.FloatElement))
1059 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
1060
1062 class MyFloat(float):
1063 pass
1064 value = objectify.DataElement(MyFloat(5.5))
1065 self.assert_(isinstance(value, objectify.FloatElement))
1066 self.assertEquals(value, 5.5)
1067 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None)
1068
1070 XML = self.XML
1071 root = XML('''\
1072 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1073 <b xsi:type="boolean">true</b>
1074 <b xsi:type="boolean">false</b>
1075 <b xsi:type="boolean">1</b>
1076 <b xsi:type="boolean">0</b>
1077
1078 <f xsi:type="float">5</f>
1079 <f xsi:type="double">5</f>
1080
1081 <s xsi:type="string">5</s>
1082 <s xsi:type="normalizedString">5</s>
1083 <s xsi:type="token">5</s>
1084 <s xsi:type="language">5</s>
1085 <s xsi:type="Name">5</s>
1086 <s xsi:type="NCName">5</s>
1087 <s xsi:type="ID">5</s>
1088 <s xsi:type="IDREF">5</s>
1089 <s xsi:type="ENTITY">5</s>
1090 <s xsi:type="NMTOKEN">5</s>
1091
1092 <l xsi:type="integer">5</l>
1093 <l xsi:type="nonPositiveInteger">5</l>
1094 <l xsi:type="negativeInteger">5</l>
1095 <l xsi:type="long">5</l>
1096 <l xsi:type="nonNegativeInteger">5</l>
1097 <l xsi:type="unsignedLong">5</l>
1098 <l xsi:type="unsignedInt">5</l>
1099 <l xsi:type="positiveInteger">5</l>
1100
1101 <i xsi:type="int">5</i>
1102 <i xsi:type="short">5</i>
1103 <i xsi:type="byte">5</i>
1104 <i xsi:type="unsignedShort">5</i>
1105 <i xsi:type="unsignedByte">5</i>
1106
1107 <n xsi:nil="true"/>
1108 </root>
1109 ''')
1110
1111 for b in root.b:
1112 self.assert_(isinstance(b, objectify.BoolElement))
1113 self.assertEquals(True, root.b[0])
1114 self.assertEquals(False, root.b[1])
1115 self.assertEquals(True, root.b[2])
1116 self.assertEquals(False, root.b[3])
1117
1118 for f in root.f:
1119 self.assert_(isinstance(f, objectify.FloatElement))
1120 self.assertEquals(5, f)
1121
1122 for s in root.s:
1123 self.assert_(isinstance(s, objectify.StringElement))
1124 self.assertEquals("5", s)
1125
1126 for i in root.i:
1127 self.assert_(isinstance(i, objectify.IntElement))
1128 self.assertEquals(5, i)
1129
1130 for l in root.l:
1131 self.assert_(isinstance(l, objectify.IntElement))
1132 self.assertEquals(5, i)
1133
1134 self.assert_(isinstance(root.n, objectify.NoneElement))
1135 self.assertEquals(None, root.n)
1136
1138 XML = self.XML
1139 root = XML('''\
1140 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1141 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1142 <b xsi:type="xsd:boolean">true</b>
1143 <b xsi:type="xsd:boolean">false</b>
1144 <b xsi:type="xsd:boolean">1</b>
1145 <b xsi:type="xsd:boolean">0</b>
1146
1147 <f xsi:type="xsd:float">5</f>
1148 <f xsi:type="xsd:double">5</f>
1149
1150 <s xsi:type="xsd:string">5</s>
1151 <s xsi:type="xsd:normalizedString">5</s>
1152 <s xsi:type="xsd:token">5</s>
1153 <s xsi:type="xsd:language">5</s>
1154 <s xsi:type="xsd:Name">5</s>
1155 <s xsi:type="xsd:NCName">5</s>
1156 <s xsi:type="xsd:ID">5</s>
1157 <s xsi:type="xsd:IDREF">5</s>
1158 <s xsi:type="xsd:ENTITY">5</s>
1159 <s xsi:type="xsd:NMTOKEN">5</s>
1160
1161 <l xsi:type="xsd:integer">5</l>
1162 <l xsi:type="xsd:nonPositiveInteger">5</l>
1163 <l xsi:type="xsd:negativeInteger">5</l>
1164 <l xsi:type="xsd:long">5</l>
1165 <l xsi:type="xsd:nonNegativeInteger">5</l>
1166 <l xsi:type="xsd:unsignedLong">5</l>
1167 <l xsi:type="xsd:unsignedInt">5</l>
1168 <l xsi:type="xsd:positiveInteger">5</l>
1169
1170 <i xsi:type="xsd:int">5</i>
1171 <i xsi:type="xsd:short">5</i>
1172 <i xsi:type="xsd:byte">5</i>
1173 <i xsi:type="xsd:unsignedShort">5</i>
1174 <i xsi:type="xsd:unsignedByte">5</i>
1175
1176 <n xsi:nil="true"/>
1177 </root>
1178 ''')
1179
1180 for b in root.b:
1181 self.assert_(isinstance(b, objectify.BoolElement))
1182 self.assertEquals(True, root.b[0])
1183 self.assertEquals(False, root.b[1])
1184 self.assertEquals(True, root.b[2])
1185 self.assertEquals(False, root.b[3])
1186
1187 for f in root.f:
1188 self.assert_(isinstance(f, objectify.FloatElement))
1189 self.assertEquals(5, f)
1190
1191 for s in root.s:
1192 self.assert_(isinstance(s, objectify.StringElement))
1193 self.assertEquals("5", s)
1194
1195 for i in root.i:
1196 self.assert_(isinstance(i, objectify.IntElement))
1197 self.assertEquals(5, i)
1198
1199 for l in root.l:
1200 self.assert_(isinstance(l, objectify.IntElement))
1201 self.assertEquals(5, l)
1202
1203 self.assert_(isinstance(root.n, objectify.NoneElement))
1204 self.assertEquals(None, root.n)
1205
1207 XML = self.XML
1208 root = XML(_bytes('<root><b>why</b><b>try</b></root>'))
1209 strs = [ str(s) for s in root.b ]
1210 self.assertEquals(["why", "try"],
1211 strs)
1212
1239
1259
1260
1261
1285
1287 XML = self.XML
1288 root = XML(_bytes("""
1289 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1290 <b xsi:nil="true"></b><b xsi:nil="true"/>
1291 </root>"""))
1292 self.assert_(root.b[0] == root.b[1])
1293 self.assertFalse(root.b[0])
1294 self.assertEquals(root.b[0], None)
1295 self.assertEquals(None, root.b[0])
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1311
1318
1322
1324 XML = self.XML
1325 root = XML(_bytes('''\
1326 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1327 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1328 <b>5</b>
1329 <b>test</b>
1330 <c>1.1</c>
1331 <c>\uF8D2</c>
1332 <x>true</x>
1333 <n xsi:nil="true" />
1334 <n></n>
1335 <b xsi:type="double">5</b>
1336 <b xsi:type="float">5</b>
1337 <s xsi:type="string">23</s>
1338 <s py:pytype="str">42</s>
1339 <f py:pytype="float">300</f>
1340 <l py:pytype="long">2</l>
1341 <t py:pytype="TREE"></t>
1342 </a>
1343 '''))
1344 objectify.annotate(root)
1345
1346 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1347 for c in root.iterchildren() ]
1348 self.assertEquals("int", child_types[ 0])
1349 self.assertEquals("str", child_types[ 1])
1350 self.assertEquals("float", child_types[ 2])
1351 self.assertEquals("str", child_types[ 3])
1352 self.assertEquals("bool", child_types[ 4])
1353 self.assertEquals("NoneType", child_types[ 5])
1354 self.assertEquals(None, child_types[ 6])
1355 self.assertEquals("float", child_types[ 7])
1356 self.assertEquals("float", child_types[ 8])
1357 self.assertEquals("str", child_types[ 9])
1358 self.assertEquals("int", child_types[10])
1359 self.assertEquals("int", child_types[11])
1360 self.assertEquals("int", child_types[12])
1361 self.assertEquals(None, child_types[13])
1362
1363 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1364
1384
1386 XML = self.XML
1387 root = XML(_bytes('''\
1388 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1389 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1390 <b>5</b>
1391 <b>test</b>
1392 <c>1.1</c>
1393 <c>\uF8D2</c>
1394 <x>true</x>
1395 <n xsi:nil="true" />
1396 <n></n>
1397 <b xsi:type="double">5</b>
1398 <b xsi:type="float">5</b>
1399 <s xsi:type="string">23</s>
1400 <s py:pytype="str">42</s>
1401 <f py:pytype="float">300</f>
1402 <l py:pytype="long">2</l>
1403 <t py:pytype="TREE"></t>
1404 </a>
1405 '''))
1406 objectify.annotate(root, ignore_old=False)
1407
1408 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1409 for c in root.iterchildren() ]
1410 self.assertEquals("int", child_types[ 0])
1411 self.assertEquals("str", child_types[ 1])
1412 self.assertEquals("float", child_types[ 2])
1413 self.assertEquals("str", child_types[ 3])
1414 self.assertEquals("bool", child_types[ 4])
1415 self.assertEquals("NoneType", child_types[ 5])
1416 self.assertEquals(None, child_types[ 6])
1417 self.assertEquals("float", child_types[ 7])
1418 self.assertEquals("float", child_types[ 8])
1419 self.assertEquals("str", child_types[ 9])
1420 self.assertEquals("str", child_types[10])
1421 self.assertEquals("float", child_types[11])
1422 self.assertEquals("int", child_types[12])
1423 self.assertEquals(TREE_PYTYPE, child_types[13])
1424
1425 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1426
1428 XML = self.XML
1429 root = XML(_bytes('''\
1430 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1431 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1432 <b>5</b>
1433 <b>test</b>
1434 <c>1.1</c>
1435 <c>\uF8D2</c>
1436 <x>true</x>
1437 <n xsi:nil="true" />
1438 <n></n>
1439 <b xsi:type="double">5</b>
1440 <b xsi:type="float">5</b>
1441 <s xsi:type="string">23</s>
1442 <s py:pytype="str">42</s>
1443 <f py:pytype="float">300</f>
1444 <l py:pytype="long">2</l>
1445 <t py:pytype="TREE"></t>
1446 </a>
1447 '''))
1448 objectify.annotate(root, ignore_old=False, ignore_xsi=False,
1449 annotate_xsi=1, annotate_pytype=1)
1450
1451
1452 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1453 for c in root.iterchildren() ]
1454 self.assertEquals("int", child_types[ 0])
1455 self.assertEquals("str", child_types[ 1])
1456 self.assertEquals("float", child_types[ 2])
1457 self.assertEquals("str", child_types[ 3])
1458 self.assertEquals("bool", child_types[ 4])
1459 self.assertEquals("NoneType", child_types[ 5])
1460 self.assertEquals(None, child_types[ 6])
1461 self.assertEquals("float", child_types[ 7])
1462 self.assertEquals("float", child_types[ 8])
1463 self.assertEquals("str", child_types[ 9])
1464 self.assertEquals("str", child_types[10])
1465 self.assertEquals("float", child_types[11])
1466 self.assertEquals("int", child_types[12])
1467 self.assertEquals(TREE_PYTYPE, child_types[13])
1468
1469 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1470
1471 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1472 for c in root.iterchildren() ]
1473
1474
1475 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1476 for c in root.iterchildren() ]
1477 self.assertEquals("xsd:integer", child_types[ 0])
1478 self.assertEquals("xsd:string", child_types[ 1])
1479 self.assertEquals("xsd:double", child_types[ 2])
1480 self.assertEquals("xsd:string", child_types[ 3])
1481 self.assertEquals("xsd:boolean", child_types[ 4])
1482 self.assertEquals(None, child_types[ 5])
1483 self.assertEquals(None, child_types[ 6])
1484 self.assertEquals("xsd:double", child_types[ 7])
1485 self.assertEquals("xsd:float", child_types[ 8])
1486 self.assertEquals("xsd:string", child_types[ 9])
1487 self.assertEquals("xsd:string", child_types[10])
1488 self.assertEquals("xsd:double", child_types[11])
1489 self.assertEquals("xsd:integer", child_types[12])
1490 self.assertEquals(None, child_types[13])
1491
1492 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1493
1495 XML = self.XML
1496 root = XML(_bytes('''\
1497 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1498 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1499 <b>5</b>
1500 <b>test</b>
1501 <c>1.1</c>
1502 <c>\uF8D2</c>
1503 <x>true</x>
1504 <n xsi:nil="true" />
1505 <n></n>
1506 <b xsi:type="double">5</b>
1507 <b xsi:type="float">5</b>
1508 <s xsi:type="string">23</s>
1509 <s py:pytype="str">42</s>
1510 <f py:pytype="float">300</f>
1511 <l py:pytype="long">2</l>
1512 <t py:pytype="TREE"></t>
1513 </a>
1514 '''))
1515 objectify.xsiannotate(root, ignore_old=False)
1516
1517 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1518 for c in root.iterchildren() ]
1519 self.assertEquals("xsd:integer", child_types[ 0])
1520 self.assertEquals("xsd:string", child_types[ 1])
1521 self.assertEquals("xsd:double", child_types[ 2])
1522 self.assertEquals("xsd:string", child_types[ 3])
1523 self.assertEquals("xsd:boolean", child_types[ 4])
1524 self.assertEquals(None, child_types[ 5])
1525 self.assertEquals(None, child_types[ 6])
1526 self.assertEquals("xsd:double", child_types[ 7])
1527 self.assertEquals("xsd:float", child_types[ 8])
1528 self.assertEquals("xsd:string", child_types[ 9])
1529 self.assertEquals("xsd:string", child_types[10])
1530 self.assertEquals("xsd:double", child_types[11])
1531 self.assertEquals("xsd:integer", child_types[12])
1532 self.assertEquals(None, child_types[13])
1533
1535 XML = self.XML
1536 root = XML(_bytes('''\
1537 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1538 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1539 <b>5</b>
1540 <b>test</b>
1541 <c>1.1</c>
1542 <c>\uF8D2</c>
1543 <x>true</x>
1544 <n xsi:nil="true" />
1545 <n></n>
1546 <b xsi:type="double">5</b>
1547 <b xsi:type="float">5</b>
1548 <s xsi:type="string">23</s>
1549 <s py:pytype="str">42</s>
1550 <f py:pytype="float">300</f>
1551 <l py:pytype="long">2</l>
1552 <t py:pytype="TREE"></t>
1553 </a>
1554 '''))
1555 objectify.pyannotate(root, ignore_old=True)
1556
1557 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1558 for c in root.iterchildren() ]
1559 self.assertEquals("int", child_types[ 0])
1560 self.assertEquals("str", child_types[ 1])
1561 self.assertEquals("float", child_types[ 2])
1562 self.assertEquals("str", child_types[ 3])
1563 self.assertEquals("bool", child_types[ 4])
1564 self.assertEquals("NoneType", child_types[ 5])
1565 self.assertEquals(None, child_types[ 6])
1566 self.assertEquals("float", child_types[ 7])
1567 self.assertEquals("float", child_types[ 8])
1568 self.assertEquals("str", child_types[ 9])
1569 self.assertEquals("int", child_types[10])
1570 self.assertEquals("int", child_types[11])
1571 self.assertEquals("int", child_types[12])
1572 self.assertEquals(None, child_types[13])
1573
1574 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1575
1595
1597 XML = self.XML
1598 root = XML('''\
1599 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1600 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1601 <b>5</b>
1602 <b>test</b>
1603 <c>1.1</c>
1604 <c>\uF8D2</c>
1605 <x>true</x>
1606 <n xsi:nil="true" />
1607 <n></n>
1608 <b xsi:type="double">5</b>
1609 <b xsi:type="float">5</b>
1610 <s xsi:type="string">23</s>
1611 <s py:pytype="str">42</s>
1612 <f py:pytype="float">300</f>
1613 <l py:pytype="long">2</l>
1614 <t py:pytype="TREE"></t>
1615 </a>
1616 ''')
1617 objectify.pyannotate(root)
1618
1619 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1620 for c in root.iterchildren() ]
1621 self.assertEquals("int", child_types[ 0])
1622 self.assertEquals("str", child_types[ 1])
1623 self.assertEquals("float", child_types[ 2])
1624 self.assertEquals("str", child_types[ 3])
1625 self.assertEquals("bool", child_types[ 4])
1626 self.assertEquals("NoneType", child_types[ 5])
1627 self.assertEquals(None, child_types[ 6])
1628 self.assertEquals("float", child_types[ 7])
1629 self.assertEquals("float", child_types[ 8])
1630 self.assertEquals("str", child_types[ 9])
1631 self.assertEquals("str", child_types[10])
1632 self.assertEquals("float", child_types[11])
1633 self.assertEquals("int", child_types[12])
1634 self.assertEquals(TREE_PYTYPE, child_types[13])
1635
1636 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1637
1639 XML = self.XML
1640 root = XML(_bytes('''\
1641 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1642 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1643 <b>5</b>
1644 <b>test</b>
1645 <c>1.1</c>
1646 <c>\uF8D2</c>
1647 <x>true</x>
1648 <n xsi:nil="true" />
1649 <n></n>
1650 <b xsi:type="double">5</b>
1651 <b xsi:type="float">5</b>
1652 <s xsi:type="string">23</s>
1653 <s py:pytype="str">42</s>
1654 <f py:pytype="float">300</f>
1655 <l py:pytype="long">2</l>
1656 <t py:pytype="TREE"></t>
1657 </a>
1658 '''))
1659 objectify.xsiannotate(root, ignore_old=True)
1660
1661 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1662 for c in root.iterchildren() ]
1663 self.assertEquals("xsd:integer", child_types[ 0])
1664 self.assertEquals("xsd:string", child_types[ 1])
1665 self.assertEquals("xsd:double", child_types[ 2])
1666 self.assertEquals("xsd:string", child_types[ 3])
1667 self.assertEquals("xsd:boolean", child_types[ 4])
1668 self.assertEquals(None, child_types[ 5])
1669 self.assertEquals(None, child_types[ 6])
1670 self.assertEquals("xsd:integer", child_types[ 7])
1671 self.assertEquals("xsd:integer", child_types[ 8])
1672 self.assertEquals("xsd:integer", child_types[ 9])
1673 self.assertEquals("xsd:string", child_types[10])
1674 self.assertEquals("xsd:double", child_types[11])
1675 self.assertEquals("xsd:integer", child_types[12])
1676 self.assertEquals(None, child_types[13])
1677
1678 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1679
1681 XML = self.XML
1682 root = XML(_bytes('''\
1683 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1684 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1685 <b>5</b>
1686 <b>test</b>
1687 <c>1.1</c>
1688 <c>\uF8D2</c>
1689 <x>true</x>
1690 <n xsi:nil="true" />
1691 <n></n>
1692 <b xsi:type="double">5</b>
1693 <b xsi:type="float">5</b>
1694 <s xsi:type="string">23</s>
1695 <s py:pytype="str">42</s>
1696 <f py:pytype="float">300</f>
1697 <l py:pytype="long">2</l>
1698 <t py:pytype="TREE"></t>
1699 </a>
1700 '''))
1701 objectify.deannotate(root)
1702
1703 for c in root.getiterator():
1704 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1705 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1706
1707 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1708
1710 XML = self.XML
1711 root = XML(_bytes('''\
1712 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1713 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1714 <b>5</b>
1715 <b>test</b>
1716 <c>1.1</c>
1717 <c>\uF8D2</c>
1718 <x>true</x>
1719 <n xsi:nil="true" />
1720 <n></n>
1721 <b xsi:type="double">5</b>
1722 <b xsi:type="float">5</b>
1723 <s xsi:type="string">23</s>
1724 <s py:pytype="str">42</s>
1725 <f py:pytype="float">300</f>
1726 <l py:pytype="long">2</l>
1727 <t py:pytype="TREE"></t>
1728 </a>
1729 '''))
1730 objectify.annotate(
1731 root, ignore_old=False, ignore_xsi=False, annotate_xsi=True,
1732 empty_pytype='str', empty_type='string')
1733 objectify.deannotate(root, pytype=False, xsi=False, xsi_nil=True)
1734
1735 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1736 for c in root.iterchildren() ]
1737 self.assertEquals("xsd:integer", child_types[ 0])
1738 self.assertEquals("xsd:string", child_types[ 1])
1739 self.assertEquals("xsd:double", child_types[ 2])
1740 self.assertEquals("xsd:string", child_types[ 3])
1741 self.assertEquals("xsd:boolean", child_types[ 4])
1742 self.assertEquals(None, child_types[ 5])
1743 self.assertEquals("xsd:string", child_types[ 6])
1744 self.assertEquals("xsd:double", child_types[ 7])
1745 self.assertEquals("xsd:float", child_types[ 8])
1746 self.assertEquals("xsd:string", child_types[ 9])
1747 self.assertEquals("xsd:string", child_types[10])
1748 self.assertEquals("xsd:double", child_types[11])
1749 self.assertEquals("xsd:integer", child_types[12])
1750 self.assertEquals(None, child_types[13])
1751
1752 self.assertEquals(None, root.n.get(XML_SCHEMA_NIL_ATTR))
1753
1754 for c in root.iterchildren():
1755 self.assertNotEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1756
1757 if (c.get(objectify.PYTYPE_ATTRIBUTE) not in [TREE_PYTYPE,
1758 "NoneType"]):
1759 self.assertNotEquals(
1760 None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1761
1763 XML = self.XML
1764 root = XML(_bytes('''\
1765 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1766 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1767 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1768 <b>5</b>
1769 <b>test</b>
1770 <c>1.1</c>
1771 <c>\uF8D2</c>
1772 <x>true</x>
1773 <n xsi:nil="true" />
1774 <n></n>
1775 <b xsi:type="xsd:double">5</b>
1776 <b xsi:type="xsd:float">5</b>
1777 <s xsi:type="xsd:string">23</s>
1778 <s py:pytype="str">42</s>
1779 <f py:pytype="float">300</f>
1780 <l py:pytype="long">2</l>
1781 <t py:pytype="TREE"></t>
1782 </a>
1783 '''))
1784 objectify.annotate(root)
1785 objectify.deannotate(root, pytype=False)
1786
1787 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1788 for c in root.iterchildren() ]
1789 self.assertEquals("int", child_types[ 0])
1790 self.assertEquals("str", child_types[ 1])
1791 self.assertEquals("float", child_types[ 2])
1792 self.assertEquals("str", child_types[ 3])
1793 self.assertEquals("bool", child_types[ 4])
1794 self.assertEquals("NoneType", child_types[ 5])
1795 self.assertEquals(None, child_types[ 6])
1796 self.assertEquals("float", child_types[ 7])
1797 self.assertEquals("float", child_types[ 8])
1798 self.assertEquals("str", child_types[ 9])
1799 self.assertEquals("int", child_types[10])
1800 self.assertEquals("int", child_types[11])
1801 self.assertEquals("int", child_types[12])
1802 self.assertEquals(None, child_types[13])
1803
1804 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1805
1806 for c in root.getiterator():
1807 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1808
1810 XML = self.XML
1811 root = XML(_bytes('''\
1812 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1813 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1814 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1815 <b xsi:type="xsd:int">5</b>
1816 <b xsi:type="xsd:string">test</b>
1817 <c xsi:type="xsd:float">1.1</c>
1818 <c xsi:type="xsd:string">\uF8D2</c>
1819 <x xsi:type="xsd:boolean">true</x>
1820 <n xsi:nil="true" />
1821 <n></n>
1822 <b xsi:type="xsd:double">5</b>
1823 <b xsi:type="xsd:float">5</b>
1824 <s xsi:type="xsd:string">23</s>
1825 <s xsi:type="xsd:string">42</s>
1826 <f xsi:type="xsd:float">300</f>
1827 <l xsi:type="xsd:long">2</l>
1828 <t py:pytype="TREE"></t>
1829 </a>
1830 '''))
1831 objectify.annotate(root)
1832 objectify.deannotate(root, xsi=False)
1833
1834 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1835 for c in root.iterchildren() ]
1836 self.assertEquals("xsd:int", child_types[ 0])
1837 self.assertEquals("xsd:string", child_types[ 1])
1838 self.assertEquals("xsd:float", child_types[ 2])
1839 self.assertEquals("xsd:string", child_types[ 3])
1840 self.assertEquals("xsd:boolean", child_types[ 4])
1841 self.assertEquals(None, child_types[ 5])
1842 self.assertEquals(None, child_types[ 6])
1843 self.assertEquals("xsd:double", child_types[ 7])
1844 self.assertEquals("xsd:float", child_types[ 8])
1845 self.assertEquals("xsd:string", child_types[ 9])
1846 self.assertEquals("xsd:string", child_types[10])
1847 self.assertEquals("xsd:float", child_types[11])
1848 self.assertEquals("xsd:long", child_types[12])
1849 self.assertEquals(None, child_types[13])
1850
1851 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1852
1853 for c in root.getiterator():
1854 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1855
1857 XML = self.XML
1858
1859 xml = _bytes('''\
1860 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1861 <b>5</b>
1862 <b>test</b>
1863 <c>1.1</c>
1864 <c>\uF8D2</c>
1865 <x>true</x>
1866 <n xsi:nil="true" />
1867 <n></n>
1868 <b xsi:type="double">5</b>
1869 </a>
1870 ''')
1871
1872 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1873 objectify.set_pytype_attribute_tag("{TEST}test")
1874
1875 root = XML(xml)
1876 objectify.annotate(root)
1877
1878 attribs = root.xpath("//@py:%s" % pytype_name,
1879 namespaces={"py" : pytype_ns})
1880 self.assertEquals(0, len(attribs))
1881 attribs = root.xpath("//@py:test",
1882 namespaces={"py" : "TEST"})
1883 self.assertEquals(7, len(attribs))
1884
1885 objectify.set_pytype_attribute_tag()
1886 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1887
1888 self.assertNotEqual("test", pytype_ns.lower())
1889 self.assertNotEqual("test", pytype_name.lower())
1890
1891 root = XML(xml)
1892 attribs = root.xpath("//@py:%s" % pytype_name,
1893 namespaces={"py" : pytype_ns})
1894 self.assertEquals(0, len(attribs))
1895
1896 objectify.annotate(root)
1897 attribs = root.xpath("//@py:%s" % pytype_name,
1898 namespaces={"py" : pytype_ns})
1899 self.assertEquals(7, len(attribs))
1900
1908
1909 def checkMyType(s):
1910 return True
1911
1912 pytype = objectify.PyType("mytype", checkMyType, NewType)
1913 self.assert_(pytype not in objectify.getRegisteredTypes())
1914 pytype.register()
1915 self.assert_(pytype in objectify.getRegisteredTypes())
1916 pytype.unregister()
1917 self.assert_(pytype not in objectify.getRegisteredTypes())
1918
1919 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1920 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1921 pytype.unregister()
1922
1923 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1924 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1925 pytype.unregister()
1926
1927 self.assertRaises(ValueError, pytype.register,
1928 before = [objectify.getRegisteredTypes()[0].name],
1929 after = [objectify.getRegisteredTypes()[1].name])
1930
1932 from datetime import datetime
1933 def parse_date(value):
1934 if len(value) != 14:
1935 raise ValueError(value)
1936 Y = int(value[0:4])
1937 M = int(value[4:6])
1938 D = int(value[6:8])
1939 h = int(value[8:10])
1940 m = int(value[10:12])
1941 s = int(value[12:14])
1942 return datetime(Y, M, D, h, m, s)
1943
1944 def stringify_date(date):
1945 return date.strftime("%Y%m%d%H%M%S")
1946
1947 class DatetimeElement(objectify.ObjectifiedDataElement):
1948 def pyval(self):
1949 return parse_date(self.text)
1950 pyval = property(pyval)
1951
1952 datetime_type = objectify.PyType(
1953 "datetime", parse_date, DatetimeElement, stringify_date)
1954 datetime_type.xmlSchemaTypes = "dateTime"
1955 datetime_type.register()
1956
1957 NAMESPACE = "http://foo.net/xmlns"
1958 NAMESPACE_MAP = {'ns': NAMESPACE}
1959
1960 r = objectify.Element("{%s}root" % NAMESPACE, nsmap=NAMESPACE_MAP)
1961 time = datetime.now()
1962 r.date = time
1963
1964 self.assert_(isinstance(r.date, DatetimeElement))
1965 self.assert_(isinstance(r.date.pyval, datetime))
1966
1967 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1968 self.assertEquals(r.date.text, stringify_date(time))
1969
1970 r.date = objectify.E.date(time)
1971
1972 self.assert_(isinstance(r.date, DatetimeElement))
1973 self.assert_(isinstance(r.date.pyval, datetime))
1974
1975 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1976 self.assertEquals(r.date.text, stringify_date(time))
1977
1978 date = objectify.DataElement(time)
1979
1980 self.assert_(isinstance(date, DatetimeElement))
1981 self.assert_(isinstance(date.pyval, datetime))
1982
1983 self.assertEquals(date.pyval, parse_date(stringify_date(time)))
1984 self.assertEquals(date.text, stringify_date(time))
1985
1991
1997
2002
2011
2018
2026
2029
2032
2051
2056
2061
2066
2071
2091
2093 root = self.XML(xml_str)
2094 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
2095 self.assertEquals(root.c1.c2.text, path(root).text)
2096
2097 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
2098 self.assertEquals(root.c1.c2[2].text, path(root).text)
2099
2100 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
2101 self.assertEquals(root.c1.c2[2].text, path(root).text)
2102
2103 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
2104 self.assertEquals(root.c1.c2[-1].text, path(root).text)
2105
2106 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
2107 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2108
2110 self.assertRaises(ValueError, objectify.ObjectPath,
2111 "root.c1[0].c2[-1-2]")
2112 self.assertRaises(ValueError, objectify.ObjectPath,
2113 ['root', 'c1[0]', 'c2[-1-2]'])
2114
2115 self.assertRaises(ValueError, objectify.ObjectPath,
2116 "root[2].c1.c2")
2117 self.assertRaises(ValueError, objectify.ObjectPath,
2118 ['root[2]', 'c1', 'c2'])
2119
2120 self.assertRaises(ValueError, objectify.ObjectPath,
2121 [])
2122 self.assertRaises(ValueError, objectify.ObjectPath,
2123 ['', '', ''])
2124
2126 root = self.XML(xml_str)
2127 path = objectify.ObjectPath("root.c1[9999].c2")
2128 self.assertRaises(AttributeError, path, root)
2129
2130 path = objectify.ObjectPath("root.c1[0].c2[9999]")
2131 self.assertRaises(AttributeError, path, root)
2132
2133 path = objectify.ObjectPath(".c1[9999].c2[0]")
2134 self.assertRaises(AttributeError, path, root)
2135
2136 path = objectify.ObjectPath("root.c1[-2].c2")
2137 self.assertRaises(AttributeError, path, root)
2138
2139 path = objectify.ObjectPath("root.c1[0].c2[-4]")
2140 self.assertRaises(AttributeError, path, root)
2141
2155
2157 root = self.XML(xml_str)
2158 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
2159 self.assertEquals(root.c1.c2.text, path.find(root).text)
2160 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
2161 self.assertEquals(root.c1.c2.text, path.find(root).text)
2162 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
2163 self.assertEquals(root.c1.c2.text, path.find(root).text)
2164 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
2165 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2166 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
2167 self.assertEquals(root.c1.c2.text, path.find(root).text)
2168 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
2169 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2170 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
2171 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2172 path.find(root).text)
2173
2186
2201
2213
2227
2229 root = self.XML(xml_str)
2230 path = objectify.ObjectPath( "root.c1.c99" )
2231 self.assertRaises(AttributeError, path.find, root)
2232
2233 new_el = self.Element("{objectified}test")
2234 new_el.a = ["TEST1", "TEST2"]
2235 new_el.a[0].set("myattr", "ATTR1")
2236 new_el.a[1].set("myattr", "ATTR2")
2237
2238 path.setattr(root, list(new_el.a))
2239
2240 self.assertEquals(2, len(root.c1.c99))
2241 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
2242 self.assertEquals("TEST1", root.c1.c99[0].text)
2243 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
2244 self.assertEquals("TEST2", root.c1.c99[1].text)
2245 self.assertEquals("TEST1", path(root).text)
2246
2255
2269
2281
2295
2310
2312 root = self.XML(xml_str)
2313 self.assertEquals(
2314 ['{objectified}root', '{objectified}root.c1',
2315 '{objectified}root.c1.c2',
2316 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]',
2317 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'],
2318 root.descendantpaths())
2319
2321 root = self.XML(xml_str)
2322 self.assertEquals(
2323 ['{objectified}c1', '{objectified}c1.c2',
2324 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]',
2325 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'],
2326 root.c1.descendantpaths())
2327
2329 root = self.XML(xml_str)
2330 self.assertEquals(
2331 ['root.{objectified}c1', 'root.{objectified}c1.c2',
2332 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]',
2333 'root.{objectified}c1.{otherNS}c2',
2334 'root.{objectified}c1.{}c2'],
2335 root.c1.descendantpaths('root'))
2336
2348
2361
2365
2369
2373
2379
2384
2386 import pickle
2387 if isinstance(stringOrElt, (etree._Element, etree._ElementTree)):
2388 elt = stringOrElt
2389 else:
2390 elt = self.XML(stringOrElt)
2391 out = BytesIO()
2392 pickle.dump(elt, out)
2393
2394 new_elt = pickle.loads(out.getvalue())
2395 self.assertEquals(
2396 etree.tostring(new_elt),
2397 etree.tostring(elt))
2398
2399
2400
2405
2410
2415
2420
2425
2430
2435
2440
2442 E = objectify.E
2443 DataElement = objectify.DataElement
2444 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1),
2445 DataElement(2.0))
2446 self.assert_(isinstance(root, objectify.ObjectifiedElement))
2447 self.assertEquals(root.text, "text")
2448 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement))
2449 self.assertEquals(root.sub.tail, "tail")
2450 self.assert_(isinstance(root.sub.subsub, objectify.StringElement))
2451 self.assertEquals(len(root.value), 2)
2452 self.assert_(isinstance(root.value[0], objectify.IntElement))
2453 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2454
2461
2462 attr = Attribute()
2463 self.assertEquals(attr.text, None)
2464 self.assertEquals(attr.get("datatype"), "TYPE")
2465 self.assertEquals(attr.get("range"), "0.,1.")
2466
2471
2478
2483
2489
2491 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2492 self.assertEquals(root.base, "http://no/such/url")
2493 self.assertEquals(
2494 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2495 root.base = "https://secret/url"
2496 self.assertEquals(root.base, "https://secret/url")
2497 self.assertEquals(
2498 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2499 "https://secret/url")
2500
2502 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2503 self.assertEquals(root.base, "http://no/such/url")
2504 self.assertEquals(
2505 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2506 root.set('{http://www.w3.org/XML/1998/namespace}base',
2507 "https://secret/url")
2508 self.assertEquals(root.base, "https://secret/url")
2509 self.assertEquals(
2510 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2511 "https://secret/url")
2512
2514 XML = self.XML
2515
2516 xml = _bytes('''\
2517 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2518 <i>5</i>
2519 <i>-5</i>
2520 <l>4294967296</l>
2521 <l>-4294967296</l>
2522 <f>1.1</f>
2523 <b>true</b>
2524 <b>false</b>
2525 <s>Strange things happen, where strings collide</s>
2526 <s>True</s>
2527 <s>False</s>
2528 <s>t</s>
2529 <s>f</s>
2530 <s></s>
2531 <s>None</s>
2532 <n xsi:nil="true" />
2533 </root>
2534 ''')
2535 root = XML(xml)
2536
2537 for i in root.i:
2538 self.assert_(isinstance(i, objectify.IntElement))
2539 for l in root.l:
2540 self.assert_(isinstance(l, objectify.IntElement))
2541 for f in root.f:
2542 self.assert_(isinstance(f, objectify.FloatElement))
2543 for b in root.b:
2544 self.assert_(isinstance(b, objectify.BoolElement))
2545 self.assertEquals(True, root.b[0])
2546 self.assertEquals(False, root.b[1])
2547 for s in root.s:
2548 self.assert_(isinstance(s, objectify.StringElement))
2549 self.assert_(isinstance(root.n, objectify.NoneElement))
2550 self.assertEquals(None, root.n)
2551
2553 suite = unittest.TestSuite()
2554 suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
2555 suite.addTests(doctest.DocTestSuite(objectify))
2556 if sys.version_info >= (2,4):
2557 suite.addTests(
2558 [make_doctest('../../../doc/objectify.txt')])
2559 return suite
2560
2561 if __name__ == '__main__':
2562 print('to test use test.py %s' % __file__)
2563