Commits

Anonymous committed dccbd33

add a test for nillable element

  • Participants
  • Parent commits c7fdecb

Comments (0)

Files changed (2)

File osa/xmltypes.py

                 Element to recover from.
         """
         # element is nill
-        if bool(element.get('nil')):
+        if element.get('{%s}nil' % xmlnamespace.NS_XSI, "false") == "true":
             return
 
         all_children_names = []
                 if self._children[ind]['min'] != 0 and \
                    self._children[ind]['nillable'] is False:
                     raise ValueError("Non-nillable %s element is nil." % name)
+            # None, i.e. nillables, should also be placed here 
+            if self._children[ind]['max'].__class__.__name__ != "int" or\
+               self._children[ind]['max'] > 1:
+                current_value = getattr(self, name, None)
+                if current_value is None:
+                    current_value = []
+                    setattr(self, name, current_value)
+                current_value.append(subvalue)
             else:
-                if self._children[ind]['max'].__class__.__name__ != "int" or\
-                   self._children[ind]['max'] > 1:
-                    current_value = getattr(self, name, None)
-                    if current_value is None:
-                        current_value = []
-                        setattr(self, name, current_value)
-                    current_value.append(subvalue)
-                else:
-                    setattr(self, name, subvalue)
+                setattr(self, name, subvalue)
             del name, ind, inst
 
         # now all children were processed, so remove them to save memory

File tests/test_xmltypes_complex.py

 
 Address = ComplexTypeMeta('Address', (), {
                 "_children":[
-                    {'name':"street", "type":XMLString, "min":1, "max": 1, "fullname":"street"},
+                    {'name':"street", "type":XMLString, "min":1, "max": 1, "nillable":False, "fullname":"street"},
                     {'name':"city", "type":XMLString, "min":1, "max": 1, "fullname":"city"},
-                    {'name':"zip", "type":XMLInteger, "min":1, "max": 1,"fullname":"zip"},
+                    {'name':"zip", "type":XMLInteger, "min":1, "max": 1, "nillable":True, "fullname":"zip"},
                     {'name':"since", "type":XMLDateTime, "min":0, "max": 1, "fullname":"since"},
                     {'name':"lattitude", "type":XMLDouble, "min":1, "max": 1, "fullname":"lattitude"},
                     {'name':"longitude", "type":XMLDouble, "min":1, "max": 1, "fullname":"longitude"},
                     {'name':"level4", "type":Level4, "min":0, "max": 'unbouneded',"fullname":"level4"},
                         ], "__doc__": "don't know"})
 
+
 class TestClassSerializer(unittest.TestCase):
+
     def test_simple_class(self):
         a = Address()
         a.street = '123 happy way'
         self.assertEqual(a.lattitude, r.lattitude)
         self.assertEqual(a.longitude, r.longitude)
         self.assertEqual(a.since, r.since)
+
     def test_tofrom_file(self):
         fname = "out.xml"
         a = Address()
         b = Address.from_file(fname)
         self.assertEqual(b, a)
         self.assertTrue(b is not a)
+
+    def test_nillable(self):
+        a = Address()
+        a.street = '123 happy way'
+        a.city = 'badtown'
+        a.zip = 32
+        a.lattitude = 4.3
+        a.longitude = 88.0
+
+        element = etree.Element('test')
+        a.to_xml( element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+        self.assertEqual(5, len(element.getchildren()))
+
+        element[2].text=None  # zip is nillable 
+        element[2].set('{%s}nil' % xmlnamespace.NS_XSI, 'true')
+        r = Address().from_xml(element)
+        self.assertEqual(a.street, r.street)
+        self.assertEqual(a.city, r.city)
+        self.assertEqual(r.zip, None)
+        self.assertEqual(a.lattitude, r.lattitude)
+        self.assertEqual(a.longitude, r.longitude)
+        self.assertEqual(a.since, r.since)
+
+        element = etree.Element('test')
+        a.to_xml( element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+        self.assertEqual(5, len(element.getchildren()))
+        element[0].text=None  # street is not nillable 
+        element[0].set('{%s}nil' % xmlnamespace.NS_XSI, 'true')
+        self.assertRaises(ValueError, Address().from_xml, element)
+
+        element = etree.Element('test')
+        a.to_xml( element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+        element.clear()
+        self.assertEqual(0, len(element.getchildren()))
+        element.set('{%s}nil' % xmlnamespace.NS_XSI, 'true')
+        r = Address().from_xml(element)
+        self.assertTrue(r is None)