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