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
325
329
333
338
340 root = self.XML(xml_str)
341 self.assertEquals("0", getattr(root.c1, "{objectified}c2").text)
342 self.assertEquals("3", getattr(root.c1, "{otherNS}c2").text)
343
345 root = self.XML(xml_str)
346 self.assertRaises(AttributeError, getattr, root.c1, "NOT_THERE")
347 self.assertRaises(AttributeError, getattr, root.c1, "{unknownNS}c2")
348
355
357 root = self.XML(xml_str)
358 self.assertEquals(1, len(root.c1))
359
360 new_el = self.Element("test", myattr="5")
361 root.addattr("c1", new_el)
362 self.assertEquals(2, len(root.c1))
363 self.assertEquals(None, root.c1[0].get("myattr"))
364 self.assertEquals("5", root.c1[1].get("myattr"))
365
367 root = self.XML(xml_str)
368 self.assertEquals(1, len(root.c1))
369
370 new_el = self.Element("test")
371 self.etree.SubElement(new_el, "a", myattr="A")
372 self.etree.SubElement(new_el, "a", myattr="B")
373
374 root.addattr("c1", list(new_el.a))
375 self.assertEquals(3, len(root.c1))
376 self.assertEquals(None, root.c1[0].get("myattr"))
377 self.assertEquals("A", root.c1[1].get("myattr"))
378 self.assertEquals("B", root.c1[2].get("myattr"))
379
386
388 root = self.XML(xml_str)
389 self.assertEquals("0", root.c1.c2[0].text)
390 self.assertEquals("1", root.c1.c2[1].text)
391 self.assertEquals("2", root.c1.c2[2].text)
392 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
393
395 root = self.XML(xml_str)
396 self.assertEquals("0", root.c1.c2[0].text)
397 self.assertEquals("0", root.c1.c2[-3].text)
398 self.assertEquals("1", root.c1.c2[-2].text)
399 self.assertEquals("2", root.c1.c2[-1].text)
400 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
401
403 root = self.XML(xml_str)
404 self.assertEquals(1, len(root))
405 self.assertEquals(1, len(root.c1))
406 self.assertEquals(3, len(root.c1.c2))
407
416
422
432
437
442
443
444
446 root = self.XML("<root><c>c1</c><c>c2</c></root>")
447 self.assertEquals(["c1", "c2"],
448 [ c.text for c in root.c[:] ])
449
451 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
452 test_list = ["c1", "c2", "c3", "c4"]
453
454 self.assertEquals(test_list,
455 [ c.text for c in root.c[:] ])
456 self.assertEquals(test_list[1:2],
457 [ c.text for c in root.c[1:2] ])
458 self.assertEquals(test_list[-3:-1],
459 [ c.text for c in root.c[-3:-1] ])
460 self.assertEquals(test_list[-3:3],
461 [ c.text for c in root.c[-3:3] ])
462 self.assertEquals(test_list[-3000:3],
463 [ c.text for c in root.c[-3000:3] ])
464 self.assertEquals(test_list[-3:3000],
465 [ c.text for c in root.c[-3:3000] ])
466
468 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
469 test_list = ["c1", "c2", "c3", "c4"]
470
471 self.assertEquals(test_list,
472 [ c.text for c in root.c[:] ])
473 self.assertEquals(test_list[2:1:-1],
474 [ c.text for c in root.c[2:1:-1] ])
475 self.assertEquals(test_list[-1:-3:-1],
476 [ c.text for c in root.c[-1:-3:-1] ])
477 self.assertEquals(test_list[2:-3:-1],
478 [ c.text for c in root.c[2:-3:-1] ])
479 self.assertEquals(test_list[2:-3000:-1],
480 [ c.text for c in root.c[2:-3000:-1] ])
481
482
483
495
497 Element = self.Element
498 root = Element("root")
499 root.c = ["c1", "c2"]
500
501 c1 = root.c[0]
502 c2 = root.c[1]
503
504 self.assertEquals([c1,c2], list(root.c))
505 self.assertEquals(["c1", "c2"],
506 [ c.text for c in root.c ])
507
508 root2 = Element("root2")
509 root2.el = [ "test", "test" ]
510 self.assertEquals(["test", "test"],
511 [ el.text for el in root2.el ])
512
513 root.c = [ root2.el, root2.el ]
514 self.assertEquals(["test", "test"],
515 [ c.text for c in root.c ])
516 self.assertEquals(["test", "test"],
517 [ el.text for el in root2.el ])
518
519 root.c[:] = [ c1, c2, c2, c1 ]
520 self.assertEquals(["c1", "c2", "c2", "c1"],
521 [ c.text for c in root.c ])
522
524 Element = self.Element
525 root = Element("root")
526 l = ["c1", "c2", "c3", "c4"]
527 root.c = l
528
529 self.assertEquals(["c1", "c2", "c3", "c4"],
530 [ c.text for c in root.c ])
531 self.assertEquals(l,
532 [ c.text for c in root.c ])
533
534 new_slice = ["cA", "cB"]
535 l[1:2] = new_slice
536 root.c[1:2] = new_slice
537
538 self.assertEquals(["c1", "cA", "cB", "c3", "c4"], l)
539 self.assertEquals(["c1", "cA", "cB", "c3", "c4"],
540 [ c.text for c in root.c ])
541 self.assertEquals(l,
542 [ c.text for c in root.c ])
543
545 Element = self.Element
546 root = Element("root")
547 l = ["c1", "c2", "c3", "c4"]
548 root.c = l
549
550 self.assertEquals(["c1", "c2", "c3", "c4"],
551 [ c.text for c in root.c ])
552 self.assertEquals(l,
553 [ c.text for c in root.c ])
554
555 new_slice = ["cA", "cB"]
556 l[1:1] = new_slice
557 root.c[1:1] = new_slice
558
559 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"], l)
560 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"],
561 [ c.text for c in root.c ])
562 self.assertEquals(l,
563 [ c.text for c in root.c ])
564
566 Element = self.Element
567 root = Element("root")
568 l = ["c1", "c2", "c3", "c4"]
569 root.c = l
570
571 self.assertEquals(["c1", "c2", "c3", "c4"],
572 [ c.text for c in root.c ])
573 self.assertEquals(l,
574 [ c.text for c in root.c ])
575
576 new_slice = ["cA", "cB"]
577 l[-2:-2] = new_slice
578 root.c[-2:-2] = new_slice
579
580 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"], l)
581 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"],
582 [ c.text for c in root.c ])
583 self.assertEquals(l,
584 [ c.text for c in root.c ])
585
593
595 Element = self.Element
596 root = Element("root")
597 l = ["c1", "c2", "c3", "c4"]
598 root.c = l
599
600 self.assertEquals(["c1", "c2", "c3", "c4"],
601 [ c.text for c in root.c ])
602 self.assertEquals(l,
603 [ c.text for c in root.c ])
604
605 new_slice = ["cA", "cB", "cC"]
606 self.assertRaises(
607 ValueError, operator.setitem,
608 l, slice(1,2,-1), new_slice)
609 self.assertRaises(
610 ValueError, operator.setitem,
611 root.c, slice(1,2,-1), new_slice)
612
614 Element = self.Element
615 root = Element("root")
616 l = ["c1", "c2", "c3", "c4"]
617 root.c = l
618
619 self.assertEquals(["c1", "c2", "c3", "c4"],
620 [ c.text for c in root.c ])
621 self.assertEquals(l,
622 [ c.text for c in root.c ])
623
624 new_slice = ["cA", "cB"]
625 l[-1:1:-1] = new_slice
626 root.c[-1:1:-1] = new_slice
627
628 self.assertEquals(["c1", "c2", "cB", "cA"], l)
629 self.assertEquals(["c1", "c2", "cB", "cA"],
630 [ c.text for c in root.c ])
631 self.assertEquals(l,
632 [ c.text for c in root.c ])
633
635 Element = self.Element
636 root = Element("root")
637 l = ["c1", "c2", "c3", "c4"]
638 root.c = l
639
640 self.assertEquals(["c1", "c2", "c3", "c4"],
641 [ c.text for c in root.c ])
642 self.assertEquals(l,
643 [ c.text for c in root.c ])
644
645 new_slice = ["cA", "cB"]
646 l[-1:-4:-2] = new_slice
647 root.c[-1:-4:-2] = new_slice
648
649 self.assertEquals(["c1", "cB", "c3", "cA"], l)
650 self.assertEquals(["c1", "cB", "c3", "cA"],
651 [ c.text for c in root.c ])
652 self.assertEquals(l,
653 [ c.text for c in root.c ])
654
655
656
664
672
674
675 Element = self.Element
676 root = Element("root")
677
678 root["text"] = "TEST"
679 self.assertEquals(["TEST"],
680 [ c.text for c in root["text"] ])
681
682 root["tail"] = "TEST"
683 self.assertEquals(["TEST"],
684 [ c.text for c in root["tail"] ])
685
686 root["pyval"] = "TEST"
687 self.assertEquals(["TEST"],
688 [ c.text for c in root["pyval"] ])
689
690 root["tag"] = "TEST"
691 self.assertEquals(["TEST"],
692 [ c.text for c in root["tag"] ])
693
701
703 XML = self.XML
704 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
705 self.assertEquals(2, len(root.findall(".//{X}b")))
706 self.assertEquals(3, len(root.findall(".//b")))
707 self.assertEquals(2, len(root.findall("b")))
708
716
730
736
738 Element = self.Element
739 SubElement = self.etree.SubElement
740 root = Element("{objectified}root")
741 root.bool = True
742 self.assertEquals(root.bool, True)
743 self.assertEquals(root.bool + root.bool, True + True)
744 self.assertEquals(True + root.bool, True + root.bool)
745 self.assertEquals(root.bool * root.bool, True * True)
746 self.assertEquals(int(root.bool), int(True))
747 self.assertEquals(complex(root.bool), complex(True))
748 self.assert_(isinstance(root.bool, objectify.BoolElement))
749
750 root.bool = False
751 self.assertEquals(root.bool, False)
752 self.assertEquals(root.bool + root.bool, False + False)
753 self.assertEquals(False + root.bool, False + root.bool)
754 self.assertEquals(root.bool * root.bool, False * False)
755 self.assertEquals(int(root.bool), int(False))
756 self.assertEquals(complex(root.bool), complex(False))
757 self.assert_(isinstance(root.bool, objectify.BoolElement))
758
767
774
781
788
790 Element = self.Element
791 SubElement = self.etree.SubElement
792 root = Element("{objectified}root")
793 root.s = "test"
794
795 self.assertEquals("test" * 5, root.s * 5)
796 self.assertEquals(5 * "test", 5 * root.s)
797
798 self.assertRaises(TypeError, operator.mul, root.s, "honk")
799 self.assertRaises(TypeError, operator.mul, "honk", root.s)
800
810
831
836
841
846
855
860
865
870
877
884
891
903
913
918
923
928
935
940
947
952
961
970
976
985
987 pyval = 1
988 pytype = "NoneType"
989 objclass = objectify.NoneElement
990 value = objectify.DataElement(pyval, _pytype=pytype)
991 self.assert_(isinstance(value, objclass),
992 "DataElement(%s, _pytype='%s') returns %s, expected %s"
993 % (pyval, pytype, type(value), objclass))
994 self.assertEquals(value.text, None)
995 self.assertEquals(value.pyval, None)
996
998
999 pyval = 1
1000 pytype = "none"
1001 objclass = objectify.NoneElement
1002 value = objectify.DataElement(pyval, _pytype=pytype)
1003 self.assert_(isinstance(value, objclass),
1004 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1005 % (pyval, pytype, type(value), objclass))
1006 self.assertEquals(value.text, None)
1007 self.assertEquals(value.pyval, None)
1008
1014 root = Element("{objectified}root")
1015 root.myfloat = MyFloat(5.5)
1016 self.assert_(isinstance(root.myfloat, objectify.FloatElement))
1017 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
1018
1020 class MyFloat(float):
1021 pass
1022 value = objectify.DataElement(MyFloat(5.5))
1023 self.assert_(isinstance(value, objectify.FloatElement))
1024 self.assertEquals(value, 5.5)
1025 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None)
1026
1028 XML = self.XML
1029 root = XML('''\
1030 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1031 <b xsi:type="boolean">true</b>
1032 <b xsi:type="boolean">false</b>
1033 <b xsi:type="boolean">1</b>
1034 <b xsi:type="boolean">0</b>
1035
1036 <f xsi:type="float">5</f>
1037 <f xsi:type="double">5</f>
1038
1039 <s xsi:type="string">5</s>
1040 <s xsi:type="normalizedString">5</s>
1041 <s xsi:type="token">5</s>
1042 <s xsi:type="language">5</s>
1043 <s xsi:type="Name">5</s>
1044 <s xsi:type="NCName">5</s>
1045 <s xsi:type="ID">5</s>
1046 <s xsi:type="IDREF">5</s>
1047 <s xsi:type="ENTITY">5</s>
1048 <s xsi:type="NMTOKEN">5</s>
1049
1050 <l xsi:type="integer">5</l>
1051 <l xsi:type="nonPositiveInteger">5</l>
1052 <l xsi:type="negativeInteger">5</l>
1053 <l xsi:type="long">5</l>
1054 <l xsi:type="nonNegativeInteger">5</l>
1055 <l xsi:type="unsignedLong">5</l>
1056 <l xsi:type="unsignedInt">5</l>
1057 <l xsi:type="positiveInteger">5</l>
1058
1059 <i xsi:type="int">5</i>
1060 <i xsi:type="short">5</i>
1061 <i xsi:type="byte">5</i>
1062 <i xsi:type="unsignedShort">5</i>
1063 <i xsi:type="unsignedByte">5</i>
1064
1065 <n xsi:nil="true"/>
1066 </root>
1067 ''')
1068
1069 for b in root.b:
1070 self.assert_(isinstance(b, objectify.BoolElement))
1071 self.assertEquals(True, root.b[0])
1072 self.assertEquals(False, root.b[1])
1073 self.assertEquals(True, root.b[2])
1074 self.assertEquals(False, root.b[3])
1075
1076 for f in root.f:
1077 self.assert_(isinstance(f, objectify.FloatElement))
1078 self.assertEquals(5, f)
1079
1080 for s in root.s:
1081 self.assert_(isinstance(s, objectify.StringElement))
1082 self.assertEquals("5", s)
1083
1084 for i in root.i:
1085 self.assert_(isinstance(i, objectify.IntElement))
1086 self.assertEquals(5, i)
1087
1088 for l in root.l:
1089 self.assert_(isinstance(l, objectify.IntElement))
1090 self.assertEquals(5, i)
1091
1092 self.assert_(isinstance(root.n, objectify.NoneElement))
1093 self.assertEquals(None, root.n)
1094
1096 XML = self.XML
1097 root = XML('''\
1098 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1099 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1100 <b xsi:type="xsd:boolean">true</b>
1101 <b xsi:type="xsd:boolean">false</b>
1102 <b xsi:type="xsd:boolean">1</b>
1103 <b xsi:type="xsd:boolean">0</b>
1104
1105 <f xsi:type="xsd:float">5</f>
1106 <f xsi:type="xsd:double">5</f>
1107
1108 <s xsi:type="xsd:string">5</s>
1109 <s xsi:type="xsd:normalizedString">5</s>
1110 <s xsi:type="xsd:token">5</s>
1111 <s xsi:type="xsd:language">5</s>
1112 <s xsi:type="xsd:Name">5</s>
1113 <s xsi:type="xsd:NCName">5</s>
1114 <s xsi:type="xsd:ID">5</s>
1115 <s xsi:type="xsd:IDREF">5</s>
1116 <s xsi:type="xsd:ENTITY">5</s>
1117 <s xsi:type="xsd:NMTOKEN">5</s>
1118
1119 <l xsi:type="xsd:integer">5</l>
1120 <l xsi:type="xsd:nonPositiveInteger">5</l>
1121 <l xsi:type="xsd:negativeInteger">5</l>
1122 <l xsi:type="xsd:long">5</l>
1123 <l xsi:type="xsd:nonNegativeInteger">5</l>
1124 <l xsi:type="xsd:unsignedLong">5</l>
1125 <l xsi:type="xsd:unsignedInt">5</l>
1126 <l xsi:type="xsd:positiveInteger">5</l>
1127
1128 <i xsi:type="xsd:int">5</i>
1129 <i xsi:type="xsd:short">5</i>
1130 <i xsi:type="xsd:byte">5</i>
1131 <i xsi:type="xsd:unsignedShort">5</i>
1132 <i xsi:type="xsd:unsignedByte">5</i>
1133
1134 <n xsi:nil="true"/>
1135 </root>
1136 ''')
1137
1138 for b in root.b:
1139 self.assert_(isinstance(b, objectify.BoolElement))
1140 self.assertEquals(True, root.b[0])
1141 self.assertEquals(False, root.b[1])
1142 self.assertEquals(True, root.b[2])
1143 self.assertEquals(False, root.b[3])
1144
1145 for f in root.f:
1146 self.assert_(isinstance(f, objectify.FloatElement))
1147 self.assertEquals(5, f)
1148
1149 for s in root.s:
1150 self.assert_(isinstance(s, objectify.StringElement))
1151 self.assertEquals("5", s)
1152
1153 for i in root.i:
1154 self.assert_(isinstance(i, objectify.IntElement))
1155 self.assertEquals(5, i)
1156
1157 for l in root.l:
1158 self.assert_(isinstance(l, objectify.IntElement))
1159 self.assertEquals(5, l)
1160
1161 self.assert_(isinstance(root.n, objectify.NoneElement))
1162 self.assertEquals(None, root.n)
1163
1165 XML = self.XML
1166 root = XML(_bytes('<root><b>why</b><b>try</b></root>'))
1167 strs = [ str(s) for s in root.b ]
1168 self.assertEquals(["why", "try"],
1169 strs)
1170
1197
1217
1218
1219
1243
1245 XML = self.XML
1246 root = XML(_bytes("""
1247 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1248 <b xsi:nil="true"></b><b xsi:nil="true"/>
1249 </root>"""))
1250 self.assert_(root.b[0] == root.b[1])
1251 self.assertFalse(root.b[0])
1252 self.assertEquals(root.b[0], None)
1253 self.assertEquals(None, root.b[0])
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1269
1276
1280
1282 XML = self.XML
1283 root = XML(_bytes('''\
1284 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1285 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1286 <b>5</b>
1287 <b>test</b>
1288 <c>1.1</c>
1289 <c>\uF8D2</c>
1290 <x>true</x>
1291 <n xsi:nil="true" />
1292 <n></n>
1293 <b xsi:type="double">5</b>
1294 <b xsi:type="float">5</b>
1295 <s xsi:type="string">23</s>
1296 <s py:pytype="str">42</s>
1297 <f py:pytype="float">300</f>
1298 <l py:pytype="long">2</l>
1299 <t py:pytype="TREE"></t>
1300 </a>
1301 '''))
1302 objectify.annotate(root)
1303
1304 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1305 for c in root.iterchildren() ]
1306 self.assertEquals("int", child_types[ 0])
1307 self.assertEquals("str", child_types[ 1])
1308 self.assertEquals("float", child_types[ 2])
1309 self.assertEquals("str", child_types[ 3])
1310 self.assertEquals("bool", child_types[ 4])
1311 self.assertEquals("NoneType", child_types[ 5])
1312 self.assertEquals(None, child_types[ 6])
1313 self.assertEquals("float", child_types[ 7])
1314 self.assertEquals("float", child_types[ 8])
1315 self.assertEquals("str", child_types[ 9])
1316 self.assertEquals("int", child_types[10])
1317 self.assertEquals("int", child_types[11])
1318 self.assertEquals("int", child_types[12])
1319 self.assertEquals(None, child_types[13])
1320
1321 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1322
1342
1344 XML = self.XML
1345 root = XML(_bytes('''\
1346 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1347 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1348 <b>5</b>
1349 <b>test</b>
1350 <c>1.1</c>
1351 <c>\uF8D2</c>
1352 <x>true</x>
1353 <n xsi:nil="true" />
1354 <n></n>
1355 <b xsi:type="double">5</b>
1356 <b xsi:type="float">5</b>
1357 <s xsi:type="string">23</s>
1358 <s py:pytype="str">42</s>
1359 <f py:pytype="float">300</f>
1360 <l py:pytype="long">2</l>
1361 <t py:pytype="TREE"></t>
1362 </a>
1363 '''))
1364 objectify.annotate(root, ignore_old=False)
1365
1366 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1367 for c in root.iterchildren() ]
1368 self.assertEquals("int", child_types[ 0])
1369 self.assertEquals("str", child_types[ 1])
1370 self.assertEquals("float", child_types[ 2])
1371 self.assertEquals("str", child_types[ 3])
1372 self.assertEquals("bool", child_types[ 4])
1373 self.assertEquals("NoneType", child_types[ 5])
1374 self.assertEquals(None, child_types[ 6])
1375 self.assertEquals("float", child_types[ 7])
1376 self.assertEquals("float", child_types[ 8])
1377 self.assertEquals("str", child_types[ 9])
1378 self.assertEquals("str", child_types[10])
1379 self.assertEquals("float", child_types[11])
1380 self.assertEquals("int", child_types[12])
1381 self.assertEquals(TREE_PYTYPE, child_types[13])
1382
1383 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
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, ignore_xsi=False,
1407 annotate_xsi=1, annotate_pytype=1)
1408
1409
1410 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1411 for c in root.iterchildren() ]
1412 self.assertEquals("int", child_types[ 0])
1413 self.assertEquals("str", child_types[ 1])
1414 self.assertEquals("float", child_types[ 2])
1415 self.assertEquals("str", child_types[ 3])
1416 self.assertEquals("bool", child_types[ 4])
1417 self.assertEquals("NoneType", child_types[ 5])
1418 self.assertEquals(None, child_types[ 6])
1419 self.assertEquals("float", child_types[ 7])
1420 self.assertEquals("float", child_types[ 8])
1421 self.assertEquals("str", child_types[ 9])
1422 self.assertEquals("str", child_types[10])
1423 self.assertEquals("float", child_types[11])
1424 self.assertEquals("int", child_types[12])
1425 self.assertEquals(TREE_PYTYPE, child_types[13])
1426
1427 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1428
1429 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1430 for c in root.iterchildren() ]
1431
1432
1433 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1434 for c in root.iterchildren() ]
1435 self.assertEquals("xsd:integer", child_types[ 0])
1436 self.assertEquals("xsd:string", child_types[ 1])
1437 self.assertEquals("xsd:double", child_types[ 2])
1438 self.assertEquals("xsd:string", child_types[ 3])
1439 self.assertEquals("xsd:boolean", child_types[ 4])
1440 self.assertEquals(None, child_types[ 5])
1441 self.assertEquals(None, child_types[ 6])
1442 self.assertEquals("xsd:double", child_types[ 7])
1443 self.assertEquals("xsd:float", child_types[ 8])
1444 self.assertEquals("xsd:string", child_types[ 9])
1445 self.assertEquals("xsd:string", child_types[10])
1446 self.assertEquals("xsd:double", child_types[11])
1447 self.assertEquals("xsd:integer", child_types[12])
1448 self.assertEquals(None, child_types[13])
1449
1450 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1451
1453 XML = self.XML
1454 root = XML(_bytes('''\
1455 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1456 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1457 <b>5</b>
1458 <b>test</b>
1459 <c>1.1</c>
1460 <c>\uF8D2</c>
1461 <x>true</x>
1462 <n xsi:nil="true" />
1463 <n></n>
1464 <b xsi:type="double">5</b>
1465 <b xsi:type="float">5</b>
1466 <s xsi:type="string">23</s>
1467 <s py:pytype="str">42</s>
1468 <f py:pytype="float">300</f>
1469 <l py:pytype="long">2</l>
1470 <t py:pytype="TREE"></t>
1471 </a>
1472 '''))
1473 objectify.xsiannotate(root, ignore_old=False)
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
1493 XML = self.XML
1494 root = XML(_bytes('''\
1495 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1496 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1497 <b>5</b>
1498 <b>test</b>
1499 <c>1.1</c>
1500 <c>\uF8D2</c>
1501 <x>true</x>
1502 <n xsi:nil="true" />
1503 <n></n>
1504 <b xsi:type="double">5</b>
1505 <b xsi:type="float">5</b>
1506 <s xsi:type="string">23</s>
1507 <s py:pytype="str">42</s>
1508 <f py:pytype="float">300</f>
1509 <l py:pytype="long">2</l>
1510 <t py:pytype="TREE"></t>
1511 </a>
1512 '''))
1513 objectify.pyannotate(root, ignore_old=True)
1514
1515 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1516 for c in root.iterchildren() ]
1517 self.assertEquals("int", child_types[ 0])
1518 self.assertEquals("str", child_types[ 1])
1519 self.assertEquals("float", child_types[ 2])
1520 self.assertEquals("str", child_types[ 3])
1521 self.assertEquals("bool", child_types[ 4])
1522 self.assertEquals("NoneType", child_types[ 5])
1523 self.assertEquals(None, child_types[ 6])
1524 self.assertEquals("float", child_types[ 7])
1525 self.assertEquals("float", child_types[ 8])
1526 self.assertEquals("str", child_types[ 9])
1527 self.assertEquals("int", child_types[10])
1528 self.assertEquals("int", child_types[11])
1529 self.assertEquals("int", child_types[12])
1530 self.assertEquals(None, child_types[13])
1531
1532 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1533
1553
1555 XML = self.XML
1556 root = XML('''\
1557 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1558 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1559 <b>5</b>
1560 <b>test</b>
1561 <c>1.1</c>
1562 <c>\uF8D2</c>
1563 <x>true</x>
1564 <n xsi:nil="true" />
1565 <n></n>
1566 <b xsi:type="double">5</b>
1567 <b xsi:type="float">5</b>
1568 <s xsi:type="string">23</s>
1569 <s py:pytype="str">42</s>
1570 <f py:pytype="float">300</f>
1571 <l py:pytype="long">2</l>
1572 <t py:pytype="TREE"></t>
1573 </a>
1574 ''')
1575 objectify.pyannotate(root)
1576
1577 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1578 for c in root.iterchildren() ]
1579 self.assertEquals("int", child_types[ 0])
1580 self.assertEquals("str", child_types[ 1])
1581 self.assertEquals("float", child_types[ 2])
1582 self.assertEquals("str", child_types[ 3])
1583 self.assertEquals("bool", child_types[ 4])
1584 self.assertEquals("NoneType", child_types[ 5])
1585 self.assertEquals(None, child_types[ 6])
1586 self.assertEquals("float", child_types[ 7])
1587 self.assertEquals("float", child_types[ 8])
1588 self.assertEquals("str", child_types[ 9])
1589 self.assertEquals("str", child_types[10])
1590 self.assertEquals("float", child_types[11])
1591 self.assertEquals("int", child_types[12])
1592 self.assertEquals(TREE_PYTYPE, child_types[13])
1593
1594 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1595
1597 XML = self.XML
1598 root = XML(_bytes('''\
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.xsiannotate(root, ignore_old=True)
1618
1619 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1620 for c in root.iterchildren() ]
1621 self.assertEquals("xsd:integer", child_types[ 0])
1622 self.assertEquals("xsd:string", child_types[ 1])
1623 self.assertEquals("xsd:double", child_types[ 2])
1624 self.assertEquals("xsd:string", child_types[ 3])
1625 self.assertEquals("xsd:boolean", child_types[ 4])
1626 self.assertEquals(None, child_types[ 5])
1627 self.assertEquals(None, child_types[ 6])
1628 self.assertEquals("xsd:integer", child_types[ 7])
1629 self.assertEquals("xsd:integer", child_types[ 8])
1630 self.assertEquals("xsd:integer", child_types[ 9])
1631 self.assertEquals("xsd:string", child_types[10])
1632 self.assertEquals("xsd:double", child_types[11])
1633 self.assertEquals("xsd:integer", child_types[12])
1634 self.assertEquals(None, 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.deannotate(root)
1660
1661 for c in root.getiterator():
1662 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1663 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1664
1665 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1666
1668 XML = self.XML
1669 root = XML(_bytes('''\
1670 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1671 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1672 <b>5</b>
1673 <b>test</b>
1674 <c>1.1</c>
1675 <c>\uF8D2</c>
1676 <x>true</x>
1677 <n xsi:nil="true" />
1678 <n></n>
1679 <b xsi:type="double">5</b>
1680 <b xsi:type="float">5</b>
1681 <s xsi:type="string">23</s>
1682 <s py:pytype="str">42</s>
1683 <f py:pytype="float">300</f>
1684 <l py:pytype="long">2</l>
1685 <t py:pytype="TREE"></t>
1686 </a>
1687 '''))
1688 objectify.xsiannotate(root)
1689 objectify.deannotate(root, xsi=False)
1690
1691 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1692 for c in root.iterchildren() ]
1693 self.assertEquals("xsd:int", child_types[ 0])
1694 self.assertEquals("xsd:string", child_types[ 1])
1695 self.assertEquals("xsd:double", child_types[ 2])
1696 self.assertEquals("xsd:string", child_types[ 3])
1697 self.assertEquals("xsd:boolean", child_types[ 4])
1698 self.assertEquals(None, child_types[ 5])
1699 self.assertEquals(None, child_types[ 6])
1700 self.assertEquals("xsd:int", child_types[ 7])
1701 self.assertEquals("xsd:int", child_types[ 8])
1702 self.assertEquals("xsd:int", child_types[ 9])
1703 self.assertEquals("xsd:string", child_types[10])
1704 self.assertEquals("xsd:double", child_types[11])
1705 self.assertEquals("xsd:integer", child_types[12])
1706 self.assertEquals(None, child_types[13])
1707
1708 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1709
1710 for c in root.getiterator():
1711 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1712
1714 XML = self.XML
1715 root = XML(_bytes('''\
1716 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1717 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1718 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
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="xsd:double">5</b>
1727 <b xsi:type="xsd:float">5</b>
1728 <s xsi:type="xsd: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(root)
1736 objectify.deannotate(root, pytype=False)
1737
1738 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1739 for c in root.iterchildren() ]
1740 self.assertEquals("int", child_types[ 0])
1741 self.assertEquals("str", child_types[ 1])
1742 self.assertEquals("float", child_types[ 2])
1743 self.assertEquals("str", child_types[ 3])
1744 self.assertEquals("bool", child_types[ 4])
1745 self.assertEquals("NoneType", child_types[ 5])
1746 self.assertEquals(None, child_types[ 6])
1747 self.assertEquals("float", child_types[ 7])
1748 self.assertEquals("float", child_types[ 8])
1749 self.assertEquals("str", child_types[ 9])
1750 self.assertEquals("int", child_types[10])
1751 self.assertEquals("int", child_types[11])
1752 self.assertEquals("int", child_types[12])
1753 self.assertEquals(None, child_types[13])
1754
1755 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1756
1757 for c in root.getiterator():
1758 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1759
1761 XML = self.XML
1762 root = XML(_bytes('''\
1763 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1764 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1765 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1766 <b xsi:type="xsd:int">5</b>
1767 <b xsi:type="xsd:string">test</b>
1768 <c xsi:type="xsd:float">1.1</c>
1769 <c xsi:type="xsd:string">\uF8D2</c>
1770 <x xsi:type="xsd:boolean">true</x>
1771 <n xsi:nil="true" />
1772 <n></n>
1773 <b xsi:type="xsd:double">5</b>
1774 <b xsi:type="xsd:float">5</b>
1775 <s xsi:type="xsd:string">23</s>
1776 <s xsi:type="xsd:string">42</s>
1777 <f xsi:type="xsd:float">300</f>
1778 <l xsi:type="xsd:long">2</l>
1779 <t py:pytype="TREE"></t>
1780 </a>
1781 '''))
1782 objectify.annotate(root)
1783 objectify.deannotate(root, xsi=False)
1784
1785 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1786 for c in root.iterchildren() ]
1787 self.assertEquals("xsd:int", child_types[ 0])
1788 self.assertEquals("xsd:string", child_types[ 1])
1789 self.assertEquals("xsd:float", child_types[ 2])
1790 self.assertEquals("xsd:string", child_types[ 3])
1791 self.assertEquals("xsd:boolean", child_types[ 4])
1792 self.assertEquals(None, child_types[ 5])
1793 self.assertEquals(None, child_types[ 6])
1794 self.assertEquals("xsd:double", child_types[ 7])
1795 self.assertEquals("xsd:float", child_types[ 8])
1796 self.assertEquals("xsd:string", child_types[ 9])
1797 self.assertEquals("xsd:string", child_types[10])
1798 self.assertEquals("xsd:float", child_types[11])
1799 self.assertEquals("xsd:long", child_types[12])
1800 self.assertEquals(None, child_types[13])
1801
1802 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1803
1804 for c in root.getiterator():
1805 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1806
1808 XML = self.XML
1809
1810 xml = _bytes('''\
1811 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1812 <b>5</b>
1813 <b>test</b>
1814 <c>1.1</c>
1815 <c>\uF8D2</c>
1816 <x>true</x>
1817 <n xsi:nil="true" />
1818 <n></n>
1819 <b xsi:type="double">5</b>
1820 </a>
1821 ''')
1822
1823 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1824 objectify.set_pytype_attribute_tag("{TEST}test")
1825
1826 root = XML(xml)
1827 objectify.annotate(root)
1828
1829 attribs = root.xpath("//@py:%s" % pytype_name,
1830 namespaces={"py" : pytype_ns})
1831 self.assertEquals(0, len(attribs))
1832 attribs = root.xpath("//@py:test",
1833 namespaces={"py" : "TEST"})
1834 self.assertEquals(7, len(attribs))
1835
1836 objectify.set_pytype_attribute_tag()
1837 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1838
1839 self.assertNotEqual("test", pytype_ns.lower())
1840 self.assertNotEqual("test", pytype_name.lower())
1841
1842 root = XML(xml)
1843 attribs = root.xpath("//@py:%s" % pytype_name,
1844 namespaces={"py" : pytype_ns})
1845 self.assertEquals(0, len(attribs))
1846
1847 objectify.annotate(root)
1848 attribs = root.xpath("//@py:%s" % pytype_name,
1849 namespaces={"py" : pytype_ns})
1850 self.assertEquals(7, len(attribs))
1851
1859
1860 def checkMyType(s):
1861 return True
1862
1863 pytype = objectify.PyType("mytype", checkMyType, NewType)
1864 self.assert_(pytype not in objectify.getRegisteredTypes())
1865 pytype.register()
1866 self.assert_(pytype in objectify.getRegisteredTypes())
1867 pytype.unregister()
1868 self.assert_(pytype not in objectify.getRegisteredTypes())
1869
1870 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1871 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1872 pytype.unregister()
1873
1874 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1875 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1876 pytype.unregister()
1877
1878 self.assertRaises(ValueError, pytype.register,
1879 before = [objectify.getRegisteredTypes()[0].name],
1880 after = [objectify.getRegisteredTypes()[1].name])
1881
1883 from datetime import datetime
1884 def parse_date(value):
1885 if len(value) != 14:
1886 raise ValueError(value)
1887 Y = int(value[0:4])
1888 M = int(value[4:6])
1889 D = int(value[6:8])
1890 h = int(value[8:10])
1891 m = int(value[10:12])
1892 s = int(value[12:14])
1893 return datetime(Y, M, D, h, m, s)
1894
1895 def stringify_date(date):
1896 return date.strftime("%Y%m%d%H%M%S")
1897
1898 class DatetimeElement(objectify.ObjectifiedDataElement):
1899 def pyval(self):
1900 return parse_date(self.text)
1901 pyval = property(pyval)
1902
1903 datetime_type = objectify.PyType(
1904 "datetime", parse_date, DatetimeElement, stringify_date)
1905 datetime_type.xmlSchemaTypes = "dateTime"
1906 datetime_type.register()
1907
1908 NAMESPACE = "http://foo.net/xmlns"
1909 NAMESPACE_MAP = {'ns': NAMESPACE}
1910
1911 r = objectify.Element("{%s}root" % NAMESPACE, nsmap=NAMESPACE_MAP)
1912 time = datetime.now()
1913 r.date = time
1914
1915 self.assert_(isinstance(r.date, DatetimeElement))
1916 self.assert_(isinstance(r.date.pyval, datetime))
1917
1918 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1919 self.assertEquals(r.date.text, stringify_date(time))
1920
1921 r.date = objectify.E.date(time)
1922
1923 self.assert_(isinstance(r.date, DatetimeElement))
1924 self.assert_(isinstance(r.date.pyval, datetime))
1925
1926 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1927 self.assertEquals(r.date.text, stringify_date(time))
1928
1934
1940
1946
1954
1973
1978
1983
1988
1993
2013
2015 root = self.XML(xml_str)
2016 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
2017 self.assertEquals(root.c1.c2.text, path(root).text)
2018
2019 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
2020 self.assertEquals(root.c1.c2[2].text, path(root).text)
2021
2022 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
2023 self.assertEquals(root.c1.c2[2].text, path(root).text)
2024
2025 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
2026 self.assertEquals(root.c1.c2[-1].text, path(root).text)
2027
2028 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
2029 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2030
2032 self.assertRaises(ValueError, objectify.ObjectPath,
2033 "root.c1[0].c2[-1-2]")
2034 self.assertRaises(ValueError, objectify.ObjectPath,
2035 ['root', 'c1[0]', 'c2[-1-2]'])
2036
2037 self.assertRaises(ValueError, objectify.ObjectPath,
2038 "root[2].c1.c2")
2039 self.assertRaises(ValueError, objectify.ObjectPath,
2040 ['root[2]', 'c1', 'c2'])
2041
2042 self.assertRaises(ValueError, objectify.ObjectPath,
2043 [])
2044 self.assertRaises(ValueError, objectify.ObjectPath,
2045 ['', '', ''])
2046
2048 root = self.XML(xml_str)
2049 path = objectify.ObjectPath("root.c1[9999].c2")
2050 self.assertRaises(AttributeError, path, root)
2051
2052 path = objectify.ObjectPath("root.c1[0].c2[9999]")
2053 self.assertRaises(AttributeError, path, root)
2054
2055 path = objectify.ObjectPath(".c1[9999].c2[0]")
2056 self.assertRaises(AttributeError, path, root)
2057
2058 path = objectify.ObjectPath("root.c1[-2].c2")
2059 self.assertRaises(AttributeError, path, root)
2060
2061 path = objectify.ObjectPath("root.c1[0].c2[-4]")
2062 self.assertRaises(AttributeError, path, root)
2063
2077
2079 root = self.XML(xml_str)
2080 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
2081 self.assertEquals(root.c1.c2.text, path.find(root).text)
2082 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
2083 self.assertEquals(root.c1.c2.text, path.find(root).text)
2084 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
2085 self.assertEquals(root.c1.c2.text, path.find(root).text)
2086 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
2087 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2088 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
2089 self.assertEquals(root.c1.c2.text, path.find(root).text)
2090 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
2091 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2092 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
2093 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2094 path.find(root).text)
2095
2108
2123
2135
2149
2151 root = self.XML(xml_str)
2152 path = objectify.ObjectPath( "root.c1.c99" )
2153 self.assertRaises(AttributeError, path.find, root)
2154
2155 new_el = self.Element("{objectified}test")
2156 new_el.a = ["TEST1", "TEST2"]
2157 new_el.a[0].set("myattr", "ATTR1")
2158 new_el.a[1].set("myattr", "ATTR2")
2159
2160 path.setattr(root, list(new_el.a))
2161
2162 self.assertEquals(2, len(root.c1.c99))
2163 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
2164 self.assertEquals("TEST1", root.c1.c99[0].text)
2165 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
2166 self.assertEquals("TEST2", root.c1.c99[1].text)
2167 self.assertEquals("TEST1", path(root).text)
2168
2177
2191
2203
2217
2232
2234 root = self.XML(xml_str)
2235 self.assertEquals(
2236 ['{objectified}root', '{objectified}root.c1',
2237 '{objectified}root.c1.c2',
2238 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]',
2239 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'],
2240 root.descendantpaths())
2241
2243 root = self.XML(xml_str)
2244 self.assertEquals(
2245 ['{objectified}c1', '{objectified}c1.c2',
2246 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]',
2247 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'],
2248 root.c1.descendantpaths())
2249
2251 root = self.XML(xml_str)
2252 self.assertEquals(
2253 ['root.{objectified}c1', 'root.{objectified}c1.c2',
2254 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]',
2255 'root.{objectified}c1.{otherNS}c2',
2256 'root.{objectified}c1.{}c2'],
2257 root.c1.descendantpaths('root'))
2258
2270
2283
2284
2285
2290
2295
2300
2305
2310
2315
2320
2325
2327 E = objectify.E
2328 DataElement = objectify.DataElement
2329 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1),
2330 DataElement(2.0))
2331 self.assert_(isinstance(root, objectify.ObjectifiedElement))
2332 self.assertEquals(root.text, "text")
2333 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement))
2334 self.assertEquals(root.sub.tail, "tail")
2335 self.assert_(isinstance(root.sub.subsub, objectify.StringElement))
2336 self.assertEquals(len(root.value), 2)
2337 self.assert_(isinstance(root.value[0], objectify.IntElement))
2338 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2339
2344
2351
2356
2362
2364 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2365 self.assertEquals(root.base, "http://no/such/url")
2366 self.assertEquals(
2367 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2368 root.base = "https://secret/url"
2369 self.assertEquals(root.base, "https://secret/url")
2370 self.assertEquals(
2371 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2372 "https://secret/url")
2373
2375 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2376 self.assertEquals(root.base, "http://no/such/url")
2377 self.assertEquals(
2378 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2379 root.set('{http://www.w3.org/XML/1998/namespace}base',
2380 "https://secret/url")
2381 self.assertEquals(root.base, "https://secret/url")
2382 self.assertEquals(
2383 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2384 "https://secret/url")
2385
2386
2388 suite = unittest.TestSuite()
2389 suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
2390 if sys.version_info >= (2,4):
2391 suite.addTests(
2392 [make_doctest('../../../doc/objectify.txt')])
2393 return suite
2394
2395 if __name__ == '__main__':
2396 print('to test use test.py %s' % __file__)
2397