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