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.annotate(
1689 root, ignore_old=False, ignore_xsi=False, annotate_xsi=True,
1690 empty_pytype='str', empty_type='string')
1691 objectify.deannotate(root, pytype=False, xsi=False, xsi_nil=True)
1692
1693 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1694 for c in root.iterchildren() ]
1695 self.assertEquals("xsd:integer", child_types[ 0])
1696 self.assertEquals("xsd:string", child_types[ 1])
1697 self.assertEquals("xsd:double", child_types[ 2])
1698 self.assertEquals("xsd:string", child_types[ 3])
1699 self.assertEquals("xsd:boolean", child_types[ 4])
1700 self.assertEquals(None, child_types[ 5])
1701 self.assertEquals("xsd:string", child_types[ 6])
1702 self.assertEquals("xsd:double", child_types[ 7])
1703 self.assertEquals("xsd:float", child_types[ 8])
1704 self.assertEquals("xsd:string", child_types[ 9])
1705 self.assertEquals("xsd:string", child_types[10])
1706 self.assertEquals("xsd:double", child_types[11])
1707 self.assertEquals("xsd:integer", child_types[12])
1708 self.assertEquals(None, child_types[13])
1709
1710 self.assertEquals(None, root.n.get(XML_SCHEMA_NIL_ATTR))
1711
1712 for c in root.iterchildren():
1713 self.assertNotEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1714
1715 if (c.get(objectify.PYTYPE_ATTRIBUTE) not in [TREE_PYTYPE,
1716 "NoneType"]):
1717 self.assertNotEquals(
1718 None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1719
1721 XML = self.XML
1722 root = XML(_bytes('''\
1723 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1724 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1725 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1726 <b>5</b>
1727 <b>test</b>
1728 <c>1.1</c>
1729 <c>\uF8D2</c>
1730 <x>true</x>
1731 <n xsi:nil="true" />
1732 <n></n>
1733 <b xsi:type="xsd:double">5</b>
1734 <b xsi:type="xsd:float">5</b>
1735 <s xsi:type="xsd:string">23</s>
1736 <s py:pytype="str">42</s>
1737 <f py:pytype="float">300</f>
1738 <l py:pytype="long">2</l>
1739 <t py:pytype="TREE"></t>
1740 </a>
1741 '''))
1742 objectify.annotate(root)
1743 objectify.deannotate(root, pytype=False)
1744
1745 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1746 for c in root.iterchildren() ]
1747 self.assertEquals("int", child_types[ 0])
1748 self.assertEquals("str", child_types[ 1])
1749 self.assertEquals("float", child_types[ 2])
1750 self.assertEquals("str", child_types[ 3])
1751 self.assertEquals("bool", child_types[ 4])
1752 self.assertEquals("NoneType", child_types[ 5])
1753 self.assertEquals(None, child_types[ 6])
1754 self.assertEquals("float", child_types[ 7])
1755 self.assertEquals("float", child_types[ 8])
1756 self.assertEquals("str", child_types[ 9])
1757 self.assertEquals("int", child_types[10])
1758 self.assertEquals("int", child_types[11])
1759 self.assertEquals("int", child_types[12])
1760 self.assertEquals(None, child_types[13])
1761
1762 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1763
1764 for c in root.getiterator():
1765 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1766
1768 XML = self.XML
1769 root = XML(_bytes('''\
1770 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1771 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1772 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1773 <b xsi:type="xsd:int">5</b>
1774 <b xsi:type="xsd:string">test</b>
1775 <c xsi:type="xsd:float">1.1</c>
1776 <c xsi:type="xsd:string">\uF8D2</c>
1777 <x xsi:type="xsd:boolean">true</x>
1778 <n xsi:nil="true" />
1779 <n></n>
1780 <b xsi:type="xsd:double">5</b>
1781 <b xsi:type="xsd:float">5</b>
1782 <s xsi:type="xsd:string">23</s>
1783 <s xsi:type="xsd:string">42</s>
1784 <f xsi:type="xsd:float">300</f>
1785 <l xsi:type="xsd:long">2</l>
1786 <t py:pytype="TREE"></t>
1787 </a>
1788 '''))
1789 objectify.annotate(root)
1790 objectify.deannotate(root, xsi=False)
1791
1792 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1793 for c in root.iterchildren() ]
1794 self.assertEquals("xsd:int", child_types[ 0])
1795 self.assertEquals("xsd:string", child_types[ 1])
1796 self.assertEquals("xsd:float", child_types[ 2])
1797 self.assertEquals("xsd:string", child_types[ 3])
1798 self.assertEquals("xsd:boolean", child_types[ 4])
1799 self.assertEquals(None, child_types[ 5])
1800 self.assertEquals(None, child_types[ 6])
1801 self.assertEquals("xsd:double", child_types[ 7])
1802 self.assertEquals("xsd:float", child_types[ 8])
1803 self.assertEquals("xsd:string", child_types[ 9])
1804 self.assertEquals("xsd:string", child_types[10])
1805 self.assertEquals("xsd:float", child_types[11])
1806 self.assertEquals("xsd:long", child_types[12])
1807 self.assertEquals(None, child_types[13])
1808
1809 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1810
1811 for c in root.getiterator():
1812 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1813
1815 XML = self.XML
1816
1817 xml = _bytes('''\
1818 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1819 <b>5</b>
1820 <b>test</b>
1821 <c>1.1</c>
1822 <c>\uF8D2</c>
1823 <x>true</x>
1824 <n xsi:nil="true" />
1825 <n></n>
1826 <b xsi:type="double">5</b>
1827 </a>
1828 ''')
1829
1830 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1831 objectify.set_pytype_attribute_tag("{TEST}test")
1832
1833 root = XML(xml)
1834 objectify.annotate(root)
1835
1836 attribs = root.xpath("//@py:%s" % pytype_name,
1837 namespaces={"py" : pytype_ns})
1838 self.assertEquals(0, len(attribs))
1839 attribs = root.xpath("//@py:test",
1840 namespaces={"py" : "TEST"})
1841 self.assertEquals(7, len(attribs))
1842
1843 objectify.set_pytype_attribute_tag()
1844 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1845
1846 self.assertNotEqual("test", pytype_ns.lower())
1847 self.assertNotEqual("test", pytype_name.lower())
1848
1849 root = XML(xml)
1850 attribs = root.xpath("//@py:%s" % pytype_name,
1851 namespaces={"py" : pytype_ns})
1852 self.assertEquals(0, len(attribs))
1853
1854 objectify.annotate(root)
1855 attribs = root.xpath("//@py:%s" % pytype_name,
1856 namespaces={"py" : pytype_ns})
1857 self.assertEquals(7, len(attribs))
1858
1866
1867 def checkMyType(s):
1868 return True
1869
1870 pytype = objectify.PyType("mytype", checkMyType, NewType)
1871 self.assert_(pytype not in objectify.getRegisteredTypes())
1872 pytype.register()
1873 self.assert_(pytype in objectify.getRegisteredTypes())
1874 pytype.unregister()
1875 self.assert_(pytype not in objectify.getRegisteredTypes())
1876
1877 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1878 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1879 pytype.unregister()
1880
1881 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1882 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1883 pytype.unregister()
1884
1885 self.assertRaises(ValueError, pytype.register,
1886 before = [objectify.getRegisteredTypes()[0].name],
1887 after = [objectify.getRegisteredTypes()[1].name])
1888
1890 from datetime import datetime
1891 def parse_date(value):
1892 if len(value) != 14:
1893 raise ValueError(value)
1894 Y = int(value[0:4])
1895 M = int(value[4:6])
1896 D = int(value[6:8])
1897 h = int(value[8:10])
1898 m = int(value[10:12])
1899 s = int(value[12:14])
1900 return datetime(Y, M, D, h, m, s)
1901
1902 def stringify_date(date):
1903 return date.strftime("%Y%m%d%H%M%S")
1904
1905 class DatetimeElement(objectify.ObjectifiedDataElement):
1906 def pyval(self):
1907 return parse_date(self.text)
1908 pyval = property(pyval)
1909
1910 datetime_type = objectify.PyType(
1911 "datetime", parse_date, DatetimeElement, stringify_date)
1912 datetime_type.xmlSchemaTypes = "dateTime"
1913 datetime_type.register()
1914
1915 NAMESPACE = "http://foo.net/xmlns"
1916 NAMESPACE_MAP = {'ns': NAMESPACE}
1917
1918 r = objectify.Element("{%s}root" % NAMESPACE, nsmap=NAMESPACE_MAP)
1919 time = datetime.now()
1920 r.date = time
1921
1922 self.assert_(isinstance(r.date, DatetimeElement))
1923 self.assert_(isinstance(r.date.pyval, datetime))
1924
1925 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1926 self.assertEquals(r.date.text, stringify_date(time))
1927
1928 r.date = objectify.E.date(time)
1929
1930 self.assert_(isinstance(r.date, DatetimeElement))
1931 self.assert_(isinstance(r.date.pyval, datetime))
1932
1933 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1934 self.assertEquals(r.date.text, stringify_date(time))
1935
1941
1947
1952
1961
1968
1976
1979
1982
2001
2006
2011
2016
2021
2041
2043 root = self.XML(xml_str)
2044 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
2045 self.assertEquals(root.c1.c2.text, path(root).text)
2046
2047 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
2048 self.assertEquals(root.c1.c2[2].text, path(root).text)
2049
2050 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
2051 self.assertEquals(root.c1.c2[2].text, path(root).text)
2052
2053 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
2054 self.assertEquals(root.c1.c2[-1].text, path(root).text)
2055
2056 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
2057 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2058
2060 self.assertRaises(ValueError, objectify.ObjectPath,
2061 "root.c1[0].c2[-1-2]")
2062 self.assertRaises(ValueError, objectify.ObjectPath,
2063 ['root', 'c1[0]', 'c2[-1-2]'])
2064
2065 self.assertRaises(ValueError, objectify.ObjectPath,
2066 "root[2].c1.c2")
2067 self.assertRaises(ValueError, objectify.ObjectPath,
2068 ['root[2]', 'c1', 'c2'])
2069
2070 self.assertRaises(ValueError, objectify.ObjectPath,
2071 [])
2072 self.assertRaises(ValueError, objectify.ObjectPath,
2073 ['', '', ''])
2074
2076 root = self.XML(xml_str)
2077 path = objectify.ObjectPath("root.c1[9999].c2")
2078 self.assertRaises(AttributeError, path, root)
2079
2080 path = objectify.ObjectPath("root.c1[0].c2[9999]")
2081 self.assertRaises(AttributeError, path, root)
2082
2083 path = objectify.ObjectPath(".c1[9999].c2[0]")
2084 self.assertRaises(AttributeError, path, root)
2085
2086 path = objectify.ObjectPath("root.c1[-2].c2")
2087 self.assertRaises(AttributeError, path, root)
2088
2089 path = objectify.ObjectPath("root.c1[0].c2[-4]")
2090 self.assertRaises(AttributeError, path, root)
2091
2105
2107 root = self.XML(xml_str)
2108 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
2109 self.assertEquals(root.c1.c2.text, path.find(root).text)
2110 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
2111 self.assertEquals(root.c1.c2.text, path.find(root).text)
2112 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
2113 self.assertEquals(root.c1.c2.text, path.find(root).text)
2114 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
2115 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2116 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
2117 self.assertEquals(root.c1.c2.text, path.find(root).text)
2118 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
2119 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2120 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
2121 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2122 path.find(root).text)
2123
2136
2151
2163
2177
2179 root = self.XML(xml_str)
2180 path = objectify.ObjectPath( "root.c1.c99" )
2181 self.assertRaises(AttributeError, path.find, root)
2182
2183 new_el = self.Element("{objectified}test")
2184 new_el.a = ["TEST1", "TEST2"]
2185 new_el.a[0].set("myattr", "ATTR1")
2186 new_el.a[1].set("myattr", "ATTR2")
2187
2188 path.setattr(root, list(new_el.a))
2189
2190 self.assertEquals(2, len(root.c1.c99))
2191 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
2192 self.assertEquals("TEST1", root.c1.c99[0].text)
2193 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
2194 self.assertEquals("TEST2", root.c1.c99[1].text)
2195 self.assertEquals("TEST1", path(root).text)
2196
2205
2219
2231
2245
2260
2262 root = self.XML(xml_str)
2263 self.assertEquals(
2264 ['{objectified}root', '{objectified}root.c1',
2265 '{objectified}root.c1.c2',
2266 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]',
2267 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'],
2268 root.descendantpaths())
2269
2271 root = self.XML(xml_str)
2272 self.assertEquals(
2273 ['{objectified}c1', '{objectified}c1.c2',
2274 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]',
2275 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'],
2276 root.c1.descendantpaths())
2277
2279 root = self.XML(xml_str)
2280 self.assertEquals(
2281 ['root.{objectified}c1', 'root.{objectified}c1.c2',
2282 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]',
2283 'root.{objectified}c1.{otherNS}c2',
2284 'root.{objectified}c1.{}c2'],
2285 root.c1.descendantpaths('root'))
2286
2298
2311
2312
2313
2318
2323
2328
2333
2338
2343
2348
2353
2355 E = objectify.E
2356 DataElement = objectify.DataElement
2357 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1),
2358 DataElement(2.0))
2359 self.assert_(isinstance(root, objectify.ObjectifiedElement))
2360 self.assertEquals(root.text, "text")
2361 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement))
2362 self.assertEquals(root.sub.tail, "tail")
2363 self.assert_(isinstance(root.sub.subsub, objectify.StringElement))
2364 self.assertEquals(len(root.value), 2)
2365 self.assert_(isinstance(root.value[0], objectify.IntElement))
2366 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2367
2372
2379
2384
2390
2392 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2393 self.assertEquals(root.base, "http://no/such/url")
2394 self.assertEquals(
2395 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2396 root.base = "https://secret/url"
2397 self.assertEquals(root.base, "https://secret/url")
2398 self.assertEquals(
2399 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2400 "https://secret/url")
2401
2403 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2404 self.assertEquals(root.base, "http://no/such/url")
2405 self.assertEquals(
2406 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2407 root.set('{http://www.w3.org/XML/1998/namespace}base',
2408 "https://secret/url")
2409 self.assertEquals(root.base, "https://secret/url")
2410 self.assertEquals(
2411 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2412 "https://secret/url")
2413
2415 XML = self.XML
2416
2417 xml = _bytes('''\
2418 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2419 <i>5</i>
2420 <i>-5</i>
2421 <l>4294967296</l>
2422 <l>-4294967296</l>
2423 <f>1.1</f>
2424 <b>true</b>
2425 <b>false</b>
2426 <s>Strange things happen, where strings collide</s>
2427 <s>True</s>
2428 <s>False</s>
2429 <s>t</s>
2430 <s>f</s>
2431 <s></s>
2432 <s>None</s>
2433 <n xsi:nil="true" />
2434 </root>
2435 ''')
2436 root = XML(xml)
2437
2438 for i in root.i:
2439 self.assert_(isinstance(i, objectify.IntElement))
2440 for l in root.l:
2441 self.assert_(isinstance(l, objectify.IntElement))
2442 for f in root.f:
2443 self.assert_(isinstance(f, objectify.FloatElement))
2444 for b in root.b:
2445 self.assert_(isinstance(b, objectify.BoolElement))
2446 self.assertEquals(True, root.b[0])
2447 self.assertEquals(False, root.b[1])
2448 for s in root.s:
2449 self.assert_(isinstance(s, objectify.StringElement))
2450 self.assert_(isinstance(root.n, objectify.NoneElement))
2451 self.assertEquals(None, root.n)
2452
2454 suite = unittest.TestSuite()
2455 suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
2456 if sys.version_info >= (2,4):
2457 suite.addTests(
2458 [make_doctest('../../../doc/objectify.txt')])
2459 return suite
2460
2461 if __name__ == '__main__':
2462 print('to test use test.py %s' % __file__)
2463