1
2
3 """
4 Tests specific to the lxml.objectify API
5 """
6
7
8 import unittest, operator
9
10 from common_imports import etree, StringIO, HelperTestCase, fileInTestDir
11 from common_imports import SillyFileLike, canonicalize, doctest
12
13 from lxml import objectify
14
15 PYTYPE_NAMESPACE = "http://codespeak.net/lxml/objectify/pytype"
16 XML_SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"
17 XML_SCHEMA_INSTANCE_NS = "http://www.w3.org/2001/XMLSchema-instance"
18 XML_SCHEMA_INSTANCE_TYPE_ATTR = "{%s}type" % XML_SCHEMA_INSTANCE_NS
19 XML_SCHEMA_NIL_ATTR = "{%s}nil" % XML_SCHEMA_INSTANCE_NS
20 TREE_PYTYPE = "TREE"
21 DEFAULT_NSMAP = { "py" : PYTYPE_NAMESPACE,
22 "xsi" : XML_SCHEMA_INSTANCE_NS,
23 "xsd" : XML_SCHEMA_NS}
24
25 objectclass2xsitype = {
26
27 objectify.IntElement: ("int", "short", "byte", "unsignedShort",
28 "unsignedByte",),
29 objectify.LongElement: ("integer", "nonPositiveInteger", "negativeInteger",
30 "long", "nonNegativeInteger", "unsignedLong",
31 "unsignedInt", "positiveInteger",),
32 objectify.FloatElement: ("float", "double"),
33 objectify.BoolElement: ("boolean",),
34 objectify.StringElement: ("string", "normalizedString", "token", "language",
35 "Name", "NCName", "ID", "IDREF", "ENTITY",
36 "NMTOKEN", ),
37
38 }
39
40 xsitype2objclass = dict(( (v, k) for k in objectclass2xsitype
41 for v in objectclass2xsitype[k] ))
42
43 objectclass2pytype = {
44
45 objectify.IntElement: "int",
46 objectify.LongElement: "long",
47 objectify.FloatElement: "float",
48 objectify.BoolElement: "bool",
49 objectify.StringElement: "str",
50
51 }
52
53 pytype2objclass = dict(( (objectclass2pytype[k], k) for k in objectclass2pytype))
54
55 xml_str = '''\
56 <obj:root xmlns:obj="objectified" xmlns:other="otherNS">
57 <obj:c1 a1="A1" a2="A2" other:a3="A3">
58 <obj:c2>0</obj:c2>
59 <obj:c2>1</obj:c2>
60 <obj:c2>2</obj:c2>
61 <other:c2>3</other:c2>
62 <c2>3</c2>
63 </obj:c1>
64 </obj:root>'''
65
67 """Test cases for lxml.objectify
68 """
69 etree = etree
70
73
85
92
96
101
108
118
123
129
137
148
152
157
164
174
179
185
193
204
206
207 value = objectify.DataElement(23, _pytype="str", _xsi="foobar",
208 attrib={"gnu": "muh", "cat": "meeow",
209 "dog": "wuff"},
210 bird="tchilp", dog="grrr")
211 self.assertEquals(value.get("gnu"), "muh")
212 self.assertEquals(value.get("cat"), "meeow")
213 self.assertEquals(value.get("dog"), "grrr")
214 self.assertEquals(value.get("bird"), "tchilp")
215
227
229
230
231 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
232 attrib={"gnu": "muh", "cat": "meeow",
233 "dog": "wuff"},
234 bird="tchilp", dog="grrr")
235 value = objectify.DataElement(arg, _pytype="NoneType")
236 self.assert_(isinstance(value, objectify.NoneElement))
237 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
238 self.assertEquals(value.text, None)
239 self.assertEquals(value.pyval, None)
240 for attr in arg.attrib:
241
242 self.assertEquals(value.get(attr), arg.get(attr))
243
245
246
247 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
248 attrib={"gnu": "muh", "cat": "meeow",
249 "dog": "wuff"},
250 bird="tchilp", dog="grrr")
251 value = objectify.DataElement(arg, _pytype="int")
252 self.assert_(isinstance(value, objectify.IntElement))
253 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
254 for attr in arg.attrib:
255 if not attr == objectify.PYTYPE_ATTRIBUTE:
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, _xsi="xsd:int")
266 self.assert_(isinstance(value, objectify.IntElement))
267 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
268 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
269 for attr in arg.attrib:
270 if not attr in [objectify.PYTYPE_ATTRIBUTE,
271 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
272 self.assertEquals(value.get(attr), arg.get(attr))
273
275
276
277 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
278 attrib={"gnu": "muh", "cat": "meeow",
279 "dog": "wuff"},
280 bird="tchilp", dog="grrr")
281 value = objectify.DataElement(arg, _pytype="int", _xsi="xsd:int")
282 self.assert_(isinstance(value, objectify.IntElement))
283 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
284 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
285 for attr in arg.attrib:
286 if not attr in [objectify.PYTYPE_ATTRIBUTE,
287 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
288 self.assertEquals(value.get(attr), arg.get(attr))
289
293
297
302
307
311
315
319
324
326 root = self.XML(xml_str)
327 self.assertEquals("0", getattr(root.c1, "{objectified}c2").text)
328 self.assertEquals("3", getattr(root.c1, "{otherNS}c2").text)
329
331 root = self.XML(xml_str)
332 self.assertRaises(AttributeError, getattr, root.c1, "NOT_THERE")
333 self.assertRaises(AttributeError, getattr, root.c1, "{unknownNS}c2")
334
341
343 root = self.XML(xml_str)
344 self.assertEquals(1, len(root.c1))
345
346 new_el = self.Element("test", myattr="5")
347 root.addattr("c1", new_el)
348 self.assertEquals(2, len(root.c1))
349 self.assertEquals(None, root.c1[0].get("myattr"))
350 self.assertEquals("5", root.c1[1].get("myattr"))
351
353 root = self.XML(xml_str)
354 self.assertEquals(1, len(root.c1))
355
356 new_el = self.Element("test")
357 self.etree.SubElement(new_el, "a", myattr="A")
358 self.etree.SubElement(new_el, "a", myattr="B")
359
360 root.addattr("c1", list(new_el.a))
361 self.assertEquals(3, len(root.c1))
362 self.assertEquals(None, root.c1[0].get("myattr"))
363 self.assertEquals("A", root.c1[1].get("myattr"))
364 self.assertEquals("B", root.c1[2].get("myattr"))
365
372
374 root = self.XML(xml_str)
375 self.assertEquals("0", root.c1.c2[0].text)
376 self.assertEquals("1", root.c1.c2[1].text)
377 self.assertEquals("2", root.c1.c2[2].text)
378 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
379
381 root = self.XML(xml_str)
382 self.assertEquals("0", root.c1.c2[0].text)
383 self.assertEquals("0", root.c1.c2[-3].text)
384 self.assertEquals("1", root.c1.c2[-2].text)
385 self.assertEquals("2", root.c1.c2[-1].text)
386 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
387
389 root = self.XML(xml_str)
390 self.assertEquals(1, len(root))
391 self.assertEquals(1, len(root.c1))
392 self.assertEquals(3, len(root.c1.c2))
393
402
408
418
423
428
430 Element = self.Element
431 SubElement = self.etree.SubElement
432 root = Element("root")
433 root.c = ["c1", "c2"]
434
435 c1 = root.c[0]
436 c2 = root.c[1]
437
438 self.assertEquals([c1,c2], list(root.c))
439 self.assertEquals(["c1", "c2"],
440 [ c.text for c in root.c ])
441
442 root2 = Element("root2")
443 root2.el = [ "test", "test" ]
444 self.assertEquals(["test", "test"],
445 [ el.text for el in root2.el ])
446
447 root.c = [ root2.el, root2.el ]
448 self.assertEquals(["test", "test"],
449 [ c.text for c in root.c ])
450 self.assertEquals(["test", "test"],
451 [ el.text for el in root2.el ])
452
453 root.c[:] = [ c1, c2, c2, c1 ]
454 self.assertEquals(["c1", "c2", "c2", "c1"],
455 [ c.text for c in root.c ])
456
465
474
476
477 Element = self.Element
478 SubElement = self.etree.SubElement
479 root = Element("root")
480
481 root["text"] = "TEST"
482 self.assertEquals(["TEST"],
483 [ c.text for c in root["text"] ])
484
485 root["tail"] = "TEST"
486 self.assertEquals(["TEST"],
487 [ c.text for c in root["tail"] ])
488
489 root["pyval"] = "TEST"
490 self.assertEquals(["TEST"],
491 [ c.text for c in root["pyval"] ])
492
493 root["tag"] = "TEST"
494 self.assertEquals(["TEST"],
495 [ c.text for c in root["tag"] ])
496
504
506 XML = self.XML
507 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
508 self.assertEquals(2, len(root.findall(".//{X}b")))
509 self.assertEquals(3, len(root.findall(".//b")))
510 self.assertEquals(2, len(root.findall("b")))
511
519
533
539
551
560
567
574
581
583 Element = self.Element
584 SubElement = self.etree.SubElement
585 root = Element("{objectified}root")
586 root.s = "test"
587
588 self.assertEquals("test" * 5, root.s * 5)
589 self.assertEquals(5 * "test", 5 * root.s)
590
591 self.assertRaises(TypeError, operator.mul, root.s, "honk")
592 self.assertRaises(TypeError, operator.mul, "honk", root.s)
593
603
624
633
638
643
648
655
662
669
671 Element = self.Element
672 SubElement = self.etree.SubElement
673 root = Element("{objectified}root")
674 root.s = u"test"
675
676 self.assertEquals(u"test" * 5, root.s * 5)
677 self.assertEquals(5 * u"test", 5 * root.s)
678
679 self.assertRaises(TypeError, operator.mul, root.s, u"honk")
680 self.assertRaises(TypeError, operator.mul, u"honk", root.s)
681
691
696
701
706
713
718
725
730
739
748
754
763
765 pyval = 1
766 pytype = "NoneType"
767 objclass = objectify.NoneElement
768 value = objectify.DataElement(pyval, _pytype=pytype)
769 self.assert_(isinstance(value, objclass),
770 "DataElement(%s, _pytype='%s') returns %s, expected %s"
771 % (pyval, pytype, type(value), objclass))
772 self.assertEquals(value.text, None)
773 self.assertEquals(value.pyval, None)
774
776
777 pyval = 1
778 pytype = "none"
779 objclass = objectify.NoneElement
780 value = objectify.DataElement(pyval, _pytype=pytype)
781 self.assert_(isinstance(value, objclass),
782 "DataElement(%s, _pytype='%s') returns %s, expected %s"
783 % (pyval, pytype, type(value), objclass))
784 self.assertEquals(value.text, None)
785 self.assertEquals(value.pyval, None)
786
792 root = Element("{objectified}root")
793 root.myfloat = MyFloat(5.5)
794 self.assert_(isinstance(root.myfloat, objectify.FloatElement))
795 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
796
798 class MyFloat(float):
799 pass
800 value = objectify.DataElement(MyFloat(5.5))
801 self.assert_(isinstance(value, objectify.FloatElement))
802 self.assertEquals(value, 5.5)
803 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None)
804
806 XML = self.XML
807 root = XML('''\
808 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
809 <b xsi:type="boolean">true</b>
810 <b xsi:type="boolean">false</b>
811 <b xsi:type="boolean">1</b>
812 <b xsi:type="boolean">0</b>
813
814 <f xsi:type="float">5</f>
815 <f xsi:type="double">5</f>
816
817 <s xsi:type="string">5</s>
818 <s xsi:type="normalizedString">5</s>
819 <s xsi:type="token">5</s>
820 <s xsi:type="language">5</s>
821 <s xsi:type="Name">5</s>
822 <s xsi:type="NCName">5</s>
823 <s xsi:type="ID">5</s>
824 <s xsi:type="IDREF">5</s>
825 <s xsi:type="ENTITY">5</s>
826 <s xsi:type="NMTOKEN">5</s>
827
828 <l xsi:type="integer">5</l>
829 <l xsi:type="nonPositiveInteger">5</l>
830 <l xsi:type="negativeInteger">5</l>
831 <l xsi:type="long">5</l>
832 <l xsi:type="nonNegativeInteger">5</l>
833 <l xsi:type="unsignedLong">5</l>
834 <l xsi:type="unsignedInt">5</l>
835 <l xsi:type="positiveInteger">5</l>
836
837 <i xsi:type="int">5</i>
838 <i xsi:type="short">5</i>
839 <i xsi:type="byte">5</i>
840 <i xsi:type="unsignedShort">5</i>
841 <i xsi:type="unsignedByte">5</i>
842
843 <n xsi:nil="true"/>
844 </root>
845 ''')
846
847 for b in root.b:
848 self.assert_(isinstance(b, objectify.BoolElement))
849 self.assertEquals(True, root.b[0])
850 self.assertEquals(False, root.b[1])
851 self.assertEquals(True, root.b[2])
852 self.assertEquals(False, root.b[3])
853
854 for f in root.f:
855 self.assert_(isinstance(f, objectify.FloatElement))
856 self.assertEquals(5, f)
857
858 for s in root.s:
859 self.assert_(isinstance(s, objectify.StringElement))
860 self.assertEquals("5", s)
861
862 for l in root.l:
863 self.assert_(isinstance(l, objectify.LongElement))
864 self.assertEquals(5L, l)
865
866 for i in root.i:
867 self.assert_(isinstance(i, objectify.IntElement))
868 self.assertEquals(5, i)
869
870 self.assert_(isinstance(root.n, objectify.NoneElement))
871 self.assertEquals(None, root.n)
872
874 XML = self.XML
875 root = XML('''\
876 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
877 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
878 <b xsi:type="xsd:boolean">true</b>
879 <b xsi:type="xsd:boolean">false</b>
880 <b xsi:type="xsd:boolean">1</b>
881 <b xsi:type="xsd:boolean">0</b>
882
883 <f xsi:type="xsd:float">5</f>
884 <f xsi:type="xsd:double">5</f>
885
886 <s xsi:type="xsd:string">5</s>
887 <s xsi:type="xsd:normalizedString">5</s>
888 <s xsi:type="xsd:token">5</s>
889 <s xsi:type="xsd:language">5</s>
890 <s xsi:type="xsd:Name">5</s>
891 <s xsi:type="xsd:NCName">5</s>
892 <s xsi:type="xsd:ID">5</s>
893 <s xsi:type="xsd:IDREF">5</s>
894 <s xsi:type="xsd:ENTITY">5</s>
895 <s xsi:type="xsd:NMTOKEN">5</s>
896
897 <l xsi:type="xsd:integer">5</l>
898 <l xsi:type="xsd:nonPositiveInteger">5</l>
899 <l xsi:type="xsd:negativeInteger">5</l>
900 <l xsi:type="xsd:long">5</l>
901 <l xsi:type="xsd:nonNegativeInteger">5</l>
902 <l xsi:type="xsd:unsignedLong">5</l>
903 <l xsi:type="xsd:unsignedInt">5</l>
904 <l xsi:type="xsd:positiveInteger">5</l>
905
906 <i xsi:type="xsd:int">5</i>
907 <i xsi:type="xsd:short">5</i>
908 <i xsi:type="xsd:byte">5</i>
909 <i xsi:type="xsd:unsignedShort">5</i>
910 <i xsi:type="xsd:unsignedByte">5</i>
911
912 <n xsi:nil="true"/>
913 </root>
914 ''')
915
916 for b in root.b:
917 self.assert_(isinstance(b, objectify.BoolElement))
918 self.assertEquals(True, root.b[0])
919 self.assertEquals(False, root.b[1])
920 self.assertEquals(True, root.b[2])
921 self.assertEquals(False, root.b[3])
922
923 for f in root.f:
924 self.assert_(isinstance(f, objectify.FloatElement))
925 self.assertEquals(5, f)
926
927 for s in root.s:
928 self.assert_(isinstance(s, objectify.StringElement))
929 self.assertEquals("5", s)
930
931 for l in root.l:
932 self.assert_(isinstance(l, objectify.LongElement))
933 self.assertEquals(5L, l)
934
935 for i in root.i:
936 self.assert_(isinstance(i, objectify.IntElement))
937 self.assertEquals(5, i)
938
939 self.assert_(isinstance(root.n, objectify.NoneElement))
940 self.assertEquals(None, root.n)
941
943 XML = self.XML
944 root = XML(u'<root><b>why</b><b>try</b></root>')
945 strs = [ str(s) for s in root.b ]
946 self.assertEquals(["why", "try"],
947 strs)
948
977
998
999
1000
1024
1026 XML = self.XML
1027 root = XML(u"""
1028 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1029 <b xsi:nil="true"></b><b xsi:nil="true"/>
1030 </root>""")
1031 self.assert_(root.b[0] == root.b[1])
1032 self.assertFalse(root.b[0])
1033 self.assertEquals(root.b[0], None)
1034 self.assertEquals(None, root.b[0])
1035
1036 for comparison in ["abc", 5, 7.3, True, [], ()]:
1037 none = root.b[1]
1038 self.assert_(none < comparison, "%s (%s) should be < %s" %
1039 (none, type(none), comparison) )
1040 self.assert_(comparison > none, "%s should be > %s (%s)" %
1041 (comparison, none, type(none)) )
1042
1048
1055
1059
1061 XML = self.XML
1062 root = XML(u'''\
1063 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1064 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1065 <b>5</b>
1066 <b>test</b>
1067 <c>1.1</c>
1068 <c>\uF8D2</c>
1069 <x>true</x>
1070 <n xsi:nil="true" />
1071 <n></n>
1072 <b xsi:type="double">5</b>
1073 <b xsi:type="float">5</b>
1074 <s xsi:type="string">23</s>
1075 <s py:pytype="str">42</s>
1076 <f py:pytype="float">300</f>
1077 <l py:pytype="long">2</l>
1078 <t py:pytype="TREE"></t>
1079 </a>
1080 ''')
1081 objectify.annotate(root)
1082
1083 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1084 for c in root.iterchildren() ]
1085 self.assertEquals("int", child_types[ 0])
1086 self.assertEquals("str", child_types[ 1])
1087 self.assertEquals("float", child_types[ 2])
1088 self.assertEquals("str", child_types[ 3])
1089 self.assertEquals("bool", child_types[ 4])
1090 self.assertEquals("NoneType", child_types[ 5])
1091 self.assertEquals(None, child_types[ 6])
1092 self.assertEquals("float", child_types[ 7])
1093 self.assertEquals("float", child_types[ 8])
1094 self.assertEquals("str", child_types[ 9])
1095 self.assertEquals("int", child_types[10])
1096 self.assertEquals("int", child_types[11])
1097 self.assertEquals("int", child_types[12])
1098 self.assertEquals(None, child_types[13])
1099
1100 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1101
1121
1123 XML = self.XML
1124 root = XML(u'''\
1125 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1126 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1127 <b>5</b>
1128 <b>test</b>
1129 <c>1.1</c>
1130 <c>\uF8D2</c>
1131 <x>true</x>
1132 <n xsi:nil="true" />
1133 <n></n>
1134 <b xsi:type="double">5</b>
1135 <b xsi:type="float">5</b>
1136 <s xsi:type="string">23</s>
1137 <s py:pytype="str">42</s>
1138 <f py:pytype="float">300</f>
1139 <l py:pytype="long">2</l>
1140 <t py:pytype="TREE"></t>
1141 </a>
1142 ''')
1143 objectify.annotate(root, ignore_old=False)
1144
1145 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1146 for c in root.iterchildren() ]
1147 self.assertEquals("int", child_types[ 0])
1148 self.assertEquals("str", child_types[ 1])
1149 self.assertEquals("float", child_types[ 2])
1150 self.assertEquals("str", child_types[ 3])
1151 self.assertEquals("bool", child_types[ 4])
1152 self.assertEquals("NoneType", child_types[ 5])
1153 self.assertEquals(None, child_types[ 6])
1154 self.assertEquals("float", child_types[ 7])
1155 self.assertEquals("float", child_types[ 8])
1156 self.assertEquals("str", child_types[ 9])
1157 self.assertEquals("str", child_types[10])
1158 self.assertEquals("float", child_types[11])
1159 self.assertEquals("long", child_types[12])
1160 self.assertEquals(TREE_PYTYPE, child_types[13])
1161
1162 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1163
1165 XML = self.XML
1166 root = XML(u'''\
1167 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1168 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1169 <b>5</b>
1170 <b>test</b>
1171 <c>1.1</c>
1172 <c>\uF8D2</c>
1173 <x>true</x>
1174 <n xsi:nil="true" />
1175 <n></n>
1176 <b xsi:type="double">5</b>
1177 <b xsi:type="float">5</b>
1178 <s xsi:type="string">23</s>
1179 <s py:pytype="str">42</s>
1180 <f py:pytype="float">300</f>
1181 <l py:pytype="long">2</l>
1182 <t py:pytype="TREE"></t>
1183 </a>
1184 ''')
1185 objectify.annotate(root, ignore_old=False, ignore_xsi=False,
1186 annotate_xsi=1, annotate_pytype=1)
1187
1188
1189 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1190 for c in root.iterchildren() ]
1191 self.assertEquals("int", child_types[ 0])
1192 self.assertEquals("str", child_types[ 1])
1193 self.assertEquals("float", child_types[ 2])
1194 self.assertEquals("str", child_types[ 3])
1195 self.assertEquals("bool", child_types[ 4])
1196 self.assertEquals("NoneType", child_types[ 5])
1197 self.assertEquals(None, child_types[ 6])
1198 self.assertEquals("float", child_types[ 7])
1199 self.assertEquals("float", child_types[ 8])
1200 self.assertEquals("str", child_types[ 9])
1201 self.assertEquals("str", child_types[10])
1202 self.assertEquals("float", child_types[11])
1203 self.assertEquals("long", child_types[12])
1204 self.assertEquals(TREE_PYTYPE, child_types[13])
1205
1206 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1207
1208 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1209 for c in root.iterchildren() ]
1210
1211
1212 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1213 for c in root.iterchildren() ]
1214 self.assertEquals("xsd:int", child_types[ 0])
1215 self.assertEquals("xsd:string", child_types[ 1])
1216 self.assertEquals("xsd:double", child_types[ 2])
1217 self.assertEquals("xsd:string", child_types[ 3])
1218 self.assertEquals("xsd:boolean", child_types[ 4])
1219 self.assertEquals(None, child_types[ 5])
1220 self.assertEquals(None, child_types[ 6])
1221 self.assertEquals("xsd:double", child_types[ 7])
1222 self.assertEquals("xsd:float", child_types[ 8])
1223 self.assertEquals("xsd:string", child_types[ 9])
1224 self.assertEquals("xsd:string", child_types[10])
1225 self.assertEquals("xsd:double", child_types[11])
1226 self.assertEquals("xsd:integer", child_types[12])
1227 self.assertEquals(None, child_types[13])
1228
1229 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1230
1232 XML = self.XML
1233 root = XML(u'''\
1234 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1235 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1236 <b>5</b>
1237 <b>test</b>
1238 <c>1.1</c>
1239 <c>\uF8D2</c>
1240 <x>true</x>
1241 <n xsi:nil="true" />
1242 <n></n>
1243 <b xsi:type="double">5</b>
1244 <b xsi:type="float">5</b>
1245 <s xsi:type="string">23</s>
1246 <s py:pytype="str">42</s>
1247 <f py:pytype="float">300</f>
1248 <l py:pytype="long">2</l>
1249 <t py:pytype="TREE"></t>
1250 </a>
1251 ''')
1252 objectify.xsiannotate(root, ignore_old=False)
1253
1254 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1255 for c in root.iterchildren() ]
1256 self.assertEquals("xsd:int", child_types[ 0])
1257 self.assertEquals("xsd:string", child_types[ 1])
1258 self.assertEquals("xsd:double", child_types[ 2])
1259 self.assertEquals("xsd:string", child_types[ 3])
1260 self.assertEquals("xsd:boolean", child_types[ 4])
1261 self.assertEquals(None, child_types[ 5])
1262 self.assertEquals(None, child_types[ 6])
1263 self.assertEquals("xsd:double", child_types[ 7])
1264 self.assertEquals("xsd:float", child_types[ 8])
1265 self.assertEquals("xsd:string", child_types[ 9])
1266 self.assertEquals("xsd:string", child_types[10])
1267 self.assertEquals("xsd:double", child_types[11])
1268 self.assertEquals("xsd:integer", child_types[12])
1269 self.assertEquals(None, child_types[13])
1270
1272 XML = self.XML
1273 root = XML(u'''\
1274 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1275 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1276 <b>5</b>
1277 <b>test</b>
1278 <c>1.1</c>
1279 <c>\uF8D2</c>
1280 <x>true</x>
1281 <n xsi:nil="true" />
1282 <n></n>
1283 <b xsi:type="double">5</b>
1284 <b xsi:type="float">5</b>
1285 <s xsi:type="string">23</s>
1286 <s py:pytype="str">42</s>
1287 <f py:pytype="float">300</f>
1288 <l py:pytype="long">2</l>
1289 <t py:pytype="TREE"></t>
1290 </a>
1291 ''')
1292 objectify.pyannotate(root, ignore_old=True)
1293
1294 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1295 for c in root.iterchildren() ]
1296 self.assertEquals("int", child_types[ 0])
1297 self.assertEquals("str", child_types[ 1])
1298 self.assertEquals("float", child_types[ 2])
1299 self.assertEquals("str", child_types[ 3])
1300 self.assertEquals("bool", child_types[ 4])
1301 self.assertEquals("NoneType", child_types[ 5])
1302 self.assertEquals(None, child_types[ 6])
1303 self.assertEquals("float", child_types[ 7])
1304 self.assertEquals("float", child_types[ 8])
1305 self.assertEquals("str", child_types[ 9])
1306 self.assertEquals("int", child_types[10])
1307 self.assertEquals("int", child_types[11])
1308 self.assertEquals("int", child_types[12])
1309 self.assertEquals(None, child_types[13])
1310
1311 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1312
1332
1334 XML = self.XML
1335 root = XML(u'''\
1336 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1337 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1338 <b>5</b>
1339 <b>test</b>
1340 <c>1.1</c>
1341 <c>\uF8D2</c>
1342 <x>true</x>
1343 <n xsi:nil="true" />
1344 <n></n>
1345 <b xsi:type="double">5</b>
1346 <b xsi:type="float">5</b>
1347 <s xsi:type="string">23</s>
1348 <s py:pytype="str">42</s>
1349 <f py:pytype="float">300</f>
1350 <l py:pytype="long">2</l>
1351 <t py:pytype="TREE"></t>
1352 </a>
1353 ''')
1354 objectify.pyannotate(root)
1355
1356 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1357 for c in root.iterchildren() ]
1358 self.assertEquals("int", child_types[ 0])
1359 self.assertEquals("str", child_types[ 1])
1360 self.assertEquals("float", child_types[ 2])
1361 self.assertEquals("str", child_types[ 3])
1362 self.assertEquals("bool", child_types[ 4])
1363 self.assertEquals("NoneType", child_types[ 5])
1364 self.assertEquals(None, child_types[ 6])
1365 self.assertEquals("float", child_types[ 7])
1366 self.assertEquals("float", child_types[ 8])
1367 self.assertEquals("str", child_types[ 9])
1368 self.assertEquals("str", child_types[10])
1369 self.assertEquals("float", child_types[11])
1370 self.assertEquals("long", child_types[12])
1371 self.assertEquals(TREE_PYTYPE, child_types[13])
1372
1373 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1374
1376 XML = self.XML
1377 root = XML(u'''\
1378 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1379 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1380 <b>5</b>
1381 <b>test</b>
1382 <c>1.1</c>
1383 <c>\uF8D2</c>
1384 <x>true</x>
1385 <n xsi:nil="true" />
1386 <n></n>
1387 <b xsi:type="double">5</b>
1388 <b xsi:type="float">5</b>
1389 <s xsi:type="string">23</s>
1390 <s py:pytype="str">42</s>
1391 <f py:pytype="float">300</f>
1392 <l py:pytype="long">2</l>
1393 <t py:pytype="TREE"></t>
1394 </a>
1395 ''')
1396 objectify.xsiannotate(root, ignore_old=True)
1397
1398 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1399 for c in root.iterchildren() ]
1400 self.assertEquals("xsd:int", child_types[ 0])
1401 self.assertEquals("xsd:string", child_types[ 1])
1402 self.assertEquals("xsd:double", child_types[ 2])
1403 self.assertEquals("xsd:string", child_types[ 3])
1404 self.assertEquals("xsd:boolean", child_types[ 4])
1405 self.assertEquals(None, child_types[ 5])
1406 self.assertEquals(None, child_types[ 6])
1407 self.assertEquals("xsd:int", child_types[ 7])
1408 self.assertEquals("xsd:int", child_types[ 8])
1409 self.assertEquals("xsd:int", child_types[ 9])
1410 self.assertEquals("xsd:string", child_types[10])
1411 self.assertEquals("xsd:double", child_types[11])
1412 self.assertEquals("xsd:integer", child_types[12])
1413 self.assertEquals(None, child_types[13])
1414
1415 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1416
1418 XML = self.XML
1419 root = XML(u'''\
1420 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1421 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1422 <b>5</b>
1423 <b>test</b>
1424 <c>1.1</c>
1425 <c>\uF8D2</c>
1426 <x>true</x>
1427 <n xsi:nil="true" />
1428 <n></n>
1429 <b xsi:type="double">5</b>
1430 <b xsi:type="float">5</b>
1431 <s xsi:type="string">23</s>
1432 <s py:pytype="str">42</s>
1433 <f py:pytype="float">300</f>
1434 <l py:pytype="long">2</l>
1435 <t py:pytype="TREE"></t>
1436 </a>
1437 ''')
1438 objectify.deannotate(root)
1439
1440 for c in root.getiterator():
1441 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1442 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1443
1444 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1445
1447 XML = self.XML
1448 root = XML(u'''\
1449 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1450 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1451 <b>5</b>
1452 <b>test</b>
1453 <c>1.1</c>
1454 <c>\uF8D2</c>
1455 <x>true</x>
1456 <n xsi:nil="true" />
1457 <n></n>
1458 <b xsi:type="double">5</b>
1459 <b xsi:type="float">5</b>
1460 <s xsi:type="string">23</s>
1461 <s py:pytype="str">42</s>
1462 <f py:pytype="float">300</f>
1463 <l py:pytype="long">2</l>
1464 <t py:pytype="TREE"></t>
1465 </a>
1466 ''')
1467 objectify.xsiannotate(root)
1468 objectify.deannotate(root, xsi=False)
1469
1470 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1471 for c in root.iterchildren() ]
1472 self.assertEquals("xsd:int", child_types[ 0])
1473 self.assertEquals("xsd:string", child_types[ 1])
1474 self.assertEquals("xsd:double", child_types[ 2])
1475 self.assertEquals("xsd:string", child_types[ 3])
1476 self.assertEquals("xsd:boolean", child_types[ 4])
1477 self.assertEquals(None, child_types[ 5])
1478 self.assertEquals(None, child_types[ 6])
1479 self.assertEquals("xsd:int", child_types[ 7])
1480 self.assertEquals("xsd:int", child_types[ 8])
1481 self.assertEquals("xsd:int", child_types[ 9])
1482 self.assertEquals("xsd:string", child_types[10])
1483 self.assertEquals("xsd:double", child_types[11])
1484 self.assertEquals("xsd:integer", child_types[12])
1485 self.assertEquals(None, child_types[13])
1486
1487 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1488
1489 for c in root.getiterator():
1490 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1491
1493 XML = self.XML
1494 root = XML(u'''\
1495 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1496 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1497 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1498 <b>5</b>
1499 <b>test</b>
1500 <c>1.1</c>
1501 <c>\uF8D2</c>
1502 <x>true</x>
1503 <n xsi:nil="true" />
1504 <n></n>
1505 <b xsi:type="xsd:double">5</b>
1506 <b xsi:type="xsd:float">5</b>
1507 <s xsi:type="xsd:string">23</s>
1508 <s py:pytype="str">42</s>
1509 <f py:pytype="float">300</f>
1510 <l py:pytype="long">2</l>
1511 <t py:pytype="TREE"></t>
1512 </a>
1513 ''')
1514 objectify.annotate(root)
1515 objectify.deannotate(root, pytype=False)
1516
1517 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1518 for c in root.iterchildren() ]
1519 self.assertEquals("int", child_types[ 0])
1520 self.assertEquals("str", child_types[ 1])
1521 self.assertEquals("float", child_types[ 2])
1522 self.assertEquals("str", child_types[ 3])
1523 self.assertEquals("bool", child_types[ 4])
1524 self.assertEquals("NoneType", child_types[ 5])
1525 self.assertEquals(None, child_types[ 6])
1526 self.assertEquals("float", child_types[ 7])
1527 self.assertEquals("float", child_types[ 8])
1528 self.assertEquals("str", child_types[ 9])
1529 self.assertEquals("int", child_types[10])
1530 self.assertEquals("int", child_types[11])
1531 self.assertEquals("int", child_types[12])
1532 self.assertEquals(None, child_types[13])
1533
1534 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1535
1536 for c in root.getiterator():
1537 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1538
1540 XML = self.XML
1541 root = XML(u'''\
1542 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1543 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1544 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1545 <b xsi:type="xsd:int">5</b>
1546 <b xsi:type="xsd:string">test</b>
1547 <c xsi:type="xsd:float">1.1</c>
1548 <c xsi:type="xsd:string">\uF8D2</c>
1549 <x xsi:type="xsd:boolean">true</x>
1550 <n xsi:nil="true" />
1551 <n></n>
1552 <b xsi:type="xsd:double">5</b>
1553 <b xsi:type="xsd:float">5</b>
1554 <s xsi:type="xsd:string">23</s>
1555 <s xsi:type="xsd:string">42</s>
1556 <f xsi:type="xsd:float">300</f>
1557 <l xsi:type="xsd:long">2</l>
1558 <t py:pytype="TREE"></t>
1559 </a>
1560 ''')
1561 objectify.annotate(root)
1562 objectify.deannotate(root, xsi=False)
1563
1564 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1565 for c in root.iterchildren() ]
1566 self.assertEquals("xsd:int", child_types[ 0])
1567 self.assertEquals("xsd:string", child_types[ 1])
1568 self.assertEquals("xsd:float", child_types[ 2])
1569 self.assertEquals("xsd:string", child_types[ 3])
1570 self.assertEquals("xsd:boolean", child_types[ 4])
1571 self.assertEquals(None, child_types[ 5])
1572 self.assertEquals(None, child_types[ 6])
1573 self.assertEquals("xsd:double", child_types[ 7])
1574 self.assertEquals("xsd:float", child_types[ 8])
1575 self.assertEquals("xsd:string", child_types[ 9])
1576 self.assertEquals("xsd:string", child_types[10])
1577 self.assertEquals("xsd:float", child_types[11])
1578 self.assertEquals("xsd:long", child_types[12])
1579 self.assertEquals(None, child_types[13])
1580
1581 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1582
1583 for c in root.getiterator():
1584 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1585
1587 XML = self.XML
1588
1589 xml = u'''\
1590 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1591 <b>5</b>
1592 <b>test</b>
1593 <c>1.1</c>
1594 <c>\uF8D2</c>
1595 <x>true</x>
1596 <n xsi:nil="true" />
1597 <n></n>
1598 <b xsi:type="double">5</b>
1599 </a>
1600 '''
1601
1602 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1603 objectify.setPytypeAttributeTag("{TEST}test")
1604
1605 root = XML(xml)
1606 objectify.annotate(root)
1607
1608 attribs = root.xpath("//@py:%s" % pytype_name,
1609 namespaces={"py" : pytype_ns})
1610 self.assertEquals(0, len(attribs))
1611 attribs = root.xpath("//@py:test",
1612 namespaces={"py" : "TEST"})
1613 self.assertEquals(7, len(attribs))
1614
1615 objectify.setPytypeAttributeTag()
1616 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1617
1618 self.assertNotEqual("test", pytype_ns.lower())
1619 self.assertNotEqual("test", pytype_name.lower())
1620
1621 root = XML(xml)
1622 attribs = root.xpath("//@py:%s" % pytype_name,
1623 namespaces={"py" : pytype_ns})
1624 self.assertEquals(0, len(attribs))
1625
1626 objectify.annotate(root)
1627 attribs = root.xpath("//@py:%s" % pytype_name,
1628 namespaces={"py" : pytype_ns})
1629 self.assertEquals(7, len(attribs))
1630
1640
1641 def checkMyType(s):
1642 return True
1643
1644 pytype = objectify.PyType("mytype", checkMyType, NewType)
1645 pytype.register()
1646 self.assert_(pytype in objectify.getRegisteredTypes())
1647 pytype.unregister()
1648
1649 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1650 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1651 pytype.unregister()
1652
1653 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1654 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1655 pytype.unregister()
1656
1657 self.assertRaises(ValueError, pytype.register,
1658 before = [objectify.getRegisteredTypes()[0].name],
1659 after = [objectify.getRegisteredTypes()[1].name])
1660
1661 finally:
1662 for pytype in objectify.getRegisteredTypes():
1663 pytype.unregister()
1664 for pytype in orig_types:
1665 pytype.register()
1666
1672
1678
1684
1692
1711
1716
1721
1726
1731
1751
1753 root = self.XML(xml_str)
1754 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
1755 self.assertEquals(root.c1.c2.text, path(root).text)
1756
1757 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
1758 self.assertEquals(root.c1.c2[2].text, path(root).text)
1759
1760 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
1761 self.assertEquals(root.c1.c2[2].text, path(root).text)
1762
1763 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
1764 self.assertEquals(root.c1.c2[-1].text, path(root).text)
1765
1766 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
1767 self.assertEquals(root.c1.c2[-3].text, path(root).text)
1768
1770 self.assertRaises(ValueError, objectify.ObjectPath,
1771 "root.c1[0].c2[-1-2]")
1772 self.assertRaises(ValueError, objectify.ObjectPath,
1773 ['root', 'c1[0]', 'c2[-1-2]'])
1774
1775 self.assertRaises(ValueError, objectify.ObjectPath,
1776 "root[2].c1.c2")
1777 self.assertRaises(ValueError, objectify.ObjectPath,
1778 ['root[2]', 'c1', 'c2'])
1779
1780 self.assertRaises(ValueError, objectify.ObjectPath,
1781 [])
1782 self.assertRaises(ValueError, objectify.ObjectPath,
1783 ['', '', ''])
1784
1786 root = self.XML(xml_str)
1787 path = objectify.ObjectPath("root.c1[9999].c2")
1788 self.assertRaises(AttributeError, path, root)
1789
1790 path = objectify.ObjectPath("root.c1[0].c2[9999]")
1791 self.assertRaises(AttributeError, path, root)
1792
1793 path = objectify.ObjectPath(".c1[9999].c2[0]")
1794 self.assertRaises(AttributeError, path, root)
1795
1796 path = objectify.ObjectPath("root.c1[-2].c2")
1797 self.assertRaises(AttributeError, path, root)
1798
1799 path = objectify.ObjectPath("root.c1[0].c2[-4]")
1800 self.assertRaises(AttributeError, path, root)
1801
1815
1817 root = self.XML(xml_str)
1818 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
1819 self.assertEquals(root.c1.c2.text, path.find(root).text)
1820 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
1821 self.assertEquals(root.c1.c2.text, path.find(root).text)
1822 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
1823 self.assertEquals(root.c1.c2.text, path.find(root).text)
1824 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
1825 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
1826 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
1827 self.assertEquals(root.c1.c2.text, path.find(root).text)
1828 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
1829 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
1830 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
1831 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
1832 path.find(root).text)
1833
1846
1861
1873
1887
1889 root = self.XML(xml_str)
1890 path = objectify.ObjectPath( "root.c1.c99" )
1891 self.assertRaises(AttributeError, path.find, root)
1892
1893 new_el = self.Element("{objectified}test")
1894 new_el.a = ["TEST1", "TEST2"]
1895 new_el.a[0].set("myattr", "ATTR1")
1896 new_el.a[1].set("myattr", "ATTR2")
1897
1898 path.setattr(root, list(new_el.a))
1899
1900 self.assertEquals(2, len(root.c1.c99))
1901 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
1902 self.assertEquals("TEST1", root.c1.c99[0].text)
1903 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
1904 self.assertEquals("TEST2", root.c1.c99[1].text)
1905 self.assertEquals("TEST1", path(root).text)
1906
1915
1929
1941
1955
1970
1972 root = self.XML(xml_str)
1973 self.assertEquals(
1974 ['{objectified}root', '{objectified}root.c1',
1975 '{objectified}root.c1.c2',
1976 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]',
1977 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'],
1978 root.descendantpaths())
1979
1981 root = self.XML(xml_str)
1982 self.assertEquals(
1983 ['{objectified}c1', '{objectified}c1.c2',
1984 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]',
1985 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'],
1986 root.c1.descendantpaths())
1987
1989 root = self.XML(xml_str)
1990 self.assertEquals(
1991 ['root.{objectified}c1', 'root.{objectified}c1.c2',
1992 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]',
1993 'root.{objectified}c1.{otherNS}c2',
1994 'root.{objectified}c1.{}c2'],
1995 root.c1.descendantpaths('root'))
1996
2008
2009
2010
2015
2020
2025
2030
2035
2040
2045
2050
2055
2057 E = objectify.E
2058 DataElement = objectify.DataElement
2059 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1),
2060 DataElement(2.0))
2061 self.assert_(isinstance(root, objectify.ObjectifiedElement))
2062 self.assertEquals(root.text, "text")
2063 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement))
2064 self.assertEquals(root.sub.tail, "tail")
2065 self.assert_(isinstance(root.sub.subsub, objectify.StringElement))
2066 self.assertEquals(len(root.value), 2)
2067 self.assert_(isinstance(root.value[0], objectify.IntElement))
2068 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2069
2071 suite = unittest.TestSuite()
2072 suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
2073 suite.addTests(
2074 [doctest.DocFileSuite('../../../doc/objectify.txt')])
2075 return suite
2076
2077 if __name__ == '__main__':
2078 print 'to test use test.py %s' % __file__
2079