lxml.tests.test_objectify
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>4</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 . ObjectifiedDataElement ( 'test' , 'toast' )
222 self . assertEqual ( value . text , 'testtoast' )
223
228
230
231 value = objectify . DataElement ( 23 , _pytype = "str" , _xsi = "foobar" ,
232 attrib = { "gnu" : "muh" , "cat" : "meeow" ,
233 "dog" : "wuff" } ,
234 bird = "tchilp" , dog = "grrr" )
235 self . assertEqual ( value . get ( "gnu" ) , "muh" )
236 self . assertEqual ( value . get ( "cat" ) , "meeow" )
237 self . assertEqual ( value . get ( "dog" ) , "grrr" )
238 self . assertEqual ( value . get ( "bird" ) , "tchilp" )
239
251
253
254
255 arg = objectify . DataElement ( 23 , _pytype = "str" , _xsi = "foobar" ,
256 attrib = { "gnu" : "muh" , "cat" : "meeow" ,
257 "dog" : "wuff" } ,
258 bird = "tchilp" , dog = "grrr" )
259 value = objectify . DataElement ( arg , _pytype = "NoneType" )
260 self . assertTrue ( isinstance ( value , objectify . NoneElement ) )
261 self . assertEqual ( value . get ( XML_SCHEMA_NIL_ATTR ) , "true" )
262 self . assertEqual ( value . text , None )
263 self . assertEqual ( value . pyval , None )
264 for attr in arg . attrib :
265
266 self . assertEqual ( value . get ( attr ) , arg . get ( attr ) )
267
269
270
271 arg = objectify . DataElement ( 23 , _pytype = "str" , _xsi = "foobar" ,
272 attrib = { "gnu" : "muh" , "cat" : "meeow" ,
273 "dog" : "wuff" } ,
274 bird = "tchilp" , dog = "grrr" )
275 value = objectify . DataElement ( arg , _pytype = "int" )
276 self . assertTrue ( isinstance ( value , objectify . IntElement ) )
277 self . assertEqual ( value . get ( objectify . PYTYPE_ATTRIBUTE ) , "int" )
278 for attr in arg . attrib :
279 if not attr == objectify . PYTYPE_ATTRIBUTE :
280 self . assertEqual ( value . get ( attr ) , arg . get ( attr ) )
281
283
284
285 arg = objectify . DataElement ( 23 , _pytype = "str" , _xsi = "foobar" ,
286 attrib = { "gnu" : "muh" , "cat" : "meeow" ,
287 "dog" : "wuff" } ,
288 bird = "tchilp" , dog = "grrr" )
289 value = objectify . DataElement ( arg , _xsi = "xsd:int" )
290 self . assertTrue ( isinstance ( value , objectify . IntElement ) )
291 self . assertEqual ( value . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR ) , "xsd:int" )
292 self . assertEqual ( value . get ( objectify . PYTYPE_ATTRIBUTE ) , "int" )
293 for attr in arg . attrib :
294 if not attr in [ objectify . PYTYPE_ATTRIBUTE ,
295 XML_SCHEMA_INSTANCE_TYPE_ATTR ] :
296 self . assertEqual ( value . get ( attr ) , arg . get ( attr ) )
297
299
300
301 arg = objectify . DataElement ( 23 , _pytype = "str" , _xsi = "foobar" ,
302 attrib = { "gnu" : "muh" , "cat" : "meeow" ,
303 "dog" : "wuff" } ,
304 bird = "tchilp" , dog = "grrr" )
305 value = objectify . DataElement ( arg , _pytype = "int" , _xsi = "xsd:int" )
306 self . assertTrue ( isinstance ( value , objectify . IntElement ) )
307 self . assertEqual ( value . get ( objectify . PYTYPE_ATTRIBUTE ) , "int" )
308 self . assertEqual ( value . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR ) , "xsd:int" )
309 for attr in arg . attrib :
310 if not attr in [ objectify . PYTYPE_ATTRIBUTE ,
311 XML_SCHEMA_INSTANCE_TYPE_ATTR ] :
312 self . assertEqual ( value . get ( attr ) , arg . get ( attr ) )
313
317
321
326
331
338
342
346
350
352 root = self . XML ( """
353 <root>
354 <foo:x xmlns:foo="/foo/bar">1</foo:x>
355 <x>2</x>
356 </root>
357 """ )
358 self . assertEqual ( 2 , root . x )
359
364
366 root = self . XML ( xml_str )
367 self . assertEqual ( "0" , getattr ( root . c1 , "{objectified}c2" ) . text )
368 self . assertEqual ( "3" , getattr ( root . c1 , "{otherNS}c2" ) . text )
369
371 root = self . XML ( xml_str )
372 self . assertRaises ( AttributeError , getattr , root . c1 , "NOT_THERE" )
373 self . assertRaises ( AttributeError , getattr , root . c1 , "{unknownNS}c2" )
374
379
381 for val in [
382 2 , 2 ** 32 , 1.2 , "Won't get fooled again" ,
383 _str ( "W\xf6n't get f\xf6\xf6led \xe4g\xe4in" , 'ISO-8859-1' ) , True ,
384 False , None ] :
385 root = self . Element ( 'root' )
386 attrname = 'val'
387 setattr ( root , attrname , val )
388 result = getattr ( root , attrname )
389 self . assertEqual ( val , result )
390 self . assertEqual ( type ( val ) , type ( result . pyval ) )
391
398
405
407 root = self . XML ( xml_str )
408 self . assertEqual ( 1 , len ( root . c1 ) )
409
410 new_el = self . Element ( "test" , myattr = "5" )
411 root . addattr ( "c1" , new_el )
412 self . assertEqual ( 2 , len ( root . c1 ) )
413 self . assertEqual ( None , root . c1 [ 0 ] . get ( "myattr" ) )
414 self . assertEqual ( "5" , root . c1 [ 1 ] . get ( "myattr" ) )
415
417 root = self . XML ( xml_str )
418 self . assertEqual ( 1 , len ( root . c1 ) )
419
420 new_el = self . Element ( "test" )
421 self . etree . SubElement ( new_el , "a" , myattr = "A" )
422 self . etree . SubElement ( new_el , "a" , myattr = "B" )
423
424 root . addattr ( "c1" , list ( new_el . a ) )
425 self . assertEqual ( 3 , len ( root . c1 ) )
426 self . assertEqual ( None , root . c1 [ 0 ] . get ( "myattr" ) )
427 self . assertEqual ( "A" , root . c1 [ 1 ] . get ( "myattr" ) )
428 self . assertEqual ( "B" , root . c1 [ 2 ] . get ( "myattr" ) )
429
436
438 root = self . XML ( xml_str )
439 self . assertEqual ( "0" , root . c1 . c2 [ 0 ] . text )
440 self . assertEqual ( "1" , root . c1 . c2 [ 1 ] . text )
441 self . assertEqual ( "2" , root . c1 . c2 [ 2 ] . text )
442 self . assertRaises ( IndexError , operator . getitem , root . c1 . c2 , 3 )
443
445 root = self . XML ( xml_str )
446 self . assertEqual ( "0" , root . c1 . c2 [ 0 ] . text )
447 self . assertEqual ( "0" , root . c1 . c2 [ - 3 ] . text )
448 self . assertEqual ( "1" , root . c1 . c2 [ - 2 ] . text )
449 self . assertEqual ( "2" , root . c1 . c2 [ - 1 ] . text )
450 self . assertRaises ( IndexError , operator . getitem , root . c1 . c2 , - 4 )
451
453 root = self . XML ( xml_str )
454 self . assertEqual ( 1 , len ( root ) )
455 self . assertEqual ( 1 , len ( root . c1 ) )
456 self . assertEqual ( 3 , len ( root . c1 . c2 ) )
457
466
472
482
487
492
493
494
496 root = self . XML ( "<root><c>c1</c><c>c2</c></root>" )
497 self . assertEqual ( [ "c1" , "c2" ] ,
498 [ c . text for c in root . c [ : ] ] )
499
501 root = self . XML ( "<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>" )
502 test_list = [ "c1" , "c2" , "c3" , "c4" ]
503
504 self . assertEqual ( test_list ,
505 [ c . text for c in root . c [ : ] ] )
506 self . assertEqual ( test_list [ 1 : 2 ] ,
507 [ c . text for c in root . c [ 1 : 2 ] ] )
508 self . assertEqual ( test_list [ - 3 : - 1 ] ,
509 [ c . text for c in root . c [ - 3 : - 1 ] ] )
510 self . assertEqual ( test_list [ - 3 : 3 ] ,
511 [ c . text for c in root . c [ - 3 : 3 ] ] )
512 self . assertEqual ( test_list [ - 3000 : 3 ] ,
513 [ c . text for c in root . c [ - 3000 : 3 ] ] )
514 self . assertEqual ( test_list [ - 3 : 3000 ] ,
515 [ c . text for c in root . c [ - 3 : 3000 ] ] )
516
518 root = self . XML ( "<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>" )
519 test_list = [ "c1" , "c2" , "c3" , "c4" ]
520
521 self . assertEqual ( test_list ,
522 [ c . text for c in root . c [ : ] ] )
523 self . assertEqual ( test_list [ 2 : 1 : - 1 ] ,
524 [ c . text for c in root . c [ 2 : 1 : - 1 ] ] )
525 self . assertEqual ( test_list [ - 1 : - 3 : - 1 ] ,
526 [ c . text for c in root . c [ - 1 : - 3 : - 1 ] ] )
527 self . assertEqual ( test_list [ 2 : - 3 : - 1 ] ,
528 [ c . text for c in root . c [ 2 : - 3 : - 1 ] ] )
529 self . assertEqual ( test_list [ 2 : - 3000 : - 1 ] ,
530 [ c . text for c in root . c [ 2 : - 3000 : - 1 ] ] )
531
532
533
545
547 Element = self . Element
548 root = Element ( "root" )
549 root . c = [ "c1" , "c2" ]
550
551 c1 = root . c [ 0 ]
552 c2 = root . c [ 1 ]
553
554 self . assertEqual ( [ c1 , c2 ] , list ( root . c ) )
555 self . assertEqual ( [ "c1" , "c2" ] ,
556 [ c . text for c in root . c ] )
557
558 root2 = Element ( "root2" )
559 root2 . el = [ "test" , "test" ]
560 self . assertEqual ( [ "test" , "test" ] ,
561 [ el . text for el in root2 . el ] )
562
563 root . c = [ root2 . el , root2 . el ]
564 self . assertEqual ( [ "test" , "test" ] ,
565 [ c . text for c in root . c ] )
566 self . assertEqual ( [ "test" , "test" ] ,
567 [ el . text for el in root2 . el ] )
568
569 root . c [ : ] = [ c1 , c2 , c2 , c1 ]
570 self . assertEqual ( [ "c1" , "c2" , "c2" , "c1" ] ,
571 [ c . text for c in root . c ] )
572
574 Element = self . Element
575 root = Element ( "root" )
576 l = [ "c1" , "c2" , "c3" , "c4" ]
577 root . c = l
578
579 self . assertEqual ( [ "c1" , "c2" , "c3" , "c4" ] ,
580 [ c . text for c in root . c ] )
581 self . assertEqual ( l ,
582 [ c . text for c in root . c ] )
583
584 new_slice = [ "cA" , "cB" ]
585 l [ 1 : 2 ] = new_slice
586 root . c [ 1 : 2 ] = new_slice
587
588 self . assertEqual ( [ "c1" , "cA" , "cB" , "c3" , "c4" ] , l )
589 self . assertEqual ( [ "c1" , "cA" , "cB" , "c3" , "c4" ] ,
590 [ c . text for c in root . c ] )
591 self . assertEqual ( l ,
592 [ c . text for c in root . c ] )
593
595 Element = self . Element
596 root = Element ( "root" )
597 l = [ "c1" , "c2" , "c3" , "c4" ]
598 root . c = l
599
600 self . assertEqual ( [ "c1" , "c2" , "c3" , "c4" ] ,
601 [ c . text for c in root . c ] )
602 self . assertEqual ( l ,
603 [ c . text for c in root . c ] )
604
605 new_slice = [ "cA" , "cB" ]
606 l [ 1 : 1 ] = new_slice
607 root . c [ 1 : 1 ] = new_slice
608
609 self . assertEqual ( [ "c1" , "cA" , "cB" , "c2" , "c3" , "c4" ] , l )
610 self . assertEqual ( [ "c1" , "cA" , "cB" , "c2" , "c3" , "c4" ] ,
611 [ c . text for c in root . c ] )
612 self . assertEqual ( l ,
613 [ c . text for c in root . c ] )
614
616 Element = self . Element
617 root = Element ( "root" )
618 l = [ "c1" , "c2" , "c3" , "c4" ]
619 root . c = l
620
621 self . assertEqual ( [ "c1" , "c2" , "c3" , "c4" ] ,
622 [ c . text for c in root . c ] )
623 self . assertEqual ( l ,
624 [ c . text for c in root . c ] )
625
626 new_slice = [ "cA" , "cB" ]
627 l [ - 2 : - 2 ] = new_slice
628 root . c [ - 2 : - 2 ] = new_slice
629
630 self . assertEqual ( [ "c1" , "c2" , "cA" , "cB" , "c3" , "c4" ] , l )
631 self . assertEqual ( [ "c1" , "c2" , "cA" , "cB" , "c3" , "c4" ] ,
632 [ c . text for c in root . c ] )
633 self . assertEqual ( l ,
634 [ c . text for c in root . c ] )
635
643
645 Element = self . Element
646 root = Element ( "root" )
647 l = [ "c1" , "c2" , "c3" , "c4" ]
648 root . c = l
649
650 self . assertEqual ( [ "c1" , "c2" , "c3" , "c4" ] ,
651 [ c . text for c in root . c ] )
652 self . assertEqual ( l ,
653 [ c . text for c in root . c ] )
654
655 new_slice = [ "cA" , "cB" , "cC" ]
656 self . assertRaises (
657 ValueError , operator . setitem ,
658 l , slice ( 1 , 2 , - 1 ) , new_slice )
659 self . assertRaises (
660 ValueError , operator . setitem ,
661 root . c , slice ( 1 , 2 , - 1 ) , new_slice )
662
664 Element = self . Element
665 root = Element ( "root" )
666 l = [ "c1" , "c2" , "c3" , "c4" ]
667 root . c = l
668
669 self . assertEqual ( [ "c1" , "c2" , "c3" , "c4" ] ,
670 [ c . text for c in root . c ] )
671 self . assertEqual ( l ,
672 [ c . text for c in root . c ] )
673
674 new_slice = [ "cA" , "cB" ]
675 l [ - 1 : 1 : - 1 ] = new_slice
676 root . c [ - 1 : 1 : - 1 ] = new_slice
677
678 self . assertEqual ( [ "c1" , "c2" , "cB" , "cA" ] , l )
679 self . assertEqual ( [ "c1" , "c2" , "cB" , "cA" ] ,
680 [ c . text for c in root . c ] )
681 self . assertEqual ( l ,
682 [ c . text for c in root . c ] )
683
685 Element = self . Element
686 root = Element ( "root" )
687 l = [ "c1" , "c2" , "c3" , "c4" ]
688 root . c = l
689
690 self . assertEqual ( [ "c1" , "c2" , "c3" , "c4" ] ,
691 [ c . text for c in root . c ] )
692 self . assertEqual ( l ,
693 [ c . text for c in root . c ] )
694
695 new_slice = [ "cA" , "cB" ]
696 l [ - 1 : - 4 : - 2 ] = new_slice
697 root . c [ - 1 : - 4 : - 2 ] = new_slice
698
699 self . assertEqual ( [ "c1" , "cB" , "c3" , "cA" ] , l )
700 self . assertEqual ( [ "c1" , "cB" , "c3" , "cA" ] ,
701 [ c . text for c in root . c ] )
702 self . assertEqual ( l ,
703 [ c . text for c in root . c ] )
704
705
706
714
722
724
725 Element = self . Element
726 root = Element ( "root" )
727
728 root [ "text" ] = "TEST"
729 self . assertEqual ( [ "TEST" ] ,
730 [ c . text for c in root [ "text" ] ] )
731
732 root [ "tail" ] = "TEST"
733 self . assertEqual ( [ "TEST" ] ,
734 [ c . text for c in root [ "tail" ] ] )
735
736 root [ "pyval" ] = "TEST"
737 self . assertEqual ( [ "TEST" ] ,
738 [ c . text for c in root [ "pyval" ] ] )
739
740 root [ "tag" ] = "TEST"
741 self . assertEqual ( [ "TEST" ] ,
742 [ c . text for c in root [ "tag" ] ] )
743
751
753 XML = self . XML
754 root = XML ( '<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>' )
755 self . assertEqual ( 2 , len ( root . findall ( ".//{X}b" ) ) )
756 self . assertEqual ( 3 , len ( root . findall ( ".//b" ) ) )
757 self . assertEqual ( 2 , len ( root . findall ( "b" ) ) )
758
766
781
787
789 Element = self . Element
790 SubElement = self . etree . SubElement
791 root = Element ( "{objectified}root" )
792 root . bool = True
793 self . assertEqual ( root . bool , True )
794 self . assertEqual ( root . bool + root . bool , True + True )
795 self . assertEqual ( True + root . bool , True + root . bool )
796 self . assertEqual ( root . bool * root . bool , True * True )
797 self . assertEqual ( int ( root . bool ) , int ( True ) )
798 self . assertEqual ( hash ( root . bool ) , hash ( True ) )
799 self . assertEqual ( complex ( root . bool ) , complex ( True ) )
800 self . assertTrue ( isinstance ( root . bool , objectify . BoolElement ) )
801
802 root . bool = False
803 self . assertEqual ( root . bool , False )
804 self . assertEqual ( root . bool + root . bool , False + False )
805 self . assertEqual ( False + root . bool , False + root . bool )
806 self . assertEqual ( root . bool * root . bool , False * False )
807 self . assertEqual ( int ( root . bool ) , int ( False ) )
808 self . assertEqual ( hash ( root . bool ) , hash ( False ) )
809 self . assertEqual ( complex ( root . bool ) , complex ( False ) )
810 self . assertTrue ( isinstance ( root . bool , objectify . BoolElement ) )
811
820
827
834
841
853
863
884
889
894
899
904
913
918
923
928
935
942
949
961
971
976
981
986
993
998
1002
1009
1014
1018
1032
1037
1049
1056
1062
1071
1080
1086
1095
1097 pyval = 1
1098 pytype = "NoneType"
1099 objclass = objectify . NoneElement
1100 value = objectify . DataElement ( pyval , _pytype = pytype )
1101 self . assertTrue ( isinstance ( value , objclass ) ,
1102 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1103 % ( pyval , pytype , type ( value ) , objclass ) )
1104 self . assertEqual ( value . text , None )
1105 self . assertEqual ( value . pyval , None )
1106
1108
1109 pyval = 1
1110 pytype = "none"
1111 objclass = objectify . NoneElement
1112 value = objectify . DataElement ( pyval , _pytype = pytype )
1113 self . assertTrue ( isinstance ( value , objclass ) ,
1114 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1115 % ( pyval , pytype , type ( value ) , objclass ) )
1116 self . assertEqual ( value . text , None )
1117 self . assertEqual ( value . pyval , None )
1118
1124 root = Element ( "{objectified}root" )
1125 root . myfloat = MyFloat ( 5.5 )
1126 self . assertTrue ( isinstance ( root . myfloat , objectify . FloatElement ) )
1127 self . assertEqual ( root . myfloat . get ( objectify . PYTYPE_ATTRIBUTE ) , None )
1128
1130 class MyFloat ( float ) :
1131 pass
1132 value = objectify . DataElement ( MyFloat ( 5.5 ) )
1133 self . assertTrue ( isinstance ( value , objectify . FloatElement ) )
1134 self . assertEqual ( value , 5.5 )
1135 self . assertEqual ( value . get ( objectify . PYTYPE_ATTRIBUTE ) , None )
1136
1138 XML = self . XML
1139 root = XML ( '''\
1140 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1141 <b xsi:type="boolean">true</b>
1142 <b xsi:type="boolean">false</b>
1143 <b xsi:type="boolean">1</b>
1144 <b xsi:type="boolean">0</b>
1145
1146 <f xsi:type="float">5</f>
1147 <f xsi:type="double">5</f>
1148
1149 <s xsi:type="string">5</s>
1150 <s xsi:type="normalizedString">5</s>
1151 <s xsi:type="token">5</s>
1152 <s xsi:type="language">5</s>
1153 <s xsi:type="Name">5</s>
1154 <s xsi:type="NCName">5</s>
1155 <s xsi:type="ID">5</s>
1156 <s xsi:type="IDREF">5</s>
1157 <s xsi:type="ENTITY">5</s>
1158 <s xsi:type="NMTOKEN">5</s>
1159
1160 <l xsi:type="integer">5</l>
1161 <l xsi:type="nonPositiveInteger">5</l>
1162 <l xsi:type="negativeInteger">5</l>
1163 <l xsi:type="long">5</l>
1164 <l xsi:type="nonNegativeInteger">5</l>
1165 <l xsi:type="unsignedLong">5</l>
1166 <l xsi:type="unsignedInt">5</l>
1167 <l xsi:type="positiveInteger">5</l>
1168
1169 <i xsi:type="int">5</i>
1170 <i xsi:type="short">5</i>
1171 <i xsi:type="byte">5</i>
1172 <i xsi:type="unsignedShort">5</i>
1173 <i xsi:type="unsignedByte">5</i>
1174
1175 <n xsi:nil="true"/>
1176 </root>
1177 ''' )
1178
1179 for b in root . b :
1180 self . assertTrue ( isinstance ( b , objectify . BoolElement ) )
1181 self . assertEqual ( True , root . b [ 0 ] )
1182 self . assertEqual ( False , root . b [ 1 ] )
1183 self . assertEqual ( True , root . b [ 2 ] )
1184 self . assertEqual ( False , root . b [ 3 ] )
1185
1186 for f in root . f :
1187 self . assertTrue ( isinstance ( f , objectify . FloatElement ) )
1188 self . assertEqual ( 5 , f )
1189
1190 for s in root . s :
1191 self . assertTrue ( isinstance ( s , objectify . StringElement ) )
1192 self . assertEqual ( "5" , s )
1193
1194 for i in root . i :
1195 self . assertTrue ( isinstance ( i , objectify . IntElement ) )
1196 self . assertEqual ( 5 , i )
1197
1198 for l in root . l :
1199 self . assertTrue ( isinstance ( l , objectify . IntElement ) )
1200 self . assertEqual ( 5 , i )
1201
1202 self . assertTrue ( isinstance ( root . n , objectify . NoneElement ) )
1203 self . assertEqual ( None , root . n )
1204
1206 XML = self . XML
1207 root = XML ( '''\
1208 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1209 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1210 <b xsi:type="xsd:boolean">true</b>
1211 <b xsi:type="xsd:boolean">false</b>
1212 <b xsi:type="xsd:boolean">1</b>
1213 <b xsi:type="xsd:boolean">0</b>
1214
1215 <f xsi:type="xsd:float">5</f>
1216 <f xsi:type="xsd:double">5</f>
1217
1218 <s xsi:type="xsd:string">5</s>
1219 <s xsi:type="xsd:normalizedString">5</s>
1220 <s xsi:type="xsd:token">5</s>
1221 <s xsi:type="xsd:language">5</s>
1222 <s xsi:type="xsd:Name">5</s>
1223 <s xsi:type="xsd:NCName">5</s>
1224 <s xsi:type="xsd:ID">5</s>
1225 <s xsi:type="xsd:IDREF">5</s>
1226 <s xsi:type="xsd:ENTITY">5</s>
1227 <s xsi:type="xsd:NMTOKEN">5</s>
1228
1229 <l xsi:type="xsd:integer">5</l>
1230 <l xsi:type="xsd:nonPositiveInteger">5</l>
1231 <l xsi:type="xsd:negativeInteger">5</l>
1232 <l xsi:type="xsd:long">5</l>
1233 <l xsi:type="xsd:nonNegativeInteger">5</l>
1234 <l xsi:type="xsd:unsignedLong">5</l>
1235 <l xsi:type="xsd:unsignedInt">5</l>
1236 <l xsi:type="xsd:positiveInteger">5</l>
1237
1238 <i xsi:type="xsd:int">5</i>
1239 <i xsi:type="xsd:short">5</i>
1240 <i xsi:type="xsd:byte">5</i>
1241 <i xsi:type="xsd:unsignedShort">5</i>
1242 <i xsi:type="xsd:unsignedByte">5</i>
1243
1244 <n xsi:nil="true"/>
1245 </root>
1246 ''' )
1247
1248 for b in root . b :
1249 self . assertTrue ( isinstance ( b , objectify . BoolElement ) )
1250 self . assertEqual ( True , root . b [ 0 ] )
1251 self . assertEqual ( False , root . b [ 1 ] )
1252 self . assertEqual ( True , root . b [ 2 ] )
1253 self . assertEqual ( False , root . b [ 3 ] )
1254
1255 for f in root . f :
1256 self . assertTrue ( isinstance ( f , objectify . FloatElement ) )
1257 self . assertEqual ( 5 , f )
1258
1259 for s in root . s :
1260 self . assertTrue ( isinstance ( s , objectify . StringElement ) )
1261 self . assertEqual ( "5" , s )
1262
1263 for i in root . i :
1264 self . assertTrue ( isinstance ( i , objectify . IntElement ) )
1265 self . assertEqual ( 5 , i )
1266
1267 for l in root . l :
1268 self . assertTrue ( isinstance ( l , objectify . IntElement ) )
1269 self . assertEqual ( 5 , l )
1270
1271 self . assertTrue ( isinstance ( root . n , objectify . NoneElement ) )
1272 self . assertEqual ( None , root . n )
1273
1275 XML = self . XML
1276 root = XML ( _bytes ( '<root><b>why</b><b>try</b></root>' ) )
1277 strs = [ str ( s ) for s in root . b ]
1278 self . assertEqual ( [ "why" , "try" ] ,
1279 strs )
1280
1282 XML = self . XML
1283 root = XML ( _bytes ( '<root><b>test</b><b>taste</b><b></b><b/></root>' ) )
1284 self . assertFalse ( root . b [ 0 ] < root . b [ 1 ] )
1285 self . assertFalse ( root . b [ 0 ] <= root . b [ 1 ] )
1286 self . assertFalse ( root . b [ 0 ] == root . b [ 1 ] )
1287
1288 self . assertTrue ( root . b [ 0 ] != root . b [ 1 ] )
1289 self . assertTrue ( root . b [ 0 ] >= root . b [ 1 ] )
1290 self . assertTrue ( root . b [ 0 ] > root . b [ 1 ] )
1291
1292 self . assertEqual ( root . b [ 0 ] , "test" )
1293 self . assertEqual ( "test" , root . b [ 0 ] )
1294
1295 self . assertEqual ( "" , root . b [ 2 ] )
1296 self . assertEqual ( root . b [ 2 ] , "" )
1297 self . assertEqual ( "" , root . b [ 3 ] )
1298 self . assertEqual ( root . b [ 3 ] , "" )
1299 self . assertEqual ( root . b [ 2 ] , root . b [ 3 ] )
1300
1301 root . b = "test"
1302 self . assertTrue ( root . b )
1303 root . b = ""
1304 self . assertFalse ( root . b )
1305 self . assertEqual ( root . b , "" )
1306 self . assertEqual ( "" , root . b )
1307
1309 XML = self . XML
1310 root = XML ( _bytes ( '<root><b>5</b><b>6</b></root>' ) )
1311 self . assertTrue ( root . b [ 0 ] < root . b [ 1 ] )
1312 self . assertTrue ( root . b [ 0 ] <= root . b [ 1 ] )
1313 self . assertTrue ( root . b [ 0 ] != root . b [ 1 ] )
1314
1315 self . assertFalse ( root . b [ 0 ] == root . b [ 1 ] )
1316 self . assertFalse ( root . b [ 0 ] >= root . b [ 1 ] )
1317 self . assertFalse ( root . b [ 0 ] > root . b [ 1 ] )
1318
1319 self . assertEqual ( root . b [ 0 ] , 5 )
1320 self . assertEqual ( 5 , root . b [ 0 ] )
1321 self . assertNotEqual ( root . b [ 0 ] , "5" )
1322
1323 root . b = 5
1324 self . assertTrue ( root . b )
1325 root . b = 0
1326 self . assertFalse ( root . b )
1327
1328
1329
1331 XML = self . XML
1332 root = XML ( _bytes ( '<root><b>false</b><b>true</b></root>' ) )
1333 self . assertTrue ( root . b [ 0 ] < root . b [ 1 ] )
1334 self . assertTrue ( root . b [ 0 ] <= root . b [ 1 ] )
1335 self . assertTrue ( root . b [ 0 ] != root . b [ 1 ] )
1336
1337 self . assertFalse ( root . b [ 0 ] == root . b [ 1 ] )
1338 self . assertFalse ( root . b [ 0 ] >= root . b [ 1 ] )
1339 self . assertFalse ( root . b [ 0 ] > root . b [ 1 ] )
1340
1341 self . assertFalse ( root . b [ 0 ] )
1342 self . assertTrue ( root . b [ 1 ] )
1343
1344 self . assertEqual ( root . b [ 0 ] , False )
1345 self . assertEqual ( False , root . b [ 0 ] )
1346 self . assertTrue ( root . b [ 0 ] < 5 )
1347 self . assertTrue ( 5 > root . b [ 0 ] )
1348
1349 root . b = True
1350 self . assertTrue ( root . b )
1351 root . b = False
1352 self . assertFalse ( root . b )
1353
1355 XML = self . XML
1356 root = XML ( _bytes ( """
1357 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1358 <b xsi:nil="true"></b><b xsi:nil="true"/>
1359 </root>""" ) )
1360 self . assertTrue ( root . b [ 0 ] == root . b [ 1 ] )
1361 self . assertFalse ( root . b [ 0 ] )
1362 self . assertEqual ( root . b [ 0 ] , None )
1363 self . assertEqual ( None , root . b [ 0 ] )
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1379
1386
1390
1392 XML = self . XML
1393 root = XML ( _bytes ( '''\
1394 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1395 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1396 <b>5</b>
1397 <b>test</b>
1398 <c>1.1</c>
1399 <c>\uF8D2</c>
1400 <x>true</x>
1401 <n xsi:nil="true" />
1402 <n></n>
1403 <b xsi:type="double">5</b>
1404 <b xsi:type="float">5</b>
1405 <s xsi:type="string">23</s>
1406 <s py:pytype="str">42</s>
1407 <f py:pytype="float">300</f>
1408 <l py:pytype="long">2</l>
1409 <t py:pytype="TREE"></t>
1410 </a>
1411 ''' ) )
1412 objectify . annotate ( root )
1413
1414 child_types = [ c . get ( objectify . PYTYPE_ATTRIBUTE )
1415 for c in root . iterchildren ( ) ]
1416 self . assertEqual ( "int" , child_types [ 0 ] )
1417 self . assertEqual ( "str" , child_types [ 1 ] )
1418 self . assertEqual ( "float" , child_types [ 2 ] )
1419 self . assertEqual ( "str" , child_types [ 3 ] )
1420 self . assertEqual ( "bool" , child_types [ 4 ] )
1421 self . assertEqual ( "NoneType" , child_types [ 5 ] )
1422 self . assertEqual ( None , child_types [ 6 ] )
1423 self . assertEqual ( "float" , child_types [ 7 ] )
1424 self . assertEqual ( "float" , child_types [ 8 ] )
1425 self . assertEqual ( "str" , child_types [ 9 ] )
1426 self . assertEqual ( "int" , child_types [ 10 ] )
1427 self . assertEqual ( "int" , child_types [ 11 ] )
1428 self . assertEqual ( "int" , child_types [ 12 ] )
1429 self . assertEqual ( None , child_types [ 13 ] )
1430
1431 self . assertEqual ( "true" , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1432
1452
1454 XML = self . XML
1455 root = XML ( _bytes ( '''\
1456 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1457 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1458 <b>5</b>
1459 <b>test</b>
1460 <c>1.1</c>
1461 <c>\uF8D2</c>
1462 <x>true</x>
1463 <n xsi:nil="true" />
1464 <n></n>
1465 <b xsi:type="double">5</b>
1466 <b xsi:type="float">5</b>
1467 <s xsi:type="string">23</s>
1468 <s py:pytype="str">42</s>
1469 <f py:pytype="float">300</f>
1470 <l py:pytype="long">2</l>
1471 <t py:pytype="TREE"></t>
1472 </a>
1473 ''' ) )
1474 objectify . annotate ( root , ignore_old = False )
1475
1476 child_types = [ c . get ( objectify . PYTYPE_ATTRIBUTE )
1477 for c in root . iterchildren ( ) ]
1478 self . assertEqual ( "int" , child_types [ 0 ] )
1479 self . assertEqual ( "str" , child_types [ 1 ] )
1480 self . assertEqual ( "float" , child_types [ 2 ] )
1481 self . assertEqual ( "str" , child_types [ 3 ] )
1482 self . assertEqual ( "bool" , child_types [ 4 ] )
1483 self . assertEqual ( "NoneType" , child_types [ 5 ] )
1484 self . assertEqual ( None , child_types [ 6 ] )
1485 self . assertEqual ( "float" , child_types [ 7 ] )
1486 self . assertEqual ( "float" , child_types [ 8 ] )
1487 self . assertEqual ( "str" , child_types [ 9 ] )
1488 self . assertEqual ( "str" , child_types [ 10 ] )
1489 self . assertEqual ( "float" , child_types [ 11 ] )
1490 self . assertEqual ( "int" , child_types [ 12 ] )
1491 self . assertEqual ( TREE_PYTYPE , child_types [ 13 ] )
1492
1493 self . assertEqual ( "true" , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1494
1496 XML = self . XML
1497 root = XML ( _bytes ( '''\
1498 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1499 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1500 <b>5</b>
1501 <b>test</b>
1502 <c>1.1</c>
1503 <c>\uF8D2</c>
1504 <x>true</x>
1505 <n xsi:nil="true" />
1506 <n></n>
1507 <b xsi:type="double">5</b>
1508 <b xsi:type="float">5</b>
1509 <s xsi:type="string">23</s>
1510 <s py:pytype="str">42</s>
1511 <f py:pytype="float">300</f>
1512 <l py:pytype="long">2</l>
1513 <t py:pytype="TREE"></t>
1514 </a>
1515 ''' ) )
1516 objectify . annotate ( root , ignore_old = False , ignore_xsi = False ,
1517 annotate_xsi = 1 , annotate_pytype = 1 )
1518
1519
1520 child_types = [ c . get ( objectify . PYTYPE_ATTRIBUTE )
1521 for c in root . iterchildren ( ) ]
1522 self . assertEqual ( "int" , child_types [ 0 ] )
1523 self . assertEqual ( "str" , child_types [ 1 ] )
1524 self . assertEqual ( "float" , child_types [ 2 ] )
1525 self . assertEqual ( "str" , child_types [ 3 ] )
1526 self . assertEqual ( "bool" , child_types [ 4 ] )
1527 self . assertEqual ( "NoneType" , child_types [ 5 ] )
1528 self . assertEqual ( None , child_types [ 6 ] )
1529 self . assertEqual ( "float" , child_types [ 7 ] )
1530 self . assertEqual ( "float" , child_types [ 8 ] )
1531 self . assertEqual ( "str" , child_types [ 9 ] )
1532 self . assertEqual ( "str" , child_types [ 10 ] )
1533 self . assertEqual ( "float" , child_types [ 11 ] )
1534 self . assertEqual ( "int" , child_types [ 12 ] )
1535 self . assertEqual ( TREE_PYTYPE , child_types [ 13 ] )
1536
1537 self . assertEqual ( "true" , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1538
1539 child_xsitypes = [ c . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR )
1540 for c in root . iterchildren ( ) ]
1541
1542
1543 child_types = [ c . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR )
1544 for c in root . iterchildren ( ) ]
1545 self . assertEqual ( "xsd:integer" , child_types [ 0 ] )
1546 self . assertEqual ( "xsd:string" , child_types [ 1 ] )
1547 self . assertEqual ( "xsd:double" , child_types [ 2 ] )
1548 self . assertEqual ( "xsd:string" , child_types [ 3 ] )
1549 self . assertEqual ( "xsd:boolean" , child_types [ 4 ] )
1550 self . assertEqual ( None , child_types [ 5 ] )
1551 self . assertEqual ( None , child_types [ 6 ] )
1552 self . assertEqual ( "xsd:double" , child_types [ 7 ] )
1553 self . assertEqual ( "xsd:float" , child_types [ 8 ] )
1554 self . assertEqual ( "xsd:string" , child_types [ 9 ] )
1555 self . assertEqual ( "xsd:string" , child_types [ 10 ] )
1556 self . assertEqual ( "xsd:double" , child_types [ 11 ] )
1557 self . assertEqual ( "xsd:integer" , child_types [ 12 ] )
1558 self . assertEqual ( None , child_types [ 13 ] )
1559
1560 self . assertEqual ( "true" , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1561
1563 XML = self . XML
1564 root = XML ( _bytes ( '''\
1565 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1566 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1567 <b>5</b>
1568 <b>test</b>
1569 <c>1.1</c>
1570 <c>\uF8D2</c>
1571 <x>true</x>
1572 <n xsi:nil="true" />
1573 <n></n>
1574 <b xsi:type="double">5</b>
1575 <b xsi:type="float">5</b>
1576 <s xsi:type="string">23</s>
1577 <s py:pytype="str">42</s>
1578 <f py:pytype="float">300</f>
1579 <l py:pytype="long">2</l>
1580 <t py:pytype="TREE"></t>
1581 </a>
1582 ''' ) )
1583 objectify . xsiannotate ( root , ignore_old = False )
1584
1585 child_types = [ c . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR )
1586 for c in root . iterchildren ( ) ]
1587 self . assertEqual ( "xsd:integer" , child_types [ 0 ] )
1588 self . assertEqual ( "xsd:string" , child_types [ 1 ] )
1589 self . assertEqual ( "xsd:double" , child_types [ 2 ] )
1590 self . assertEqual ( "xsd:string" , child_types [ 3 ] )
1591 self . assertEqual ( "xsd:boolean" , child_types [ 4 ] )
1592 self . assertEqual ( None , child_types [ 5 ] )
1593 self . assertEqual ( None , child_types [ 6 ] )
1594 self . assertEqual ( "xsd:double" , child_types [ 7 ] )
1595 self . assertEqual ( "xsd:float" , child_types [ 8 ] )
1596 self . assertEqual ( "xsd:string" , child_types [ 9 ] )
1597 self . assertEqual ( "xsd:string" , child_types [ 10 ] )
1598 self . assertEqual ( "xsd:double" , child_types [ 11 ] )
1599 self . assertEqual ( "xsd:integer" , child_types [ 12 ] )
1600 self . assertEqual ( None , child_types [ 13 ] )
1601
1603 XML = self . XML
1604 root = XML ( _bytes ( '''\
1605 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1606 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1607 <b>5</b>
1608 <b>test</b>
1609 <c>1.1</c>
1610 <c>\uF8D2</c>
1611 <x>true</x>
1612 <n xsi:nil="true" />
1613 <n></n>
1614 <b xsi:type="double">5</b>
1615 <b xsi:type="float">5</b>
1616 <s xsi:type="string">23</s>
1617 <s py:pytype="str">42</s>
1618 <f py:pytype="float">300</f>
1619 <l py:pytype="long">2</l>
1620 <t py:pytype="TREE"></t>
1621 </a>
1622 ''' ) )
1623 objectify . pyannotate ( root , ignore_old = True )
1624
1625 child_types = [ c . get ( objectify . PYTYPE_ATTRIBUTE )
1626 for c in root . iterchildren ( ) ]
1627 self . assertEqual ( "int" , child_types [ 0 ] )
1628 self . assertEqual ( "str" , child_types [ 1 ] )
1629 self . assertEqual ( "float" , child_types [ 2 ] )
1630 self . assertEqual ( "str" , child_types [ 3 ] )
1631 self . assertEqual ( "bool" , child_types [ 4 ] )
1632 self . assertEqual ( "NoneType" , child_types [ 5 ] )
1633 self . assertEqual ( None , child_types [ 6 ] )
1634 self . assertEqual ( "float" , child_types [ 7 ] )
1635 self . assertEqual ( "float" , child_types [ 8 ] )
1636 self . assertEqual ( "str" , child_types [ 9 ] )
1637 self . assertEqual ( "int" , child_types [ 10 ] )
1638 self . assertEqual ( "int" , child_types [ 11 ] )
1639 self . assertEqual ( "int" , child_types [ 12 ] )
1640 self . assertEqual ( None , child_types [ 13 ] )
1641
1642 self . assertEqual ( "true" , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1643
1663
1665 XML = self . XML
1666 root = XML ( '''\
1667 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1668 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1669 <b>5</b>
1670 <b>test</b>
1671 <c>1.1</c>
1672 <c>\uF8D2</c>
1673 <x>true</x>
1674 <n xsi:nil="true" />
1675 <n></n>
1676 <b xsi:type="double">5</b>
1677 <b xsi:type="float">5</b>
1678 <s xsi:type="string">23</s>
1679 <s py:pytype="str">42</s>
1680 <f py:pytype="float">300</f>
1681 <l py:pytype="long">2</l>
1682 <t py:pytype="TREE"></t>
1683 </a>
1684 ''' )
1685 objectify . pyannotate ( root )
1686
1687 child_types = [ c . get ( objectify . PYTYPE_ATTRIBUTE )
1688 for c in root . iterchildren ( ) ]
1689 self . assertEqual ( "int" , child_types [ 0 ] )
1690 self . assertEqual ( "str" , child_types [ 1 ] )
1691 self . assertEqual ( "float" , child_types [ 2 ] )
1692 self . assertEqual ( "str" , child_types [ 3 ] )
1693 self . assertEqual ( "bool" , child_types [ 4 ] )
1694 self . assertEqual ( "NoneType" , child_types [ 5 ] )
1695 self . assertEqual ( None , child_types [ 6 ] )
1696 self . assertEqual ( "float" , child_types [ 7 ] )
1697 self . assertEqual ( "float" , child_types [ 8 ] )
1698 self . assertEqual ( "str" , child_types [ 9 ] )
1699 self . assertEqual ( "str" , child_types [ 10 ] )
1700 self . assertEqual ( "float" , child_types [ 11 ] )
1701 self . assertEqual ( "int" , child_types [ 12 ] )
1702 self . assertEqual ( TREE_PYTYPE , child_types [ 13 ] )
1703
1704 self . assertEqual ( "true" , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1705
1707 XML = self . XML
1708 root = XML ( _bytes ( '''\
1709 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1710 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1711 <b>5</b>
1712 <b>test</b>
1713 <c>1.1</c>
1714 <c>\uF8D2</c>
1715 <x>true</x>
1716 <n xsi:nil="true" />
1717 <n></n>
1718 <b xsi:type="double">5</b>
1719 <b xsi:type="float">5</b>
1720 <s xsi:type="string">23</s>
1721 <s py:pytype="str">42</s>
1722 <f py:pytype="float">300</f>
1723 <l py:pytype="long">2</l>
1724 <t py:pytype="TREE"></t>
1725 </a>
1726 ''' ) )
1727 objectify . xsiannotate ( root , ignore_old = True )
1728
1729 child_types = [ c . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR )
1730 for c in root . iterchildren ( ) ]
1731 self . assertEqual ( "xsd:integer" , child_types [ 0 ] )
1732 self . assertEqual ( "xsd:string" , child_types [ 1 ] )
1733 self . assertEqual ( "xsd:double" , child_types [ 2 ] )
1734 self . assertEqual ( "xsd:string" , child_types [ 3 ] )
1735 self . assertEqual ( "xsd:boolean" , child_types [ 4 ] )
1736 self . assertEqual ( None , child_types [ 5 ] )
1737 self . assertEqual ( None , child_types [ 6 ] )
1738 self . assertEqual ( "xsd:integer" , child_types [ 7 ] )
1739 self . assertEqual ( "xsd:integer" , child_types [ 8 ] )
1740 self . assertEqual ( "xsd:integer" , child_types [ 9 ] )
1741 self . assertEqual ( "xsd:string" , child_types [ 10 ] )
1742 self . assertEqual ( "xsd:double" , child_types [ 11 ] )
1743 self . assertEqual ( "xsd:integer" , child_types [ 12 ] )
1744 self . assertEqual ( None , child_types [ 13 ] )
1745
1746 self . assertEqual ( "true" , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1747
1749 XML = self . XML
1750 root = XML ( _bytes ( '''\
1751 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1752 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1753 <b>5</b>
1754 <b>test</b>
1755 <c>1.1</c>
1756 <c>\uF8D2</c>
1757 <x>true</x>
1758 <n xsi:nil="true" />
1759 <n></n>
1760 <b xsi:type="double">5</b>
1761 <b xsi:type="float">5</b>
1762 <s xsi:type="string">23</s>
1763 <s py:pytype="str">42</s>
1764 <f py:pytype="float">300</f>
1765 <l py:pytype="long">2</l>
1766 <t py:pytype="TREE"></t>
1767 </a>
1768 ''' ) )
1769 objectify . deannotate ( root )
1770
1771 for c in root . getiterator ( ) :
1772 self . assertEqual ( None , c . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR ) )
1773 self . assertEqual ( None , c . get ( objectify . PYTYPE_ATTRIBUTE ) )
1774
1775 self . assertEqual ( "true" , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1776
1778 XML = self . XML
1779 root = XML ( _bytes ( '''\
1780 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1781 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1782 <b>5</b>
1783 <b>test</b>
1784 <c>1.1</c>
1785 <c>\uF8D2</c>
1786 <x>true</x>
1787 <n xsi:nil="true" />
1788 <n></n>
1789 <b xsi:type="double">5</b>
1790 <b xsi:type="float">5</b>
1791 <s xsi:type="string">23</s>
1792 <s py:pytype="str">42</s>
1793 <f py:pytype="float">300</f>
1794 <l py:pytype="long">2</l>
1795 <t py:pytype="TREE"></t>
1796 </a>
1797 ''' ) )
1798 objectify . annotate (
1799 root , ignore_old = False , ignore_xsi = False , annotate_xsi = True ,
1800 empty_pytype = 'str' , empty_type = 'string' )
1801 objectify . deannotate ( root , pytype = False , xsi = False , xsi_nil = True )
1802
1803 child_types = [ c . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR )
1804 for c in root . iterchildren ( ) ]
1805 self . assertEqual ( "xsd:integer" , child_types [ 0 ] )
1806 self . assertEqual ( "xsd:string" , child_types [ 1 ] )
1807 self . assertEqual ( "xsd:double" , child_types [ 2 ] )
1808 self . assertEqual ( "xsd:string" , child_types [ 3 ] )
1809 self . assertEqual ( "xsd:boolean" , child_types [ 4 ] )
1810 self . assertEqual ( None , child_types [ 5 ] )
1811 self . assertEqual ( "xsd:string" , child_types [ 6 ] )
1812 self . assertEqual ( "xsd:double" , child_types [ 7 ] )
1813 self . assertEqual ( "xsd:float" , child_types [ 8 ] )
1814 self . assertEqual ( "xsd:string" , child_types [ 9 ] )
1815 self . assertEqual ( "xsd:string" , child_types [ 10 ] )
1816 self . assertEqual ( "xsd:double" , child_types [ 11 ] )
1817 self . assertEqual ( "xsd:integer" , child_types [ 12 ] )
1818 self . assertEqual ( None , child_types [ 13 ] )
1819
1820 self . assertEqual ( None , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1821
1822 for c in root . iterchildren ( ) :
1823 self . assertNotEqual ( None , c . get ( objectify . PYTYPE_ATTRIBUTE ) )
1824
1825 if ( c . get ( objectify . PYTYPE_ATTRIBUTE ) not in [ TREE_PYTYPE ,
1826 "NoneType" ] ) :
1827 self . assertNotEqual (
1828 None , c . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR ) )
1829
1831 XML = self . XML
1832 root = XML ( _bytes ( '''\
1833 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1834 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1835 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1836 <b>5</b>
1837 <b>test</b>
1838 <c>1.1</c>
1839 <c>\uF8D2</c>
1840 <x>true</x>
1841 <n xsi:nil="true" />
1842 <n></n>
1843 <b xsi:type="xsd:double">5</b>
1844 <b xsi:type="xsd:float">5</b>
1845 <s xsi:type="xsd:string">23</s>
1846 <s py:pytype="str">42</s>
1847 <f py:pytype="float">300</f>
1848 <l py:pytype="long">2</l>
1849 <t py:pytype="TREE"></t>
1850 </a>
1851 ''' ) )
1852 objectify . annotate ( root )
1853 objectify . deannotate ( root , pytype = False )
1854
1855 child_types = [ c . get ( objectify . PYTYPE_ATTRIBUTE )
1856 for c in root . iterchildren ( ) ]
1857 self . assertEqual ( "int" , child_types [ 0 ] )
1858 self . assertEqual ( "str" , child_types [ 1 ] )
1859 self . assertEqual ( "float" , child_types [ 2 ] )
1860 self . assertEqual ( "str" , child_types [ 3 ] )
1861 self . assertEqual ( "bool" , child_types [ 4 ] )
1862 self . assertEqual ( "NoneType" , child_types [ 5 ] )
1863 self . assertEqual ( None , child_types [ 6 ] )
1864 self . assertEqual ( "float" , child_types [ 7 ] )
1865 self . assertEqual ( "float" , child_types [ 8 ] )
1866 self . assertEqual ( "str" , child_types [ 9 ] )
1867 self . assertEqual ( "int" , child_types [ 10 ] )
1868 self . assertEqual ( "int" , child_types [ 11 ] )
1869 self . assertEqual ( "int" , child_types [ 12 ] )
1870 self . assertEqual ( None , child_types [ 13 ] )
1871
1872 self . assertEqual ( "true" , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1873
1874 for c in root . getiterator ( ) :
1875 self . assertEqual ( None , c . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR ) )
1876
1878 XML = self . XML
1879 root = XML ( _bytes ( '''\
1880 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1881 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1882 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1883 <b xsi:type="xsd:int">5</b>
1884 <b xsi:type="xsd:string">test</b>
1885 <c xsi:type="xsd:float">1.1</c>
1886 <c xsi:type="xsd:string">\uF8D2</c>
1887 <x xsi:type="xsd:boolean">true</x>
1888 <n xsi:nil="true" />
1889 <n></n>
1890 <b xsi:type="xsd:double">5</b>
1891 <b xsi:type="xsd:float">5</b>
1892 <s xsi:type="xsd:string">23</s>
1893 <s xsi:type="xsd:string">42</s>
1894 <f xsi:type="xsd:float">300</f>
1895 <l xsi:type="xsd:long">2</l>
1896 <t py:pytype="TREE"></t>
1897 </a>
1898 ''' ) )
1899 objectify . annotate ( root )
1900 objectify . deannotate ( root , xsi = False )
1901
1902 child_types = [ c . get ( XML_SCHEMA_INSTANCE_TYPE_ATTR )
1903 for c in root . iterchildren ( ) ]
1904 self . assertEqual ( "xsd:int" , child_types [ 0 ] )
1905 self . assertEqual ( "xsd:string" , child_types [ 1 ] )
1906 self . assertEqual ( "xsd:float" , child_types [ 2 ] )
1907 self . assertEqual ( "xsd:string" , child_types [ 3 ] )
1908 self . assertEqual ( "xsd:boolean" , child_types [ 4 ] )
1909 self . assertEqual ( None , child_types [ 5 ] )
1910 self . assertEqual ( None , child_types [ 6 ] )
1911 self . assertEqual ( "xsd:double" , child_types [ 7 ] )
1912 self . assertEqual ( "xsd:float" , child_types [ 8 ] )
1913 self . assertEqual ( "xsd:string" , child_types [ 9 ] )
1914 self . assertEqual ( "xsd:string" , child_types [ 10 ] )
1915 self . assertEqual ( "xsd:float" , child_types [ 11 ] )
1916 self . assertEqual ( "xsd:long" , child_types [ 12 ] )
1917 self . assertEqual ( None , child_types [ 13 ] )
1918
1919 self . assertEqual ( "true" , root . n . get ( XML_SCHEMA_NIL_ATTR ) )
1920
1921 for c in root . getiterator ( ) :
1922 self . assertEqual ( None , c . get ( objectify . PYTYPE_ATTRIBUTE ) )
1923
1925 XML = self . XML
1926
1927 xml = _bytes ( '''\
1928 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1929 <b>5</b>
1930 <b>test</b>
1931 <c>1.1</c>
1932 <c>\uF8D2</c>
1933 <x>true</x>
1934 <n xsi:nil="true" />
1935 <n></n>
1936 <b xsi:type="double">5</b>
1937 </a>
1938 ''' )
1939
1940 pytype_ns , pytype_name = objectify . PYTYPE_ATTRIBUTE [ 1 : ] . split ( '}' )
1941 objectify . set_pytype_attribute_tag ( "{TEST}test" )
1942
1943 root = XML ( xml )
1944 objectify . annotate ( root )
1945
1946 attribs = root . xpath ( "//@py:%s" % pytype_name ,
1947 namespaces = { "py" : pytype_ns } )
1948 self . assertEqual ( 0 , len ( attribs ) )
1949 attribs = root . xpath ( "//@py:test" ,
1950 namespaces = { "py" : "TEST" } )
1951 self . assertEqual ( 7 , len ( attribs ) )
1952
1953 objectify . set_pytype_attribute_tag ( )
1954 pytype_ns , pytype_name = objectify . PYTYPE_ATTRIBUTE [ 1 : ] . split ( '}' )
1955
1956 self . assertNotEqual ( "test" , pytype_ns . lower ( ) )
1957 self . assertNotEqual ( "test" , pytype_name . lower ( ) )
1958
1959 root = XML ( xml )
1960 attribs = root . xpath ( "//@py:%s" % pytype_name ,
1961 namespaces = { "py" : pytype_ns } )
1962 self . assertEqual ( 0 , len ( attribs ) )
1963
1964 objectify . annotate ( root )
1965 attribs = root . xpath ( "//@py:%s" % pytype_name ,
1966 namespaces = { "py" : pytype_ns } )
1967 self . assertEqual ( 7 , len ( attribs ) )
1968
1976
1977 def checkMyType ( s ) :
1978 return True
1979
1980 pytype = objectify . PyType ( "mytype" , checkMyType , NewType )
1981 self . assertTrue ( pytype not in objectify . getRegisteredTypes ( ) )
1982 pytype . register ( )
1983 self . assertTrue ( pytype in objectify . getRegisteredTypes ( ) )
1984 pytype . unregister ( )
1985 self . assertTrue ( pytype not in objectify . getRegisteredTypes ( ) )
1986
1987 pytype . register ( before = [ objectify . getRegisteredTypes ( ) [ 0 ] . name ] )
1988 self . assertEqual ( pytype , objectify . getRegisteredTypes ( ) [ 0 ] )
1989 pytype . unregister ( )
1990
1991 pytype . register ( after = [ objectify . getRegisteredTypes ( ) [ 0 ] . name ] )
1992 self . assertNotEqual ( pytype , objectify . getRegisteredTypes ( ) [ 0 ] )
1993 pytype . unregister ( )
1994
1995 self . assertRaises ( ValueError , pytype . register ,
1996 before = [ objectify . getRegisteredTypes ( ) [ 0 ] . name ] ,
1997 after = [ objectify . getRegisteredTypes ( ) [ 1 ] . name ] )
1998
2000 from datetime import datetime
2001 def parse_date ( value ) :
2002 if len ( value ) != 14 :
2003 raise ValueError ( value )
2004 Y = int ( value [ 0 : 4 ] )
2005 M = int ( value [ 4 : 6 ] )
2006 D = int ( value [ 6 : 8 ] )
2007 h = int ( value [ 8 : 10 ] )
2008 m = int ( value [ 10 : 12 ] )
2009 s = int ( value [ 12 : 14 ] )
2010 return datetime ( Y , M , D , h , m , s )
2011
2012 def stringify_date ( date ) :
2013 return date . strftime ( "%Y%m%d%H%M%S" )
2014
2015 class DatetimeElement ( objectify . ObjectifiedDataElement ) :
2016 def pyval ( self ) :
2017 return parse_date ( self . text )
2018 pyval = property ( pyval )
2019
2020 datetime_type = objectify . PyType (
2021 "datetime" , parse_date , DatetimeElement , stringify_date )
2022 datetime_type . xmlSchemaTypes = "dateTime"
2023 datetime_type . register ( )
2024
2025 NAMESPACE = "http://foo.net/xmlns"
2026 NAMESPACE_MAP = { 'ns' : NAMESPACE }
2027
2028 r = objectify . Element ( "{%s}root" % NAMESPACE , nsmap = NAMESPACE_MAP )
2029 time = datetime . now ( )
2030 r . date = time
2031
2032 self . assertTrue ( isinstance ( r . date , DatetimeElement ) )
2033 self . assertTrue ( isinstance ( r . date . pyval , datetime ) )
2034
2035 self . assertEqual ( r . date . pyval , parse_date ( stringify_date ( time ) ) )
2036 self . assertEqual ( r . date . text , stringify_date ( time ) )
2037
2038 r . date = objectify . E . date ( time )
2039
2040 self . assertTrue ( isinstance ( r . date , DatetimeElement ) )
2041 self . assertTrue ( isinstance ( r . date . pyval , datetime ) )
2042
2043 self . assertEqual ( r . date . pyval , parse_date ( stringify_date ( time ) ) )
2044 self . assertEqual ( r . date . text , stringify_date ( time ) )
2045
2046 date = objectify . DataElement ( time )
2047
2048 self . assertTrue ( isinstance ( date , DatetimeElement ) )
2049 self . assertTrue ( isinstance ( date . pyval , datetime ) )
2050
2051 self . assertEqual ( date . pyval , parse_date ( stringify_date ( time ) ) )
2052 self . assertEqual ( date . text , stringify_date ( time ) )
2053
2059
2065
2070
2079
2086
2094
2097
2100
2119
2124
2129
2134
2139
2159
2161 root = self . XML ( xml_str )
2162 path = objectify . ObjectPath ( [ 'root' , 'c1[0]' , 'c2[0]' ] )
2163 self . assertEqual ( root . c1 . c2 . text , path ( root ) . text )
2164
2165 path = objectify . ObjectPath ( [ 'root' , 'c1[0]' , 'c2[2]' ] )
2166 self . assertEqual ( root . c1 . c2 [ 2 ] . text , path ( root ) . text )
2167
2168 path = objectify . ObjectPath ( [ 'root' , 'c1' , 'c2[2]' ] )
2169 self . assertEqual ( root . c1 . c2 [ 2 ] . text , path ( root ) . text )
2170
2171 path = objectify . ObjectPath ( [ 'root' , 'c1' , 'c2[-1]' ] )
2172 self . assertEqual ( root . c1 . c2 [ - 1 ] . text , path ( root ) . text )
2173
2174 path = objectify . ObjectPath ( [ 'root' , 'c1' , 'c2[-3]' ] )
2175 self . assertEqual ( root . c1 . c2 [ - 3 ] . text , path ( root ) . text )
2176
2178 self . assertRaises ( ValueError , objectify . ObjectPath ,
2179 "root.c1[0].c2[-1-2]" )
2180 self . assertRaises ( ValueError , objectify . ObjectPath ,
2181 [ 'root' , 'c1[0]' , 'c2[-1-2]' ] )
2182
2183 self . assertRaises ( ValueError , objectify . ObjectPath ,
2184 "root[2].c1.c2" )
2185 self . assertRaises ( ValueError , objectify . ObjectPath ,
2186 [ 'root[2]' , 'c1' , 'c2' ] )
2187
2188 self . assertRaises ( ValueError , objectify . ObjectPath ,
2189 [ ] )
2190 self . assertRaises ( ValueError , objectify . ObjectPath ,
2191 [ '' , '' , '' ] )
2192
2194 root = self . XML ( xml_str )
2195 path = objectify . ObjectPath ( "root.c1[9999].c2" )
2196 self . assertRaises ( AttributeError , path , root )
2197
2198 path = objectify . ObjectPath ( "root.c1[0].c2[9999]" )
2199 self . assertRaises ( AttributeError , path , root )
2200
2201 path = objectify . ObjectPath ( ".c1[9999].c2[0]" )
2202 self . assertRaises ( AttributeError , path , root )
2203
2204 path = objectify . ObjectPath ( "root.c1[-2].c2" )
2205 self . assertRaises ( AttributeError , path , root )
2206
2207 path = objectify . ObjectPath ( "root.c1[0].c2[-4]" )
2208 self . assertRaises ( AttributeError , path , root )
2209
2223
2225 root = self . XML ( xml_str )
2226 path = objectify . ObjectPath ( [ '{objectified}root' , 'c1' , 'c2' ] )
2227 self . assertEqual ( root . c1 . c2 . text , path . find ( root ) . text )
2228 path = objectify . ObjectPath ( [ '{objectified}root' , '{objectified}c1' , 'c2' ] )
2229 self . assertEqual ( root . c1 . c2 . text , path . find ( root ) . text )
2230 path = objectify . ObjectPath ( [ 'root' , '{objectified}c1' , '{objectified}c2' ] )
2231 self . assertEqual ( root . c1 . c2 . text , path . find ( root ) . text )
2232 path = objectify . ObjectPath ( [ 'root' , '{objectified}c1' , '{objectified}c2[2]' ] )
2233 self . assertEqual ( root . c1 . c2 [ 2 ] . text , path . find ( root ) . text )
2234 path = objectify . ObjectPath ( [ 'root' , 'c1' , '{objectified}c2' ] )
2235 self . assertEqual ( root . c1 . c2 . text , path . find ( root ) . text )
2236 path = objectify . ObjectPath ( [ 'root' , 'c1' , '{objectified}c2[2]' ] )
2237 self . assertEqual ( root . c1 . c2 [ 2 ] . text , path . find ( root ) . text )
2238 path = objectify . ObjectPath ( [ 'root' , 'c1' , '{otherNS}c2' ] )
2239 self . assertEqual ( getattr ( root . c1 , '{otherNS}c2' ) . text ,
2240 path . find ( root ) . text )
2241
2254
2269
2281
2295
2297 root = self . XML ( xml_str )
2298 path = objectify . ObjectPath ( "root.c1.c99" )
2299 self . assertRaises ( AttributeError , path . find , root )
2300
2301 new_el = self . Element ( "{objectified}test" )
2302 new_el . a = [ "TEST1" , "TEST2" ]
2303 new_el . a [ 0 ] . set ( "myattr" , "ATTR1" )
2304 new_el . a [ 1 ] . set ( "myattr" , "ATTR2" )
2305
2306 path . setattr ( root , list ( new_el . a ) )
2307
2308 self . assertEqual ( 2 , len ( root . c1 . c99 ) )
2309 self . assertEqual ( "ATTR1" , root . c1 . c99 [ 0 ] . get ( "myattr" ) )
2310 self . assertEqual ( "TEST1" , root . c1 . c99 [ 0 ] . text )
2311 self . assertEqual ( "ATTR2" , root . c1 . c99 [ 1 ] . get ( "myattr" ) )
2312 self . assertEqual ( "TEST2" , root . c1 . c99 [ 1 ] . text )
2313 self . assertEqual ( "TEST1" , path ( root ) . text )
2314
2323
2337
2349
2363
2378
2380 root = self . XML ( xml_str )
2381 self . assertEqual (
2382 [ '{objectified}root' , '{objectified}root.c1' ,
2383 '{objectified}root.c1.c2' ,
2384 '{objectified}root.c1.c2[1]' , '{objectified}root.c1.c2[2]' ,
2385 '{objectified}root.c1.{otherNS}c2' , '{objectified}root.c1.{}c2' ] ,
2386 root . descendantpaths ( ) )
2387
2389 root = self . XML ( xml_str )
2390 self . assertEqual (
2391 [ '{objectified}c1' , '{objectified}c1.c2' ,
2392 '{objectified}c1.c2[1]' , '{objectified}c1.c2[2]' ,
2393 '{objectified}c1.{otherNS}c2' , '{objectified}c1.{}c2' ] ,
2394 root . c1 . descendantpaths ( ) )
2395
2397 root = self . XML ( xml_str )
2398 self . assertEqual (
2399 [ 'root.{objectified}c1' , 'root.{objectified}c1.c2' ,
2400 'root.{objectified}c1.c2[1]' , 'root.{objectified}c1.c2[2]' ,
2401 'root.{objectified}c1.{otherNS}c2' ,
2402 'root.{objectified}c1.{}c2' ] ,
2403 root . c1 . descendantpaths ( 'root' ) )
2404
2416
2429
2433
2437
2441
2447
2452
2454 import pickle
2455 if isinstance ( stringOrElt , ( etree . _Element , etree . _ElementTree ) ) :
2456 elt = stringOrElt
2457 else :
2458 elt = self . XML ( stringOrElt )
2459 out = BytesIO ( )
2460 pickle . dump ( elt , out )
2461
2462 new_elt = pickle . loads ( out . getvalue ( ) )
2463 self . assertEqual (
2464 etree . tostring ( new_elt ) ,
2465 etree . tostring ( elt ) )
2466
2467
2468
2473
2478
2483
2488
2493
2498
2503
2508
2510 E = objectify . E
2511 DataElement = objectify . DataElement
2512 root = E . root ( "text" , E . sub ( E . subsub ( ) ) , "tail" , DataElement ( 1 ) ,
2513 DataElement ( 2.0 ) )
2514 self . assertTrue ( isinstance ( root , objectify . ObjectifiedElement ) )
2515 self . assertEqual ( root . text , "text" )
2516 self . assertTrue ( isinstance ( root . sub , objectify . ObjectifiedElement ) )
2517 self . assertEqual ( root . sub . tail , "tail" )
2518 self . assertTrue ( isinstance ( root . sub . subsub , objectify . StringElement ) )
2519 self . assertEqual ( len ( root . value ) , 2 )
2520 self . assertTrue ( isinstance ( root . value [ 0 ] , objectify . IntElement ) )
2521 self . assertTrue ( isinstance ( root . value [ 1 ] , objectify . FloatElement ) )
2522
2529
2530 attr = Attribute ( )
2531 self . assertEqual ( attr . text , None )
2532 self . assertEqual ( attr . get ( "datatype" ) , "TYPE" )
2533 self . assertEqual ( attr . get ( "range" ) , "0.,1." )
2534
2539
2546
2551
2557
2559 root = objectify . XML ( _bytes ( "<root/>" ) , base_url = "http://no/such/url" )
2560 self . assertEqual ( root . base , "http://no/such/url" )
2561 self . assertEqual (
2562 root . get ( '{http://www.w3.org/XML/1998/namespace}base' ) , None )
2563 root . base = "https://secret/url"
2564 self . assertEqual ( root . base , "https://secret/url" )
2565 self . assertEqual (
2566 root . get ( '{http://www.w3.org/XML/1998/namespace}base' ) ,
2567 "https://secret/url" )
2568
2570 root = objectify . XML ( _bytes ( "<root/>" ) , base_url = "http://no/such/url" )
2571 self . assertEqual ( root . base , "http://no/such/url" )
2572 self . assertEqual (
2573 root . get ( '{http://www.w3.org/XML/1998/namespace}base' ) , None )
2574 root . set ( '{http://www.w3.org/XML/1998/namespace}base' ,
2575 "https://secret/url" )
2576 self . assertEqual ( root . base , "https://secret/url" )
2577 self . assertEqual (
2578 root . get ( '{http://www.w3.org/XML/1998/namespace}base' ) ,
2579 "https://secret/url" )
2580
2582 XML = self . XML
2583
2584 xml = _bytes ( '''\
2585 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2586 <i>5</i>
2587 <i>-5</i>
2588 <l>4294967296</l>
2589 <l>-4294967296</l>
2590 <f>1.1</f>
2591 <b>true</b>
2592 <b>false</b>
2593 <s>Strange things happen, where strings collide</s>
2594 <s>True</s>
2595 <s>False</s>
2596 <s>t</s>
2597 <s>f</s>
2598 <s></s>
2599 <s>None</s>
2600 <n xsi:nil="true" />
2601 </root>
2602 ''' )
2603 root = XML ( xml )
2604
2605 for i in root . i :
2606 self . assertTrue ( isinstance ( i , objectify . IntElement ) )
2607 for l in root . l :
2608 self . assertTrue ( isinstance ( l , objectify . IntElement ) )
2609 for f in root . f :
2610 self . assertTrue ( isinstance ( f , objectify . FloatElement ) )
2611 for b in root . b :
2612 self . assertTrue ( isinstance ( b , objectify . BoolElement ) )
2613 self . assertEqual ( True , root . b [ 0 ] )
2614 self . assertEqual ( False , root . b [ 1 ] )
2615 for s in root . s :
2616 self . assertTrue ( isinstance ( s , objectify . StringElement ) )
2617 self . assertTrue ( isinstance ( root . n , objectify . NoneElement ) )
2618 self . assertEqual ( None , root . n )
2619
2621 suite = unittest . TestSuite ( )
2622 suite . addTests ( [ unittest . makeSuite ( ObjectifyTestCase ) ] )
2623 suite . addTests ( doctest . DocTestSuite ( objectify ) )
2624 if sys . version_info >= ( 2 , 4 ) :
2625 suite . addTests (
2626 [ make_doctest ( '../../../doc/objectify.txt' ) ] )
2627 return suite
2628
2629 if __name__ == '__main__' :
2630 print ( 'to test use test.py %s' % __file__ )
2631