Merge "Add method validate_integer"

This commit is contained in:
Zuul 2017-12-05 19:38:06 +00:00 committed by Gerrit Code Review
commit 58fb709f58
3 changed files with 63 additions and 0 deletions

View File

@ -438,6 +438,35 @@ def check_string_length(value, name=None, min_length=0, max_length=None):
raise ValueError(msg)
def validate_integer(value, name, min_value=None, max_value=None):
"""Make sure that value is a valid integer, potentially within range.
:param value: value of the integer
:param name: name of the integer
:param min_value: min_value of the integer
:param max_value: max_value of the integer
:returns: integer
"""
try:
value = int(str(value))
except (ValueError, UnicodeEncodeError):
msg = _('%(value_name)s must be an integer'
) % {'value_name': name}
raise ValueError(msg)
if min_value is not None and value < min_value:
msg = _('%(value_name)s must be >= %(min_value)d'
) % {'value_name': name, 'min_value': min_value}
raise ValueError(msg)
if max_value is not None and value > max_value:
msg = _('%(value_name)s must be <= %(max_value)d'
) % {'value_name': name, 'max_value': max_value}
raise ValueError(msg)
return value
def split_path(path, minsegs=1, maxsegs=None, rest_with_last=False):
"""Validate and split the given HTTP request path.

View File

@ -18,6 +18,7 @@
import copy
import math
import ddt
import mock
from oslotest import base as test_base
import six
@ -804,3 +805,35 @@ class SplitByCommas(test_base.BaseTestCase):
def test_with_escaped_quotes_in_row_inside_quoted(self):
self.check(['a"b""c', 'd'], r'"a\"b\"\"c",d')
@ddt.ddt
class ValidateIntegerTestCase(test_base.BaseTestCase):
@ddt.unpack
@ddt.data({"value": 42, "name": "answer", "output": 42},
{"value": "42", "name": "answer", "output": 42},
{"value": "7", "name": "lucky", "output": 7,
"min_value": 7, "max_value": 8},
{"value": 7, "name": "lucky", "output": 7,
"min_value": 6, "max_value": 7},
{"value": 300, "name": "Spartaaa!!!", "output": 300,
"min_value": 300},
{"value": "300", "name": "Spartaaa!!!", "output": 300,
"max_value": 300})
def test_valid_inputs(self, output, value, name, **kwargs):
self.assertEqual(strutils.validate_integer(value, name,
**kwargs), output)
@ddt.unpack
@ddt.data({"value": "im-not-an-int", "name": ''},
{"value": 3.14, "name": "Pie"},
{"value": "299", "name": "Sparta no-show",
"min_value": 300, "max_value": 300},
{"value": 55, "name": "doing 55 in a 54",
"max_value": 54},
{"value": six.unichr(129), "name": "UnicodeError",
"max_value": 1000})
def test_invalid_inputs(self, value, name, **kwargs):
self.assertRaises(ValueError, strutils.validate_integer,
value, name, **kwargs)

View File

@ -10,6 +10,7 @@ testrepository>=0.0.18 # Apache-2.0/BSD
testscenarios>=0.4 # Apache-2.0/BSD
testtools>=2.2.0 # MIT
oslotest>=1.10.0 # Apache-2.0
ddt>=1.0.1 # MIT
# when we can require tox>= 1.4, this can go into tox.ini:
# [testenv:cover]