1
2
3 """
4 Tests specific to the lxml.objectify API
5 """
6
7
8 import unittest, operator, sys
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)
54 for k in objectclass2pytype])
55
56 xml_str = '''\
57 <obj:root xmlns:obj="objectified" xmlns:other="otherNS">
58 <obj:c1 a1="A1" a2="A2" other:a3="A3">
59 <obj:c2>0</obj:c2>
60 <obj:c2>1</obj:c2>
61 <obj:c2>2</obj:c2>
62 <other:c2>3</other:c2>
63 <c2>3</c2>
64 </obj:c1>
65 </obj:root>'''
66
68 """Test cases for lxml.objectify
69 """
70 etree = etree
71
74
86
93
97
102
109
111 nsmap = {"my": "someNS",
112 "myother": "someOtherNS",
113 "myxsd": XML_SCHEMA_NS}
114 elt = objectify.Element("test", nsmap=nsmap)
115 self.assert_(PYTYPE_NAMESPACE in elt.nsmap.values())
116 for prefix, ns in nsmap.items():
117 self.assert_(prefix in elt.nsmap)
118 self.assertEquals(nsmap[prefix], elt.nsmap[prefix])
119
124
130
138
149
153
158
165
167 nsmap = {"my": "someNS",
168 "myother": "someOtherNS",
169 "myxsd": XML_SCHEMA_NS,}
170 value = objectify.DataElement("test", nsmap=nsmap)
171 self.assert_(PYTYPE_NAMESPACE in value.nsmap.values())
172 for prefix, ns in nsmap.items():
173 self.assert_(prefix in value.nsmap)
174 self.assertEquals(nsmap[prefix], value.nsmap[prefix])
175
180
186
194
205
207
208 value = objectify.DataElement(23, _pytype="str", _xsi="foobar",
209 attrib={"gnu": "muh", "cat": "meeow",
210 "dog": "wuff"},
211 bird="tchilp", dog="grrr")
212 self.assertEquals(value.get("gnu"), "muh")
213 self.assertEquals(value.get("cat"), "meeow")
214 self.assertEquals(value.get("dog"), "grrr")
215 self.assertEquals(value.get("bird"), "tchilp")
216
228
230
231
232 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
233 attrib={"gnu": "muh", "cat": "meeow",
234 "dog": "wuff"},
235 bird="tchilp", dog="grrr")
236 value = objectify.DataElement(arg, _pytype="NoneType")
237 self.assert_(isinstance(value, objectify.NoneElement))
238 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
239 self.assertEquals(value.text, None)
240 self.assertEquals(value.pyval, None)
241 for attr in arg.attrib:
242
243 self.assertEquals(value.get(attr), arg.get(attr))
244
246
247
248 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
249 attrib={"gnu": "muh", "cat": "meeow",
250 "dog": "wuff"},
251 bird="tchilp", dog="grrr")
252 value = objectify.DataElement(arg, _pytype="int")
253 self.assert_(isinstance(value, objectify.IntElement))
254 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
255 for attr in arg.attrib:
256 if not attr == objectify.PYTYPE_ATTRIBUTE:
257 self.assertEquals(value.get(attr), arg.get(attr))
258
260
261
262 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
263 attrib={"gnu": "muh", "cat": "meeow",
264 "dog": "wuff"},
265 bird="tchilp", dog="grrr")
266 value = objectify.DataElement(arg, _xsi="xsd:int")
267 self.assert_(isinstance(value, objectify.IntElement))
268 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
269 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
270 for attr in arg.attrib:
271 if not attr in [objectify.PYTYPE_ATTRIBUTE,
272 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
273 self.assertEquals(value.get(attr), arg.get(attr))
274
276
277
278 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
279 attrib={"gnu": "muh", "cat": "meeow",
280 "dog": "wuff"},
281 bird="tchilp", dog="grrr")
282 value = objectify.DataElement(arg, _pytype="int", _xsi="xsd:int")
283 self.assert_(isinstance(value, objectify.IntElement))
284 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
285 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
286 for attr in arg.attrib:
287 if not attr in [objectify.PYTYPE_ATTRIBUTE,
288 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
289 self.assertEquals(value.get(attr), arg.get(attr))
290
294
298
303
308
312
316
320
325
327 root = self.XML(xml_str)
328 self.assertEquals("0", getattr(root.c1, "{objectified}c2").text)
329 self.assertEquals("3", getattr(root.c1, "{otherNS}c2").text)
330
332 root = self.XML(xml_str)
333 self.assertRaises(AttributeError, getattr, root.c1, "NOT_THERE")
334 self.assertRaises(AttributeError, getattr, root.c1, "{unknownNS}c2")
335
342
344 root = self.XML(xml_str)
345 self.assertEquals(1, len(root.c1))
346
347 new_el = self.Element("test", myattr="5")
348 root.addattr("c1", new_el)
349 self.assertEquals(2, len(root.c1))
350 self.assertEquals(None, root.c1[0].get("myattr"))
351 self.assertEquals("5", root.c1[1].get("myattr"))
352
354 root = self.XML(xml_str)
355 self.assertEquals(1, len(root.c1))
356
357 new_el = self.Element("test")
358 self.etree.SubElement(new_el, "a", myattr="A")
359 self.etree.SubElement(new_el, "a", myattr="B")
360
361 root.addattr("c1", list(new_el.a))
362 self.assertEquals(3, len(root.c1))
363 self.assertEquals(None, root.c1[0].get("myattr"))
364 self.assertEquals("A", root.c1[1].get("myattr"))
365 self.assertEquals("B", root.c1[2].get("myattr"))
366
373
375 root = self.XML(xml_str)
376 self.assertEquals("0", root.c1.c2[0].text)
377 self.assertEquals("1", root.c1.c2[1].text)
378 self.assertEquals("2", root.c1.c2[2].text)
379 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
380
382 root = self.XML(xml_str)
383 self.assertEquals("0", root.c1.c2[0].text)
384 self.assertEquals("0", root.c1.c2[-3].text)
385 self.assertEquals("1", root.c1.c2[-2].text)
386 self.assertEquals("2", root.c1.c2[-1].text)
387 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
388
390 root = self.XML(xml_str)
391 self.assertEquals(1, len(root))
392 self.assertEquals(1, len(root.c1))
393 self.assertEquals(3, len(root.c1.c2))
394
396 root = self.XML(xml_str)
397 self.assertEquals([root],
398 list(iter(root)))
399 self.assertEquals([root.c1],
400 list(iter(root.c1)))
401 self.assertEquals([root.c1.c2[0], root.c1.c2[1], root.c1.c2[2]],
402 list(iter((root.c1.c2))))
403
409
419
424
429
430
431
433 root = self.XML("<root><c>c1</c><c>c2</c></root>")
434 self.assertEquals(["c1", "c2"],
435 [ c.text for c in root.c[:] ])
436
438 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
439 test_list = ["c1", "c2", "c3", "c4"]
440
441 self.assertEquals(test_list,
442 [ c.text for c in root.c[:] ])
443 self.assertEquals(test_list[1:2],
444 [ c.text for c in root.c[1:2] ])
445 self.assertEquals(test_list[-3:-1],
446 [ c.text for c in root.c[-3:-1] ])
447 self.assertEquals(test_list[-3:3],
448 [ c.text for c in root.c[-3:3] ])
449 self.assertEquals(test_list[-3000:3],
450 [ c.text for c in root.c[-3000:3] ])
451 self.assertEquals(test_list[-3:3000],
452 [ c.text for c in root.c[-3:3000] ])
453
455 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
456 test_list = ["c1", "c2", "c3", "c4"]
457
458 self.assertEquals(test_list,
459 [ c.text for c in root.c[:] ])
460 self.assertEquals(test_list[2:1:-1],
461 [ c.text for c in root.c[2:1:-1] ])
462 self.assertEquals(test_list[-1:-3:-1],
463 [ c.text for c in root.c[-1:-3:-1] ])
464 self.assertEquals(test_list[2:-3:-1],
465 [ c.text for c in root.c[2:-3:-1] ])
466 self.assertEquals(test_list[2:-3000:-1],
467 [ c.text for c in root.c[2:-3000:-1] ])
468
469
470
482
484 Element = self.Element
485 root = Element("root")
486 root.c = ["c1", "c2"]
487
488 c1 = root.c[0]
489 c2 = root.c[1]
490
491 self.assertEquals([c1,c2], list(root.c))
492 self.assertEquals(["c1", "c2"],
493 [ c.text for c in root.c ])
494
495 root2 = Element("root2")
496 root2.el = [ "test", "test" ]
497 self.assertEquals(["test", "test"],
498 [ el.text for el in root2.el ])
499
500 root.c = [ root2.el, root2.el ]
501 self.assertEquals(["test", "test"],
502 [ c.text for c in root.c ])
503 self.assertEquals(["test", "test"],
504 [ el.text for el in root2.el ])
505
506 root.c[:] = [ c1, c2, c2, c1 ]
507 self.assertEquals(["c1", "c2", "c2", "c1"],
508 [ c.text for c in root.c ])
509
511 Element = self.Element
512 root = Element("root")
513 l = ["c1", "c2", "c3", "c4"]
514 root.c = l
515
516 self.assertEquals(["c1", "c2", "c3", "c4"],
517 [ c.text for c in root.c ])
518 self.assertEquals(l,
519 [ c.text for c in root.c ])
520
521 new_slice = ["cA", "cB"]
522 l[1:2] = new_slice
523 root.c[1:2] = new_slice
524
525 self.assertEquals(["c1", "cA", "cB", "c3", "c4"], l)
526 self.assertEquals(["c1", "cA", "cB", "c3", "c4"],
527 [ c.text for c in root.c ])
528 self.assertEquals(l,
529 [ c.text for c in root.c ])
530
532 Element = self.Element
533 root = Element("root")
534 l = ["c1", "c2", "c3", "c4"]
535 root.c = l
536
537 self.assertEquals(["c1", "c2", "c3", "c4"],
538 [ c.text for c in root.c ])
539 self.assertEquals(l,
540 [ c.text for c in root.c ])
541
542 new_slice = ["cA", "cB"]
543 l[1:1] = new_slice
544 root.c[1:1] = new_slice
545
546 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"], l)
547 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"],
548 [ c.text for c in root.c ])
549 self.assertEquals(l,
550 [ c.text for c in root.c ])
551
553 Element = self.Element
554 root = Element("root")
555 l = ["c1", "c2", "c3", "c4"]
556 root.c = l
557
558 self.assertEquals(["c1", "c2", "c3", "c4"],
559 [ c.text for c in root.c ])
560 self.assertEquals(l,
561 [ c.text for c in root.c ])
562
563 new_slice = ["cA", "cB"]
564 l[-2:-2] = new_slice
565 root.c[-2:-2] = new_slice
566
567 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"], l)
568 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"],
569 [ c.text for c in root.c ])
570 self.assertEquals(l,
571 [ c.text for c in root.c ])
572
580
582 Element = self.Element
583 root = Element("root")
584 l = ["c1", "c2", "c3", "c4"]
585 root.c = l
586
587 self.assertEquals(["c1", "c2", "c3", "c4"],
588 [ c.text for c in root.c ])
589 self.assertEquals(l,
590 [ c.text for c in root.c ])
591
592 new_slice = ["cA", "cB", "cC"]
593 self.assertRaises(
594 ValueError, operator.setitem,
595 l, slice(1,2,-1), new_slice)
596 self.assertRaises(
597 ValueError, operator.setitem,
598 root.c, slice(1,2,-1), new_slice)
599
601 Element = self.Element
602 root = Element("root")
603 l = ["c1", "c2", "c3", "c4"]
604 root.c = l
605
606 self.assertEquals(["c1", "c2", "c3", "c4"],
607 [ c.text for c in root.c ])
608 self.assertEquals(l,
609 [ c.text for c in root.c ])
610
611 new_slice = ["cA", "cB"]
612 l[-1:1:-1] = new_slice
613 root.c[-1:1:-1] = new_slice
614
615 self.assertEquals(["c1", "c2", "cB", "cA"], l)
616 self.assertEquals(["c1", "c2", "cB", "cA"],
617 [ c.text for c in root.c ])
618 self.assertEquals(l,
619 [ c.text for c in root.c ])
620
622 Element = self.Element
623 root = Element("root")
624 l = ["c1", "c2", "c3", "c4"]
625 root.c = l
626
627 self.assertEquals(["c1", "c2", "c3", "c4"],
628 [ c.text for c in root.c ])
629 self.assertEquals(l,
630 [ c.text for c in root.c ])
631
632 new_slice = ["cA", "cB"]
633 l[-1:-4:-2] = new_slice
634 root.c[-1:-4:-2] = new_slice
635
636 self.assertEquals(["c1", "cB", "c3", "cA"], l)
637 self.assertEquals(["c1", "cB", "c3", "cA"],
638 [ c.text for c in root.c ])
639 self.assertEquals(l,
640 [ c.text for c in root.c ])
641
642
643
651
659
661
662 Element = self.Element
663 root = Element("root")
664
665 root["text"] = "TEST"
666 self.assertEquals(["TEST"],
667 [ c.text for c in root["text"] ])
668
669 root["tail"] = "TEST"
670 self.assertEquals(["TEST"],
671 [ c.text for c in root["tail"] ])
672
673 root["pyval"] = "TEST"
674 self.assertEquals(["TEST"],
675 [ c.text for c in root["pyval"] ])
676
677 root["tag"] = "TEST"
678 self.assertEquals(["TEST"],
679 [ c.text for c in root["tag"] ])
680
688
690 XML = self.XML
691 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
692 self.assertEquals(2, len(root.findall(".//{X}b")))
693 self.assertEquals(3, len(root.findall(".//b")))
694 self.assertEquals(2, len(root.findall("b")))
695
703
717
723
735
744
751
758
765
767 Element = self.Element
768 SubElement = self.etree.SubElement
769 root = Element("{objectified}root")
770 root.s = "test"
771
772 self.assertEquals("test" * 5, root.s * 5)
773 self.assertEquals(5 * "test", 5 * root.s)
774
775 self.assertRaises(TypeError, operator.mul, root.s, "honk")
776 self.assertRaises(TypeError, operator.mul, "honk", root.s)
777
787
808
817
822
827
832
839
846
853
855 Element = self.Element
856 SubElement = self.etree.SubElement
857 root = Element("{objectified}root")
858 root.s = u"test"
859
860 self.assertEquals(u"test" * 5, root.s * 5)
861 self.assertEquals(5 * u"test", 5 * root.s)
862
863 self.assertRaises(TypeError, operator.mul, root.s, u"honk")
864 self.assertRaises(TypeError, operator.mul, u"honk", root.s)
865
875
880
885
890
897
902
909
914
916 for xsi, objclass in xsitype2objclass.iteritems():
917
918 pyval = 1
919 value = objectify.DataElement(pyval, _xsi=xsi)
920 self.assert_(isinstance(value, objclass),
921 "DataElement(%s, _xsi='%s') returns %s, expected %s"
922 % (pyval, xsi, type(value), objclass))
923
925 for xsi, objclass in xsitype2objclass.iteritems():
926
927 pyval = 1
928 value = objectify.DataElement(pyval, _xsi="xsd:%s" % xsi)
929 self.assert_(isinstance(value, objclass),
930 "DataElement(%s, _xsi='%s') returns %s, expected %s"
931 % (pyval, xsi, type(value), objclass))
932
938
940 for pytype, objclass in pytype2objclass.iteritems():
941
942 pyval = 1
943 value = objectify.DataElement(pyval, _pytype=pytype)
944 self.assert_(isinstance(value, objclass),
945 "DataElement(%s, _pytype='%s') returns %s, expected %s"
946 % (pyval, pytype, type(value), objclass))
947
949 pyval = 1
950 pytype = "NoneType"
951 objclass = objectify.NoneElement
952 value = objectify.DataElement(pyval, _pytype=pytype)
953 self.assert_(isinstance(value, objclass),
954 "DataElement(%s, _pytype='%s') returns %s, expected %s"
955 % (pyval, pytype, type(value), objclass))
956 self.assertEquals(value.text, None)
957 self.assertEquals(value.pyval, None)
958
960
961 pyval = 1
962 pytype = "none"
963 objclass = objectify.NoneElement
964 value = objectify.DataElement(pyval, _pytype=pytype)
965 self.assert_(isinstance(value, objclass),
966 "DataElement(%s, _pytype='%s') returns %s, expected %s"
967 % (pyval, pytype, type(value), objclass))
968 self.assertEquals(value.text, None)
969 self.assertEquals(value.pyval, None)
970
976 root = Element("{objectified}root")
977 root.myfloat = MyFloat(5.5)
978 self.assert_(isinstance(root.myfloat, objectify.FloatElement))
979 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
980
982 class MyFloat(float):
983 pass
984 value = objectify.DataElement(MyFloat(5.5))
985 self.assert_(isinstance(value, objectify.FloatElement))
986 self.assertEquals(value, 5.5)
987 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None)
988
990 XML = self.XML
991 root = XML('''\
992 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
993 <b xsi:type="boolean">true</b>
994 <b xsi:type="boolean">false</b>
995 <b xsi:type="boolean">1</b>
996 <b xsi:type="boolean">0</b>
997
998 <f xsi:type="float">5</f>
999 <f xsi:type="double">5</f>
1000
1001 <s xsi:type="string">5</s>
1002 <s xsi:type="normalizedString">5</s>
1003 <s xsi:type="token">5</s>
1004 <s xsi:type="language">5</s>
1005 <s xsi:type="Name">5</s>
1006 <s xsi:type="NCName">5</s>
1007 <s xsi:type="ID">5</s>
1008 <s xsi:type="IDREF">5</s>
1009 <s xsi:type="ENTITY">5</s>
1010 <s xsi:type="NMTOKEN">5</s>
1011
1012 <l xsi:type="integer">5</l>
1013 <l xsi:type="nonPositiveInteger">5</l>
1014 <l xsi:type="negativeInteger">5</l>
1015 <l xsi:type="long">5</l>
1016 <l xsi:type="nonNegativeInteger">5</l>
1017 <l xsi:type="unsignedLong">5</l>
1018 <l xsi:type="unsignedInt">5</l>
1019 <l xsi:type="positiveInteger">5</l>
1020
1021 <i xsi:type="int">5</i>
1022 <i xsi:type="short">5</i>
1023 <i xsi:type="byte">5</i>
1024 <i xsi:type="unsignedShort">5</i>
1025 <i xsi:type="unsignedByte">5</i>
1026
1027 <n xsi:nil="true"/>
1028 </root>
1029 ''')
1030
1031 for b in root.b:
1032 self.assert_(isinstance(b, objectify.BoolElement))
1033 self.assertEquals(True, root.b[0])
1034 self.assertEquals(False, root.b[1])
1035 self.assertEquals(True, root.b[2])
1036 self.assertEquals(False, root.b[3])
1037
1038 for f in root.f:
1039 self.assert_(isinstance(f, objectify.FloatElement))
1040 self.assertEquals(5, f)
1041
1042 for s in root.s:
1043 self.assert_(isinstance(s, objectify.StringElement))
1044 self.assertEquals("5", s)
1045
1046 for l in root.l:
1047 self.assert_(isinstance(l, objectify.LongElement))
1048 self.assertEquals(5L, l)
1049
1050 for i in root.i:
1051 self.assert_(isinstance(i, objectify.IntElement))
1052 self.assertEquals(5, i)
1053
1054 self.assert_(isinstance(root.n, objectify.NoneElement))
1055 self.assertEquals(None, root.n)
1056
1058 XML = self.XML
1059 root = XML('''\
1060 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1061 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1062 <b xsi:type="xsd:boolean">true</b>
1063 <b xsi:type="xsd:boolean">false</b>
1064 <b xsi:type="xsd:boolean">1</b>
1065 <b xsi:type="xsd:boolean">0</b>
1066
1067 <f xsi:type="xsd:float">5</f>
1068 <f xsi:type="xsd:double">5</f>
1069
1070 <s xsi:type="xsd:string">5</s>
1071 <s xsi:type="xsd:normalizedString">5</s>
1072 <s xsi:type="xsd:token">5</s>
1073 <s xsi:type="xsd:language">5</s>
1074 <s xsi:type="xsd:Name">5</s>
1075 <s xsi:type="xsd:NCName">5</s>
1076 <s xsi:type="xsd:ID">5</s>
1077 <s xsi:type="xsd:IDREF">5</s>
1078 <s xsi:type="xsd:ENTITY">5</s>
1079 <s xsi:type="xsd:NMTOKEN">5</s>
1080
1081 <l xsi:type="xsd:integer">5</l>
1082 <l xsi:type="xsd:nonPositiveInteger">5</l>
1083 <l xsi:type="xsd:negativeInteger">5</l>
1084 <l xsi:type="xsd:long">5</l>
1085 <l xsi:type="xsd:nonNegativeInteger">5</l>
1086 <l xsi:type="xsd:unsignedLong">5</l>
1087 <l xsi:type="xsd:unsignedInt">5</l>
1088 <l xsi:type="xsd:positiveInteger">5</l>
1089
1090 <i xsi:type="xsd:int">5</i>
1091 <i xsi:type="xsd:short">5</i>
1092 <i xsi:type="xsd:byte">5</i>
1093 <i xsi:type="xsd:unsignedShort">5</i>
1094 <i xsi:type="xsd:unsignedByte">5</i>
1095
1096 <n xsi:nil="true"/>
1097 </root>
1098 ''')
1099
1100 for b in root.b:
1101 self.assert_(isinstance(b, objectify.BoolElement))
1102 self.assertEquals(True, root.b[0])
1103 self.assertEquals(False, root.b[1])
1104 self.assertEquals(True, root.b[2])
1105 self.assertEquals(False, root.b[3])
1106
1107 for f in root.f:
1108 self.assert_(isinstance(f, objectify.FloatElement))
1109 self.assertEquals(5, f)
1110
1111 for s in root.s:
1112 self.assert_(isinstance(s, objectify.StringElement))
1113 self.assertEquals("5", s)
1114
1115 for l in root.l:
1116 self.assert_(isinstance(l, objectify.LongElement))
1117 self.assertEquals(5L, l)
1118
1119 for i in root.i:
1120 self.assert_(isinstance(i, objectify.IntElement))
1121 self.assertEquals(5, i)
1122
1123 self.assert_(isinstance(root.n, objectify.NoneElement))
1124 self.assertEquals(None, root.n)
1125
1127 XML = self.XML
1128 root = XML(u'<root><b>why</b><b>try</b></root>')
1129 strs = [ str(s) for s in root.b ]
1130 self.assertEquals(["why", "try"],
1131 strs)
1132
1134 XML = self.XML
1135 root = XML(u'<root><b>test</b><b>taste</b><b></b><b/></root>')
1136 self.assertFalse(root.b[0] < root.b[1])
1137 self.assertFalse(root.b[0] <= root.b[1])
1138 self.assertFalse(root.b[0] == root.b[1])
1139
1140 self.assert_(root.b[0] != root.b[1])
1141 self.assert_(root.b[0] >= root.b[1])
1142 self.assert_(root.b[0] > root.b[1])
1143
1144 self.assertEquals(root.b[0], "test")
1145 self.assertEquals("test", root.b[0])
1146 self.assert_(root.b[0] > 5)
1147 self.assert_(5 < root.b[0])
1148
1149 self.assertEquals("", root.b[2])
1150 self.assertEquals(root.b[2], "")
1151 self.assertEquals("", root.b[3])
1152 self.assertEquals(root.b[3], "")
1153 self.assertEquals(root.b[2], root.b[3])
1154
1155 root.b = "test"
1156 self.assert_(root.b)
1157 root.b = ""
1158 self.assertFalse(root.b)
1159 self.assertEquals(root.b, "")
1160 self.assertEquals("", root.b)
1161
1182
1183
1184
1208
1210 XML = self.XML
1211 root = XML(u"""
1212 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1213 <b xsi:nil="true"></b><b xsi:nil="true"/>
1214 </root>""")
1215 self.assert_(root.b[0] == root.b[1])
1216 self.assertFalse(root.b[0])
1217 self.assertEquals(root.b[0], None)
1218 self.assertEquals(None, root.b[0])
1219
1220 for comparison in ["abc", 5, 7.3, True, [], ()]:
1221 none = root.b[1]
1222 self.assert_(none < comparison, "%s (%s) should be < %s" %
1223 (none, type(none), comparison) )
1224 self.assert_(comparison > none, "%s should be > %s (%s)" %
1225 (comparison, none, type(none)) )
1226
1232
1239
1243
1245 XML = self.XML
1246 root = XML(u'''\
1247 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1248 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1249 <b>5</b>
1250 <b>test</b>
1251 <c>1.1</c>
1252 <c>\uF8D2</c>
1253 <x>true</x>
1254 <n xsi:nil="true" />
1255 <n></n>
1256 <b xsi:type="double">5</b>
1257 <b xsi:type="float">5</b>
1258 <s xsi:type="string">23</s>
1259 <s py:pytype="str">42</s>
1260 <f py:pytype="float">300</f>
1261 <l py:pytype="long">2</l>
1262 <t py:pytype="TREE"></t>
1263 </a>
1264 ''')
1265 objectify.annotate(root)
1266
1267 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1268 for c in root.iterchildren() ]
1269 self.assertEquals("int", child_types[ 0])
1270 self.assertEquals("str", child_types[ 1])
1271 self.assertEquals("float", child_types[ 2])
1272 self.assertEquals("str", child_types[ 3])
1273 self.assertEquals("bool", child_types[ 4])
1274 self.assertEquals("NoneType", child_types[ 5])
1275 self.assertEquals(None, child_types[ 6])
1276 self.assertEquals("float", child_types[ 7])
1277 self.assertEquals("float", child_types[ 8])
1278 self.assertEquals("str", child_types[ 9])
1279 self.assertEquals("int", child_types[10])
1280 self.assertEquals("int", child_types[11])
1281 self.assertEquals("int", child_types[12])
1282 self.assertEquals(None, child_types[13])
1283
1284 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1285
1305
1307 XML = self.XML
1308 root = XML(u'''\
1309 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1310 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1311 <b>5</b>
1312 <b>test</b>
1313 <c>1.1</c>
1314 <c>\uF8D2</c>
1315 <x>true</x>
1316 <n xsi:nil="true" />
1317 <n></n>
1318 <b xsi:type="double">5</b>
1319 <b xsi:type="float">5</b>
1320 <s xsi:type="string">23</s>
1321 <s py:pytype="str">42</s>
1322 <f py:pytype="float">300</f>
1323 <l py:pytype="long">2</l>
1324 <t py:pytype="TREE"></t>
1325 </a>
1326 ''')
1327 objectify.annotate(root, ignore_old=False)
1328
1329 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1330 for c in root.iterchildren() ]
1331 self.assertEquals("int", child_types[ 0])
1332 self.assertEquals("str", child_types[ 1])
1333 self.assertEquals("float", child_types[ 2])
1334 self.assertEquals("str", child_types[ 3])
1335 self.assertEquals("bool", child_types[ 4])
1336 self.assertEquals("NoneType", child_types[ 5])
1337 self.assertEquals(None, child_types[ 6])
1338 self.assertEquals("float", child_types[ 7])
1339 self.assertEquals("float", child_types[ 8])
1340 self.assertEquals("str", child_types[ 9])
1341 self.assertEquals("str", child_types[10])
1342 self.assertEquals("float", child_types[11])
1343 self.assertEquals("long", child_types[12])
1344 self.assertEquals(TREE_PYTYPE, child_types[13])
1345
1346 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1347
1349 XML = self.XML
1350 root = XML(u'''\
1351 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1352 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1353 <b>5</b>
1354 <b>test</b>
1355 <c>1.1</c>
1356 <c>\uF8D2</c>
1357 <x>true</x>
1358 <n xsi:nil="true" />
1359 <n></n>
1360 <b xsi:type="double">5</b>
1361 <b xsi:type="float">5</b>
1362 <s xsi:type="string">23</s>
1363 <s py:pytype="str">42</s>
1364 <f py:pytype="float">300</f>
1365 <l py:pytype="long">2</l>
1366 <t py:pytype="TREE"></t>
1367 </a>
1368 ''')
1369 objectify.annotate(root, ignore_old=False, ignore_xsi=False,
1370 annotate_xsi=1, annotate_pytype=1)
1371
1372
1373 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1374 for c in root.iterchildren() ]
1375 self.assertEquals("int", child_types[ 0])
1376 self.assertEquals("str", child_types[ 1])
1377 self.assertEquals("float", child_types[ 2])
1378 self.assertEquals("str", child_types[ 3])
1379 self.assertEquals("bool", child_types[ 4])
1380 self.assertEquals("NoneType", child_types[ 5])
1381 self.assertEquals(None, child_types[ 6])
1382 self.assertEquals("float", child_types[ 7])
1383 self.assertEquals("float", child_types[ 8])
1384 self.assertEquals("str", child_types[ 9])
1385 self.assertEquals("str", child_types[10])
1386 self.assertEquals("float", child_types[11])
1387 self.assertEquals("long", child_types[12])
1388 self.assertEquals(TREE_PYTYPE, child_types[13])
1389
1390 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1391
1392 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1393 for c in root.iterchildren() ]
1394
1395
1396 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1397 for c in root.iterchildren() ]
1398 self.assertEquals("xsd:int", child_types[ 0])
1399 self.assertEquals("xsd:string", child_types[ 1])
1400 self.assertEquals("xsd:double", child_types[ 2])
1401 self.assertEquals("xsd:string", child_types[ 3])
1402 self.assertEquals("xsd:boolean", child_types[ 4])
1403 self.assertEquals(None, child_types[ 5])
1404 self.assertEquals(None, child_types[ 6])
1405 self.assertEquals("xsd:double", child_types[ 7])
1406 self.assertEquals("xsd:float", child_types[ 8])
1407 self.assertEquals("xsd:string", child_types[ 9])
1408 self.assertEquals("xsd:string", child_types[10])
1409 self.assertEquals("xsd:double", child_types[11])
1410 self.assertEquals("xsd:integer", child_types[12])
1411 self.assertEquals(None, child_types[13])
1412
1413 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1414
1416 XML = self.XML
1417 root = XML(u'''\
1418 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1419 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1420 <b>5</b>
1421 <b>test</b>
1422 <c>1.1</c>
1423 <c>\uF8D2</c>
1424 <x>true</x>
1425 <n xsi:nil="true" />
1426 <n></n>
1427 <b xsi:type="double">5</b>
1428 <b xsi:type="float">5</b>
1429 <s xsi:type="string">23</s>
1430 <s py:pytype="str">42</s>
1431 <f py:pytype="float">300</f>
1432 <l py:pytype="long">2</l>
1433 <t py:pytype="TREE"></t>
1434 </a>
1435 ''')
1436 objectify.xsiannotate(root, ignore_old=False)
1437
1438 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1439 for c in root.iterchildren() ]
1440 self.assertEquals("xsd:int", child_types[ 0])
1441 self.assertEquals("xsd:string", child_types[ 1])
1442 self.assertEquals("xsd:double", child_types[ 2])
1443 self.assertEquals("xsd:string", child_types[ 3])
1444 self.assertEquals("xsd:boolean", child_types[ 4])
1445 self.assertEquals(None, child_types[ 5])
1446 self.assertEquals(None, child_types[ 6])
1447 self.assertEquals("xsd:double", child_types[ 7])
1448 self.assertEquals("xsd:float", child_types[ 8])
1449 self.assertEquals("xsd:string", child_types[ 9])
1450 self.assertEquals("xsd:string", child_types[10])
1451 self.assertEquals("xsd:double", child_types[11])
1452 self.assertEquals("xsd:integer", child_types[12])
1453 self.assertEquals(None, child_types[13])
1454
1456 XML = self.XML
1457 root = XML(u'''\
1458 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1459 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1460 <b>5</b>
1461 <b>test</b>
1462 <c>1.1</c>
1463 <c>\uF8D2</c>
1464 <x>true</x>
1465 <n xsi:nil="true" />
1466 <n></n>
1467 <b xsi:type="double">5</b>
1468 <b xsi:type="float">5</b>
1469 <s xsi:type="string">23</s>
1470 <s py:pytype="str">42</s>
1471 <f py:pytype="float">300</f>
1472 <l py:pytype="long">2</l>
1473 <t py:pytype="TREE"></t>
1474 </a>
1475 ''')
1476 objectify.pyannotate(root, ignore_old=True)
1477
1478 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1479 for c in root.iterchildren() ]
1480 self.assertEquals("int", child_types[ 0])
1481 self.assertEquals("str", child_types[ 1])
1482 self.assertEquals("float", child_types[ 2])
1483 self.assertEquals("str", child_types[ 3])
1484 self.assertEquals("bool", child_types[ 4])
1485 self.assertEquals("NoneType", child_types[ 5])
1486 self.assertEquals(None, child_types[ 6])
1487 self.assertEquals("float", child_types[ 7])
1488 self.assertEquals("float", child_types[ 8])
1489 self.assertEquals("str", child_types[ 9])
1490 self.assertEquals("int", child_types[10])
1491 self.assertEquals("int", child_types[11])
1492 self.assertEquals("int", child_types[12])
1493 self.assertEquals(None, child_types[13])
1494
1495 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1496
1516
1518 XML = self.XML
1519 root = XML(u'''\
1520 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1521 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1522 <b>5</b>
1523 <b>test</b>
1524 <c>1.1</c>
1525 <c>\uF8D2</c>
1526 <x>true</x>
1527 <n xsi:nil="true" />
1528 <n></n>
1529 <b xsi:type="double">5</b>
1530 <b xsi:type="float">5</b>
1531 <s xsi:type="string">23</s>
1532 <s py:pytype="str">42</s>
1533 <f py:pytype="float">300</f>
1534 <l py:pytype="long">2</l>
1535 <t py:pytype="TREE"></t>
1536 </a>
1537 ''')
1538 objectify.pyannotate(root)
1539
1540 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1541 for c in root.iterchildren() ]
1542 self.assertEquals("int", child_types[ 0])
1543 self.assertEquals("str", child_types[ 1])
1544 self.assertEquals("float", child_types[ 2])
1545 self.assertEquals("str", child_types[ 3])
1546 self.assertEquals("bool", child_types[ 4])
1547 self.assertEquals("NoneType", child_types[ 5])
1548 self.assertEquals(None, child_types[ 6])
1549 self.assertEquals("float", child_types[ 7])
1550 self.assertEquals("float", child_types[ 8])
1551 self.assertEquals("str", child_types[ 9])
1552 self.assertEquals("str", child_types[10])
1553 self.assertEquals("float", child_types[11])
1554 self.assertEquals("long", child_types[12])
1555 self.assertEquals(TREE_PYTYPE, child_types[13])
1556
1557 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1558
1560 XML = self.XML
1561 root = XML(u'''\
1562 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1563 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1564 <b>5</b>
1565 <b>test</b>
1566 <c>1.1</c>
1567 <c>\uF8D2</c>
1568 <x>true</x>
1569 <n xsi:nil="true" />
1570 <n></n>
1571 <b xsi:type="double">5</b>
1572 <b xsi:type="float">5</b>
1573 <s xsi:type="string">23</s>
1574 <s py:pytype="str">42</s>
1575 <f py:pytype="float">300</f>
1576 <l py:pytype="long">2</l>
1577 <t py:pytype="TREE"></t>
1578 </a>
1579 ''')
1580 objectify.xsiannotate(root, ignore_old=True)
1581
1582 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1583 for c in root.iterchildren() ]
1584 self.assertEquals("xsd:int", child_types[ 0])
1585 self.assertEquals("xsd:string", child_types[ 1])
1586 self.assertEquals("xsd:double", child_types[ 2])
1587 self.assertEquals("xsd:string", child_types[ 3])
1588 self.assertEquals("xsd:boolean", child_types[ 4])
1589 self.assertEquals(None, child_types[ 5])
1590 self.assertEquals(None, child_types[ 6])
1591 self.assertEquals("xsd:int", child_types[ 7])
1592 self.assertEquals("xsd:int", child_types[ 8])
1593 self.assertEquals("xsd:int", child_types[ 9])
1594 self.assertEquals("xsd:string", child_types[10])
1595 self.assertEquals("xsd:double", child_types[11])
1596 self.assertEquals("xsd:integer", child_types[12])
1597 self.assertEquals(None, child_types[13])
1598
1599 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1600
1602 XML = self.XML
1603 root = XML(u'''\
1604 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1605 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1606 <b>5</b>
1607 <b>test</b>
1608 <c>1.1</c>
1609 <c>\uF8D2</c>
1610 <x>true</x>
1611 <n xsi:nil="true" />
1612 <n></n>
1613 <b xsi:type="double">5</b>
1614 <b xsi:type="float">5</b>
1615 <s xsi:type="string">23</s>
1616 <s py:pytype="str">42</s>
1617 <f py:pytype="float">300</f>
1618 <l py:pytype="long">2</l>
1619 <t py:pytype="TREE"></t>
1620 </a>
1621 ''')
1622 objectify.deannotate(root)
1623
1624 for c in root.getiterator():
1625 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1626 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1627
1628 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1629
1631 XML = self.XML
1632 root = XML(u'''\
1633 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1634 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1635 <b>5</b>
1636 <b>test</b>
1637 <c>1.1</c>
1638 <c>\uF8D2</c>
1639 <x>true</x>
1640 <n xsi:nil="true" />
1641 <n></n>
1642 <b xsi:type="double">5</b>
1643 <b xsi:type="float">5</b>
1644 <s xsi:type="string">23</s>
1645 <s py:pytype="str">42</s>
1646 <f py:pytype="float">300</f>
1647 <l py:pytype="long">2</l>
1648 <t py:pytype="TREE"></t>
1649 </a>
1650 ''')
1651 objectify.xsiannotate(root)
1652 objectify.deannotate(root, xsi=False)
1653
1654 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1655 for c in root.iterchildren() ]
1656 self.assertEquals("xsd:int", child_types[ 0])
1657 self.assertEquals("xsd:string", child_types[ 1])
1658 self.assertEquals("xsd:double", child_types[ 2])
1659 self.assertEquals("xsd:string", child_types[ 3])
1660 self.assertEquals("xsd:boolean", child_types[ 4])
1661 self.assertEquals(None, child_types[ 5])
1662 self.assertEquals(None, child_types[ 6])
1663 self.assertEquals("xsd:int", child_types[ 7])
1664 self.assertEquals("xsd:int", child_types[ 8])
1665 self.assertEquals("xsd:int", child_types[ 9])
1666 self.assertEquals("xsd:string", child_types[10])
1667 self.assertEquals("xsd:double", child_types[11])
1668 self.assertEquals("xsd:integer", child_types[12])
1669 self.assertEquals(None, child_types[13])
1670
1671 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1672
1673 for c in root.getiterator():
1674 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1675
1677 XML = self.XML
1678 root = XML(u'''\
1679 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1680 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1681 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1682 <b>5</b>
1683 <b>test</b>
1684 <c>1.1</c>
1685 <c>\uF8D2</c>
1686 <x>true</x>
1687 <n xsi:nil="true" />
1688 <n></n>
1689 <b xsi:type="xsd:double">5</b>
1690 <b xsi:type="xsd:float">5</b>
1691 <s xsi:type="xsd:string">23</s>
1692 <s py:pytype="str">42</s>
1693 <f py:pytype="float">300</f>
1694 <l py:pytype="long">2</l>
1695 <t py:pytype="TREE"></t>
1696 </a>
1697 ''')
1698 objectify.annotate(root)
1699 objectify.deannotate(root, pytype=False)
1700
1701 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1702 for c in root.iterchildren() ]
1703 self.assertEquals("int", child_types[ 0])
1704 self.assertEquals("str", child_types[ 1])
1705 self.assertEquals("float", child_types[ 2])
1706 self.assertEquals("str", child_types[ 3])
1707 self.assertEquals("bool", child_types[ 4])
1708 self.assertEquals("NoneType", child_types[ 5])
1709 self.assertEquals(None, child_types[ 6])
1710 self.assertEquals("float", child_types[ 7])
1711 self.assertEquals("float", child_types[ 8])
1712 self.assertEquals("str", child_types[ 9])
1713 self.assertEquals("int", child_types[10])
1714 self.assertEquals("int", child_types[11])
1715 self.assertEquals("int", child_types[12])
1716 self.assertEquals(None, child_types[13])
1717
1718 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1719
1720 for c in root.getiterator():
1721 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1722
1724 XML = self.XML
1725 root = XML(u'''\
1726 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1727 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1728 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1729 <b xsi:type="xsd:int">5</b>
1730 <b xsi:type="xsd:string">test</b>
1731 <c xsi:type="xsd:float">1.1</c>
1732 <c xsi:type="xsd:string">\uF8D2</c>
1733 <x xsi:type="xsd:boolean">true</x>
1734 <n xsi:nil="true" />
1735 <n></n>
1736 <b xsi:type="xsd:double">5</b>
1737 <b xsi:type="xsd:float">5</b>
1738 <s xsi:type="xsd:string">23</s>
1739 <s xsi:type="xsd:string">42</s>
1740 <f xsi:type="xsd:float">300</f>
1741 <l xsi:type="xsd:long">2</l>
1742 <t py:pytype="TREE"></t>
1743 </a>
1744 ''')
1745 objectify.annotate(root)
1746 objectify.deannotate(root, xsi=False)
1747
1748 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1749 for c in root.iterchildren() ]
1750 self.assertEquals("xsd:int", child_types[ 0])
1751 self.assertEquals("xsd:string", child_types[ 1])
1752 self.assertEquals("xsd:float", child_types[ 2])
1753 self.assertEquals("xsd:string", child_types[ 3])
1754 self.assertEquals("xsd:boolean", child_types[ 4])
1755 self.assertEquals(None, child_types[ 5])
1756 self.assertEquals(None, child_types[ 6])
1757 self.assertEquals("xsd:double", child_types[ 7])
1758 self.assertEquals("xsd:float", child_types[ 8])
1759 self.assertEquals("xsd:string", child_types[ 9])
1760 self.assertEquals("xsd:string", child_types[10])
1761 self.assertEquals("xsd:float", child_types[11])
1762 self.assertEquals("xsd:long", child_types[12])
1763 self.assertEquals(None, child_types[13])
1764
1765 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1766
1767 for c in root.getiterator():
1768 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1769
1771 XML = self.XML
1772
1773 xml = u'''\
1774 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1775 <b>5</b>
1776 <b>test</b>
1777 <c>1.1</c>
1778 <c>\uF8D2</c>
1779 <x>true</x>
1780 <n xsi:nil="true" />
1781 <n></n>
1782 <b xsi:type="double">5</b>
1783 </a>
1784 '''
1785
1786 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1787 objectify.set_pytype_attribute_tag("{TEST}test")
1788
1789 root = XML(xml)
1790 objectify.annotate(root)
1791
1792 attribs = root.xpath("//@py:%s" % pytype_name,
1793 namespaces={"py" : pytype_ns})
1794 self.assertEquals(0, len(attribs))
1795 attribs = root.xpath("//@py:test",
1796 namespaces={"py" : "TEST"})
1797 self.assertEquals(7, len(attribs))
1798
1799 objectify.set_pytype_attribute_tag()
1800 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1801
1802 self.assertNotEqual("test", pytype_ns.lower())
1803 self.assertNotEqual("test", pytype_name.lower())
1804
1805 root = XML(xml)
1806 attribs = root.xpath("//@py:%s" % pytype_name,
1807 namespaces={"py" : pytype_ns})
1808 self.assertEquals(0, len(attribs))
1809
1810 objectify.annotate(root)
1811 attribs = root.xpath("//@py:%s" % pytype_name,
1812 namespaces={"py" : pytype_ns})
1813 self.assertEquals(7, len(attribs))
1814
1824
1825 def checkMyType(s):
1826 return True
1827
1828 pytype = objectify.PyType("mytype", checkMyType, NewType)
1829 pytype.register()
1830 self.assert_(pytype in objectify.getRegisteredTypes())
1831 pytype.unregister()
1832
1833 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1834 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1835 pytype.unregister()
1836
1837 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1838 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1839 pytype.unregister()
1840
1841 self.assertRaises(ValueError, pytype.register,
1842 before = [objectify.getRegisteredTypes()[0].name],
1843 after = [objectify.getRegisteredTypes()[1].name])
1844
1845 finally:
1846 for pytype in objectify.getRegisteredTypes():
1847 pytype.unregister()
1848 for pytype in orig_types:
1849 pytype.register()
1850
1856
1862
1868
1876
1895
1900
1905
1910
1915
1935
1937 root = self.XML(xml_str)
1938 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
1939 self.assertEquals(root.c1.c2.text, path(root).text)
1940
1941 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
1942 self.assertEquals(root.c1.c2[2].text, path(root).text)
1943
1944 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
1945 self.assertEquals(root.c1.c2[2].text, path(root).text)
1946
1947 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
1948 self.assertEquals(root.c1.c2[-1].text, path(root).text)
1949
1950 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
1951 self.assertEquals(root.c1.c2[-3].text, path(root).text)
1952
1954 self.assertRaises(ValueError, objectify.ObjectPath,
1955 "root.c1[0].c2[-1-2]")
1956 self.assertRaises(ValueError, objectify.ObjectPath,
1957 ['root', 'c1[0]', 'c2[-1-2]'])
1958
1959 self.assertRaises(ValueError, objectify.ObjectPath,
1960 "root[2].c1.c2")
1961 self.assertRaises(ValueError, objectify.ObjectPath,
1962 ['root[2]', 'c1', 'c2'])
1963
1964 self.assertRaises(ValueError, objectify.ObjectPath,
1965 [])
1966 self.assertRaises(ValueError, objectify.ObjectPath,
1967 ['', '', ''])
1968
1970 root = self.XML(xml_str)
1971 path = objectify.ObjectPath("root.c1[9999].c2")
1972 self.assertRaises(AttributeError, path, root)
1973
1974 path = objectify.ObjectPath("root.c1[0].c2[9999]")
1975 self.assertRaises(AttributeError, path, root)
1976
1977 path = objectify.ObjectPath(".c1[9999].c2[0]")
1978 self.assertRaises(AttributeError, path, root)
1979
1980 path = objectify.ObjectPath("root.c1[-2].c2")
1981 self.assertRaises(AttributeError, path, root)
1982
1983 path = objectify.ObjectPath("root.c1[0].c2[-4]")
1984 self.assertRaises(AttributeError, path, root)
1985
1999
2001 root = self.XML(xml_str)
2002 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
2003 self.assertEquals(root.c1.c2.text, path.find(root).text)
2004 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
2005 self.assertEquals(root.c1.c2.text, path.find(root).text)
2006 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
2007 self.assertEquals(root.c1.c2.text, path.find(root).text)
2008 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
2009 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2010 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
2011 self.assertEquals(root.c1.c2.text, path.find(root).text)
2012 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
2013 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2014 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
2015 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2016 path.find(root).text)
2017
2030
2045
2057
2071
2073 root = self.XML(xml_str)
2074 path = objectify.ObjectPath( "root.c1.c99" )
2075 self.assertRaises(AttributeError, path.find, root)
2076
2077 new_el = self.Element("{objectified}test")
2078 new_el.a = ["TEST1", "TEST2"]
2079 new_el.a[0].set("myattr", "ATTR1")
2080 new_el.a[1].set("myattr", "ATTR2")
2081
2082 path.setattr(root, list(new_el.a))
2083
2084 self.assertEquals(2, len(root.c1.c99))
2085 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
2086 self.assertEquals("TEST1", root.c1.c99[0].text)
2087 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
2088 self.assertEquals("TEST2", root.c1.c99[1].text)
2089 self.assertEquals("TEST1", path(root).text)
2090
2099
2113
2125
2139
2154
2156 root = self.XML(xml_str)
2157 self.assertEquals(
2158 ['{objectified}root', '{objectified}root.c1',