Merge "Make Boolean property a bool type. 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."
This commit is contained in:
commit
1d66f9b943
|
@ -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()
|
||||
|
|
|
@ -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'}}
|
||||
|
|
Loading…
Reference in New Issue