Package lxml :: Package tests :: Module test_objectify
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_objectify

   1  # -*- coding: utf-8 -*- 
   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) # needed for Py3 
  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      # objectify built-in 
  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      # None: xsi:nil="true" 
  42      } 
  43   
  44  xsitype2objclass = dict([ (v, k) for k in objectclass2xsitype 
  45                            for v in objectclass2xsitype[k] ]) 
  46   
  47  objectclass2pytype = { 
  48      # objectify built-in 
  49      objectify.IntElement: "int", 
  50      objectify.FloatElement: "float", 
  51      objectify.BoolElement: "bool", 
  52      objectify.StringElement: "str", 
  53      # None: xsi:nil="true" 
  54      } 
  55   
  56  pytype2objclass = dict([ (objectclass2pytype[k], k) 
  57                           for k in objectclass2pytype]) 
  58   
  59  xml_str = '''\ 
  60  <obj:root xmlns:obj="objectified" xmlns:other="otherNS"> 
  61    <obj:c1 a1="A1" a2="A2" other:a3="A3"> 
  62      <obj:c2>0</obj:c2> 
  63      <obj:c2>1</obj:c2> 
  64      <obj:c2>2</obj:c2> 
  65      <other:c2>3</other:c2> 
  66      <c2>3</c2> 
  67    </obj:c1> 
  68  </obj:root>''' 
  69   
70 -class ObjectifyTestCase(HelperTestCase):
71 """Test cases for lxml.objectify 72 """ 73 etree = etree 74
75 - def XML(self, xml):
76 return self.etree.XML(xml, self.parser)
77
78 - def setUp(self):
79 super(ObjectifyTestCase, self).setUp() 80 self.parser = self.etree.XMLParser(remove_blank_text=True) 81 self.lookup = etree.ElementNamespaceClassLookup( 82 objectify.ObjectifyElementClassLookup() ) 83 self.parser.set_element_class_lookup(self.lookup) 84 85 self.Element = self.parser.makeelement 86 87 ns = self.lookup.get_namespace("otherNS") 88 ns[None] = self.etree.ElementBase 89 90 self._orig_types = objectify.getRegisteredTypes()
91
92 - def tearDown(self):
93 self.lookup.get_namespace("otherNS").clear() 94 objectify.set_pytype_attribute_tag() 95 del self.lookup 96 del self.parser 97 98 for pytype in objectify.getRegisteredTypes(): 99 pytype.unregister() 100 for pytype in self._orig_types: 101 pytype.register() 102 del self._orig_types 103 104 super(ObjectifyTestCase, self).tearDown()
105 106
107 - def test_element_nsmap_default(self):
108 elt = objectify.Element("test") 109 self.assertEquals(elt.nsmap, DEFAULT_NSMAP)
110
111 - def test_element_nsmap_empty(self):
112 nsmap = {} 113 elt = objectify.Element("test", nsmap=nsmap) 114 self.assertEquals(list(elt.nsmap.values()), [PYTYPE_NAMESPACE])
115
117 nsmap = {"mypy": PYTYPE_NAMESPACE, 118 "myxsi": XML_SCHEMA_INSTANCE_NS, 119 "myxsd": XML_SCHEMA_NS} 120 elt = objectify.Element("test", nsmap=nsmap) 121 self.assertEquals(elt.nsmap, nsmap)
122
123 - def test_element_nsmap_custom(self):
124 nsmap = {"my": "someNS", 125 "myother": "someOtherNS", 126 "myxsd": XML_SCHEMA_NS} 127 elt = objectify.Element("test", nsmap=nsmap) 128 self.assert_(PYTYPE_NAMESPACE in elt.nsmap.values()) 129 for prefix, ns in nsmap.items(): 130 self.assert_(prefix in elt.nsmap) 131 self.assertEquals(nsmap[prefix], elt.nsmap[prefix])
132
134 root = objectify.Element("root") 135 root.sub = objectify.Element("test") 136 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
137
139 root = objectify.Element("root") 140 nsmap = {} 141 root.sub = objectify.Element("test", nsmap=nsmap) 142 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
143
145 root = objectify.Element("root") 146 nsmap = {"mypy": PYTYPE_NAMESPACE, 147 "myxsi": XML_SCHEMA_INSTANCE_NS, 148 "myxsd": XML_SCHEMA_NS} 149 root.sub = objectify.Element("test", nsmap=nsmap) 150 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
151
153 root = objectify.Element("root") 154 nsmap = {"my": "someNS", 155 "myother": "someOtherNS", 156 "myxsd": XML_SCHEMA_NS,} 157 root.sub = objectify.Element("test", nsmap=nsmap) 158 expected = nsmap.copy() 159 del expected["myxsd"] 160 expected.update(DEFAULT_NSMAP) 161 self.assertEquals(root.sub.nsmap, expected)
162
164 value = objectify.DataElement("test this") 165 self.assertEquals(value.nsmap, DEFAULT_NSMAP)
166
168 nsmap = {} 169 value = objectify.DataElement("test this", nsmap=nsmap) 170 self.assertEquals(list(value.nsmap.values()), [PYTYPE_NAMESPACE])
171
173 nsmap = {"mypy": PYTYPE_NAMESPACE, 174 "myxsi": XML_SCHEMA_INSTANCE_NS, 175 "myxsd": XML_SCHEMA_NS} 176 value = objectify.DataElement("test this", nsmap=nsmap) 177 self.assertEquals(value.nsmap, nsmap)
178
180 nsmap = {"my": "someNS", 181 "myother": "someOtherNS", 182 "myxsd": XML_SCHEMA_NS,} 183 value = objectify.DataElement("test", nsmap=nsmap) 184 self.assert_(PYTYPE_NAMESPACE in value.nsmap.values()) 185 for prefix, ns in nsmap.items(): 186 self.assert_(prefix in value.nsmap) 187 self.assertEquals(nsmap[prefix], value.nsmap[prefix])
188
190 root = objectify.Element("root") 191 root.value = objectify.DataElement("test this") 192 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
193
195 root = objectify.Element("root") 196 nsmap = {} 197 root.value = objectify.DataElement("test this", nsmap=nsmap) 198 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
199
201 root = objectify.Element("root") 202 nsmap = {"mypy": PYTYPE_NAMESPACE, 203 "myxsi": XML_SCHEMA_INSTANCE_NS, 204 "myxsd": XML_SCHEMA_NS} 205 root.value = objectify.DataElement("test this", nsmap=nsmap) 206 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
207
209 root = objectify.Element("root") 210 nsmap = {"my": "someNS", 211 "myother": "someOtherNS", 212 "myxsd": XML_SCHEMA_NS} 213 root.value = objectify.DataElement("test", nsmap=nsmap) 214 expected = nsmap.copy() 215 del expected["myxsd"] 216 expected.update(DEFAULT_NSMAP) 217 self.assertEquals(root.value.nsmap, expected)
218
220 # keyword arguments override attrib entries 221 value = objectify.DataElement(23, _pytype="str", _xsi="foobar", 222 attrib={"gnu": "muh", "cat": "meeow", 223 "dog": "wuff"}, 224 bird="tchilp", dog="grrr") 225 self.assertEquals(value.get("gnu"), "muh") 226 self.assertEquals(value.get("cat"), "meeow") 227 self.assertEquals(value.get("dog"), "grrr") 228 self.assertEquals(value.get("bird"), "tchilp")
229
231 # Check that DataElement preserves all attributes ObjectifiedDataElement 232 # arguments 233 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar", 234 attrib={"gnu": "muh", "cat": "meeow", 235 "dog": "wuff"}, 236 bird="tchilp", dog="grrr") 237 value = objectify.DataElement(arg) 238 self.assert_(isinstance(value, objectify.StringElement)) 239 for attr in arg.attrib: 240 self.assertEquals(value.get(attr), arg.get(attr))
241
243 # Check that _pytype arg overrides original py:pytype of 244 # ObjectifiedDataElement 245 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar", 246 attrib={"gnu": "muh", "cat": "meeow", 247 "dog": "wuff"}, 248 bird="tchilp", dog="grrr") 249 value = objectify.DataElement(arg, _pytype="NoneType") 250 self.assert_(isinstance(value, objectify.NoneElement)) 251 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true") 252 self.assertEquals(value.text, None) 253 self.assertEquals(value.pyval, None) 254 for attr in arg.attrib: 255 #if not attr == objectify.PYTYPE_ATTRIBUTE: 256 self.assertEquals(value.get(attr), arg.get(attr))
257
259 # Check that _pytype arg overrides original py:pytype of 260 # ObjectifiedDataElement 261 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar", 262 attrib={"gnu": "muh", "cat": "meeow", 263 "dog": "wuff"}, 264 bird="tchilp", dog="grrr") 265 value = objectify.DataElement(arg, _pytype="int") 266 self.assert_(isinstance(value, objectify.IntElement)) 267 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int") 268 for attr in arg.attrib: 269 if not attr == objectify.PYTYPE_ATTRIBUTE: 270 self.assertEquals(value.get(attr), arg.get(attr))
271
273 # Check that _xsi arg overrides original xsi:type of given 274 # ObjectifiedDataElement 275 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar", 276 attrib={"gnu": "muh", "cat": "meeow", 277 "dog": "wuff"}, 278 bird="tchilp", dog="grrr") 279 value = objectify.DataElement(arg, _xsi="xsd:int") 280 self.assert_(isinstance(value, objectify.IntElement)) 281 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int") 282 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int") 283 for attr in arg.attrib: 284 if not attr in [objectify.PYTYPE_ATTRIBUTE, 285 XML_SCHEMA_INSTANCE_TYPE_ATTR]: 286 self.assertEquals(value.get(attr), arg.get(attr))
287
289 # Check that _pytype and _xsi args override original py:pytype and 290 # xsi:type attributes of given ObjectifiedDataElement 291 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar", 292 attrib={"gnu": "muh", "cat": "meeow", 293 "dog": "wuff"}, 294 bird="tchilp", dog="grrr") 295 value = objectify.DataElement(arg, _pytype="int", _xsi="xsd:int") 296 self.assert_(isinstance(value, objectify.IntElement)) 297 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int") 298 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int") 299 for attr in arg.attrib: 300 if not attr in [objectify.PYTYPE_ATTRIBUTE, 301 XML_SCHEMA_INSTANCE_TYPE_ATTR]: 302 self.assertEquals(value.get(attr), arg.get(attr))
303
305 self.assertRaises(ValueError, objectify.DataElement, 3.1415, 306 _pytype="int")
307
309 self.assertRaises(ValueError, objectify.DataElement, 3.1415, 310 _xsi="xsd:int")
311
313 arg = objectify.DataElement(3.1415) 314 self.assertRaises(ValueError, objectify.DataElement, arg, 315 _pytype="int")
316
318 arg = objectify.DataElement(3.1415) 319 self.assertRaises(ValueError, objectify.DataElement, arg, 320 _xsi="xsd:int")
321
322 - def test_root(self):
323 root = self.Element("test") 324 self.assert_(isinstance(root, objectify.ObjectifiedElement))
325
326 - def test_str(self):
327 root = self.Element("test") 328 self.assertEquals('', str(root))
329
330 - def test_child(self):
331 root = self.XML(xml_str) 332 self.assertEquals("0", root.c1.c2.text)
333
334 - def test_countchildren(self):
335 root = self.XML(xml_str) 336 self.assertEquals(1, root.countchildren()) 337 self.assertEquals(5, root.c1.countchildren())
338
339 - def test_child_getattr(self):
340 root = self.XML(xml_str) 341 self.assertEquals("0", getattr(root.c1, "{objectified}c2").text) 342 self.assertEquals("3", getattr(root.c1, "{otherNS}c2").text)
343
344 - def test_child_nonexistant(self):
345 root = self.XML(xml_str) 346 self.assertRaises(AttributeError, getattr, root.c1, "NOT_THERE") 347 self.assertRaises(AttributeError, getattr, root.c1, "{unknownNS}c2")
348
349 - def test_setattr(self):
350 for val in [ 351 2, 2**32, 1.2, "Won't get fooled again", 352 _str("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1'), True, 353 False, None]: 354 root = self.Element('root') 355 attrname = 'val' 356 setattr(root, attrname, val) 357 result = getattr(root, attrname) 358 self.assertEquals(val, result) 359 self.assertEquals(type(val), type(result.pyval))
360
361 - def test_setattr_nonunicode(self):
362 root = self.Element('root') 363 attrname = 'val' 364 val = _bytes("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1') 365 self.assertRaises(ValueError, setattr, root, attrname, val) 366 self.assertRaises(AttributeError, getattr, root, attrname)
367
368 - def test_addattr(self):
369 root = self.XML(xml_str) 370 self.assertEquals(1, len(root.c1)) 371 root.addattr("c1", "test") 372 self.assertEquals(2, len(root.c1)) 373 self.assertEquals("test", root.c1[1].text)
374
375 - def test_addattr_element(self):
376 root = self.XML(xml_str) 377 self.assertEquals(1, len(root.c1)) 378 379 new_el = self.Element("test", myattr="5") 380 root.addattr("c1", new_el) 381 self.assertEquals(2, len(root.c1)) 382 self.assertEquals(None, root.c1[0].get("myattr")) 383 self.assertEquals("5", root.c1[1].get("myattr"))
384
385 - def test_addattr_list(self):
386 root = self.XML(xml_str) 387 self.assertEquals(1, len(root.c1)) 388 389 new_el = self.Element("test") 390 self.etree.SubElement(new_el, "a", myattr="A") 391 self.etree.SubElement(new_el, "a", myattr="B") 392 393 root.addattr("c1", list(new_el.a)) 394 self.assertEquals(3, len(root.c1)) 395 self.assertEquals(None, root.c1[0].get("myattr")) 396 self.assertEquals("A", root.c1[1].get("myattr")) 397 self.assertEquals("B", root.c1[2].get("myattr"))
398
399 - def test_child_addattr(self):
400 root = self.XML(xml_str) 401 self.assertEquals(3, len(root.c1.c2)) 402 root.c1.addattr("c2", 3) 403 self.assertEquals(4, len(root.c1.c2)) 404 self.assertEquals("3", root.c1.c2[3].text)
405
406 - def test_child_index(self):
407 root = self.XML(xml_str) 408 self.assertEquals("0", root.c1.c2[0].text) 409 self.assertEquals("1", root.c1.c2[1].text) 410 self.assertEquals("2", root.c1.c2[2].text) 411 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
412
413 - def test_child_index_neg(self):
414 root = self.XML(xml_str) 415 self.assertEquals("0", root.c1.c2[0].text) 416 self.assertEquals("0", root.c1.c2[-3].text) 417 self.assertEquals("1", root.c1.c2[-2].text) 418 self.assertEquals("2", root.c1.c2[-1].text) 419 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
420
421 - def test_child_len(self):
422 root = self.XML(xml_str) 423 self.assertEquals(1, len(root)) 424 self.assertEquals(1, len(root.c1)) 425 self.assertEquals(3, len(root.c1.c2))
426
427 - def test_child_iter(self):
428 root = self.XML(xml_str) 429 self.assertEquals([root], 430 list(iter(root))) 431 self.assertEquals([root.c1], 432 list(iter(root.c1))) 433 self.assertEquals([root.c1.c2[0], root.c1.c2[1], root.c1.c2[2]], 434 list(iter((root.c1.c2))))
435
436 - def test_class_lookup(self):
437 root = self.XML(xml_str) 438 self.assert_(isinstance(root.c1.c2, objectify.ObjectifiedElement)) 439 self.assertFalse(isinstance(getattr(root.c1, "{otherNS}c2"), 440 objectify.ObjectifiedElement))
441
442 - def test_dir(self):
443 root = self.XML(xml_str) 444 dir_c1 = dir(objectify.ObjectifiedElement) + ['c1'] 445 dir_c1.sort() 446 dir_c2 = dir(objectify.ObjectifiedElement) + ['c2'] 447 dir_c2.sort() 448 449 self.assertEquals(dir_c1, dir(root)) 450 self.assertEquals(dir_c2, dir(root.c1))
451
452 - def test_vars(self):
453 root = self.XML(xml_str) 454 self.assertEquals({'c1' : root.c1}, vars(root)) 455 self.assertEquals({'c2' : root.c1.c2}, vars(root.c1))
456
457 - def test_child_set_ro(self):
458 root = self.XML(xml_str) 459 self.assertRaises(TypeError, setattr, root.c1.c2, 'text', "test") 460 self.assertRaises(TypeError, setattr, root.c1.c2, 'pyval', "test")
461 462 # slicing 463
464 - def test_getslice_complete(self):
465 root = self.XML("<root><c>c1</c><c>c2</c></root>") 466 self.assertEquals(["c1", "c2"], 467 [ c.text for c in root.c[:] ])
468
469 - def test_getslice_partial(self):
470 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>") 471 test_list = ["c1", "c2", "c3", "c4"] 472 473 self.assertEquals(test_list, 474 [ c.text for c in root.c[:] ]) 475 self.assertEquals(test_list[1:2], 476 [ c.text for c in root.c[1:2] ]) 477 self.assertEquals(test_list[-3:-1], 478 [ c.text for c in root.c[-3:-1] ]) 479 self.assertEquals(test_list[-3:3], 480 [ c.text for c in root.c[-3:3] ]) 481 self.assertEquals(test_list[-3000:3], 482 [ c.text for c in root.c[-3000:3] ]) 483 self.assertEquals(test_list[-3:3000], 484 [ c.text for c in root.c[-3:3000] ])
485
486 - def test_getslice_partial_neg(self):
487 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>") 488 test_list = ["c1", "c2", "c3", "c4"] 489 490 self.assertEquals(test_list, 491 [ c.text for c in root.c[:] ]) 492 self.assertEquals(test_list[2:1:-1], 493 [ c.text for c in root.c[2:1:-1] ]) 494 self.assertEquals(test_list[-1:-3:-1], 495 [ c.text for c in root.c[-1:-3:-1] ]) 496 self.assertEquals(test_list[2:-3:-1], 497 [ c.text for c in root.c[2:-3:-1] ]) 498 self.assertEquals(test_list[2:-3000:-1], 499 [ c.text for c in root.c[2:-3000:-1] ])
500 501 # slice assignment 502
503 - def test_setslice_complete(self):
504 Element = self.Element 505 root = Element("root") 506 root.c = ["c1", "c2"] 507 508 c1 = root.c[0] 509 c2 = root.c[1] 510 511 self.assertEquals([c1,c2], list(root.c)) 512 self.assertEquals(["c1", "c2"], 513 [ c.text for c in root.c ])
514
515 - def test_setslice_elements(self):
516 Element = self.Element 517 root = Element("root") 518 root.c = ["c1", "c2"] 519 520 c1 = root.c[0] 521 c2 = root.c[1] 522 523 self.assertEquals([c1,c2], list(root.c)) 524 self.assertEquals(["c1", "c2"], 525 [ c.text for c in root.c ]) 526 527 root2 = Element("root2") 528 root2.el = [ "test", "test" ] 529 self.assertEquals(["test", "test"], 530 [ el.text for el in root2.el ]) 531 532 root.c = [ root2.el, root2.el ] 533 self.assertEquals(["test", "test"], 534 [ c.text for c in root.c ]) 535 self.assertEquals(["test", "test"], 536 [ el.text for el in root2.el ]) 537 538 root.c[:] = [ c1, c2, c2, c1 ] 539 self.assertEquals(["c1", "c2", "c2", "c1"], 540 [ c.text for c in root.c ])
541
542 - def test_setslice_partial(self):
543 Element = self.Element 544 root = Element("root") 545 l = ["c1", "c2", "c3", "c4"] 546 root.c = l 547 548 self.assertEquals(["c1", "c2", "c3", "c4"], 549 [ c.text for c in root.c ]) 550 self.assertEquals(l, 551 [ c.text for c in root.c ]) 552 553 new_slice = ["cA", "cB"] 554 l[1:2] = new_slice 555 root.c[1:2] = new_slice 556 557 self.assertEquals(["c1", "cA", "cB", "c3", "c4"], l) 558 self.assertEquals(["c1", "cA", "cB", "c3", "c4"], 559 [ c.text for c in root.c ]) 560 self.assertEquals(l, 561 [ c.text for c in root.c ])
562
563 - def test_setslice_insert(self):
564 Element = self.Element 565 root = Element("root") 566 l = ["c1", "c2", "c3", "c4"] 567 root.c = l 568 569 self.assertEquals(["c1", "c2", "c3", "c4"], 570 [ c.text for c in root.c ]) 571 self.assertEquals(l, 572 [ c.text for c in root.c ]) 573 574 new_slice = ["cA", "cB"] 575 l[1:1] = new_slice 576 root.c[1:1] = new_slice 577 578 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"], l) 579 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"], 580 [ c.text for c in root.c ]) 581 self.assertEquals(l, 582 [ c.text for c in root.c ])
583
584 - def test_setslice_insert_neg(self):
585 Element = self.Element 586 root = Element("root") 587 l = ["c1", "c2", "c3", "c4"] 588 root.c = l 589 590 self.assertEquals(["c1", "c2", "c3", "c4"], 591 [ c.text for c in root.c ]) 592 self.assertEquals(l, 593 [ c.text for c in root.c ]) 594 595 new_slice = ["cA", "cB"] 596 l[-2:-2] = new_slice 597 root.c[-2:-2] = new_slice 598 599 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"], l) 600 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"], 601 [ c.text for c in root.c ]) 602 self.assertEquals(l, 603 [ c.text for c in root.c ])
604
605 - def test_setslice_empty(self):
606 Element = self.Element 607 root = Element("root") 608 609 root.c = [] 610 self.assertRaises( 611 AttributeError, getattr, root, 'c')
612
614 Element = self.Element 615 root = Element("root") 616 l = ["c1", "c2", "c3", "c4"] 617 root.c = l 618 619 self.assertEquals(["c1", "c2", "c3", "c4"], 620 [ c.text for c in root.c ]) 621 self.assertEquals(l, 622 [ c.text for c in root.c ]) 623 624 new_slice = ["cA", "cB", "cC"] 625 self.assertRaises( 626 ValueError, operator.setitem, 627 l, slice(1,2,-1), new_slice) 628 self.assertRaises( 629 ValueError, operator.setitem, 630 root.c, slice(1,2,-1), new_slice)
631
632 - def test_setslice_partial_neg(self):
633 Element = self.Element 634 root = Element("root") 635 l = ["c1", "c2", "c3", "c4"] 636 root.c = l 637 638 self.assertEquals(["c1", "c2", "c3", "c4"], 639 [ c.text for c in root.c ]) 640 self.assertEquals(l, 641 [ c.text for c in root.c ]) 642 643 new_slice = ["cA", "cB"] 644 l[-1:1:-1] = new_slice 645 root.c[-1:1:-1] = new_slice 646 647 self.assertEquals(["c1", "c2", "cB", "cA"], l) 648 self.assertEquals(["c1", "c2", "cB", "cA"], 649 [ c.text for c in root.c ]) 650 self.assertEquals(l, 651 [ c.text for c in root.c ])
652
654 Element = self.Element 655 root = Element("root") 656 l = ["c1", "c2", "c3", "c4"] 657 root.c = l 658 659 self.assertEquals(["c1", "c2", "c3", "c4"], 660 [ c.text for c in root.c ]) 661 self.assertEquals(l, 662 [ c.text for c in root.c ]) 663 664 new_slice = ["cA", "cB"] 665 l[-1:-4:-2] = new_slice 666 root.c[-1:-4:-2] = new_slice 667 668 self.assertEquals(["c1", "cB", "c3", "cA"], l) 669 self.assertEquals(["c1", "cB", "c3", "cA"], 670 [ c.text for c in root.c ]) 671 self.assertEquals(l, 672 [ c.text for c in root.c ])
673 674 # other stuff 675
676 - def test_set_string(self):
677 # make sure strings are not handled as sequences 678 Element = self.Element 679 root = Element("root") 680 root.c = "TEST" 681 self.assertEquals(["TEST"], 682 [ c.text for c in root.c ])
683
684 - def test_setitem_string(self):
685 # make sure strings are set as children 686 Element = self.Element 687 root = Element("root") 688 root["c"] = "TEST" 689 self.assertEquals(["TEST"], 690 [ c.text for c in root.c ])
691
693 # make sure 'text' etc. are set as children 694 Element = self.Element 695 root = Element("root") 696 697 root["text"] = "TEST" 698 self.assertEquals(["TEST"], 699 [ c.text for c in root["text"] ]) 700 701 root["tail"] = "TEST" 702 self.assertEquals(["TEST"], 703 [ c.text for c in root["tail"] ]) 704 705 root["pyval"] = "TEST" 706 self.assertEquals(["TEST"], 707 [ c.text for c in root["pyval"] ]) 708 709 root["tag"] = "TEST" 710 self.assertEquals(["TEST"], 711 [ c.text for c in root["tag"] ])
712
713 - def test_findall(self):
714 XML = self.XML 715 root = XML('<a><b><c/></b><b/><c><b/></c></a>') 716 self.assertEquals(1, len(root.findall("c"))) 717 self.assertEquals(2, len(root.findall(".//c"))) 718 self.assertEquals(3, len(root.findall(".//b"))) 719 self.assert_(root.findall(".//b")[1] is root.getchildren()[1])
720
721 - def test_findall_ns(self):
722 XML = self.XML 723 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>') 724 self.assertEquals(2, len(root.findall(".//{X}b"))) 725 self.assertEquals(3, len(root.findall(".//b"))) 726 self.assertEquals(2, len(root.findall("b")))
727
728 - def test_build_tree(self):
729 root = self.Element('root') 730 root.a = 5 731 root.b = 6 732 self.assert_(isinstance(root, objectify.ObjectifiedElement)) 733 self.assert_(isinstance(root.a, objectify.IntElement)) 734 self.assert_(isinstance(root.b, objectify.IntElement))
735
736 - def test_type_NoneType(self):
737 Element = self.Element 738 SubElement = self.etree.SubElement 739 740 nil_attr = XML_SCHEMA_NIL_ATTR 741 root = Element("{objectified}root") 742 SubElement(root, "{objectified}none") 743 SubElement(root, "{objectified}none", {nil_attr : "true"}) 744 self.assertFalse(isinstance(root.none, objectify.NoneElement)) 745 self.assertFalse(isinstance(root.none[0], objectify.NoneElement)) 746 self.assert_(isinstance(root.none[1], objectify.NoneElement)) 747 self.assertEquals(root.none[1], None) 748 self.assertFalse(root.none[1])
749
750 - def test_data_element_NoneType(self):
751 value = objectify.DataElement(None) 752 self.assert_(isinstance(value, objectify.NoneElement)) 753 self.assertEquals(value, None) 754 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
755
756 - def test_type_bool(self):
757 Element = self.Element 758 SubElement = self.etree.SubElement 759 root = Element("{objectified}root") 760 root.bool = True 761 self.assertEquals(root.bool, True) 762 self.assertEquals(root.bool + root.bool, True + True) 763 self.assertEquals(True + root.bool, True + root.bool) 764 self.assertEquals(root.bool * root.bool, True * True) 765 self.assertEquals(int(root.bool), int(True)) 766 self.assertEquals(complex(root.bool), complex(True)) 767 self.assert_(isinstance(root.bool, objectify.BoolElement)) 768 769 root.bool = False 770 self.assertEquals(root.bool, False) 771 self.assertEquals(root.bool + root.bool, False + False) 772 self.assertEquals(False + root.bool, False + root.bool) 773 self.assertEquals(root.bool * root.bool, False * False) 774 self.assertEquals(int(root.bool), int(False)) 775 self.assertEquals(complex(root.bool), complex(False)) 776 self.assert_(isinstance(root.bool, objectify.BoolElement))
777
778 - def test_data_element_bool(self):
779 value = objectify.DataElement(True) 780 self.assert_(isinstance(value, objectify.BoolElement)) 781 self.assertEquals(value, True) 782 783 value = objectify.DataElement(False) 784 self.assert_(isinstance(value, objectify.BoolElement)) 785 self.assertEquals(value, False)
786
787 - def test_type_str(self):
788 Element = self.Element 789 SubElement = self.etree.SubElement 790 root = Element("{objectified}root") 791 root.s = "test" 792 self.assert_(isinstance(root.s, objectify.StringElement))
793
794 - def test_type_str_intliteral(self):
795 Element = self.Element 796 SubElement = self.etree.SubElement 797 root = Element("{objectified}root") 798 root.s = "3" 799 self.assert_(isinstance(root.s, objectify.StringElement))
800
801 - def test_type_str_floatliteral(self):
802 Element = self.Element 803 SubElement = self.etree.SubElement 804 root = Element("{objectified}root") 805 root.s = "3.72" 806 self.assert_(isinstance(root.s, objectify.StringElement))
807
808 - def test_type_str_mul(self):
809 Element = self.Element 810 SubElement = self.etree.SubElement 811 root = Element("{objectified}root") 812 root.s = "test" 813 814 self.assertEquals("test" * 5, root.s * 5) 815 self.assertEquals(5 * "test", 5 * root.s) 816 817 self.assertRaises(TypeError, operator.mul, root.s, "honk") 818 self.assertRaises(TypeError, operator.mul, "honk", root.s)
819
820 - def test_type_str_add(self):
821 Element = self.Element 822 SubElement = self.etree.SubElement 823 root = Element("{objectified}root") 824 root.s = "test" 825 826 s = "toast" 827 self.assertEquals("test" + s, root.s + s) 828 self.assertEquals(s + "test", s + root.s)
829
830 - def test_type_str_mod(self):
831 s = "%d %f %s %r" 832 el = objectify.DataElement(s) 833 values = (1, 7.0, "abcd", None) 834 self.assertEquals(s % values, el % values) 835 836 s = "%d" 837 el = objectify.DataElement(s) 838 val = 5 839 self.assertEquals(s % val, el % val) 840 841 s = "%d %s" 842 el = objectify.DataElement(s) 843 val = 5 844 self.assertRaises(TypeError, el.__mod__, val) 845 846 s = "" 847 el = objectify.DataElement(s) 848 val = 5 849 self.assertRaises(TypeError, el.__mod__, val)
850
851 - def test_type_str_as_int(self):
852 v = "1" 853 el = objectify.DataElement(v) 854 self.assertEquals(int(el), 1)
855
856 - def test_type_str_as_float(self):
857 v = "1" 858 el = objectify.DataElement(v) 859 self.assertEquals(float(el), 1)
860
861 - def test_type_str_as_complex(self):
862 v = "1" 863 el = objectify.DataElement(v) 864 self.assertEquals(complex(el), 1)
865
867 s = "%d %f %s %r" 868 el = objectify.DataElement(s) 869 values = (objectify.DataElement(1), 870 objectify.DataElement(7.0), 871 objectify.DataElement("abcd"), 872 objectify.DataElement(None)) 873 self.assertEquals(s % values, el % values)
874
875 - def test_data_element_str(self):
876 value = objectify.DataElement("test") 877 self.assert_(isinstance(value, objectify.StringElement)) 878 self.assertEquals(value, "test")
879
881 value = objectify.DataElement("3") 882 self.assert_(isinstance(value, objectify.StringElement)) 883 self.assertEquals(value, "3")
884
886 value = objectify.DataElement("3.20") 887 self.assert_(isinstance(value, objectify.StringElement)) 888 self.assertEquals(value, "3.20")
889
890 - def test_type_ustr(self):
891 Element = self.Element 892 SubElement = self.etree.SubElement 893 root = Element("{objectified}root") 894 root.s = _str("test") 895 self.assert_(isinstance(root.s, objectify.StringElement))
896
897 - def test_type_ustr_intliteral(self):
898 Element = self.Element 899 SubElement = self.etree.SubElement 900 root = Element("{objectified}root") 901 root.s = _str("3") 902 self.assert_(isinstance(root.s, objectify.StringElement))
903
905 Element = self.Element 906 SubElement = self.etree.SubElement 907 root = Element("{objectified}root") 908 root.s = _str("3.72") 909 self.assert_(isinstance(root.s, objectify.StringElement))
910
911 - def test_type_ustr_mul(self):
912 Element = self.Element 913 SubElement = self.etree.SubElement 914 root = Element("{objectified}root") 915 root.s = _str("test") 916 917 self.assertEquals(_str("test") * 5, root.s * 5) 918 self.assertEquals(5 * _str("test"), 5 * root.s) 919 920 self.assertRaises(TypeError, operator.mul, root.s, _str("honk")) 921 self.assertRaises(TypeError, operator.mul, _str("honk"), root.s)
922
923 - def test_type_ustr_add(self):
924 Element = self.Element 925 SubElement = self.etree.SubElement 926 root = Element("{objectified}root") 927 root.s = _str("test") 928 929 s = _str("toast") 930 self.assertEquals(_str("test") + s, root.s + s) 931 self.assertEquals(s + _str("test"), s + root.s)
932
933 - def test_data_element_ustr(self):
934 value = objectify.DataElement(_str("test")) 935 self.assert_(isinstance(value, objectify.StringElement)) 936 self.assertEquals(value, _str("test"))
937
939 value = objectify.DataElement("3") 940 self.assert_(isinstance(value, objectify.StringElement)) 941 self.assertEquals(value, _str("3"))
942
944 value = objectify.DataElement(_str("3.20")) 945 self.assert_(isinstance(value, objectify.StringElement)) 946 self.assertEquals(value, _str("3.20"))
947
948 - def test_type_int(self):
949 Element = self.Element 950 SubElement = self.etree.SubElement 951 root = Element("{objectified}root") 952 root.none = 5 953 self.assert_(isinstance(root.none, objectify.IntElement))
954
955 - def test_data_element_int(self):
956 value = objectify.DataElement(5) 957 self.assert_(isinstance(value, objectify.IntElement)) 958 self.assertEquals(value, 5)
959
960 - def test_type_float(self):
961 Element = self.Element 962 SubElement = self.etree.SubElement 963 root = Element("{objectified}root") 964 root.none = 5.5 965 self.assert_(isinstance(root.none, objectify.FloatElement))
966
967 - def test_data_element_float(self):
968 value = objectify.DataElement(5.5) 969 self.assert_(isinstance(value, objectify.FloatElement)) 970 self.assertEquals(value, 5.5)
971
972 - def test_data_element_xsitypes(self):
973 for xsi, objclass in xsitype2objclass.items(): 974 # 1 is a valid value for all ObjectifiedDataElement classes 975 pyval = 1 976 value = objectify.DataElement(pyval, _xsi=xsi) 977 self.assert_(isinstance(value, objclass), 978 "DataElement(%s, _xsi='%s') returns %s, expected %s" 979 % (pyval, xsi, type(value), objclass))
980
982 for xsi, objclass in xsitype2objclass.items(): 983 # 1 is a valid value for all ObjectifiedDataElement classes 984 pyval = 1 985 value = objectify.DataElement(pyval, _xsi="xsd:%s" % xsi) 986 self.assert_(isinstance(value, objclass), 987 "DataElement(%s, _xsi='%s') returns %s, expected %s" 988 % (pyval, xsi, type(value), objclass))
989
991 for xsi, objclass in xsitype2objclass.items(): 992 # 1 is a valid value for all ObjectifiedDataElement classes 993 self.assertRaises(ValueError, objectify.DataElement, 1, 994 _xsi="foo:%s" % xsi)
995
996 - def test_data_element_pytypes(self):
997 for pytype, objclass in pytype2objclass.items(): 998 # 1 is a valid value for all ObjectifiedDataElement classes 999 pyval = 1 1000 value = objectify.DataElement(pyval, _pytype=pytype) 1001 self.assert_(isinstance(value, objclass), 1002 "DataElement(%s, _pytype='%s') returns %s, expected %s" 1003 % (pyval, pytype, type(value), objclass))
1004
1006 pyval = 1 1007 pytype = "NoneType" 1008 objclass = objectify.NoneElement 1009 value = objectify.DataElement(pyval, _pytype=pytype) 1010 self.assert_(isinstance(value, objclass), 1011 "DataElement(%s, _pytype='%s') returns %s, expected %s" 1012 % (pyval, pytype, type(value), objclass)) 1013 self.assertEquals(value.text, None) 1014 self.assertEquals(value.pyval, None)
1015
1017 # pre-2.0 lxml called NoneElement "none" 1018 pyval = 1 1019 pytype = "none" 1020 objclass = objectify.NoneElement 1021 value = objectify.DataElement(pyval, _pytype=pytype) 1022 self.assert_(isinstance(value, objclass), 1023 "DataElement(%s, _pytype='%s') returns %s, expected %s" 1024 % (pyval, pytype, type(value), objclass)) 1025 self.assertEquals(value.text, None) 1026 self.assertEquals(value.pyval, None)
1027
1028 - def test_type_unregistered(self):
1029 Element = self.Element 1030 SubElement = self.etree.SubElement 1031 class MyFloat(float): 1032 pass
1033 root = Element("{objectified}root") 1034 root.myfloat = MyFloat(5.5) 1035 self.assert_(isinstance(root.myfloat, objectify.FloatElement)) 1036 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
1037
1038 - def test_data_element_unregistered(self):
1039 class MyFloat(float): 1040 pass
1041 value = objectify.DataElement(MyFloat(5.5)) 1042 self.assert_(isinstance(value, objectify.FloatElement)) 1043 self.assertEquals(value, 5.5) 1044 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None) 1045
1046 - def test_schema_types(self):
1047 XML = self.XML 1048 root = XML('''\ 1049 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 1050 <b xsi:type="boolean">true</b> 1051 <b xsi:type="boolean">false</b> 1052 <b xsi:type="boolean">1</b> 1053 <b xsi:type="boolean">0</b> 1054 1055 <f xsi:type="float">5</f> 1056 <f xsi:type="double">5</f> 1057 1058 <s xsi:type="string">5</s> 1059 <s xsi:type="normalizedString">5</s> 1060 <s xsi:type="token">5</s> 1061 <s xsi:type="language">5</s> 1062 <s xsi:type="Name">5</s> 1063 <s xsi:type="NCName">5</s> 1064 <s xsi:type="ID">5</s> 1065 <s xsi:type="IDREF">5</s> 1066 <s xsi:type="ENTITY">5</s> 1067 <s xsi:type="NMTOKEN">5</s> 1068 1069 <l xsi:type="integer">5</l> 1070 <l xsi:type="nonPositiveInteger">5</l> 1071 <l xsi:type="negativeInteger">5</l> 1072 <l xsi:type="long">5</l> 1073 <l xsi:type="nonNegativeInteger">5</l> 1074 <l xsi:type="unsignedLong">5</l> 1075 <l xsi:type="unsignedInt">5</l> 1076 <l xsi:type="positiveInteger">5</l> 1077 1078 <i xsi:type="int">5</i> 1079 <i xsi:type="short">5</i> 1080 <i xsi:type="byte">5</i> 1081 <i xsi:type="unsignedShort">5</i> 1082 <i xsi:type="unsignedByte">5</i> 1083 1084 <n xsi:nil="true"/> 1085 </root> 1086 ''') 1087 1088 for b in root.b: 1089 self.assert_(isinstance(b, objectify.BoolElement)) 1090 self.assertEquals(True, root.b[0]) 1091 self.assertEquals(False, root.b[1]) 1092 self.assertEquals(True, root.b[2]) 1093 self.assertEquals(False, root.b[3]) 1094 1095 for f in root.f: 1096 self.assert_(isinstance(f, objectify.FloatElement)) 1097 self.assertEquals(5, f) 1098 1099 for s in root.s: 1100 self.assert_(isinstance(s, objectify.StringElement)) 1101 self.assertEquals("5", s) 1102 1103 for i in root.i: 1104 self.assert_(isinstance(i, objectify.IntElement)) 1105 self.assertEquals(5, i) 1106 1107 for l in root.l: 1108 self.assert_(isinstance(l, objectify.IntElement)) 1109 self.assertEquals(5, i) 1110 1111 self.assert_(isinstance(root.n, objectify.NoneElement)) 1112 self.assertEquals(None, root.n)
1113
1114 - def test_schema_types_prefixed(self):
1115 XML = self.XML 1116 root = XML('''\ 1117 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1118 xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 1119 <b xsi:type="xsd:boolean">true</b> 1120 <b xsi:type="xsd:boolean">false</b> 1121 <b xsi:type="xsd:boolean">1</b> 1122 <b xsi:type="xsd:boolean">0</b> 1123 1124 <f xsi:type="xsd:float">5</f> 1125 <f xsi:type="xsd:double">5</f> 1126 1127 <s xsi:type="xsd:string">5</s> 1128 <s xsi:type="xsd:normalizedString">5</s> 1129 <s xsi:type="xsd:token">5</s> 1130 <s xsi:type="xsd:language">5</s> 1131 <s xsi:type="xsd:Name">5</s> 1132 <s xsi:type="xsd:NCName">5</s> 1133 <s xsi:type="xsd:ID">5</s> 1134 <s xsi:type="xsd:IDREF">5</s> 1135 <s xsi:type="xsd:ENTITY">5</s> 1136 <s xsi:type="xsd:NMTOKEN">5</s> 1137 1138 <l xsi:type="xsd:integer">5</l> 1139 <l xsi:type="xsd:nonPositiveInteger">5</l> 1140 <l xsi:type="xsd:negativeInteger">5</l> 1141 <l xsi:type="xsd:long">5</l> 1142 <l xsi:type="xsd:nonNegativeInteger">5</l> 1143 <l xsi:type="xsd:unsignedLong">5</l> 1144 <l xsi:type="xsd:unsignedInt">5</l> 1145 <l xsi:type="xsd:positiveInteger">5</l> 1146 1147 <i xsi:type="xsd:int">5</i> 1148 <i xsi:type="xsd:short">5</i> 1149 <i xsi:type="xsd:byte">5</i> 1150 <i xsi:type="xsd:unsignedShort">5</i> 1151 <i xsi:type="xsd:unsignedByte">5</i> 1152 1153 <n xsi:nil="true"/> 1154 </root> 1155 ''') 1156 1157 for b in root.b: 1158 self.assert_(isinstance(b, objectify.BoolElement)) 1159 self.assertEquals(True, root.b[0]) 1160 self.assertEquals(False, root.b[1]) 1161 self.assertEquals(True, root.b[2]) 1162 self.assertEquals(False, root.b[3]) 1163 1164 for f in root.f: 1165 self.assert_(isinstance(f, objectify.FloatElement)) 1166 self.assertEquals(5, f) 1167 1168 for s in root.s: 1169 self.assert_(isinstance(s, objectify.StringElement)) 1170 self.assertEquals("5", s) 1171 1172 for i in root.i: 1173 self.assert_(isinstance(i, objectify.IntElement)) 1174 self.assertEquals(5, i) 1175 1176 for l in root.l: 1177 self.assert_(isinstance(l, objectify.IntElement)) 1178 self.assertEquals(5, l) 1179 1180 self.assert_(isinstance(root.n, objectify.NoneElement)) 1181 self.assertEquals(None, root.n)
1182
1183 - def test_type_str_sequence(self):
1184 XML = self.XML 1185 root = XML(_bytes('<root><b>why</b><b>try</b></root>')) 1186 strs = [ str(s) for s in root.b ] 1187 self.assertEquals(["why", "try"], 1188 strs)
1189
1190 - def test_type_str_cmp(self):
1191 XML = self.XML 1192 root = XML(_bytes('<root><b>test</b><b>taste</b><b></b><b/></root>')) 1193 self.assertFalse(root.b[0] < root.b[1]) 1194 self.assertFalse(root.b[0] <= root.b[1]) 1195 self.assertFalse(root.b[0] == root.b[1]) 1196 1197 self.assert_(root.b[0] != root.b[1]) 1198 self.assert_(root.b[0] >= root.b[1]) 1199 self.assert_(root.b[0] > root.b[1]) 1200 1201 self.assertEquals(root.b[0], "test") 1202 self.assertEquals("test", root.b[0]) 1203 1204 self.assertEquals("", root.b[2]) 1205 self.assertEquals(root.b[2], "") 1206 self.assertEquals("", root.b[3]) 1207 self.assertEquals(root.b[3], "") 1208 self.assertEquals(root.b[2], root.b[3]) 1209 1210 root.b = "test" 1211 self.assert_(root.b) 1212 root.b = "" 1213 self.assertFalse(root.b) 1214 self.assertEquals(root.b, "") 1215 self.assertEquals("", root.b)
1216
1217 - def test_type_int_cmp(self):
1218 XML = self.XML 1219 root = XML(_bytes('<root><b>5</b><b>6</b></root>')) 1220 self.assert_(root.b[0] < root.b[1]) 1221 self.assert_(root.b[0] <= root.b[1]) 1222 self.assert_(root.b[0] != root.b[1]) 1223 1224 self.assertFalse(root.b[0] == root.b[1]) 1225 self.assertFalse(root.b[0] >= root.b[1]) 1226 self.assertFalse(root.b[0] > root.b[1]) 1227 1228 self.assertEquals(root.b[0], 5) 1229 self.assertEquals(5, root.b[0]) 1230 self.assertNotEquals(root.b[0], "5") 1231 1232 root.b = 5 1233 self.assert_(root.b) 1234 root.b = 0 1235 self.assertFalse(root.b)
1236 1237 # float + long share the NumberElement implementation with int 1238
1239 - def test_type_bool_cmp(self):
1240 XML = self.XML 1241 root = XML(_bytes('<root><b>false</b><b>true</b></root>')) 1242 self.assert_(root.b[0] < root.b[1]) 1243 self.assert_(root.b[0] <= root.b[1]) 1244 self.assert_(root.b[0] != root.b[1]) 1245 1246 self.assertFalse(root.b[0] == root.b[1]) 1247 self.assertFalse(root.b[0] >= root.b[1]) 1248 self.assertFalse(root.b[0] > root.b[1]) 1249 1250 self.assertFalse(root.b[0]) 1251 self.assert_(root.b[1]) 1252 1253 self.assertEquals(root.b[0], False) 1254 self.assertEquals(False, root.b[0]) 1255 self.assert_(root.b[0] < 5) 1256 self.assert_(5 > root.b[0]) 1257 1258 root.b = True 1259 self.assert_(root.b) 1260 root.b = False 1261 self.assertFalse(root.b)
1262
1263 - def test_type_none_cmp(self):
1264 XML = self.XML 1265 root = XML(_bytes(""" 1266 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 1267 <b xsi:nil="true"></b><b xsi:nil="true"/> 1268 </root>""")) 1269 self.assert_(root.b[0] == root.b[1]) 1270 self.assertFalse(root.b[0]) 1271 self.assertEquals(root.b[0], None) 1272 self.assertEquals(None, root.b[0])
1273 1274 # doesn't work in Py3: 1275 1276 #for comparison in ["abc", 5, 7.3, True, [], ()]: 1277 # none = root.b[1] 1278 # self.assert_(none < comparison, "%s (%s) should be < %s" % 1279 # (none, type(none), comparison) ) 1280 # self.assert_(comparison > none, "%s should be > %s (%s)" % 1281 # (comparison, none, type(none)) ) 1282
1283 - def test_dataelement_xsi(self):
1284 el = objectify.DataElement(1, _xsi="string") 1285 self.assertEquals( 1286 el.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), 1287 'xsd:string')
1288
1289 - def test_dataelement_xsi_nsmap(self):
1290 el = objectify.DataElement(1, _xsi="string", 1291 nsmap={'schema': XML_SCHEMA_NS}) 1292 self.assertEquals( 1293 el.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), 1294 'schema:string')
1295
1296 - def test_dataelement_xsi_prefix_error(self):
1297 self.assertRaises(ValueError, objectify.DataElement, 1, 1298 _xsi="foo:string")
1299
1300 - def test_pytype_annotation(self):
1301 XML = self.XML 1302 root = XML(_bytes('''\ 1303 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1304 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1305 <b>5</b> 1306 <b>test</b> 1307 <c>1.1</c> 1308 <c>\uF8D2</c> 1309 <x>true</x> 1310 <n xsi:nil="true" /> 1311 <n></n> 1312 <b xsi:type="double">5</b> 1313 <b xsi:type="float">5</b> 1314 <s xsi:type="string">23</s> 1315 <s py:pytype="str">42</s> 1316 <f py:pytype="float">300</f> 1317 <l py:pytype="long">2</l> 1318 <t py:pytype="TREE"></t> 1319 </a> 1320 ''')) 1321 objectify.annotate(root) 1322 1323 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1324 for c in root.iterchildren() ] 1325 self.assertEquals("int", child_types[ 0]) 1326 self.assertEquals("str", child_types[ 1]) 1327 self.assertEquals("float", child_types[ 2]) 1328 self.assertEquals("str", child_types[ 3]) 1329 self.assertEquals("bool", child_types[ 4]) 1330 self.assertEquals("NoneType", child_types[ 5]) 1331 self.assertEquals(None, child_types[ 6]) 1332 self.assertEquals("float", child_types[ 7]) 1333 self.assertEquals("float", child_types[ 8]) 1334 self.assertEquals("str", child_types[ 9]) 1335 self.assertEquals("int", child_types[10]) 1336 self.assertEquals("int", child_types[11]) 1337 self.assertEquals("int", child_types[12]) 1338 self.assertEquals(None, child_types[13]) 1339 1340 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1341
1342 - def test_pytype_annotation_empty(self):
1343 XML = self.XML 1344 root = XML(_bytes('''\ 1345 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1346 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1347 <n></n> 1348 </a> 1349 ''')) 1350 objectify.annotate(root) 1351 1352 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1353 for c in root.iterchildren() ] 1354 self.assertEquals(None, child_types[0]) 1355 1356 objectify.annotate(root, empty_pytype="str") 1357 1358 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1359 for c in root.iterchildren() ] 1360 self.assertEquals("str", child_types[0])
1361
1362 - def test_pytype_annotation_use_old(self):
1363 XML = self.XML 1364 root = XML(_bytes('''\ 1365 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1366 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1367 <b>5</b> 1368 <b>test</b> 1369 <c>1.1</c> 1370 <c>\uF8D2</c> 1371 <x>true</x> 1372 <n xsi:nil="true" /> 1373 <n></n> 1374 <b xsi:type="double">5</b> 1375 <b xsi:type="float">5</b> 1376 <s xsi:type="string">23</s> 1377 <s py:pytype="str">42</s> 1378 <f py:pytype="float">300</f> 1379 <l py:pytype="long">2</l> 1380 <t py:pytype="TREE"></t> 1381 </a> 1382 ''')) 1383 objectify.annotate(root, ignore_old=False) 1384 1385 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1386 for c in root.iterchildren() ] 1387 self.assertEquals("int", child_types[ 0]) 1388 self.assertEquals("str", child_types[ 1]) 1389 self.assertEquals("float", child_types[ 2]) 1390 self.assertEquals("str", child_types[ 3]) 1391 self.assertEquals("bool", child_types[ 4]) 1392 self.assertEquals("NoneType", child_types[ 5]) 1393 self.assertEquals(None, child_types[ 6]) 1394 self.assertEquals("float", child_types[ 7]) 1395 self.assertEquals("float", child_types[ 8]) 1396 self.assertEquals("str", child_types[ 9]) 1397 self.assertEquals("str", child_types[10]) 1398 self.assertEquals("float", child_types[11]) 1399 self.assertEquals("int", child_types[12]) 1400 self.assertEquals(TREE_PYTYPE, child_types[13]) 1401 1402 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1403
1404 - def test_pytype_xsitype_annotation(self):
1405 XML = self.XML 1406 root = XML(_bytes('''\ 1407 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1408 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1409 <b>5</b> 1410 <b>test</b> 1411 <c>1.1</c> 1412 <c>\uF8D2</c> 1413 <x>true</x> 1414 <n xsi:nil="true" /> 1415 <n></n> 1416 <b xsi:type="double">5</b> 1417 <b xsi:type="float">5</b> 1418 <s xsi:type="string">23</s> 1419 <s py:pytype="str">42</s> 1420 <f py:pytype="float">300</f> 1421 <l py:pytype="long">2</l> 1422 <t py:pytype="TREE"></t> 1423 </a> 1424 ''')) 1425 objectify.annotate(root, ignore_old=False, ignore_xsi=False, 1426 annotate_xsi=1, annotate_pytype=1) 1427 1428 # check py annotations 1429 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1430 for c in root.iterchildren() ] 1431 self.assertEquals("int", child_types[ 0]) 1432 self.assertEquals("str", child_types[ 1]) 1433 self.assertEquals("float", child_types[ 2]) 1434 self.assertEquals("str", child_types[ 3]) 1435 self.assertEquals("bool", child_types[ 4]) 1436 self.assertEquals("NoneType", child_types[ 5]) 1437 self.assertEquals(None, child_types[ 6]) 1438 self.assertEquals("float", child_types[ 7]) 1439 self.assertEquals("float", child_types[ 8]) 1440 self.assertEquals("str", child_types[ 9]) 1441 self.assertEquals("str", child_types[10]) 1442 self.assertEquals("float", child_types[11]) 1443 self.assertEquals("int", child_types[12]) 1444 self.assertEquals(TREE_PYTYPE, child_types[13]) 1445 1446 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR)) 1447 1448 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1449 for c in root.iterchildren() ] 1450 1451 # check xsi annotations 1452 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1453 for c in root.iterchildren() ] 1454 self.assertEquals("xsd:integer", child_types[ 0]) 1455 self.assertEquals("xsd:string", child_types[ 1]) 1456 self.assertEquals("xsd:double", child_types[ 2]) 1457 self.assertEquals("xsd:string", child_types[ 3]) 1458 self.assertEquals("xsd:boolean", child_types[ 4]) 1459 self.assertEquals(None, child_types[ 5]) 1460 self.assertEquals(None, child_types[ 6]) 1461 self.assertEquals("xsd:double", child_types[ 7]) 1462 self.assertEquals("xsd:float", child_types[ 8]) 1463 self.assertEquals("xsd:string", child_types[ 9]) 1464 self.assertEquals("xsd:string", child_types[10]) 1465 self.assertEquals("xsd:double", child_types[11]) 1466 self.assertEquals("xsd:integer", child_types[12]) 1467 self.assertEquals(None, child_types[13]) 1468 1469 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1470
1471 - def test_xsiannotate_use_old(self):
1472 XML = self.XML 1473 root = XML(_bytes('''\ 1474 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1475 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1476 <b>5</b> 1477 <b>test</b> 1478 <c>1.1</c> 1479 <c>\uF8D2</c> 1480 <x>true</x> 1481 <n xsi:nil="true" /> 1482 <n></n> 1483 <b xsi:type="double">5</b> 1484 <b xsi:type="float">5</b> 1485 <s xsi:type="string">23</s> 1486 <s py:pytype="str">42</s> 1487 <f py:pytype="float">300</f> 1488 <l py:pytype="long">2</l> 1489 <t py:pytype="TREE"></t> 1490 </a> 1491 ''')) 1492 objectify.xsiannotate(root, ignore_old=False) 1493 1494 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1495 for c in root.iterchildren() ] 1496 self.assertEquals("xsd:integer", child_types[ 0]) 1497 self.assertEquals("xsd:string", child_types[ 1]) 1498 self.assertEquals("xsd:double", child_types[ 2]) 1499 self.assertEquals("xsd:string", child_types[ 3]) 1500 self.assertEquals("xsd:boolean", child_types[ 4]) 1501 self.assertEquals(None, child_types[ 5]) 1502 self.assertEquals(None, child_types[ 6]) 1503 self.assertEquals("xsd:double", child_types[ 7]) 1504 self.assertEquals("xsd:float", child_types[ 8]) 1505 self.assertEquals("xsd:string", child_types[ 9]) 1506 self.assertEquals("xsd:string", child_types[10]) 1507 self.assertEquals("xsd:double", child_types[11]) 1508 self.assertEquals("xsd:integer", child_types[12]) 1509 self.assertEquals(None, child_types[13])
1510
1511 - def test_pyannotate_ignore_old(self):
1512 XML = self.XML 1513 root = XML(_bytes('''\ 1514 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1515 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1516 <b>5</b> 1517 <b>test</b> 1518 <c>1.1</c> 1519 <c>\uF8D2</c> 1520 <x>true</x> 1521 <n xsi:nil="true" /> 1522 <n></n> 1523 <b xsi:type="double">5</b> 1524 <b xsi:type="float">5</b> 1525 <s xsi:type="string">23</s> 1526 <s py:pytype="str">42</s> 1527 <f py:pytype="float">300</f> 1528 <l py:pytype="long">2</l> 1529 <t py:pytype="TREE"></t> 1530 </a> 1531 ''')) 1532 objectify.pyannotate(root, ignore_old=True) 1533 1534 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1535 for c in root.iterchildren() ] 1536 self.assertEquals("int", child_types[ 0]) 1537 self.assertEquals("str", child_types[ 1]) 1538 self.assertEquals("float", child_types[ 2]) 1539 self.assertEquals("str", child_types[ 3]) 1540 self.assertEquals("bool", child_types[ 4]) 1541 self.assertEquals("NoneType", child_types[ 5]) 1542 self.assertEquals(None, child_types[ 6]) 1543 self.assertEquals("float", child_types[ 7]) 1544 self.assertEquals("float", child_types[ 8]) 1545 self.assertEquals("str", child_types[ 9]) 1546 self.assertEquals("int", child_types[10]) 1547 self.assertEquals("int", child_types[11]) 1548 self.assertEquals("int", child_types[12]) 1549 self.assertEquals(None, child_types[13]) 1550 1551 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1552
1553 - def test_pyannotate_empty(self):
1554 XML = self.XML 1555 root = XML('''\ 1556 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1557 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1558 <n></n> 1559 </a> 1560 ''') 1561 objectify.pyannotate(root) 1562 1563 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1564 for c in root.iterchildren() ] 1565 self.assertEquals(None, child_types[0]) 1566 1567 objectify.annotate(root, empty_pytype="str") 1568 1569 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1570 for c in root.iterchildren() ] 1571 self.assertEquals("str", child_types[0])
1572
1573 - def test_pyannotate_use_old(self):
1574 XML = self.XML 1575 root = XML('''\ 1576 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1577 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1578 <b>5</b> 1579 <b>test</b> 1580 <c>1.1</c> 1581 <c>\uF8D2</c> 1582 <x>true</x> 1583 <n xsi:nil="true" /> 1584 <n></n> 1585 <b xsi:type="double">5</b> 1586 <b xsi:type="float">5</b> 1587 <s xsi:type="string">23</s> 1588 <s py:pytype="str">42</s> 1589 <f py:pytype="float">300</f> 1590 <l py:pytype="long">2</l> 1591 <t py:pytype="TREE"></t> 1592 </a> 1593 ''') 1594 objectify.pyannotate(root) 1595 1596 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1597 for c in root.iterchildren() ] 1598 self.assertEquals("int", child_types[ 0]) 1599 self.assertEquals("str", child_types[ 1]) 1600 self.assertEquals("float", child_types[ 2]) 1601 self.assertEquals("str", child_types[ 3]) 1602 self.assertEquals("bool", child_types[ 4]) 1603 self.assertEquals("NoneType", child_types[ 5]) 1604 self.assertEquals(None, child_types[ 6]) 1605 self.assertEquals("float", child_types[ 7]) 1606 self.assertEquals("float", child_types[ 8]) 1607 self.assertEquals("str", child_types[ 9]) 1608 self.assertEquals("str", child_types[10]) 1609 self.assertEquals("float", child_types[11]) 1610 self.assertEquals("int", child_types[12]) 1611 self.assertEquals(TREE_PYTYPE, child_types[13]) 1612 1613 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1614
1615 - def test_xsiannotate_ignore_old(self):
1616 XML = self.XML 1617 root = XML(_bytes('''\ 1618 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1619 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1620 <b>5</b> 1621 <b>test</b> 1622 <c>1.1</c> 1623 <c>\uF8D2</c> 1624 <x>true</x> 1625 <n xsi:nil="true" /> 1626 <n></n> 1627 <b xsi:type="double">5</b> 1628 <b xsi:type="float">5</b> 1629 <s xsi:type="string">23</s> 1630 <s py:pytype="str">42</s> 1631 <f py:pytype="float">300</f> 1632 <l py:pytype="long">2</l> 1633 <t py:pytype="TREE"></t> 1634 </a> 1635 ''')) 1636 objectify.xsiannotate(root, ignore_old=True) 1637 1638 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1639 for c in root.iterchildren() ] 1640 self.assertEquals("xsd:integer", child_types[ 0]) 1641 self.assertEquals("xsd:string", child_types[ 1]) 1642 self.assertEquals("xsd:double", child_types[ 2]) 1643 self.assertEquals("xsd:string", child_types[ 3]) 1644 self.assertEquals("xsd:boolean", child_types[ 4]) 1645 self.assertEquals(None, child_types[ 5]) 1646 self.assertEquals(None, child_types[ 6]) 1647 self.assertEquals("xsd:integer", child_types[ 7]) 1648 self.assertEquals("xsd:integer", child_types[ 8]) 1649 self.assertEquals("xsd:integer", child_types[ 9]) 1650 self.assertEquals("xsd:string", child_types[10]) 1651 self.assertEquals("xsd:double", child_types[11]) 1652 self.assertEquals("xsd:integer", child_types[12]) 1653 self.assertEquals(None, child_types[13]) 1654 1655 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1656
1657 - def test_deannotate(self):
1658 XML = self.XML 1659 root = XML(_bytes('''\ 1660 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1661 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1662 <b>5</b> 1663 <b>test</b> 1664 <c>1.1</c> 1665 <c>\uF8D2</c> 1666 <x>true</x> 1667 <n xsi:nil="true" /> 1668 <n></n> 1669 <b xsi:type="double">5</b> 1670 <b xsi:type="float">5</b> 1671 <s xsi:type="string">23</s> 1672 <s py:pytype="str">42</s> 1673 <f py:pytype="float">300</f> 1674 <l py:pytype="long">2</l> 1675 <t py:pytype="TREE"></t> 1676 </a> 1677 ''')) 1678 objectify.deannotate(root) 1679 1680 for c in root.getiterator(): 1681 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)) 1682 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE)) 1683 1684 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1685
1686 - def test_xsinil_deannotate(self):
1687 XML = self.XML 1688 root = XML(_bytes('''\ 1689 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1690 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1691 <b>5</b> 1692 <b>test</b> 1693 <c>1.1</c> 1694 <c>\uF8D2</c> 1695 <x>true</x> 1696 <n xsi:nil="true" /> 1697 <n></n> 1698 <b xsi:type="double">5</b> 1699 <b xsi:type="float">5</b> 1700 <s xsi:type="string">23</s> 1701 <s py:pytype="str">42</s> 1702 <f py:pytype="float">300</f> 1703 <l py:pytype="long">2</l> 1704 <t py:pytype="TREE"></t> 1705 </a> 1706 ''')) 1707 objectify.annotate( 1708 root, ignore_old=False, ignore_xsi=False, annotate_xsi=True, 1709 empty_pytype='str', empty_type='string') 1710 objectify.deannotate(root, pytype=False, xsi=False, xsi_nil=True) 1711 1712 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1713 for c in root.iterchildren() ] 1714 self.assertEquals("xsd:integer", child_types[ 0]) 1715 self.assertEquals("xsd:string", child_types[ 1]) 1716 self.assertEquals("xsd:double", child_types[ 2]) 1717 self.assertEquals("xsd:string", child_types[ 3]) 1718 self.assertEquals("xsd:boolean", child_types[ 4]) 1719 self.assertEquals(None, child_types[ 5]) 1720 self.assertEquals("xsd:string", child_types[ 6]) 1721 self.assertEquals("xsd:double", child_types[ 7]) 1722 self.assertEquals("xsd:float", child_types[ 8]) 1723 self.assertEquals("xsd:string", child_types[ 9]) 1724 self.assertEquals("xsd:string", child_types[10]) 1725 self.assertEquals("xsd:double", child_types[11]) 1726 self.assertEquals("xsd:integer", child_types[12]) 1727 self.assertEquals(None, child_types[13]) 1728 1729 self.assertEquals(None, root.n.get(XML_SCHEMA_NIL_ATTR)) 1730 1731 for c in root.iterchildren(): 1732 self.assertNotEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE)) 1733 # these have no equivalent in xsi:type 1734 if (c.get(objectify.PYTYPE_ATTRIBUTE) not in [TREE_PYTYPE, 1735 "NoneType"]): 1736 self.assertNotEquals( 1737 None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1738
1739 - def test_xsitype_deannotate(self):
1740 XML = self.XML 1741 root = XML(_bytes('''\ 1742 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1743 xmlns:py="http://codespeak.net/lxml/objectify/pytype" 1744 xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 1745 <b>5</b> 1746 <b>test</b> 1747 <c>1.1</c> 1748 <c>\uF8D2</c> 1749 <x>true</x> 1750 <n xsi:nil="true" /> 1751 <n></n> 1752 <b xsi:type="xsd:double">5</b> 1753 <b xsi:type="xsd:float">5</b> 1754 <s xsi:type="xsd:string">23</s> 1755 <s py:pytype="str">42</s> 1756 <f py:pytype="float">300</f> 1757 <l py:pytype="long">2</l> 1758 <t py:pytype="TREE"></t> 1759 </a> 1760 ''')) 1761 objectify.annotate(root) 1762 objectify.deannotate(root, pytype=False) 1763 1764 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1765 for c in root.iterchildren() ] 1766 self.assertEquals("int", child_types[ 0]) 1767 self.assertEquals("str", child_types[ 1]) 1768 self.assertEquals("float", child_types[ 2]) 1769 self.assertEquals("str", child_types[ 3]) 1770 self.assertEquals("bool", child_types[ 4]) 1771 self.assertEquals("NoneType", child_types[ 5]) 1772 self.assertEquals(None, child_types[ 6]) 1773 self.assertEquals("float", child_types[ 7]) 1774 self.assertEquals("float", child_types[ 8]) 1775 self.assertEquals("str", child_types[ 9]) 1776 self.assertEquals("int", child_types[10]) 1777 self.assertEquals("int", child_types[11]) 1778 self.assertEquals("int", child_types[12]) 1779 self.assertEquals(None, child_types[13]) 1780 1781 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR)) 1782 1783 for c in root.getiterator(): 1784 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1785
1786 - def test_pytype_deannotate(self):
1787 XML = self.XML 1788 root = XML(_bytes('''\ 1789 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1790 xmlns:py="http://codespeak.net/lxml/objectify/pytype" 1791 xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 1792 <b xsi:type="xsd:int">5</b> 1793 <b xsi:type="xsd:string">test</b> 1794 <c xsi:type="xsd:float">1.1</c> 1795 <c xsi:type="xsd:string">\uF8D2</c> 1796 <x xsi:type="xsd:boolean">true</x> 1797 <n xsi:nil="true" /> 1798 <n></n> 1799 <b xsi:type="xsd:double">5</b> 1800 <b xsi:type="xsd:float">5</b> 1801 <s xsi:type="xsd:string">23</s> 1802 <s xsi:type="xsd:string">42</s> 1803 <f xsi:type="xsd:float">300</f> 1804 <l xsi:type="xsd:long">2</l> 1805 <t py:pytype="TREE"></t> 1806 </a> 1807 ''')) 1808 objectify.annotate(root) 1809 objectify.deannotate(root, xsi=False) 1810 1811 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1812 for c in root.iterchildren() ] 1813 self.assertEquals("xsd:int", child_types[ 0]) 1814 self.assertEquals("xsd:string", child_types[ 1]) 1815 self.assertEquals("xsd:float", child_types[ 2]) 1816 self.assertEquals("xsd:string", child_types[ 3]) 1817 self.assertEquals("xsd:boolean", child_types[ 4]) 1818 self.assertEquals(None, child_types[ 5]) 1819 self.assertEquals(None, child_types[ 6]) 1820 self.assertEquals("xsd:double", child_types[ 7]) 1821 self.assertEquals("xsd:float", child_types[ 8]) 1822 self.assertEquals("xsd:string", child_types[ 9]) 1823 self.assertEquals("xsd:string", child_types[10]) 1824 self.assertEquals("xsd:float", child_types[11]) 1825 self.assertEquals("xsd:long", child_types[12]) 1826 self.assertEquals(None, child_types[13]) 1827 1828 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR)) 1829 1830 for c in root.getiterator(): 1831 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1832
1833 - def test_change_pytype_attribute(self):
1834 XML = self.XML 1835 1836 xml = _bytes('''\ 1837 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 1838 <b>5</b> 1839 <b>test</b> 1840 <c>1.1</c> 1841 <c>\uF8D2</c> 1842 <x>true</x> 1843 <n xsi:nil="true" /> 1844 <n></n> 1845 <b xsi:type="double">5</b> 1846 </a> 1847 ''') 1848 1849 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}') 1850 objectify.set_pytype_attribute_tag("{TEST}test") 1851 1852 root = XML(xml) 1853 objectify.annotate(root) 1854 1855 attribs = root.xpath("//@py:%s" % pytype_name, 1856 namespaces={"py" : pytype_ns}) 1857 self.assertEquals(0, len(attribs)) 1858 attribs = root.xpath("//@py:test", 1859 namespaces={"py" : "TEST"}) 1860 self.assertEquals(7, len(attribs)) 1861 1862 objectify.set_pytype_attribute_tag() 1863 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}') 1864 1865 self.assertNotEqual("test", pytype_ns.lower()) 1866 self.assertNotEqual("test", pytype_name.lower()) 1867 1868 root = XML(xml) 1869 attribs = root.xpath("//@py:%s" % pytype_name, 1870 namespaces={"py" : pytype_ns}) 1871 self.assertEquals(0, len(attribs)) 1872 1873 objectify.annotate(root) 1874 attribs = root.xpath("//@py:%s" % pytype_name, 1875 namespaces={"py" : pytype_ns}) 1876 self.assertEquals(7, len(attribs))
1877
1878 - def test_registered_types(self):
1879 orig_types = objectify.getRegisteredTypes() 1880 orig_types[0].unregister() 1881 self.assertEquals(orig_types[1:], objectify.getRegisteredTypes()) 1882 1883 class NewType(objectify.ObjectifiedDataElement): 1884 pass
1885 1886 def checkMyType(s): 1887 return True 1888 1889 pytype = objectify.PyType("mytype", checkMyType, NewType) 1890 self.assert_(pytype not in objectify.getRegisteredTypes()) 1891 pytype.register() 1892 self.assert_(pytype in objectify.getRegisteredTypes()) 1893 pytype.unregister() 1894 self.assert_(pytype not in objectify.getRegisteredTypes()) 1895 1896 pytype.register(before = [objectify.getRegisteredTypes()[0].name]) 1897 self.assertEquals(pytype, objectify.getRegisteredTypes()[0]) 1898 pytype.unregister() 1899 1900 pytype.register(after = [objectify.getRegisteredTypes()[0].name]) 1901 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0]) 1902 pytype.unregister() 1903 1904 self.assertRaises(ValueError, pytype.register, 1905 before = [objectify.getRegisteredTypes()[0].name], 1906 after = [objectify.getRegisteredTypes()[1].name]) 1907
1908 - def test_registered_type_stringify(self):
1909 from datetime import datetime 1910 def parse_date(value): 1911 if len(value) != 14: 1912 raise ValueError(value) 1913 Y = int(value[0:4]) 1914 M = int(value[4:6]) 1915 D = int(value[6:8]) 1916 h = int(value[8:10]) 1917 m = int(value[10:12]) 1918 s = int(value[12:14]) 1919 return datetime(Y, M, D, h, m, s)
1920 1921 def stringify_date(date): 1922 return date.strftime("%Y%m%d%H%M%S") 1923 1924 class DatetimeElement(objectify.ObjectifiedDataElement): 1925 def pyval(self): 1926 return parse_date(self.text) 1927 pyval = property(pyval) 1928 1929 datetime_type = objectify.PyType( 1930 "datetime", parse_date, DatetimeElement, stringify_date) 1931 datetime_type.xmlSchemaTypes = "dateTime" 1932 datetime_type.register() 1933 1934 NAMESPACE = "http://foo.net/xmlns" 1935 NAMESPACE_MAP = {'ns': NAMESPACE} 1936 1937 r = objectify.Element("{%s}root" % NAMESPACE, nsmap=NAMESPACE_MAP) 1938 time = datetime.now() 1939 r.date = time 1940 1941 self.assert_(isinstance(r.date, DatetimeElement)) 1942 self.assert_(isinstance(r.date.pyval, datetime)) 1943 1944 self.assertEquals(r.date.pyval, parse_date(stringify_date(time))) 1945 self.assertEquals(r.date.text, stringify_date(time)) 1946 1947 r.date = objectify.E.date(time) 1948 1949 self.assert_(isinstance(r.date, DatetimeElement)) 1950 self.assert_(isinstance(r.date.pyval, datetime)) 1951 1952 self.assertEquals(r.date.pyval, parse_date(stringify_date(time))) 1953 self.assertEquals(r.date.text, stringify_date(time)) 1954
1955 - def test_object_path(self):
1956 root = self.XML(xml_str) 1957 path = objectify.ObjectPath( "root.c1.c2" ) 1958 self.assertEquals(root.c1.c2.text, path.find(root).text) 1959 self.assertEquals(root.c1.c2.text, path(root).text)
1960
1961 - def test_object_path_list(self):
1962 root = self.XML(xml_str) 1963 path = objectify.ObjectPath( ['root', 'c1', 'c2'] ) 1964 self.assertEquals(root.c1.c2.text, path.find(root).text) 1965 self.assertEquals(root.c1.c2.text, path(root).text)
1966
1967 - def test_object_path_fail(self):
1968 root = self.XML(xml_str) 1969 path = objectify.ObjectPath( "root.c1.c99" ) 1970 self.assertRaises(AttributeError, path, root)
1971
1972 - def test_object_path_default_absolute(self):
1973 root = self.XML(xml_str) 1974 path = objectify.ObjectPath( "root.c1.c99" ) 1975 self.assertEquals(None, path(root, None)) 1976 path = objectify.ObjectPath( "root.c99.c2" ) 1977 self.assertEquals(None, path(root, None)) 1978 path = objectify.ObjectPath( "notroot.c99.c2" ) 1979 self.assertEquals(None, path(root, None))
1980
1981 - def test_object_path_default_relative(self):
1982 root = self.XML(xml_str) 1983 path = objectify.ObjectPath( ".c1.c99" ) 1984 self.assertEquals(None, path(root, None)) 1985 path = objectify.ObjectPath( ".c99.c2" ) 1986 self.assertEquals(None, path(root, None))
1987
1988 - def test_object_path_syntax(self):
1989 root = self.XML(xml_str) 1990 path = objectify.ObjectPath("root . {objectified}c1. c2") 1991 self.assertEquals(root.c1.c2.text, path(root).text) 1992 1993 path = objectify.ObjectPath(" root.{objectified} c1.c2 [ 0 ] ") 1994 self.assertEquals(root.c1.c2.text, path(root).text)
1995
1996 - def test_object_path_fail_parse_empty(self):
1997 self.assertRaises(ValueError, objectify.ObjectPath, "")
1998
1999 - def test_object_path_fail_parse_empty_list(self):
2000 self.assertRaises(ValueError, objectify.ObjectPath, [])
2001
2002 - def test_object_path_hasattr(self):
2003 root = self.XML(xml_str) 2004 path = objectify.ObjectPath( "root" ) 2005 self.assert_(path.hasattr(root)) 2006 path = objectify.ObjectPath( "root.c1" ) 2007 self.assert_(path.hasattr(root)) 2008 path = objectify.ObjectPath( "root.c1.c2" ) 2009 self.assert_(path.hasattr(root)) 2010 path = objectify.ObjectPath( "root.c1.{otherNS}c2" ) 2011 self.assert_(path.hasattr(root)) 2012 path = objectify.ObjectPath( "root.c1.c2[1]" ) 2013 self.assert_(path.hasattr(root)) 2014 path = objectify.ObjectPath( "root.c1.c2[2]" ) 2015 self.assert_(path.hasattr(root)) 2016 path = objectify.ObjectPath( "root.c1.c2[3]" ) 2017 self.assertFalse(path.hasattr(root)) 2018 path = objectify.ObjectPath( "root.c1[1].c2" ) 2019 self.assertFalse(path.hasattr(root))
2020
2021 - def test_object_path_dot(self):
2022 root = self.XML(xml_str) 2023 path = objectify.ObjectPath( "." ) 2024 self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
2025
2026 - def test_object_path_dot_list(self):
2027 root = self.XML(xml_str) 2028 path = objectify.ObjectPath( [''] ) 2029 self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
2030
2031 - def test_object_path_dot_root(self):
2032 root = self.XML(xml_str) 2033 path = objectify.ObjectPath( ".c1.c2" ) 2034 self.assertEquals(root.c1.c2.text, path(root).text)
2035
2036 - def test_object_path_dot_root_list(self):
2037 root = self.XML(xml_str) 2038 path = objectify.ObjectPath( ['', 'c1', 'c2'] ) 2039 self.assertEquals(root.c1.c2.text, path(root).text)
2040
2041 - def test_object_path_index(self):
2042 root = self.XML(xml_str) 2043 path = objectify.ObjectPath( "root.c1[0].c2[0]" ) 2044 self.assertEquals(root.c1.c2.text, path(root).text) 2045 2046 path = objectify.ObjectPath( "root.c1[0].c2" ) 2047 self.assertEquals(root.c1.c2.text, path(root).text) 2048 2049 path = objectify.ObjectPath( "root.c1[0].c2[1]" ) 2050 self.assertEquals(root.c1.c2[1].text, path(root).text) 2051 2052 path = objectify.ObjectPath( "root.c1.c2[2]" ) 2053 self.assertEquals(root.c1.c2[2].text, path(root).text) 2054 2055 path = objectify.ObjectPath( "root.c1.c2[-1]" ) 2056 self.assertEquals(root.c1.c2[-1].text, path(root).text) 2057 2058 path = objectify.ObjectPath( "root.c1.c2[-3]" ) 2059 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2060
2061 - def test_object_path_index_list(self):
2062 root = self.XML(xml_str) 2063 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] ) 2064 self.assertEquals(root.c1.c2.text, path(root).text) 2065 2066 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] ) 2067 self.assertEquals(root.c1.c2[2].text, path(root).text) 2068 2069 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] ) 2070 self.assertEquals(root.c1.c2[2].text, path(root).text) 2071 2072 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] ) 2073 self.assertEquals(root.c1.c2[-1].text, path(root).text) 2074 2075 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] ) 2076 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2077
2078 - def test_object_path_index_fail_parse(self):
2079 self.assertRaises(ValueError, objectify.ObjectPath, 2080 "root.c1[0].c2[-1-2]") 2081 self.assertRaises(ValueError, objectify.ObjectPath, 2082 ['root', 'c1[0]', 'c2[-1-2]']) 2083 2084 self.assertRaises(ValueError, objectify.ObjectPath, 2085 "root[2].c1.c2") 2086 self.assertRaises(ValueError, objectify.ObjectPath, 2087 ['root[2]', 'c1', 'c2']) 2088 2089 self.assertRaises(ValueError, objectify.ObjectPath, 2090 []) 2091 self.assertRaises(ValueError, objectify.ObjectPath, 2092 ['', '', ''])
2093
2094 - def test_object_path_index_fail_lookup(self):
2095 root = self.XML(xml_str) 2096 path = objectify.ObjectPath("root.c1[9999].c2") 2097 self.assertRaises(AttributeError, path, root) 2098 2099 path = objectify.ObjectPath("root.c1[0].c2[9999]") 2100 self.assertRaises(AttributeError, path, root) 2101 2102 path = objectify.ObjectPath(".c1[9999].c2[0]") 2103 self.assertRaises(AttributeError, path, root) 2104 2105 path = objectify.ObjectPath("root.c1[-2].c2") 2106 self.assertRaises(AttributeError, path, root) 2107 2108 path = objectify.ObjectPath("root.c1[0].c2[-4]") 2109 self.assertRaises(AttributeError, path, root)
2110
2111 - def test_object_path_ns(self):
2112 root = self.XML(xml_str) 2113 path = objectify.ObjectPath( "{objectified}root.c1.c2" ) 2114 self.assertEquals(root.c1.c2.text, path.find(root).text) 2115 path = objectify.ObjectPath( "{objectified}root.{objectified}c1.c2" ) 2116 self.assertEquals(root.c1.c2.text, path.find(root).text) 2117 path = objectify.ObjectPath( "root.{objectified}c1.{objectified}c2" ) 2118 self.assertEquals(root.c1.c2.text, path.find(root).text) 2119 path = objectify.ObjectPath( "root.c1.{objectified}c2" ) 2120 self.assertEquals(root.c1.c2.text, path.find(root).text) 2121 path = objectify.ObjectPath( "root.c1.{otherNS}c2" ) 2122 self.assertEquals(getattr(root.c1, '{otherNS}c2').text, 2123 path.find(root).text)
2124
2125 - def test_object_path_ns_list(self):
2126 root = self.XML(xml_str) 2127 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] ) 2128 self.assertEquals(root.c1.c2.text, path.find(root).text) 2129 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] ) 2130 self.assertEquals(root.c1.c2.text, path.find(root).text) 2131 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] ) 2132 self.assertEquals(root.c1.c2.text, path.find(root).text) 2133 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] ) 2134 self.assertEquals(root.c1.c2[2].text, path.find(root).text) 2135 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] ) 2136 self.assertEquals(root.c1.c2.text, path.find(root).text) 2137 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] ) 2138 self.assertEquals(root.c1.c2[2].text, path.find(root).text) 2139 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] ) 2140 self.assertEquals(getattr(root.c1, '{otherNS}c2').text, 2141 path.find(root).text)
2142
2143 - def test_object_path_set(self):
2144 root = self.XML(xml_str) 2145 path = objectify.ObjectPath( "root.c1.c2" ) 2146 self.assertEquals(root.c1.c2.text, path.find(root).text) 2147 self.assertEquals("1", root.c1.c2[1].text) 2148 2149 new_value = "my new value" 2150 path.setattr(root, new_value) 2151 2152 self.assertEquals(new_value, root.c1.c2.text) 2153 self.assertEquals(new_value, path(root).text) 2154 self.assertEquals("1", root.c1.c2[1].text)
2155
2156 - def test_object_path_set_element(self):
2157 root = self.XML(xml_str) 2158 path = objectify.ObjectPath( "root.c1.c2" ) 2159 self.assertEquals(root.c1.c2.text, path.find(root).text) 2160 self.assertEquals("1", root.c1.c2[1].text) 2161 2162 new_el = self.Element("{objectified}test") 2163 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST" 2164 path.setattr(root, new_el.sub) 2165 2166 self.assertEquals("ATTR", root.c1.c2.get("myattr")) 2167 self.assertEquals("TEST", root.c1.c2.a.text) 2168 self.assertEquals("TEST", path(root).a.text) 2169 self.assertEquals("1", root.c1.c2[1].text)
2170
2171 - def test_object_path_set_create(self):
2172 root = self.XML(xml_str) 2173 path = objectify.ObjectPath( "root.c1.c99" ) 2174 self.assertRaises(AttributeError, path.find, root) 2175 2176 new_value = "my new value" 2177 path.setattr(root, new_value) 2178 2179 self.assertEquals(1, len(root.c1.c99)) 2180 self.assertEquals(new_value, root.c1.c99.text) 2181 self.assertEquals(new_value, path(root).text)
2182
2183 - def test_object_path_set_create_element(self):
2184 root = self.XML(xml_str) 2185 path = objectify.ObjectPath( "root.c1.c99" ) 2186 self.assertRaises(AttributeError, path.find, root) 2187 2188 new_el = self.Element("{objectified}test") 2189 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST" 2190 path.setattr(root, new_el.sub) 2191 2192 self.assertEquals(1, len(root.c1.c99)) 2193 self.assertEquals("ATTR", root.c1.c99.get("myattr")) 2194 self.assertEquals("TEST", root.c1.c99.a.text) 2195 self.assertEquals("TEST", path(root).a.text)
2196
2197 - def test_object_path_set_create_list(self):
2198 root = self.XML(xml_str) 2199 path = objectify.ObjectPath( "root.c1.c99" ) 2200 self.assertRaises(AttributeError, path.find, root) 2201 2202 new_el = self.Element("{objectified}test") 2203 new_el.a = ["TEST1", "TEST2"] 2204 new_el.a[0].set("myattr", "ATTR1") 2205 new_el.a[1].set("myattr", "ATTR2") 2206 2207 path.setattr(root, list(new_el.a)) 2208 2209 self.assertEquals(2, len(root.c1.c99)) 2210 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr")) 2211 self.assertEquals("TEST1", root.c1.c99[0].text) 2212 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr")) 2213 self.assertEquals("TEST2", root.c1.c99[1].text) 2214 self.assertEquals("TEST1", path(root).text)
2215
2216 - def test_object_path_addattr(self):
2217 root = self.XML(xml_str) 2218 path = objectify.ObjectPath( "root.c1.c2" ) 2219 self.assertEquals(3, len(root.c1.c2)) 2220 path.addattr(root, "test") 2221 self.assertEquals(4, len(root.c1.c2)) 2222 self.assertEquals(["0", "1", "2", "test"], 2223 [el.text for el in root.c1.c2])
2224
2225 - def test_object_path_addattr_element(self):
2226 root = self.XML(xml_str) 2227 path = objectify.ObjectPath( "root.c1.c2" ) 2228 self.assertEquals(3, len(root.c1.c2)) 2229 2230 new_el = self.Element("{objectified}test") 2231 etree.SubElement(new_el, "{objectified}sub").a = "TEST" 2232 2233 path.addattr(root, new_el.sub) 2234 self.assertEquals(4, len(root.c1.c2)) 2235 self.assertEquals("TEST", root.c1.c2[3].a.text) 2236 self.assertEquals(["0", "1", "2"], 2237 [el.text for el in root.c1.c2[:3]])
2238
2239 - def test_object_path_addattr_create(self):
2240 root = self.XML(xml_str) 2241 path = objectify.ObjectPath( "root.c1.c99" ) 2242 self.assertRaises(AttributeError, path.find, root) 2243 2244 new_value = "my new value" 2245 path.addattr(root, new_value) 2246 2247 self.assertEquals(1, len(root.c1.c99)) 2248 self.assertEquals(new_value, root.c1.c99.text) 2249 self.assertEquals(new_value, path(root).text)
2250
2251 - def test_object_path_addattr_create_element(self):
2252 root = self.XML(xml_str) 2253 path = objectify.ObjectPath( "root.c1.c99" ) 2254 self.assertRaises(AttributeError, path.find, root) 2255 2256 new_el = self.Element("{objectified}test") 2257 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST" 2258 2259 path.addattr(root, new_el.sub) 2260 self.assertEquals(1, len(root.c1.c99)) 2261 self.assertEquals("TEST", root.c1.c99.a.text) 2262 self.assertEquals("TEST", path(root).a.text) 2263 self.assertEquals("ATTR", root.c1.c99.get("myattr"))
2264
2265 - def test_object_path_addattr_create_list(self):
2266 root = self.XML(xml_str) 2267 path = objectify.ObjectPath( "root.c1.c99" ) 2268 self.assertRaises(AttributeError, path.find, root) 2269 2270 new_el = self.Element("{objectified}test") 2271 new_el.a = ["TEST1", "TEST2"] 2272 2273 self.assertEquals(2, len(new_el.a)) 2274 2275 path.addattr(root, list(new_el.a)) 2276 self.assertEquals(2, len(root.c1.c99)) 2277 self.assertEquals("TEST1", root.c1.c99.text) 2278 self.assertEquals("TEST2", path(root)[1].text)
2279
2280 - def test_descendant_paths(self):
2281 root = self.XML(xml_str) 2282 self.assertEquals( 2283 ['{objectified}root', '{objectified}root.c1', 2284 '{objectified}root.c1.c2', 2285 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]', 2286 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'], 2287 root.descendantpaths())
2288
2289 - def test_descendant_paths_child(self):
2290 root = self.XML(xml_str) 2291 self.assertEquals( 2292 ['{objectified}c1', '{objectified}c1.c2', 2293 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]', 2294 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'], 2295 root.c1.descendantpaths())
2296
2297 - def test_descendant_paths_prefix(self):
2298 root = self.XML(xml_str) 2299 self.assertEquals( 2300 ['root.{objectified}c1', 'root.{objectified}c1.c2', 2301 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]', 2302 'root.{objectified}c1.{otherNS}c2', 2303 'root.{objectified}c1.{}c2'], 2304 root.c1.descendantpaths('root'))
2305
2306 - def test_pickle(self):
2307 import pickle 2308 2309 root = self.XML(xml_str) 2310 out = BytesIO() 2311 pickle.dump(root, out) 2312 2313 new_root = pickle.loads(out.getvalue()) 2314 self.assertEquals( 2315 etree.tostring(new_root), 2316 etree.tostring(root))
2317
2318 - def test_pickle_elementtree(self):
2319 import pickle 2320 2321 tree = etree.ElementTree(self.XML(xml_str + "<?my pi?>")) 2322 out = BytesIO() 2323 pickle.dump(tree, out) 2324 2325 new_tree = pickle.loads(out.getvalue()) 2326 self.assert_(isinstance(new_tree, etree._ElementTree)) 2327 self.assertEquals( 2328 etree.tostring(new_tree), 2329 etree.tostring(tree))
2330 2331 # E-Factory tests, need to use sub-elements as root element is always 2332 # type-looked-up as ObjectifiedElement (no annotations)
2333 - def test_efactory_int(self):
2334 E = objectify.E 2335 root = E.root(E.val(23)) 2336 self.assert_(isinstance(root.val, objectify.IntElement))
2337
2338 - def test_efactory_float(self):
2339 E = objectify.E 2340 root = E.root(E.val(233.23)) 2341 self.assert_(isinstance(root.val, objectify.FloatElement))
2342
2343 - def test_efactory_str(self):
2344 E = objectify.E 2345 root = E.root(E.val("what?")) 2346 self.assert_(isinstance(root.val, objectify.StringElement))
2347
2348 - def test_efactory_unicode(self):
2349 E = objectify.E 2350 root = E.root(E.val(_str("blöödy häll", encoding="ISO-8859-1"))) 2351 self.assert_(isinstance(root.val, objectify.StringElement))
2352
2353 - def test_efactory_bool(self):
2354 E = objectify.E 2355 root = E.root(E.val(True)) 2356 self.assert_(isinstance(root.val, objectify.BoolElement))
2357
2358 - def test_efactory_none(self):
2359 E = objectify.E 2360 root = E.root(E.val(None)) 2361 self.assert_(isinstance(root.val, objectify.NoneElement))
2362
2363 - def test_efactory_value_concatenation(self):
2364 E = objectify.E 2365 root = E.root(E.val(1, "foo", 2.0, "bar ", True, None)) 2366 self.assert_(isinstance(root.val, objectify.StringElement))
2367
2368 - def test_efactory_attrib(self):
2369 E = objectify.E 2370 root = E.root(foo="bar") 2371 self.assertEquals(root.get("foo"), "bar")
2372
2373 - def test_efactory_nested(self):
2374 E = objectify.E 2375 DataElement = objectify.DataElement 2376 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1), 2377 DataElement(2.0)) 2378 self.assert_(isinstance(root, objectify.ObjectifiedElement)) 2379 self.assertEquals(root.text, "text") 2380 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement)) 2381 self.assertEquals(root.sub.tail, "tail") 2382 self.assert_(isinstance(root.sub.subsub, objectify.StringElement)) 2383 self.assertEquals(len(root.value), 2) 2384 self.assert_(isinstance(root.value[0], objectify.IntElement)) 2385 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2386
2387 - def test_efactory_subtype(self):
2388 class Attribute(objectify.ObjectifiedDataElement): 2389 def __init__(self): 2390 objectify.ObjectifiedDataElement.__init__(self) 2391 self.set("datatype", "TYPE") 2392 self.set("range", "0.,1.")
2393 2394 attr = Attribute() 2395 self.assertEquals(attr.text, None) 2396 self.assertEquals(attr.get("datatype"), "TYPE") 2397 self.assertEquals(attr.get("range"), "0.,1.") 2398
2399 - def test_XML_base_url_docinfo(self):
2400 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url") 2401 docinfo = root.getroottree().docinfo 2402 self.assertEquals(docinfo.URL, "http://no/such/url")
2403
2404 - def test_XML_set_base_url_docinfo(self):
2405 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url") 2406 docinfo = root.getroottree().docinfo 2407 self.assertEquals(docinfo.URL, "http://no/such/url") 2408 docinfo.URL = "https://secret/url" 2409 self.assertEquals(docinfo.URL, "https://secret/url")
2410
2411 - def test_parse_stringio_base_url(self):
2412 tree = objectify.parse(BytesIO("<root/>"), base_url="http://no/such/url") 2413 docinfo = tree.docinfo 2414 self.assertEquals(docinfo.URL, "http://no/such/url")
2415
2416 - def test_parse_base_url_docinfo(self):
2417 tree = objectify.parse(fileInTestDir('include/test_xinclude.xml'), 2418 base_url="http://no/such/url") 2419 docinfo = tree.docinfo 2420 self.assertEquals(docinfo.URL, "http://no/such/url")
2421
2422 - def test_xml_base(self):
2423 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url") 2424 self.assertEquals(root.base, "http://no/such/url") 2425 self.assertEquals( 2426 root.get('{http://www.w3.org/XML/1998/namespace}base'), None) 2427 root.base = "https://secret/url" 2428 self.assertEquals(root.base, "https://secret/url") 2429 self.assertEquals( 2430 root.get('{http://www.w3.org/XML/1998/namespace}base'), 2431 "https://secret/url")
2432
2433 - def test_xml_base_attribute(self):
2434 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url") 2435 self.assertEquals(root.base, "http://no/such/url") 2436 self.assertEquals( 2437 root.get('{http://www.w3.org/XML/1998/namespace}base'), None) 2438 root.set('{http://www.w3.org/XML/1998/namespace}base', 2439 "https://secret/url") 2440 self.assertEquals(root.base, "https://secret/url") 2441 self.assertEquals( 2442 root.get('{http://www.w3.org/XML/1998/namespace}base'), 2443 "https://secret/url")
2444
2445 - def test_standard_lookup(self):
2446 XML = self.XML 2447 2448 xml = _bytes('''\ 2449 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 2450 <i>5</i> 2451 <i>-5</i> 2452 <l>4294967296</l> 2453 <l>-4294967296</l> 2454 <f>1.1</f> 2455 <b>true</b> 2456 <b>false</b> 2457 <s>Strange things happen, where strings collide</s> 2458 <s>True</s> 2459 <s>False</s> 2460 <s>t</s> 2461 <s>f</s> 2462 <s></s> 2463 <s>None</s> 2464 <n xsi:nil="true" /> 2465 </root> 2466 ''') 2467 root = XML(xml) 2468 2469 for i in root.i: 2470 self.assert_(isinstance(i, objectify.IntElement)) 2471 for l in root.l: 2472 self.assert_(isinstance(l, objectify.IntElement)) 2473 for f in root.f: 2474 self.assert_(isinstance(f, objectify.FloatElement)) 2475 for b in root.b: 2476 self.assert_(isinstance(b, objectify.BoolElement)) 2477 self.assertEquals(True, root.b[0]) 2478 self.assertEquals(False, root.b[1]) 2479 for s in root.s: 2480 self.assert_(isinstance(s, objectify.StringElement)) 2481 self.assert_(isinstance(root.n, objectify.NoneElement)) 2482 self.assertEquals(None, root.n)
2483
2484 -def test_suite():
2485 suite = unittest.TestSuite() 2486 suite.addTests([unittest.makeSuite(ObjectifyTestCase)]) 2487 suite.addTests(doctest.DocTestSuite(objectify)) 2488 if sys.version_info >= (2,4): 2489 suite.addTests( 2490 [make_doctest('../../../doc/objectify.txt')]) 2491 return suite
2492 2493 if __name__ == '__main__': 2494 print('to test use test.py %s' % __file__) 2495