]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Make Boolean property a bool type.
authorSteve Baker <sbaker@redhat.com>
Tue, 6 Nov 2012 02:38:53 +0000 (15:38 +1300)
committerSteve Baker <sbaker@redhat.com>
Tue, 6 Nov 2012 02:39:23 +0000 (15:39 +1300)
Since json has a native bool type, allow bool or string as the property value.
Validating a Boolean type value will now convert a string to a bool.

Change-Id: Ia1843e0f56e5afb2914708e42b62f2063279317f

heat/engine/resources/properties.py
heat/tests/test_properties.py

index 3ef80e4bde7b9fab2b2a8b7c705a6ae92fc4aed6..a3af960ef213a8bb1b5243a449ed187e096b2f1d 100644 (file)
@@ -118,11 +118,13 @@ class Property(object):
         return children
 
     def _validate_bool(self, value):
+        if isinstance(value, bool):
+            return value
         normalised = value.lower()
         if normalised not in ['true', 'false']:
             raise ValueError('"%s" is not a valid boolean')
 
-        return normalised
+        return normalised == 'true'
 
     def validate_data(self, value):
         t = self.type()
index 564c73a8f5822a2e89bcd9c8d6e4faa0cd14597c..a20a7e3e7a0c5db814c313edeb554249d92bc3c9 100644 (file)
@@ -167,11 +167,15 @@ class PropertyTest(unittest.TestCase):
 
     def test_boolean_true(self):
         p = properties.Property({'Type': 'Boolean'})
-        self.assertEqual(p.validate_data('True'), 'true')
+        self.assertEqual(p.validate_data('True'), True)
+        self.assertEqual(p.validate_data('true'), True)
+        self.assertEqual(p.validate_data(True), True)
 
     def test_boolean_false(self):
         p = properties.Property({'Type': 'Boolean'})
-        self.assertEqual(p.validate_data('False'), 'false')
+        self.assertEqual(p.validate_data('False'), False)
+        self.assertEqual(p.validate_data('false'), False)
+        self.assertEqual(p.validate_data(False), False)
 
     def test_boolean_invalid(self):
         p = properties.Property({'Type': 'Boolean'})
@@ -212,7 +216,7 @@ class PropertyTest(unittest.TestCase):
     def test_map_schema_good(self):
         map_schema = {'valid': {'Type': 'Boolean'}}
         p = properties.Property({'Type': 'Map', 'Schema': map_schema})
-        self.assertEqual(p.validate_data({'valid': 'TRUE'}), {'valid': 'true'})
+        self.assertEqual(p.validate_data({'valid': 'TRUE'}), {'valid': True})
 
     def test_map_schema_bad_data(self):
         map_schema = {'valid': {'Type': 'Boolean'}}
@@ -235,8 +239,8 @@ class PropertyTest(unittest.TestCase):
         p = properties.Property({'Type': 'List', 'Schema': list_schema})
         self.assertEqual(p.validate_data([{'valid': 'TRUE'},
                                           {'valid': 'False'}]),
-                                         [{'valid': 'true'},
-                                          {'valid': 'false'}])
+                                         [{'valid': True},
+                                          {'valid': False}])
 
     def test_list_schema_bad_data(self):
         map_schema = {'valid': {'Type': 'Boolean'}}