Merge "Let Integer property convert strings to numbers"
This commit is contained in:
commit
e3f6a7f184
@ -236,9 +236,12 @@ class Property(object):
|
||||
def _validate_integer(self, value):
|
||||
if value is None:
|
||||
value = self.has_default() and self.default() or 0
|
||||
if not isinstance(value, (int, long)):
|
||||
raise TypeError(_('value is not an integer'))
|
||||
return self._validate_number(value)
|
||||
try:
|
||||
value = int(value)
|
||||
except ValueError:
|
||||
raise TypeError(_("Value '%s' is not an integer") % value)
|
||||
else:
|
||||
return value
|
||||
|
||||
def _validate_number(self, value):
|
||||
if value is None:
|
||||
|
@ -683,7 +683,20 @@ class PropertyTest(testtools.TestCase):
|
||||
def test_int_bad(self):
|
||||
schema = {'Type': 'Integer'}
|
||||
p = properties.Property(schema)
|
||||
self.assertRaises(TypeError, p.validate_data, '3')
|
||||
ex = self.assertRaises(TypeError, p.validate_data, [1])
|
||||
self.assertEqual("int() argument must be a string or a number, "
|
||||
"not 'list'", str(ex))
|
||||
|
||||
def test_int_from_str_good(self):
|
||||
schema = {'Type': 'Integer'}
|
||||
p = properties.Property(schema)
|
||||
self.assertEqual(3, p.validate_data('3'))
|
||||
|
||||
def test_int_from_str_bad(self):
|
||||
schema = {'Type': 'Integer'}
|
||||
p = properties.Property(schema)
|
||||
ex = self.assertRaises(TypeError, p.validate_data, '3a')
|
||||
self.assertEqual("Value '3a' is not an integer", str(ex))
|
||||
|
||||
def test_integer_low(self):
|
||||
schema = {'Type': 'Integer',
|
||||
|
Loading…
x
Reference in New Issue
Block a user