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 
   9   
  10  from common_imports import etree, StringIO, HelperTestCase, fileInTestDir 
  11  from common_imports import SillyFileLike, canonicalize, doctest 
  12   
  13  from lxml import objectify 
  14   
  15  PYTYPE_NAMESPACE = "http://codespeak.net/lxml/objectify/pytype" 
  16  XML_SCHEMA_NS = "http://www.w3.org/2001/XMLSchema" 
  17  XML_SCHEMA_INSTANCE_NS = "http://www.w3.org/2001/XMLSchema-instance" 
  18  XML_SCHEMA_INSTANCE_TYPE_ATTR = "{%s}type" % XML_SCHEMA_INSTANCE_NS 
  19  XML_SCHEMA_NIL_ATTR = "{%s}nil" % XML_SCHEMA_INSTANCE_NS 
  20  TREE_PYTYPE = "TREE" 
  21  DEFAULT_NSMAP = { "py" : PYTYPE_NAMESPACE, 
  22                    "xsi" : XML_SCHEMA_INSTANCE_NS, 
  23                    "xsd" : XML_SCHEMA_NS} 
  24   
  25  objectclass2xsitype = { 
  26      # objectify built-in 
  27      objectify.IntElement: ("int", "short", "byte", "unsignedShort", 
  28                             "unsignedByte",), 
  29      objectify.LongElement: ("integer", "nonPositiveInteger", "negativeInteger", 
  30                              "long", "nonNegativeInteger", "unsignedLong", 
  31                              "unsignedInt", "positiveInteger",), 
  32      objectify.FloatElement: ("float", "double"), 
  33      objectify.BoolElement: ("boolean",), 
  34      objectify.StringElement: ("string", "normalizedString", "token", "language", 
  35                                "Name", "NCName", "ID", "IDREF", "ENTITY", 
  36                                "NMTOKEN", ), 
  37      # None: xsi:nil="true" 
  38      } 
  39   
  40  xsitype2objclass = dict(( (v, k) for k in objectclass2xsitype 
  41                            for v in objectclass2xsitype[k] )) 
  42   
  43  objectclass2pytype = { 
  44      # objectify built-in 
  45      objectify.IntElement: "int", 
  46      objectify.LongElement: "long", 
  47      objectify.FloatElement: "float", 
  48      objectify.BoolElement: "bool", 
  49      objectify.StringElement: "str", 
  50      # None: xsi:nil="true" 
  51      } 
  52   
  53  pytype2objclass = dict(( (objectclass2pytype[k], k) for k in objectclass2pytype)) 
  54   
  55  xml_str = '''\ 
  56  <obj:root xmlns:obj="objectified" xmlns:other="otherNS"> 
  57    <obj:c1 a1="A1" a2="A2" other:a3="A3"> 
  58      <obj:c2>0</obj:c2> 
  59      <obj:c2>1</obj:c2> 
  60      <obj:c2>2</obj:c2> 
  61      <other:c2>3</other:c2> 
  62      <c2>3</c2> 
  63    </obj:c1> 
  64  </obj:root>''' 
  65   
66 -class ObjectifyTestCase(HelperTestCase):
67 """Test cases for lxml.objectify 68 """ 69 etree = etree 70
71 - def XML(self, xml):
72 return self.etree.XML(xml, self.parser)
73
74 - def setUp(self):
75 super(ObjectifyTestCase, self).setUp() 76 self.parser = self.etree.XMLParser(remove_blank_text=True) 77 self.lookup = etree.ElementNamespaceClassLookup( 78 objectify.ObjectifyElementClassLookup() ) 79 self.parser.setElementClassLookup(self.lookup) 80 81 self.Element = self.parser.makeelement 82 83 ns = self.lookup.get_namespace("otherNS") 84 ns[None] = self.etree.ElementBase
85
86 - def tearDown(self):
87 self.lookup.get_namespace("otherNS").clear() 88 objectify.setPytypeAttributeTag() 89 del self.lookup 90 del self.parser 91 super(ObjectifyTestCase, self).tearDown()
92
94 elt = objectify.Element("test") 95 self.assertEquals(elt.nsmap, DEFAULT_NSMAP)
96
97 - def test_element_nsmap_empty(self):
98 nsmap = {} 99 elt = objectify.Element("test", nsmap=nsmap) 100 self.assertEquals(elt.nsmap.values(), [PYTYPE_NAMESPACE])
101
103 nsmap = {"mypy": PYTYPE_NAMESPACE, 104 "myxsi": XML_SCHEMA_INSTANCE_NS, 105 "myxsd": XML_SCHEMA_NS} 106 elt = objectify.Element("test", nsmap=nsmap) 107 self.assertEquals(elt.nsmap, nsmap)
108
109 - def test_element_nsmap_custom(self):
110 nsmap = {"my": "someNS", 111 "myother": "someOtherNS", 112 "myxsd": XML_SCHEMA_NS} 113 elt = objectify.Element("test", nsmap=nsmap) 114 self.assert_(PYTYPE_NAMESPACE in elt.nsmap.values()) 115 for prefix, ns in nsmap.items(): 116 self.assert_(prefix in elt.nsmap) 117 self.assertEquals(nsmap[prefix], elt.nsmap[prefix])
118
120 root = objectify.Element("root") 121 root.sub = objectify.Element("test") 122 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
123
125 root = objectify.Element("root") 126 nsmap = {} 127 root.sub = objectify.Element("test", nsmap=nsmap) 128 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
129
131 root = objectify.Element("root") 132 nsmap = {"mypy": PYTYPE_NAMESPACE, 133 "myxsi": XML_SCHEMA_INSTANCE_NS, 134 "myxsd": XML_SCHEMA_NS} 135 root.sub = objectify.Element("test", nsmap=nsmap) 136 self.assertEquals(root.sub.nsmap, DEFAULT_NSMAP)
137
139 root = objectify.Element("root") 140 nsmap = {"my": "someNS", 141 "myother": "someOtherNS", 142 "myxsd": XML_SCHEMA_NS,} 143 root.sub = objectify.Element("test", nsmap=nsmap) 144 expected = nsmap.copy() 145 del expected["myxsd"] 146 expected.update(DEFAULT_NSMAP) 147 self.assertEquals(root.sub.nsmap, expected)
148
150 value = objectify.DataElement("test this") 151 self.assertEquals(value.nsmap, DEFAULT_NSMAP)
152
154 nsmap = {} 155 value = objectify.DataElement("test this", nsmap=nsmap) 156 self.assertEquals(value.nsmap.values(), [PYTYPE_NAMESPACE])
157
159 nsmap = {"mypy": PYTYPE_NAMESPACE, 160 "myxsi": XML_SCHEMA_INSTANCE_NS, 161 "myxsd": XML_SCHEMA_NS} 162 value = objectify.DataElement("test this", nsmap=nsmap) 163 self.assertEquals(value.nsmap, nsmap)
164
166 nsmap = {"my": "someNS", 167 "myother": "someOtherNS", 168 "myxsd": XML_SCHEMA_NS,} 169 value = objectify.DataElement("test", nsmap=nsmap) 170 self.assert_(PYTYPE_NAMESPACE in value.nsmap.values()) 171 for prefix, ns in nsmap.items(): 172 self.assert_(prefix in value.nsmap) 173 self.assertEquals(nsmap[prefix], value.nsmap[prefix])
174
176 root = objectify.Element("root") 177 root.value = objectify.DataElement("test this") 178 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
179
181 root = objectify.Element("root") 182 nsmap = {} 183 root.value = objectify.DataElement("test this", nsmap=nsmap) 184 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
185
187 root = objectify.Element("root") 188 nsmap = {"mypy": PYTYPE_NAMESPACE, 189 "myxsi": XML_SCHEMA_INSTANCE_NS, 190 "myxsd": XML_SCHEMA_NS} 191 root.value = objectify.DataElement("test this", nsmap=nsmap) 192 self.assertEquals(root.value.nsmap, DEFAULT_NSMAP)
193
195 root = objectify.Element("root") 196 nsmap = {"my": "someNS", 197 "myother": "someOtherNS", 198 "myxsd": XML_SCHEMA_NS} 199 root.value = objectify.DataElement("test", nsmap=nsmap) 200 expected = nsmap.copy() 201 del expected["myxsd"] 202 expected.update(DEFAULT_NSMAP) 203 self.assertEquals(root.value.nsmap, expected)
204
206 # keyword arguments override attrib entries 207 value = objectify.DataElement(23, _pytype="str", _xsi="foobar", 208 attrib={"gnu": "muh", "cat": "meeow", 209 "dog": "wuff"}, 210 bird="tchilp", dog="grrr") 211 self.assertEquals(value.get("gnu"), "muh") 212 self.assertEquals(value.get("cat"), "meeow") 213 self.assertEquals(value.get("dog"), "grrr") 214 self.assertEquals(value.get("bird"), "tchilp")
215
217 # Check that DataElement preserves all attributes ObjectifiedDataElement 218 # arguments 219 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar", 220 attrib={"gnu": "muh", "cat": "meeow", 221 "dog": "wuff"}, 222 bird="tchilp", dog="grrr") 223 value = objectify.DataElement(arg) 224 self.assert_(isinstance(value, objectify.StringElement)) 225 for attr in arg.attrib: 226 self.assertEquals(value.get(attr), arg.get(attr))
227
229 # Check that _pytype arg overrides original py:pytype of 230 # ObjectifiedDataElement 231 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar", 232 attrib={"gnu": "muh", "cat": "meeow", 233 "dog": "wuff"}, 234 bird="tchilp", dog="grrr") 235 value = objectify.DataElement(arg, _pytype="NoneType") 236 self.assert_(isinstance(value, objectify.NoneElement)) 237 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true") 238 self.assertEquals(value.text, None) 239 self.assertEquals(value.pyval, None) 240 for attr in arg.attrib: 241 #if not attr == objectify.PYTYPE_ATTRIBUTE: 242 self.assertEquals(value.get(attr), arg.get(attr))
243
245 # Check that _pytype arg overrides original py:pytype of 246 # ObjectifiedDataElement 247 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar", 248 attrib={"gnu": "muh", "cat": "meeow", 249 "dog": "wuff"}, 250 bird="tchilp", dog="grrr") 251 value = objectify.DataElement(arg, _pytype="int") 252 self.assert_(isinstance(value, objectify.IntElement)) 253 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int") 254 for attr in arg.attrib: 255 if not attr == objectify.PYTYPE_ATTRIBUTE: 256 self.assertEquals(value.get(attr), arg.get(attr))
257
259 # Check that _xsi arg overrides original xsi:type of given 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, _xsi="xsd:int") 266 self.assert_(isinstance(value, objectify.IntElement)) 267 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int") 268 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int") 269 for attr in arg.attrib: 270 if not attr in [objectify.PYTYPE_ATTRIBUTE, 271 XML_SCHEMA_INSTANCE_TYPE_ATTR]: 272 self.assertEquals(value.get(attr), arg.get(attr))
273
275 # Check that _pytype and _xsi args override original py:pytype and 276 # xsi:type attributes of given ObjectifiedDataElement 277 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar", 278 attrib={"gnu": "muh", "cat": "meeow", 279 "dog": "wuff"}, 280 bird="tchilp", dog="grrr") 281 value = objectify.DataElement(arg, _pytype="int", _xsi="xsd:int") 282 self.assert_(isinstance(value, objectify.IntElement)) 283 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int") 284 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int") 285 for attr in arg.attrib: 286 if not attr in [objectify.PYTYPE_ATTRIBUTE, 287 XML_SCHEMA_INSTANCE_TYPE_ATTR]: 288 self.assertEquals(value.get(attr), arg.get(attr))
289
291 self.assertRaises(ValueError, objectify.DataElement, 3.1415, 292 _pytype="int")
293
295 self.assertRaises(ValueError, objectify.DataElement, 3.1415, 296 _xsi="xsd:int")
297
299 arg = objectify.DataElement(3.1415) 300 self.assertRaises(ValueError, objectify.DataElement, arg, 301 _pytype="int")
302
304 arg = objectify.DataElement(3.1415) 305 self.assertRaises(ValueError, objectify.DataElement, arg, 306 _xsi="xsd:int")
307
308 - def test_root(self):
309 root = self.Element("test") 310 self.assert_(isinstance(root, objectify.ObjectifiedElement))
311
312 - def test_str(self):
313 root = self.Element("test") 314 self.assertEquals('', str(root))
315
316 - def test_child(self):
317 root = self.XML(xml_str) 318 self.assertEquals("0", root.c1.c2.text)
319
320 - def test_countchildren(self):
321 root = self.XML(xml_str) 322 self.assertEquals(1, root.countchildren()) 323 self.assertEquals(5, root.c1.countchildren())
324
325 - def test_child_getattr(self):
326 root = self.XML(xml_str) 327 self.assertEquals("0", getattr(root.c1, "{objectified}c2").text) 328 self.assertEquals("3", getattr(root.c1, "{otherNS}c2").text)
329
330 - def test_child_nonexistant(self):
331 root = self.XML(xml_str) 332 self.assertRaises(AttributeError, getattr, root.c1, "NOT_THERE") 333 self.assertRaises(AttributeError, getattr, root.c1, "{unknownNS}c2")
334
335 - def test_addattr(self):
336 root = self.XML(xml_str) 337 self.assertEquals(1, len(root.c1)) 338 root.addattr("c1", "test") 339 self.assertEquals(2, len(root.c1)) 340 self.assertEquals("test", root.c1[1].text)
341
342 - def test_addattr_element(self):
343 root = self.XML(xml_str) 344 self.assertEquals(1, len(root.c1)) 345 346 new_el = self.Element("test", myattr="5") 347 root.addattr("c1", new_el) 348 self.assertEquals(2, len(root.c1)) 349 self.assertEquals(None, root.c1[0].get("myattr")) 350 self.assertEquals("5", root.c1[1].get("myattr"))
351
352 - def test_addattr_list(self):
353 root = self.XML(xml_str) 354 self.assertEquals(1, len(root.c1)) 355 356 new_el = self.Element("test") 357 self.etree.SubElement(new_el, "a", myattr="A") 358 self.etree.SubElement(new_el, "a", myattr="B") 359 360 root.addattr("c1", list(new_el.a)) 361 self.assertEquals(3, len(root.c1)) 362 self.assertEquals(None, root.c1[0].get("myattr")) 363 self.assertEquals("A", root.c1[1].get("myattr")) 364 self.assertEquals("B", root.c1[2].get("myattr"))
365
366 - def test_child_addattr(self):
367 root = self.XML(xml_str) 368 self.assertEquals(3, len(root.c1.c2)) 369 root.c1.addattr("c2", 3) 370 self.assertEquals(4, len(root.c1.c2)) 371 self.assertEquals("3", root.c1.c2[3].text)
372
373 - def test_child_index(self):
374 root = self.XML(xml_str) 375 self.assertEquals("0", root.c1.c2[0].text) 376 self.assertEquals("1", root.c1.c2[1].text) 377 self.assertEquals("2", root.c1.c2[2].text) 378 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
379
380 - def test_child_index_neg(self):
381 root = self.XML(xml_str) 382 self.assertEquals("0", root.c1.c2[0].text) 383 self.assertEquals("0", root.c1.c2[-3].text) 384 self.assertEquals("1", root.c1.c2[-2].text) 385 self.assertEquals("2", root.c1.c2[-1].text) 386 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
387
388 - def test_child_len(self):
389 root = self.XML(xml_str) 390 self.assertEquals(1, len(root)) 391 self.assertEquals(1, len(root.c1)) 392 self.assertEquals(3, len(root.c1.c2))
393
394 - def test_child_iter(self):
395 root = self.XML(xml_str) 396 self.assertEquals([root], 397 list(iter(root))) 398 self.assertEquals([root.c1], 399 list(iter(root.c1))) 400 self.assertEquals([root.c1.c2[0], root.c1.c2[1], root.c1.c2[2]], 401 list(iter((root.c1.c2))))
402
403 - def test_class_lookup(self):
404 root = self.XML(xml_str) 405 self.assert_(isinstance(root.c1.c2, objectify.ObjectifiedElement)) 406 self.assertFalse(isinstance(getattr(root.c1, "{otherNS}c2"), 407 objectify.ObjectifiedElement))
408
409 - def test_dir(self):
410 root = self.XML(xml_str) 411 dir_c1 = dir(objectify.ObjectifiedElement) + ['c1'] 412 dir_c1.sort() 413 dir_c2 = dir(objectify.ObjectifiedElement) + ['c2'] 414 dir_c2.sort() 415 416 self.assertEquals(dir_c1, dir(root)) 417 self.assertEquals(dir_c2, dir(root.c1))
418
419 - def test_vars(self):
420 root = self.XML(xml_str) 421 self.assertEquals({'c1' : root.c1}, vars(root)) 422 self.assertEquals({'c2' : root.c1.c2}, vars(root.c1))
423
424 - def test_child_set_ro(self):
425 root = self.XML(xml_str) 426 self.assertRaises(TypeError, setattr, root.c1.c2, 'text', "test") 427 self.assertRaises(TypeError, setattr, root.c1.c2, 'pyval', "test")
428
429 - def test_setslice(self):
430 Element = self.Element 431 SubElement = self.etree.SubElement 432 root = Element("root") 433 root.c = ["c1", "c2"] 434 435 c1 = root.c[0] 436 c2 = root.c[1] 437 438 self.assertEquals([c1,c2], list(root.c)) 439 self.assertEquals(["c1", "c2"], 440 [ c.text for c in root.c ]) 441 442 root2 = Element("root2") 443 root2.el = [ "test", "test" ] 444 self.assertEquals(["test", "test"], 445 [ el.text for el in root2.el ]) 446 447 root.c = [ root2.el, root2.el ] 448 self.assertEquals(["test", "test"], 449 [ c.text for c in root.c ]) 450 self.assertEquals(["test", "test"], 451 [ el.text for el in root2.el ]) 452 453 root.c[:] = [ c1, c2, c2, c1 ] 454 self.assertEquals(["c1", "c2", "c2", "c1"], 455 [ c.text for c in root.c ])
456
457 - def test_set_string(self):
458 # make sure strings are not handled as sequences 459 Element = self.Element 460 SubElement = self.etree.SubElement 461 root = Element("root") 462 root.c = "TEST" 463 self.assertEquals(["TEST"], 464 [ c.text for c in root.c ])
465
466 - def test_setitem_string(self):
467 # make sure strings are set as children 468 Element = self.Element 469 SubElement = self.etree.SubElement 470 root = Element("root") 471 root["c"] = "TEST" 472 self.assertEquals(["TEST"], 473 [ c.text for c in root.c ])
474
476 # make sure 'text' etc. are set as children 477 Element = self.Element 478 SubElement = self.etree.SubElement 479 root = Element("root") 480 481 root["text"] = "TEST" 482 self.assertEquals(["TEST"], 483 [ c.text for c in root["text"] ]) 484 485 root["tail"] = "TEST" 486 self.assertEquals(["TEST"], 487 [ c.text for c in root["tail"] ]) 488 489 root["pyval"] = "TEST" 490 self.assertEquals(["TEST"], 491 [ c.text for c in root["pyval"] ]) 492 493 root["tag"] = "TEST" 494 self.assertEquals(["TEST"], 495 [ c.text for c in root["tag"] ])
496
497 - def test_findall(self):
498 XML = self.XML 499 root = XML('<a><b><c/></b><b/><c><b/></c></a>') 500 self.assertEquals(1, len(root.findall("c"))) 501 self.assertEquals(2, len(root.findall(".//c"))) 502 self.assertEquals(3, len(root.findall(".//b"))) 503 self.assert_(root.findall(".//b")[1] is root.getchildren()[1])
504
505 - def test_findall_ns(self):
506 XML = self.XML 507 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>') 508 self.assertEquals(2, len(root.findall(".//{X}b"))) 509 self.assertEquals(3, len(root.findall(".//b"))) 510 self.assertEquals(2, len(root.findall("b")))
511
512 - def test_build_tree(self):
513 root = self.Element('root') 514 root.a = 5 515 root.b = 6 516 self.assert_(isinstance(root, objectify.ObjectifiedElement)) 517 self.assert_(isinstance(root.a, objectify.IntElement)) 518 self.assert_(isinstance(root.b, objectify.IntElement))
519
520 - def test_type_NoneType(self):
521 Element = self.Element 522 SubElement = self.etree.SubElement 523 524 nil_attr = XML_SCHEMA_NIL_ATTR 525 root = Element("{objectified}root") 526 SubElement(root, "{objectified}none") 527 SubElement(root, "{objectified}none", {nil_attr : "true"}) 528 self.assertFalse(isinstance(root.none, objectify.NoneElement)) 529 self.assertFalse(isinstance(root.none[0], objectify.NoneElement)) 530 self.assert_(isinstance(root.none[1], objectify.NoneElement)) 531 self.assertEquals(root.none[1], None) 532 self.assertFalse(root.none[1])
533
534 - def test_data_element_NoneType(self):
535 value = objectify.DataElement(None) 536 self.assert_(isinstance(value, objectify.NoneElement)) 537 self.assertEquals(value, None) 538 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
539
540 - def test_type_bool(self):
541 Element = self.Element 542 SubElement = self.etree.SubElement 543 root = Element("{objectified}root") 544 root.bool = True 545 self.assertEquals(root.bool, True) 546 self.assert_(isinstance(root.bool, objectify.BoolElement)) 547 548 root.bool = False 549 self.assertEquals(root.bool, False) 550 self.assert_(isinstance(root.bool, objectify.BoolElement))
551
552 - def test_data_element_bool(self):
553 value = objectify.DataElement(True) 554 self.assert_(isinstance(value, objectify.BoolElement)) 555 self.assertEquals(value, True) 556 557 value = objectify.DataElement(False) 558 self.assert_(isinstance(value, objectify.BoolElement)) 559 self.assertEquals(value, False)
560
561 - def test_type_str(self):
562 Element = self.Element 563 SubElement = self.etree.SubElement 564 root = Element("{objectified}root") 565 root.s = "test" 566 self.assert_(isinstance(root.s, objectify.StringElement))
567
568 - def test_type_str_intliteral(self):
569 Element = self.Element 570 SubElement = self.etree.SubElement 571 root = Element("{objectified}root") 572 root.s = "3" 573 self.assert_(isinstance(root.s, objectify.StringElement))
574
575 - def test_type_str_floatliteral(self):
576 Element = self.Element 577 SubElement = self.etree.SubElement 578 root = Element("{objectified}root") 579 root.s = "3.72" 580 self.assert_(isinstance(root.s, objectify.StringElement))
581
582 - def test_type_str_mul(self):
583 Element = self.Element 584 SubElement = self.etree.SubElement 585 root = Element("{objectified}root") 586 root.s = "test" 587 588 self.assertEquals("test" * 5, root.s * 5) 589 self.assertEquals(5 * "test", 5 * root.s) 590 591 self.assertRaises(TypeError, operator.mul, root.s, "honk") 592 self.assertRaises(TypeError, operator.mul, "honk", root.s)
593
594 - def test_type_str_add(self):
595 Element = self.Element 596 SubElement = self.etree.SubElement 597 root = Element("{objectified}root") 598 root.s = "test" 599 600 s = "toast" 601 self.assertEquals("test" + s, root.s + s) 602 self.assertEquals(s + "test", s + root.s)
603
604 - def test_type_str_mod(self):
605 s = "%d %f %s %r" 606 el = objectify.DataElement(s) 607 values = (1, 7.0, "abcd", None) 608 self.assertEquals(s % values, el % values) 609 610 s = "%d" 611 el = objectify.DataElement(s) 612 val = 5 613 self.assertEquals(s % val, el % val) 614 615 s = "%d %s" 616 el = objectify.DataElement(s) 617 val = 5 618 self.assertRaises(TypeError, el.__mod__, val) 619 620 s = "" 621 el = objectify.DataElement(s) 622 val = 5 623 self.assertRaises(TypeError, el.__mod__, val)
624
626 s = "%d %f %s %r" 627 el = objectify.DataElement(s) 628 values = (objectify.DataElement(1), 629 objectify.DataElement(7.0), 630 objectify.DataElement("abcd"), 631 objectify.DataElement(None)) 632 self.assertEquals(s % values, el % values)
633
634 - def test_data_element_str(self):
635 value = objectify.DataElement("test") 636 self.assert_(isinstance(value, objectify.StringElement)) 637 self.assertEquals(value, "test")
638
640 value = objectify.DataElement("3") 641 self.assert_(isinstance(value, objectify.StringElement)) 642 self.assertEquals(value, "3")
643
645 value = objectify.DataElement("3.20") 646 self.assert_(isinstance(value, objectify.StringElement)) 647 self.assertEquals(value, "3.20")
648
649 - def test_type_ustr(self):
650 Element = self.Element 651 SubElement = self.etree.SubElement 652 root = Element("{objectified}root") 653 root.s = u"test" 654 self.assert_(isinstance(root.s, objectify.StringElement))
655
656 - def test_type_ustr_intliteral(self):
657 Element = self.Element 658 SubElement = self.etree.SubElement 659 root = Element("{objectified}root") 660 root.s = u"3" 661 self.assert_(isinstance(root.s, objectify.StringElement))
662
664 Element = self.Element 665 SubElement = self.etree.SubElement 666 root = Element("{objectified}root") 667 root.s = u"3.72" 668 self.assert_(isinstance(root.s, objectify.StringElement))
669
670 - def test_type_ustr_mul(self):
671 Element = self.Element 672 SubElement = self.etree.SubElement 673 root = Element("{objectified}root") 674 root.s = u"test" 675 676 self.assertEquals(u"test" * 5, root.s * 5) 677 self.assertEquals(5 * u"test", 5 * root.s) 678 679 self.assertRaises(TypeError, operator.mul, root.s, u"honk") 680 self.assertRaises(TypeError, operator.mul, u"honk", root.s)
681
682 - def test_type_ustr_add(self):
683 Element = self.Element 684 SubElement = self.etree.SubElement 685 root = Element("{objectified}root") 686 root.s = u"test" 687 688 s = u"toast" 689 self.assertEquals(u"test" + s, root.s + s) 690 self.assertEquals(s + u"test", s + root.s)
691
692 - def test_data_element_ustr(self):
693 value = objectify.DataElement(u"test") 694 self.assert_(isinstance(value, objectify.StringElement)) 695 self.assertEquals(value, u"test")
696
698 value = objectify.DataElement("3") 699 self.assert_(isinstance(value, objectify.StringElement)) 700 self.assertEquals(value, u"3")
701
703 value = objectify.DataElement(u"3.20") 704 self.assert_(isinstance(value, objectify.StringElement)) 705 self.assertEquals(value, u"3.20")
706
707 - def test_type_int(self):
708 Element = self.Element 709 SubElement = self.etree.SubElement 710 root = Element("{objectified}root") 711 root.none = 5 712 self.assert_(isinstance(root.none, objectify.IntElement))
713
714 - def test_data_element_int(self):
715 value = objectify.DataElement(5) 716 self.assert_(isinstance(value, objectify.IntElement)) 717 self.assertEquals(value, 5)
718
719 - def test_type_float(self):
720 Element = self.Element 721 SubElement = self.etree.SubElement 722 root = Element("{objectified}root") 723 root.none = 5.5 724 self.assert_(isinstance(root.none, objectify.FloatElement))
725
726 - def test_data_element_float(self):
727 value = objectify.DataElement(5.5) 728 self.assert_(isinstance(value, objectify.FloatElement)) 729 self.assertEquals(value, 5.5)
730
731 - def test_data_element_xsitypes(self):
732 for xsi, objclass in xsitype2objclass.iteritems(): 733 # 1 is a valid value for all ObjectifiedDataElement classes 734 pyval = 1 735 value = objectify.DataElement(pyval, _xsi=xsi) 736 self.assert_(isinstance(value, objclass), 737 "DataElement(%s, _xsi='%s') returns %s, expected %s" 738 % (pyval, xsi, type(value), objclass))
739
741 for xsi, objclass in xsitype2objclass.iteritems(): 742 # 1 is a valid value for all ObjectifiedDataElement classes 743 pyval = 1 744 value = objectify.DataElement(pyval, _xsi="xsd:%s" % xsi) 745 self.assert_(isinstance(value, objclass), 746 "DataElement(%s, _xsi='%s') returns %s, expected %s" 747 % (pyval, xsi, type(value), objclass))
748
750 for xsi, objclass in xsitype2objclass.iteritems(): 751 # 1 is a valid value for all ObjectifiedDataElement classes 752 self.assertRaises(ValueError, objectify.DataElement, 1, 753 _xsi="foo:%s" % xsi)
754
755 - def test_data_element_pytypes(self):
756 for pytype, objclass in pytype2objclass.iteritems(): 757 # 1 is a valid value for all ObjectifiedDataElement classes 758 pyval = 1 759 value = objectify.DataElement(pyval, _pytype=pytype) 760 self.assert_(isinstance(value, objclass), 761 "DataElement(%s, _pytype='%s') returns %s, expected %s" 762 % (pyval, pytype, type(value), objclass))
763
765 pyval = 1 766 pytype = "NoneType" 767 objclass = objectify.NoneElement 768 value = objectify.DataElement(pyval, _pytype=pytype) 769 self.assert_(isinstance(value, objclass), 770 "DataElement(%s, _pytype='%s') returns %s, expected %s" 771 % (pyval, pytype, type(value), objclass)) 772 self.assertEquals(value.text, None) 773 self.assertEquals(value.pyval, None)
774
776 # pre-2.0 lxml called NoneElement "none" 777 pyval = 1 778 pytype = "none" 779 objclass = objectify.NoneElement 780 value = objectify.DataElement(pyval, _pytype=pytype) 781 self.assert_(isinstance(value, objclass), 782 "DataElement(%s, _pytype='%s') returns %s, expected %s" 783 % (pyval, pytype, type(value), objclass)) 784 self.assertEquals(value.text, None) 785 self.assertEquals(value.pyval, None)
786
787 - def test_type_unregistered(self):
788 Element = self.Element 789 SubElement = self.etree.SubElement 790 class MyFloat(float): 791 pass
792 root = Element("{objectified}root") 793 root.myfloat = MyFloat(5.5) 794 self.assert_(isinstance(root.myfloat, objectify.FloatElement)) 795 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
796
797 - def test_data_element_unregistered(self):
798 class MyFloat(float): 799 pass
800 value = objectify.DataElement(MyFloat(5.5)) 801 self.assert_(isinstance(value, objectify.FloatElement)) 802 self.assertEquals(value, 5.5) 803 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None) 804
805 - def test_schema_types(self):
806 XML = self.XML 807 root = XML('''\ 808 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 809 <b xsi:type="boolean">true</b> 810 <b xsi:type="boolean">false</b> 811 <b xsi:type="boolean">1</b> 812 <b xsi:type="boolean">0</b> 813 814 <f xsi:type="float">5</f> 815 <f xsi:type="double">5</f> 816 817 <s xsi:type="string">5</s> 818 <s xsi:type="normalizedString">5</s> 819 <s xsi:type="token">5</s> 820 <s xsi:type="language">5</s> 821 <s xsi:type="Name">5</s> 822 <s xsi:type="NCName">5</s> 823 <s xsi:type="ID">5</s> 824 <s xsi:type="IDREF">5</s> 825 <s xsi:type="ENTITY">5</s> 826 <s xsi:type="NMTOKEN">5</s> 827 828 <l xsi:type="integer">5</l> 829 <l xsi:type="nonPositiveInteger">5</l> 830 <l xsi:type="negativeInteger">5</l> 831 <l xsi:type="long">5</l> 832 <l xsi:type="nonNegativeInteger">5</l> 833 <l xsi:type="unsignedLong">5</l> 834 <l xsi:type="unsignedInt">5</l> 835 <l xsi:type="positiveInteger">5</l> 836 837 <i xsi:type="int">5</i> 838 <i xsi:type="short">5</i> 839 <i xsi:type="byte">5</i> 840 <i xsi:type="unsignedShort">5</i> 841 <i xsi:type="unsignedByte">5</i> 842 843 <n xsi:nil="true"/> 844 </root> 845 ''') 846 847 for b in root.b: 848 self.assert_(isinstance(b, objectify.BoolElement)) 849 self.assertEquals(True, root.b[0]) 850 self.assertEquals(False, root.b[1]) 851 self.assertEquals(True, root.b[2]) 852 self.assertEquals(False, root.b[3]) 853 854 for f in root.f: 855 self.assert_(isinstance(f, objectify.FloatElement)) 856 self.assertEquals(5, f) 857 858 for s in root.s: 859 self.assert_(isinstance(s, objectify.StringElement)) 860 self.assertEquals("5", s) 861 862 for l in root.l: 863 self.assert_(isinstance(l, objectify.LongElement)) 864 self.assertEquals(5L, l) 865 866 for i in root.i: 867 self.assert_(isinstance(i, objectify.IntElement)) 868 self.assertEquals(5, i) 869 870 self.assert_(isinstance(root.n, objectify.NoneElement)) 871 self.assertEquals(None, root.n)
872
873 - def test_schema_types_prefixed(self):
874 XML = self.XML 875 root = XML('''\ 876 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 877 xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 878 <b xsi:type="xsd:boolean">true</b> 879 <b xsi:type="xsd:boolean">false</b> 880 <b xsi:type="xsd:boolean">1</b> 881 <b xsi:type="xsd:boolean">0</b> 882 883 <f xsi:type="xsd:float">5</f> 884 <f xsi:type="xsd:double">5</f> 885 886 <s xsi:type="xsd:string">5</s> 887 <s xsi:type="xsd:normalizedString">5</s> 888 <s xsi:type="xsd:token">5</s> 889 <s xsi:type="xsd:language">5</s> 890 <s xsi:type="xsd:Name">5</s> 891 <s xsi:type="xsd:NCName">5</s> 892 <s xsi:type="xsd:ID">5</s> 893 <s xsi:type="xsd:IDREF">5</s> 894 <s xsi:type="xsd:ENTITY">5</s> 895 <s xsi:type="xsd:NMTOKEN">5</s> 896 897 <l xsi:type="xsd:integer">5</l> 898 <l xsi:type="xsd:nonPositiveInteger">5</l> 899 <l xsi:type="xsd:negativeInteger">5</l> 900 <l xsi:type="xsd:long">5</l> 901 <l xsi:type="xsd:nonNegativeInteger">5</l> 902 <l xsi:type="xsd:unsignedLong">5</l> 903 <l xsi:type="xsd:unsignedInt">5</l> 904 <l xsi:type="xsd:positiveInteger">5</l> 905 906 <i xsi:type="xsd:int">5</i> 907 <i xsi:type="xsd:short">5</i> 908 <i xsi:type="xsd:byte">5</i> 909 <i xsi:type="xsd:unsignedShort">5</i> 910 <i xsi:type="xsd:unsignedByte">5</i> 911 912 <n xsi:nil="true"/> 913 </root> 914 ''') 915 916 for b in root.b: 917 self.assert_(isinstance(b, objectify.BoolElement)) 918 self.assertEquals(True, root.b[0]) 919 self.assertEquals(False, root.b[1]) 920 self.assertEquals(True, root.b[2]) 921 self.assertEquals(False, root.b[3]) 922 923 for f in root.f: 924 self.assert_(isinstance(f, objectify.FloatElement)) 925 self.assertEquals(5, f) 926 927 for s in root.s: 928 self.assert_(isinstance(s, objectify.StringElement)) 929 self.assertEquals("5", s) 930 931 for l in root.l: 932 self.assert_(isinstance(l, objectify.LongElement)) 933 self.assertEquals(5L, l) 934 935 for i in root.i: 936 self.assert_(isinstance(i, objectify.IntElement)) 937 self.assertEquals(5, i) 938 939 self.assert_(isinstance(root.n, objectify.NoneElement)) 940 self.assertEquals(None, root.n)
941
942 - def test_type_str_sequence(self):
943 XML = self.XML 944 root = XML(u'<root><b>why</b><b>try</b></root>') 945 strs = [ str(s) for s in root.b ] 946 self.assertEquals(["why", "try"], 947 strs)
948
949 - def test_type_str_cmp(self):
950 XML = self.XML 951 root = XML(u'<root><b>test</b><b>taste</b><b></b><b/></root>') 952 self.assertFalse(root.b[0] < root.b[1]) 953 self.assertFalse(root.b[0] <= root.b[1]) 954 self.assertFalse(root.b[0] == root.b[1]) 955 956 self.assert_(root.b[0] != root.b[1]) 957 self.assert_(root.b[0] >= root.b[1]) 958 self.assert_(root.b[0] > root.b[1]) 959 960 self.assertEquals(root.b[0], "test") 961 self.assertEquals("test", root.b[0]) 962 self.assert_(root.b[0] > 5) 963 self.assert_(5 < root.b[0]) 964 965 self.assertEquals("", root.b[2]) 966 self.assertEquals(root.b[2], "") 967 self.assertEquals("", root.b[3]) 968 self.assertEquals(root.b[3], "") 969 self.assertEquals(root.b[2], root.b[3]) 970 971 root.b = "test" 972 self.assert_(root.b) 973 root.b = "" 974 self.assertFalse(root.b) 975 self.assertEquals(root.b, "") 976 self.assertEquals("", root.b)
977
978 - def test_type_int_cmp(self):
979 XML = self.XML 980 root = XML(u'<root><b>5</b><b>6</b></root>') 981 self.assert_(root.b[0] < root.b[1]) 982 self.assert_(root.b[0] <= root.b[1]) 983 self.assert_(root.b[0] != root.b[1]) 984 985 self.assertFalse(root.b[0] == root.b[1]) 986 self.assertFalse(root.b[0] >= root.b[1]) 987 self.assertFalse(root.b[0] > root.b[1]) 988 989 self.assertEquals(root.b[0], 5) 990 self.assertEquals(5, root.b[0]) 991 self.assert_(root.b[0] < "5") 992 self.assert_("5" > root.b[0]) 993 994 root.b = 5 995 self.assert_(root.b) 996 root.b = 0 997 self.assertFalse(root.b)
998 999 # float + long share the NumberElement implementation with int 1000
1001 - def test_type_bool_cmp(self):
1002 XML = self.XML 1003 root = XML(u'<root><b>false</b><b>true</b></root>') 1004 self.assert_(root.b[0] < root.b[1]) 1005 self.assert_(root.b[0] <= root.b[1]) 1006 self.assert_(root.b[0] != root.b[1]) 1007 1008 self.assertFalse(root.b[0] == root.b[1]) 1009 self.assertFalse(root.b[0] >= root.b[1]) 1010 self.assertFalse(root.b[0] > root.b[1]) 1011 1012 self.assertFalse(root.b[0]) 1013 self.assert_(root.b[1]) 1014 1015 self.assertEquals(root.b[0], False) 1016 self.assertEquals(False, root.b[0]) 1017 self.assert_(root.b[0] < 5) 1018 self.assert_(5 > root.b[0]) 1019 1020 root.b = True 1021 self.assert_(root.b) 1022 root.b = False 1023 self.assertFalse(root.b)
1024
1025 - def test_type_none_cmp(self):
1026 XML = self.XML 1027 root = XML(u""" 1028 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 1029 <b xsi:nil="true"></b><b xsi:nil="true"/> 1030 </root>""") 1031 self.assert_(root.b[0] == root.b[1]) 1032 self.assertFalse(root.b[0]) 1033 self.assertEquals(root.b[0], None) 1034 self.assertEquals(None, root.b[0]) 1035 1036 for comparison in ["abc", 5, 7.3, True, [], ()]: 1037 none = root.b[1] 1038 self.assert_(none < comparison, "%s (%s) should be < %s" % 1039 (none, type(none), comparison) ) 1040 self.assert_(comparison > none, "%s should be > %s (%s)" % 1041 (comparison, none, type(none)) )
1042
1043 - def test_dataelement_xsi(self):
1044 el = objectify.DataElement(1, _xsi="string") 1045 self.assertEquals( 1046 el.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), 1047 'xsd:string')
1048
1049 - def test_dataelement_xsi_nsmap(self):
1050 el = objectify.DataElement(1, _xsi="string", 1051 nsmap={'schema': XML_SCHEMA_NS}) 1052 self.assertEquals( 1053 el.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), 1054 'schema:string')
1055
1056 - def test_dataelement_xsi_prefix_error(self):
1057 self.assertRaises(ValueError, objectify.DataElement, 1, 1058 _xsi="foo:string")
1059
1060 - def test_pytype_annotation(self):
1061 XML = self.XML 1062 root = XML(u'''\ 1063 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1064 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1065 <b>5</b> 1066 <b>test</b> 1067 <c>1.1</c> 1068 <c>\uF8D2</c> 1069 <x>true</x> 1070 <n xsi:nil="true" /> 1071 <n></n> 1072 <b xsi:type="double">5</b> 1073 <b xsi:type="float">5</b> 1074 <s xsi:type="string">23</s> 1075 <s py:pytype="str">42</s> 1076 <f py:pytype="float">300</f> 1077 <l py:pytype="long">2</l> 1078 <t py:pytype="TREE"></t> 1079 </a> 1080 ''') 1081 objectify.annotate(root) 1082 1083 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1084 for c in root.iterchildren() ] 1085 self.assertEquals("int", child_types[ 0]) 1086 self.assertEquals("str", child_types[ 1]) 1087 self.assertEquals("float", child_types[ 2]) 1088 self.assertEquals("str", child_types[ 3]) 1089 self.assertEquals("bool", child_types[ 4]) 1090 self.assertEquals("NoneType", child_types[ 5]) 1091 self.assertEquals(None, child_types[ 6]) 1092 self.assertEquals("float", child_types[ 7]) 1093 self.assertEquals("float", child_types[ 8]) 1094 self.assertEquals("str", child_types[ 9]) 1095 self.assertEquals("int", child_types[10]) 1096 self.assertEquals("int", child_types[11]) 1097 self.assertEquals("int", child_types[12]) 1098 self.assertEquals(None, child_types[13]) 1099 1100 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1101
1102 - def test_pytype_annotation_empty(self):
1103 XML = self.XML 1104 root = XML(u'''\ 1105 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1106 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1107 <n></n> 1108 </a> 1109 ''') 1110 objectify.annotate(root) 1111 1112 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1113 for c in root.iterchildren() ] 1114 self.assertEquals(None, child_types[0]) 1115 1116 objectify.annotate(root, empty_pytype="str") 1117 1118 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1119 for c in root.iterchildren() ] 1120 self.assertEquals("str", child_types[0])
1121
1122 - def test_pytype_annotation_use_old(self):
1123 XML = self.XML 1124 root = XML(u'''\ 1125 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1126 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1127 <b>5</b> 1128 <b>test</b> 1129 <c>1.1</c> 1130 <c>\uF8D2</c> 1131 <x>true</x> 1132 <n xsi:nil="true" /> 1133 <n></n> 1134 <b xsi:type="double">5</b> 1135 <b xsi:type="float">5</b> 1136 <s xsi:type="string">23</s> 1137 <s py:pytype="str">42</s> 1138 <f py:pytype="float">300</f> 1139 <l py:pytype="long">2</l> 1140 <t py:pytype="TREE"></t> 1141 </a> 1142 ''') 1143 objectify.annotate(root, ignore_old=False) 1144 1145 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1146 for c in root.iterchildren() ] 1147 self.assertEquals("int", child_types[ 0]) 1148 self.assertEquals("str", child_types[ 1]) 1149 self.assertEquals("float", child_types[ 2]) 1150 self.assertEquals("str", child_types[ 3]) 1151 self.assertEquals("bool", child_types[ 4]) 1152 self.assertEquals("NoneType", child_types[ 5]) 1153 self.assertEquals(None, child_types[ 6]) 1154 self.assertEquals("float", child_types[ 7]) 1155 self.assertEquals("float", child_types[ 8]) 1156 self.assertEquals("str", child_types[ 9]) 1157 self.assertEquals("str", child_types[10]) 1158 self.assertEquals("float", child_types[11]) 1159 self.assertEquals("long", child_types[12]) 1160 self.assertEquals(TREE_PYTYPE, child_types[13]) 1161 1162 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1163
1164 - def test_pytype_xsitype_annotation(self):
1165 XML = self.XML 1166 root = XML(u'''\ 1167 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1168 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1169 <b>5</b> 1170 <b>test</b> 1171 <c>1.1</c> 1172 <c>\uF8D2</c> 1173 <x>true</x> 1174 <n xsi:nil="true" /> 1175 <n></n> 1176 <b xsi:type="double">5</b> 1177 <b xsi:type="float">5</b> 1178 <s xsi:type="string">23</s> 1179 <s py:pytype="str">42</s> 1180 <f py:pytype="float">300</f> 1181 <l py:pytype="long">2</l> 1182 <t py:pytype="TREE"></t> 1183 </a> 1184 ''') 1185 objectify.annotate(root, ignore_old=False, ignore_xsi=False, 1186 annotate_xsi=1, annotate_pytype=1) 1187 1188 # check py annotations 1189 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1190 for c in root.iterchildren() ] 1191 self.assertEquals("int", child_types[ 0]) 1192 self.assertEquals("str", child_types[ 1]) 1193 self.assertEquals("float", child_types[ 2]) 1194 self.assertEquals("str", child_types[ 3]) 1195 self.assertEquals("bool", child_types[ 4]) 1196 self.assertEquals("NoneType", child_types[ 5]) 1197 self.assertEquals(None, child_types[ 6]) 1198 self.assertEquals("float", child_types[ 7]) 1199 self.assertEquals("float", child_types[ 8]) 1200 self.assertEquals("str", child_types[ 9]) 1201 self.assertEquals("str", child_types[10]) 1202 self.assertEquals("float", child_types[11]) 1203 self.assertEquals("long", child_types[12]) 1204 self.assertEquals(TREE_PYTYPE, child_types[13]) 1205 1206 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR)) 1207 1208 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1209 for c in root.iterchildren() ] 1210 1211 # check xsi annotations 1212 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1213 for c in root.iterchildren() ] 1214 self.assertEquals("xsd:int", child_types[ 0]) 1215 self.assertEquals("xsd:string", child_types[ 1]) 1216 self.assertEquals("xsd:double", child_types[ 2]) 1217 self.assertEquals("xsd:string", child_types[ 3]) 1218 self.assertEquals("xsd:boolean", child_types[ 4]) 1219 self.assertEquals(None, child_types[ 5]) 1220 self.assertEquals(None, child_types[ 6]) 1221 self.assertEquals("xsd:double", child_types[ 7]) 1222 self.assertEquals("xsd:float", child_types[ 8]) 1223 self.assertEquals("xsd:string", child_types[ 9]) 1224 self.assertEquals("xsd:string", child_types[10]) 1225 self.assertEquals("xsd:double", child_types[11]) 1226 self.assertEquals("xsd:integer", child_types[12]) 1227 self.assertEquals(None, child_types[13]) 1228 1229 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1230
1231 - def test_xsiannotate_use_old(self):
1232 XML = self.XML 1233 root = XML(u'''\ 1234 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1235 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1236 <b>5</b> 1237 <b>test</b> 1238 <c>1.1</c> 1239 <c>\uF8D2</c> 1240 <x>true</x> 1241 <n xsi:nil="true" /> 1242 <n></n> 1243 <b xsi:type="double">5</b> 1244 <b xsi:type="float">5</b> 1245 <s xsi:type="string">23</s> 1246 <s py:pytype="str">42</s> 1247 <f py:pytype="float">300</f> 1248 <l py:pytype="long">2</l> 1249 <t py:pytype="TREE"></t> 1250 </a> 1251 ''') 1252 objectify.xsiannotate(root, ignore_old=False) 1253 1254 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1255 for c in root.iterchildren() ] 1256 self.assertEquals("xsd:int", child_types[ 0]) 1257 self.assertEquals("xsd:string", child_types[ 1]) 1258 self.assertEquals("xsd:double", child_types[ 2]) 1259 self.assertEquals("xsd:string", child_types[ 3]) 1260 self.assertEquals("xsd:boolean", child_types[ 4]) 1261 self.assertEquals(None, child_types[ 5]) 1262 self.assertEquals(None, child_types[ 6]) 1263 self.assertEquals("xsd:double", child_types[ 7]) 1264 self.assertEquals("xsd:float", child_types[ 8]) 1265 self.assertEquals("xsd:string", child_types[ 9]) 1266 self.assertEquals("xsd:string", child_types[10]) 1267 self.assertEquals("xsd:double", child_types[11]) 1268 self.assertEquals("xsd:integer", child_types[12]) 1269 self.assertEquals(None, child_types[13])
1270
1271 - def test_pyannotate_ignore_old(self):
1272 XML = self.XML 1273 root = XML(u'''\ 1274 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1275 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1276 <b>5</b> 1277 <b>test</b> 1278 <c>1.1</c> 1279 <c>\uF8D2</c> 1280 <x>true</x> 1281 <n xsi:nil="true" /> 1282 <n></n> 1283 <b xsi:type="double">5</b> 1284 <b xsi:type="float">5</b> 1285 <s xsi:type="string">23</s> 1286 <s py:pytype="str">42</s> 1287 <f py:pytype="float">300</f> 1288 <l py:pytype="long">2</l> 1289 <t py:pytype="TREE"></t> 1290 </a> 1291 ''') 1292 objectify.pyannotate(root, ignore_old=True) 1293 1294 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1295 for c in root.iterchildren() ] 1296 self.assertEquals("int", child_types[ 0]) 1297 self.assertEquals("str", child_types[ 1]) 1298 self.assertEquals("float", child_types[ 2]) 1299 self.assertEquals("str", child_types[ 3]) 1300 self.assertEquals("bool", child_types[ 4]) 1301 self.assertEquals("NoneType", child_types[ 5]) 1302 self.assertEquals(None, child_types[ 6]) 1303 self.assertEquals("float", child_types[ 7]) 1304 self.assertEquals("float", child_types[ 8]) 1305 self.assertEquals("str", child_types[ 9]) 1306 self.assertEquals("int", child_types[10]) 1307 self.assertEquals("int", child_types[11]) 1308 self.assertEquals("int", child_types[12]) 1309 self.assertEquals(None, child_types[13]) 1310 1311 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1312
1313 - def test_pyannotate_empty(self):
1314 XML = self.XML 1315 root = XML(u'''\ 1316 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1317 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1318 <n></n> 1319 </a> 1320 ''') 1321 objectify.pyannotate(root) 1322 1323 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1324 for c in root.iterchildren() ] 1325 self.assertEquals(None, child_types[0]) 1326 1327 objectify.annotate(root, empty_pytype="str") 1328 1329 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1330 for c in root.iterchildren() ] 1331 self.assertEquals("str", child_types[0])
1332
1333 - def test_pyannotate_use_old(self):
1334 XML = self.XML 1335 root = XML(u'''\ 1336 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1337 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1338 <b>5</b> 1339 <b>test</b> 1340 <c>1.1</c> 1341 <c>\uF8D2</c> 1342 <x>true</x> 1343 <n xsi:nil="true" /> 1344 <n></n> 1345 <b xsi:type="double">5</b> 1346 <b xsi:type="float">5</b> 1347 <s xsi:type="string">23</s> 1348 <s py:pytype="str">42</s> 1349 <f py:pytype="float">300</f> 1350 <l py:pytype="long">2</l> 1351 <t py:pytype="TREE"></t> 1352 </a> 1353 ''') 1354 objectify.pyannotate(root) 1355 1356 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1357 for c in root.iterchildren() ] 1358 self.assertEquals("int", child_types[ 0]) 1359 self.assertEquals("str", child_types[ 1]) 1360 self.assertEquals("float", child_types[ 2]) 1361 self.assertEquals("str", child_types[ 3]) 1362 self.assertEquals("bool", child_types[ 4]) 1363 self.assertEquals("NoneType", child_types[ 5]) 1364 self.assertEquals(None, child_types[ 6]) 1365 self.assertEquals("float", child_types[ 7]) 1366 self.assertEquals("float", child_types[ 8]) 1367 self.assertEquals("str", child_types[ 9]) 1368 self.assertEquals("str", child_types[10]) 1369 self.assertEquals("float", child_types[11]) 1370 self.assertEquals("long", child_types[12]) 1371 self.assertEquals(TREE_PYTYPE, child_types[13]) 1372 1373 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1374
1375 - def test_xsiannotate_ignore_old(self):
1376 XML = self.XML 1377 root = XML(u'''\ 1378 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1379 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1380 <b>5</b> 1381 <b>test</b> 1382 <c>1.1</c> 1383 <c>\uF8D2</c> 1384 <x>true</x> 1385 <n xsi:nil="true" /> 1386 <n></n> 1387 <b xsi:type="double">5</b> 1388 <b xsi:type="float">5</b> 1389 <s xsi:type="string">23</s> 1390 <s py:pytype="str">42</s> 1391 <f py:pytype="float">300</f> 1392 <l py:pytype="long">2</l> 1393 <t py:pytype="TREE"></t> 1394 </a> 1395 ''') 1396 objectify.xsiannotate(root, ignore_old=True) 1397 1398 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1399 for c in root.iterchildren() ] 1400 self.assertEquals("xsd:int", child_types[ 0]) 1401 self.assertEquals("xsd:string", child_types[ 1]) 1402 self.assertEquals("xsd:double", child_types[ 2]) 1403 self.assertEquals("xsd:string", child_types[ 3]) 1404 self.assertEquals("xsd:boolean", child_types[ 4]) 1405 self.assertEquals(None, child_types[ 5]) 1406 self.assertEquals(None, child_types[ 6]) 1407 self.assertEquals("xsd:int", child_types[ 7]) 1408 self.assertEquals("xsd:int", child_types[ 8]) 1409 self.assertEquals("xsd:int", child_types[ 9]) 1410 self.assertEquals("xsd:string", child_types[10]) 1411 self.assertEquals("xsd:double", child_types[11]) 1412 self.assertEquals("xsd:integer", child_types[12]) 1413 self.assertEquals(None, child_types[13]) 1414 1415 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1416
1417 - def test_deannotate(self):
1418 XML = self.XML 1419 root = XML(u'''\ 1420 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1421 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1422 <b>5</b> 1423 <b>test</b> 1424 <c>1.1</c> 1425 <c>\uF8D2</c> 1426 <x>true</x> 1427 <n xsi:nil="true" /> 1428 <n></n> 1429 <b xsi:type="double">5</b> 1430 <b xsi:type="float">5</b> 1431 <s xsi:type="string">23</s> 1432 <s py:pytype="str">42</s> 1433 <f py:pytype="float">300</f> 1434 <l py:pytype="long">2</l> 1435 <t py:pytype="TREE"></t> 1436 </a> 1437 ''') 1438 objectify.deannotate(root) 1439 1440 for c in root.getiterator(): 1441 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)) 1442 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE)) 1443 1444 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1445
1446 - def test_pytype_deannotate(self):
1447 XML = self.XML 1448 root = XML(u'''\ 1449 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1450 xmlns:py="http://codespeak.net/lxml/objectify/pytype"> 1451 <b>5</b> 1452 <b>test</b> 1453 <c>1.1</c> 1454 <c>\uF8D2</c> 1455 <x>true</x> 1456 <n xsi:nil="true" /> 1457 <n></n> 1458 <b xsi:type="double">5</b> 1459 <b xsi:type="float">5</b> 1460 <s xsi:type="string">23</s> 1461 <s py:pytype="str">42</s> 1462 <f py:pytype="float">300</f> 1463 <l py:pytype="long">2</l> 1464 <t py:pytype="TREE"></t> 1465 </a> 1466 ''') 1467 objectify.xsiannotate(root) 1468 objectify.deannotate(root, xsi=False) 1469 1470 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1471 for c in root.iterchildren() ] 1472 self.assertEquals("xsd:int", child_types[ 0]) 1473 self.assertEquals("xsd:string", child_types[ 1]) 1474 self.assertEquals("xsd:double", child_types[ 2]) 1475 self.assertEquals("xsd:string", child_types[ 3]) 1476 self.assertEquals("xsd:boolean", child_types[ 4]) 1477 self.assertEquals(None, child_types[ 5]) 1478 self.assertEquals(None, child_types[ 6]) 1479 self.assertEquals("xsd:int", child_types[ 7]) 1480 self.assertEquals("xsd:int", child_types[ 8]) 1481 self.assertEquals("xsd:int", child_types[ 9]) 1482 self.assertEquals("xsd:string", child_types[10]) 1483 self.assertEquals("xsd:double", child_types[11]) 1484 self.assertEquals("xsd:integer", child_types[12]) 1485 self.assertEquals(None, child_types[13]) 1486 1487 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR)) 1488 1489 for c in root.getiterator(): 1490 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1491
1492 - def test_xsitype_deannotate(self):
1493 XML = self.XML 1494 root = XML(u'''\ 1495 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1496 xmlns:py="http://codespeak.net/lxml/objectify/pytype" 1497 xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 1498 <b>5</b> 1499 <b>test</b> 1500 <c>1.1</c> 1501 <c>\uF8D2</c> 1502 <x>true</x> 1503 <n xsi:nil="true" /> 1504 <n></n> 1505 <b xsi:type="xsd:double">5</b> 1506 <b xsi:type="xsd:float">5</b> 1507 <s xsi:type="xsd:string">23</s> 1508 <s py:pytype="str">42</s> 1509 <f py:pytype="float">300</f> 1510 <l py:pytype="long">2</l> 1511 <t py:pytype="TREE"></t> 1512 </a> 1513 ''') 1514 objectify.annotate(root) 1515 objectify.deannotate(root, pytype=False) 1516 1517 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE) 1518 for c in root.iterchildren() ] 1519 self.assertEquals("int", child_types[ 0]) 1520 self.assertEquals("str", child_types[ 1]) 1521 self.assertEquals("float", child_types[ 2]) 1522 self.assertEquals("str", child_types[ 3]) 1523 self.assertEquals("bool", child_types[ 4]) 1524 self.assertEquals("NoneType", child_types[ 5]) 1525 self.assertEquals(None, child_types[ 6]) 1526 self.assertEquals("float", child_types[ 7]) 1527 self.assertEquals("float", child_types[ 8]) 1528 self.assertEquals("str", child_types[ 9]) 1529 self.assertEquals("int", child_types[10]) 1530 self.assertEquals("int", child_types[11]) 1531 self.assertEquals("int", child_types[12]) 1532 self.assertEquals(None, child_types[13]) 1533 1534 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR)) 1535 1536 for c in root.getiterator(): 1537 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1538
1539 - def test_pytype_deannotate(self):
1540 XML = self.XML 1541 root = XML(u'''\ 1542 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1543 xmlns:py="http://codespeak.net/lxml/objectify/pytype" 1544 xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 1545 <b xsi:type="xsd:int">5</b> 1546 <b xsi:type="xsd:string">test</b> 1547 <c xsi:type="xsd:float">1.1</c> 1548 <c xsi:type="xsd:string">\uF8D2</c> 1549 <x xsi:type="xsd:boolean">true</x> 1550 <n xsi:nil="true" /> 1551 <n></n> 1552 <b xsi:type="xsd:double">5</b> 1553 <b xsi:type="xsd:float">5</b> 1554 <s xsi:type="xsd:string">23</s> 1555 <s xsi:type="xsd:string">42</s> 1556 <f xsi:type="xsd:float">300</f> 1557 <l xsi:type="xsd:long">2</l> 1558 <t py:pytype="TREE"></t> 1559 </a> 1560 ''') 1561 objectify.annotate(root) 1562 objectify.deannotate(root, xsi=False) 1563 1564 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR) 1565 for c in root.iterchildren() ] 1566 self.assertEquals("xsd:int", child_types[ 0]) 1567 self.assertEquals("xsd:string", child_types[ 1]) 1568 self.assertEquals("xsd:float", child_types[ 2]) 1569 self.assertEquals("xsd:string", child_types[ 3]) 1570 self.assertEquals("xsd:boolean", child_types[ 4]) 1571 self.assertEquals(None, child_types[ 5]) 1572 self.assertEquals(None, child_types[ 6]) 1573 self.assertEquals("xsd:double", child_types[ 7]) 1574 self.assertEquals("xsd:float", child_types[ 8]) 1575 self.assertEquals("xsd:string", child_types[ 9]) 1576 self.assertEquals("xsd:string", child_types[10]) 1577 self.assertEquals("xsd:float", child_types[11]) 1578 self.assertEquals("xsd:long", child_types[12]) 1579 self.assertEquals(None, child_types[13]) 1580 1581 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR)) 1582 1583 for c in root.getiterator(): 1584 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1585
1586 - def test_change_pytype_attribute(self):
1587 XML = self.XML 1588 1589 xml = u'''\ 1590 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 1591 <b>5</b> 1592 <b>test</b> 1593 <c>1.1</c> 1594 <c>\uF8D2</c> 1595 <x>true</x> 1596 <n xsi:nil="true" /> 1597 <n></n> 1598 <b xsi:type="double">5</b> 1599 </a> 1600 ''' 1601 1602 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}') 1603 objectify.setPytypeAttributeTag("{TEST}test") 1604 1605 root = XML(xml) 1606 objectify.annotate(root) 1607 1608 attribs = root.xpath("//@py:%s" % pytype_name, {"py" : pytype_ns}) 1609 self.assertEquals(0, len(attribs)) 1610 attribs = root.xpath("//@py:test", {"py" : "TEST"}) 1611 self.assertEquals(7, len(attribs)) 1612 1613 objectify.setPytypeAttributeTag() 1614 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}') 1615 1616 self.assertNotEqual("test", pytype_ns.lower()) 1617 self.assertNotEqual("test", pytype_name.lower()) 1618 1619 root = XML(xml) 1620 attribs = root.xpath("//@py:%s" % pytype_name, {"py" : pytype_ns}) 1621 self.assertEquals(0, len(attribs)) 1622 1623 objectify.annotate(root) 1624 attribs = root.xpath("//@py:%s" % pytype_name, {"py" : pytype_ns}) 1625 self.assertEquals(7, len(attribs))
1626
1627 - def test_registered_types(self):
1628 orig_types = objectify.getRegisteredTypes() 1629 1630 try: 1631 orig_types[0].unregister() 1632 self.assertEquals(orig_types[1:], objectify.getRegisteredTypes()) 1633 1634 class NewType(objectify.ObjectifiedDataElement): 1635 pass
1636 1637 def checkMyType(s): 1638 return True 1639 1640 pytype = objectify.PyType("mytype", checkMyType, NewType) 1641 pytype.register() 1642 self.assert_(pytype in objectify.getRegisteredTypes()) 1643 pytype.unregister() 1644 1645 pytype.register(before = [objectify.getRegisteredTypes()[0].name]) 1646 self.assertEquals(pytype, objectify.getRegisteredTypes()[0]) 1647 pytype.unregister() 1648 1649 pytype.register(after = [objectify.getRegisteredTypes()[0].name]) 1650 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0]) 1651 pytype.unregister() 1652 1653 self.assertRaises(ValueError, pytype.register, 1654 before = [objectify.getRegisteredTypes()[0].name], 1655 after = [objectify.getRegisteredTypes()[1].name]) 1656 1657 finally: 1658 for pytype in objectify.getRegisteredTypes(): 1659 pytype.unregister() 1660 for pytype in orig_types: 1661 pytype.register() 1662
1663 - def test_object_path(self):
1664 root = self.XML(xml_str) 1665 path = objectify.ObjectPath( "root.c1.c2" ) 1666 self.assertEquals(root.c1.c2.text, path.find(root).text) 1667 self.assertEquals(root.c1.c2.text, path(root).text)
1668
1669 - def test_object_path_list(self):
1670 root = self.XML(xml_str) 1671 path = objectify.ObjectPath( ['root', 'c1', 'c2'] ) 1672 self.assertEquals(root.c1.c2.text, path.find(root).text) 1673 self.assertEquals(root.c1.c2.text, path(root).text)
1674
1675 - def test_object_path_fail(self):
1676 root = self.XML(xml_str) 1677 path = objectify.ObjectPath( "root.c1.c99" ) 1678 self.assertRaises(AttributeError, path, root) 1679 self.assertEquals(None, path(root, None))
1680
1681 - def test_object_path_syntax(self):
1682 root = self.XML(xml_str) 1683 path = objectify.ObjectPath("root . {objectified}c1. c2") 1684 self.assertEquals(root.c1.c2.text, path(root).text) 1685 1686 path = objectify.ObjectPath(" root.{objectified} c1.c2 [ 0 ] ") 1687 self.assertEquals(root.c1.c2.text, path(root).text)
1688
1689 - def test_object_path_hasattr(self):
1690 root = self.XML(xml_str) 1691 path = objectify.ObjectPath( "root" ) 1692 self.assert_(path.hasattr(root)) 1693 path = objectify.ObjectPath( "root.c1" ) 1694 self.assert_(path.hasattr(root)) 1695 path = objectify.ObjectPath( "root.c1.c2" ) 1696 self.assert_(path.hasattr(root)) 1697 path = objectify.ObjectPath( "root.c1.{otherNS}c2" ) 1698 self.assert_(path.hasattr(root)) 1699 path = objectify.ObjectPath( "root.c1.c2[1]" ) 1700 self.assert_(path.hasattr(root)) 1701 path = objectify.ObjectPath( "root.c1.c2[2]" ) 1702 self.assert_(path.hasattr(root)) 1703 path = objectify.ObjectPath( "root.c1.c2[3]" ) 1704 self.assertFalse(path.hasattr(root)) 1705 path = objectify.ObjectPath( "root.c1[1].c2" ) 1706 self.assertFalse(path.hasattr(root))
1707
1708 - def test_object_path_dot(self):
1709 root = self.XML(xml_str) 1710 path = objectify.ObjectPath( "." ) 1711 self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
1712
1713 - def test_object_path_dot_list(self):
1714 root = self.XML(xml_str) 1715 path = objectify.ObjectPath( [''] ) 1716 self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
1717
1718 - def test_object_path_dot_root(self):
1719 root = self.XML(xml_str) 1720 path = objectify.ObjectPath( ".c1.c2" ) 1721 self.assertEquals(root.c1.c2.text, path(root).text)
1722
1723 - def test_object_path_dot_root_list(self):
1724 root = self.XML(xml_str) 1725 path = objectify.ObjectPath( ['', 'c1', 'c2'] ) 1726 self.assertEquals(root.c1.c2.text, path(root).text)
1727
1728 - def test_object_path_index(self):
1729 root = self.XML(xml_str) 1730 path = objectify.ObjectPath( "root.c1[0].c2[0]" ) 1731 self.assertEquals(root.c1.c2.text, path(root).text) 1732 1733 path = objectify.ObjectPath( "root.c1[0].c2" ) 1734 self.assertEquals(root.c1.c2.text, path(root).text) 1735 1736 path = objectify.ObjectPath( "root.c1[0].c2[1]" ) 1737 self.assertEquals(root.c1.c2[1].text, path(root).text) 1738 1739 path = objectify.ObjectPath( "root.c1.c2[2]" ) 1740 self.assertEquals(root.c1.c2[2].text, path(root).text) 1741 1742 path = objectify.ObjectPath( "root.c1.c2[-1]" ) 1743 self.assertEquals(root.c1.c2[-1].text, path(root).text) 1744 1745 path = objectify.ObjectPath( "root.c1.c2[-3]" ) 1746 self.assertEquals(root.c1.c2[-3].text, path(root).text)
1747
1748 - def test_object_path_index_list(self):
1749 root = self.XML(xml_str) 1750 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] ) 1751 self.assertEquals(root.c1.c2.text, path(root).text) 1752 1753 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] ) 1754 self.assertEquals(root.c1.c2[2].text, path(root).text) 1755 1756 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] ) 1757 self.assertEquals(root.c1.c2[2].text, path(root).text) 1758 1759 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] ) 1760 self.assertEquals(root.c1.c2[-1].text, path(root).text) 1761 1762 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] ) 1763 self.assertEquals(root.c1.c2[-3].text, path(root).text)
1764
1765 - def test_object_path_index_fail_parse(self):
1766 self.assertRaises(ValueError, objectify.ObjectPath, 1767 "root.c1[0].c2[-1-2]") 1768 self.assertRaises(ValueError, objectify.ObjectPath, 1769 ['root', 'c1[0]', 'c2[-1-2]']) 1770 1771 self.assertRaises(ValueError, objectify.ObjectPath, 1772 "root[2].c1.c2") 1773 self.assertRaises(ValueError, objectify.ObjectPath, 1774 ['root[2]', 'c1', 'c2']) 1775 1776 self.assertRaises(ValueError, objectify.ObjectPath, 1777 []) 1778 self.assertRaises(ValueError, objectify.ObjectPath, 1779 ['', '', ''])
1780
1781 - def test_object_path_index_fail_lookup(self):
1782 root = self.XML(xml_str) 1783 path = objectify.ObjectPath("root.c1[9999].c2") 1784 self.assertRaises(AttributeError, path, root) 1785 1786 path = objectify.ObjectPath("root.c1[0].c2[9999]") 1787 self.assertRaises(AttributeError, path, root) 1788 1789 path = objectify.ObjectPath(".c1[9999].c2[0]") 1790 self.assertRaises(AttributeError, path, root) 1791 1792 path = objectify.ObjectPath("root.c1[-2].c2") 1793 self.assertRaises(AttributeError, path, root) 1794 1795 path = objectify.ObjectPath("root.c1[0].c2[-4]") 1796 self.assertRaises(AttributeError, path, root)
1797
1798 - def test_object_path_ns(self):
1799 root = self.XML(xml_str) 1800 path = objectify.ObjectPath( "{objectified}root.c1.c2" ) 1801 self.assertEquals(root.c1.c2.text, path.find(root).text) 1802 path = objectify.ObjectPath( "{objectified}root.{objectified}c1.c2" ) 1803 self.assertEquals(root.c1.c2.text, path.find(root).text) 1804 path = objectify.ObjectPath( "root.{objectified}c1.{objectified}c2" ) 1805 self.assertEquals(root.c1.c2.text, path.find(root).text) 1806 path = objectify.ObjectPath( "root.c1.{objectified}c2" ) 1807 self.assertEquals(root.c1.c2.text, path.find(root).text) 1808 path = objectify.ObjectPath( "root.c1.{otherNS}c2" ) 1809 self.assertEquals(getattr(root.c1, '{otherNS}c2').text, 1810 path.find(root).text)
1811
1812 - def test_object_path_ns_list(self):
1813 root = self.XML(xml_str) 1814 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] ) 1815 self.assertEquals(root.c1.c2.text, path.find(root).text) 1816 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] ) 1817 self.assertEquals(root.c1.c2.text, path.find(root).text) 1818 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] ) 1819 self.assertEquals(root.c1.c2.text, path.find(root).text) 1820 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] ) 1821 self.assertEquals(root.c1.c2[2].text, path.find(root).text) 1822 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] ) 1823 self.assertEquals(root.c1.c2.text, path.find(root).text) 1824 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] ) 1825 self.assertEquals(root.c1.c2[2].text, path.find(root).text) 1826 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] ) 1827 self.assertEquals(getattr(root.c1, '{otherNS}c2').text, 1828 path.find(root).text)
1829
1830 - def test_object_path_set(self):
1831 root = self.XML(xml_str) 1832 path = objectify.ObjectPath( "root.c1.c2" ) 1833 self.assertEquals(root.c1.c2.text, path.find(root).text) 1834 self.assertEquals("1", root.c1.c2[1].text) 1835 1836 new_value = "my new value" 1837 path.setattr(root, new_value) 1838 1839 self.assertEquals(new_value, root.c1.c2.text) 1840 self.assertEquals(new_value, path(root).text) 1841 self.assertEquals("1", root.c1.c2[1].text)
1842
1843 - def test_object_path_set_element(self):
1844 root = self.XML(xml_str) 1845 path = objectify.ObjectPath( "root.c1.c2" ) 1846 self.assertEquals(root.c1.c2.text, path.find(root).text) 1847 self.assertEquals("1", root.c1.c2[1].text) 1848 1849 new_el = self.Element("{objectified}test") 1850 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST" 1851 path.setattr(root, new_el.sub) 1852 1853 self.assertEquals("ATTR", root.c1.c2.get("myattr")) 1854 self.assertEquals("TEST", root.c1.c2.a.text) 1855 self.assertEquals("TEST", path(root).a.text) 1856 self.assertEquals("1", root.c1.c2[1].text)
1857
1858 - def test_object_path_set_create(self):
1859 root = self.XML(xml_str) 1860 path = objectify.ObjectPath( "root.c1.c99" ) 1861 self.assertRaises(AttributeError, path.find, root) 1862 1863 new_value = "my new value" 1864 path.setattr(root, new_value) 1865 1866 self.assertEquals(1, len(root.c1.c99)) 1867 self.assertEquals(new_value, root.c1.c99.text) 1868 self.assertEquals(new_value, path(root).text)
1869
1870 - def test_object_path_set_create_element(self):
1871 root = self.XML(xml_str) 1872 path = objectify.ObjectPath( "root.c1.c99" ) 1873 self.assertRaises(AttributeError, path.find, root) 1874 1875 new_el = self.Element("{objectified}test") 1876 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST" 1877 path.setattr(root, new_el.sub) 1878 1879 self.assertEquals(1, len(root.c1.c99)) 1880 self.assertEquals("ATTR", root.c1.c99.get("myattr")) 1881 self.assertEquals("TEST", root.c1.c99.a.text) 1882 self.assertEquals("TEST", path(root).a.text)
1883
1884 - def test_object_path_set_create_list(self):
1885 root = self.XML(xml_str) 1886 path = objectify.ObjectPath( "root.c1.c99" ) 1887 self.assertRaises(AttributeError, path.find, root) 1888 1889 new_el = self.Element("{objectified}test") 1890 new_el.a = ["TEST1", "TEST2"] 1891 new_el.a[0].set("myattr", "ATTR1") 1892 new_el.a[1].set("myattr", "ATTR2") 1893 1894 path.setattr(root, list(new_el.a)) 1895 1896 self.assertEquals(2, len(root.c1.c99)) 1897 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr")) 1898 self.assertEquals("TEST1", root.c1.c99[0].text) 1899 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr")) 1900 self.assertEquals("TEST2", root.c1.c99[1].text) 1901 self.assertEquals("TEST1", path(root).text)
1902
1903 - def test_object_path_addattr(self):
1904 root = self.XML(xml_str) 1905 path = objectify.ObjectPath( "root.c1.c2" ) 1906 self.assertEquals(3, len(root.c1.c2)) 1907 path.addattr(root, "test") 1908 self.assertEquals(4, len(root.c1.c2)) 1909 self.assertEquals(["0", "1", "2", "test"], 1910 [el.text for el in root.c1.c2])
1911
1912 - def test_object_path_addattr_element(self):
1913 root = self.XML(xml_str) 1914 path = objectify.ObjectPath( "root.c1.c2" ) 1915 self.assertEquals(3, len(root.c1.c2)) 1916 1917 new_el = self.Element("{objectified}test") 1918 etree.SubElement(new_el, "{objectified}sub").a = "TEST" 1919 1920 path.addattr(root, new_el.sub) 1921 self.assertEquals(4, len(root.c1.c2)) 1922 self.assertEquals("TEST", root.c1.c2[3].a.text) 1923 self.assertEquals(["0", "1", "2"], 1924 [el.text for el in root.c1.c2[:3]])
1925
1926 - def test_object_path_addattr_create(self):
1927 root = self.XML(xml_str) 1928 path = objectify.ObjectPath( "root.c1.c99" ) 1929 self.assertRaises(AttributeError, path.find, root) 1930 1931 new_value = "my new value" 1932 path.addattr(root, new_value) 1933 1934 self.assertEquals(1, len(root.c1.c99)) 1935 self.assertEquals(new_value, root.c1.c99.text) 1936 self.assertEquals(new_value, path(root).text)
1937
1938 - def test_object_path_addattr_create_element(self):
1939 root = self.XML(xml_str) 1940 path = objectify.ObjectPath( "root.c1.c99" ) 1941 self.assertRaises(AttributeError, path.find, root) 1942 1943 new_el = self.Element("{objectified}test") 1944 etree.SubElement(new_el, "{objectified}sub", myattr="ATTR").a = "TEST" 1945 1946 path.addattr(root, new_el.sub) 1947 self.assertEquals(1, len(root.c1.c99)) 1948 self.assertEquals("TEST", root.c1.c99.a.text) 1949 self.assertEquals("TEST", path(root).a.text) 1950 self.assertEquals("ATTR", root.c1.c99.get("myattr"))
1951
1952 - def test_object_path_addattr_create_list(self):
1953 root = self.XML(xml_str) 1954 path = objectify.ObjectPath( "root.c1.c99" ) 1955 self.assertRaises(AttributeError, path.find, root) 1956 1957 new_el = self.Element("{objectified}test") 1958 new_el.a = ["TEST1", "TEST2"] 1959 1960 self.assertEquals(2, len(new_el.a)) 1961 1962 path.addattr(root, list(new_el.a)) 1963 self.assertEquals(2, len(root.c1.c99)) 1964 self.assertEquals("TEST1", root.c1.c99.text) 1965 self.assertEquals("TEST2", path(root)[1].text)
1966
1967 - def test_descendant_paths(self):
1968 root = self.XML(xml_str) 1969 self.assertEquals( 1970 ['{objectified}root', '{objectified}root.c1', 1971 '{objectified}root.c1.c2', 1972 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]', 1973 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'], 1974 root.descendantpaths())
1975
1976 - def test_descendant_paths_child(self):
1977 root = self.XML(xml_str) 1978 self.assertEquals( 1979 ['{objectified}c1', '{objectified}c1.c2', 1980 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]', 1981 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'], 1982 root.c1.descendantpaths())
1983
1984 - def test_descendant_paths_prefix(self):
1985 root = self.XML(xml_str) 1986 self.assertEquals( 1987 ['root.{objectified}c1', 'root.{objectified}c1.c2', 1988 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]', 1989 'root.{objectified}c1.{otherNS}c2', 1990 'root.{objectified}c1.{}c2'], 1991 root.c1.descendantpaths('root'))
1992
1993 - def test_pickle(self):
1994 import pickle 1995 1996 root = self.XML(xml_str) 1997 out = StringIO() 1998 pickle.dump(root, out) 1999 2000 new_root = pickle.loads(out.getvalue()) 2001 self.assertEquals( 2002 etree.tostring(new_root), 2003 etree.tostring(root))
2004 2005 # E-Factory tests, need to use sub-elements as root element is always 2006 # type-looked-up as ObjectifiedElement (no annotations)
2007 - def test_efactory_int(self):
2008 E = objectify.E 2009 root = E.root(E.val(23)) 2010 self.assert_(isinstance(root.val, objectify.IntElement))
2011
2012 - def test_efactory_long(self):
2013 E = objectify.E 2014 root = E.root(E.val(23L)) 2015 self.assert_(isinstance(root.val, objectify.LongElement))
2016
2017 - def test_efactory_float(self):
2018 E = objectify.E 2019 root = E.root(E.val(233.23)) 2020 self.assert_(isinstance(root.val, objectify.FloatElement))
2021
2022 - def test_efactory_str(self):
2023 E = objectify.E 2024 root = E.root(E.val("what?")) 2025 self.assert_(isinstance(root.val, objectify.StringElement))
2026
2027 - def test_efactory_unicode(self):
2028 E = objectify.E 2029 root = E.root(E.val(unicode("blöödy häll", encoding="ISO-8859-1"))) 2030 self.assert_(isinstance(root.val, objectify.StringElement))
2031
2032 - def test_efactory_bool(self):
2033 E = objectify.E 2034 root = E.root(E.val(True)) 2035 self.assert_(isinstance(root.val, objectify.BoolElement))
2036
2037 - def test_efactory_none(self):
2038 E = objectify.E 2039 root = E.root(E.val(None)) 2040 self.assert_(isinstance(root.val, objectify.NoneElement))
2041
2042 - def test_efactory_value_concatenation(self):
2043 E = objectify.E 2044 root = E.root(E.val(1, "foo", 2.0, "bar ", True, None)) 2045 self.assert_(isinstance(root.val, objectify.StringElement))
2046
2047 - def test_efactory_attrib(self):
2048 E = objectify.E 2049 root = E.root(foo="bar") 2050 self.assertEquals(root.get("foo"), "bar")
2051
2052 - def test_efactory_nested(self):
2053 E = objectify.E 2054 DataElement = objectify.DataElement 2055 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1), 2056 DataElement(2.0)) 2057 self.assert_(isinstance(root, objectify.ObjectifiedElement)) 2058 self.assertEquals(root.text, "text") 2059 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement)) 2060 self.assertEquals(root.sub.tail, "tail") 2061 self.assert_(isinstance(root.sub.subsub, objectify.StringElement)) 2062 self.assertEquals(len(root.value), 2) 2063 self.assert_(isinstance(root.value[0], objectify.IntElement)) 2064 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2065
2066 -def test_suite():
2067 suite = unittest.TestSuite() 2068 suite.addTests([unittest.makeSuite(ObjectifyTestCase)]) 2069 suite.addTests( 2070 [doctest.DocFileSuite('../../../doc/objectify.txt')]) 2071 return suite
2072 2073 if __name__ == '__main__': 2074 print 'to test use test.py %s' % __file__ 2075