Fix serialize BoolStr formatter
The BoolStr formatter used the same method to serialize and deserialize boolean strings. As a result, when changing the value of a resource property that uses this formatter, a boolean will be stored within the resource instead of a boolean string (which was stored before the update). Change-Id: I6117a8e4c3281fefafd5605067a7182b5f03028c
This commit is contained in:
parent
176df5511d
commit
4c7ead9503
@ -111,23 +111,26 @@ class UNIXEpoch(Formatter):
|
||||
|
||||
class BoolStr(Formatter):
|
||||
|
||||
# The behavior here primarily exists for the deserialize method
|
||||
# to be producing Python booleans.
|
||||
|
||||
@classmethod
|
||||
def deserialize(cls, value):
|
||||
return cls.convert(value)
|
||||
|
||||
@classmethod
|
||||
def serialize(cls, value):
|
||||
return cls.convert(value)
|
||||
|
||||
@classmethod
|
||||
def convert(cls, value):
|
||||
"""Convert a boolean string to a boolean"""
|
||||
expr = str(value).lower()
|
||||
if "true" == expr:
|
||||
return True
|
||||
elif "false" == expr:
|
||||
return False
|
||||
else:
|
||||
raise ValueError("Unable to convert as boolean: %s" % value)
|
||||
raise ValueError("Unable to deserialize boolean string: %s"
|
||||
% value)
|
||||
|
||||
@classmethod
|
||||
def serialize(cls, value):
|
||||
"""Convert a boolean to a boolean string"""
|
||||
if isinstance(value, bool):
|
||||
if value:
|
||||
return "true"
|
||||
else:
|
||||
return "false"
|
||||
else:
|
||||
raise ValueError("Unable to serialize boolean string: %s"
|
||||
% value)
|
||||
|
@ -19,24 +19,25 @@ from openstack import format
|
||||
|
||||
class TestBoolStrFormatter(testtools.TestCase):
|
||||
|
||||
# NOTE: serialize/deserialize go through the same code path
|
||||
def test_deserialize(self):
|
||||
self.assertTrue(format.BoolStr.deserialize(True))
|
||||
self.assertTrue(format.BoolStr.deserialize('True'))
|
||||
self.assertTrue(format.BoolStr.deserialize('TRUE'))
|
||||
self.assertTrue(format.BoolStr.deserialize('true'))
|
||||
self.assertFalse(format.BoolStr.deserialize(False))
|
||||
self.assertFalse(format.BoolStr.deserialize('False'))
|
||||
self.assertFalse(format.BoolStr.deserialize('FALSE'))
|
||||
self.assertFalse(format.BoolStr.deserialize('false'))
|
||||
self.assertRaises(ValueError, format.BoolStr.deserialize, None)
|
||||
self.assertRaises(ValueError, format.BoolStr.deserialize, '')
|
||||
self.assertRaises(ValueError, format.BoolStr.deserialize, 'INVALID')
|
||||
|
||||
def test_format_true(self):
|
||||
self.assertTrue(format.BoolStr.serialize(True))
|
||||
self.assertTrue(format.BoolStr.serialize('True'))
|
||||
self.assertTrue(format.BoolStr.serialize('TRUE'))
|
||||
self.assertTrue(format.BoolStr.serialize('true'))
|
||||
|
||||
def test_format_false(self):
|
||||
self.assertFalse(format.BoolStr.serialize(False))
|
||||
self.assertFalse(format.BoolStr.serialize('False'))
|
||||
self.assertFalse(format.BoolStr.serialize('FALSE'))
|
||||
self.assertFalse(format.BoolStr.serialize('false'))
|
||||
|
||||
def test_format_fails(self):
|
||||
def test_serialize(self):
|
||||
self.assertEqual('true', format.BoolStr.serialize(True))
|
||||
self.assertEqual('false', format.BoolStr.serialize(False))
|
||||
self.assertRaises(ValueError, format.BoolStr.serialize, None)
|
||||
self.assertRaises(ValueError, format.BoolStr.serialize, '')
|
||||
self.assertRaises(ValueError, format.BoolStr.serialize, 'INVALID')
|
||||
self.assertRaises(ValueError, format.BoolStr.serialize, 'True')
|
||||
|
||||
|
||||
class TestISO8601Formatter(testtools.TestCase):
|
||||
|
Loading…
Reference in New Issue
Block a user