Merge "Add validator to test integers"

This commit is contained in:
Jenkins 2016-07-22 18:03:42 +00:00 committed by Gerrit Code Review
commit fd36a04cba
2 changed files with 59 additions and 0 deletions

View File

@ -141,6 +141,40 @@ def validate_boolean(data, valid_values=None):
return msg
def validate_integer(data, valid_values=None):
"""This function validates if the data is an integer.
It checks both number or string provided to validate it's an
integer and returns a message with the error if it's not
:param data: The string or number to validate as integer
:param valid_values: values to limit the 'data' to
:return: Message if not an integer.
"""
if valid_values is not None:
msg = validate_values(data=data, valid_values=valid_values)
if msg:
return msg
msg = _("'%s' is not an integer") % data
try:
fl_n = float(data)
int_n = int(data)
except (ValueError, TypeError, OverflowError):
LOG.debug(msg)
return msg
# Fail test if non equal or boolean
if fl_n != int_n:
LOG.debug(msg)
return msg
elif isinstance(data, bool):
msg = _("'%s' is not an integer:boolean") % data
LOG.debug(msg)
return msg
def validate_range(data, valid_values=None):
"""Check that integer value is within a range provided.
@ -580,6 +614,7 @@ validators = {'type:dict': validate_dict,
'type:uuid_list': validate_uuid_list,
'type:values': validate_values,
'type:boolean': validate_boolean,
'type:integer': validate_integer,
'type:list_of_unique_strings': validate_list_of_unique_strings}

View File

@ -181,6 +181,30 @@ class TestAttributeValidation(base.BaseTestCase):
msg = validators.validate_boolean("fasle")
self.assertEqual("'fasle' is not a valid boolean value", msg)
def test_validate_integer(self):
msg = validators.validate_integer(1)
self.assertIsNone(msg)
msg = validators.validate_integer(0.1)
self.assertEqual("'0.1' is not an integer", msg)
msg = validators.validate_integer("1")
self.assertIsNone(msg)
msg = validators.validate_integer("0.1")
self.assertEqual("'0.1' is not an integer", msg)
msg = validators.validate_integer(True)
self.assertEqual("'True' is not an integer:boolean", msg)
msg = validators.validate_integer(False)
self.assertEqual("'False' is not an integer:boolean", msg)
msg = validators.validate_integer(float('Inf'))
self.assertEqual("'inf' is not an integer", msg)
msg = validators.validate_integer(None)
self.assertEqual("'None' is not an integer", msg)
def test_validate_integer_values(self):
msg = validators.validate_integer(2, [2, 3, 4, 5])
self.assertIsNone(msg)
msg = validators.validate_integer(1, [2, 3, 4, 5])
self.assertEqual("'1' is not in [2, 3, 4, 5]", msg)
def test_validate_no_whitespace(self):
data = 'no_white_space'
result = validators.validate_no_whitespace(data)