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