lxml.tests.test_objectify

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

# Source Code for Module lxml.tests.test_objectify

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