Adds test scripts for _validate_values

Shortened _validate_values logic
Wrote unittests for _validate_values

Change-Id: I0f947d20b73aa96524121257784f9ba42489e477
This commit is contained in:
Zhongyue Luo 2012-11-09 10:26:53 +08:00
parent 6eac551fce
commit a49887dd50
2 changed files with 15 additions and 5 deletions

View File

@ -42,11 +42,8 @@ def _validate_boolean(data, valid_values=None):
def _validate_values(data, valid_values=None):
if data in valid_values:
return
else:
msg_dict = dict(data=data, values=valid_values)
msg = _("%(data)s is not in %(values)s") % msg_dict
if data not in valid_values:
msg = _("'%(data)s' is not in %(valid_values)s") % locals()
LOG.debug("validate_values: %s", msg)
return msg

View File

@ -63,6 +63,19 @@ class TestAttributes(unittest2.TestCase):
msg = attributes._validate_boolean(None)
self.assertEquals(msg, "'None' is not boolean")
def test_values(self):
msg = attributes._validate_values(4, [4, 6])
self.assertIsNone(msg)
msg = attributes._validate_values(4, (4, 6))
self.assertIsNone(msg)
msg = attributes._validate_values(7, [4, 6])
self.assertEquals(msg, "'7' is not in [4, 6]")
msg = attributes._validate_values(7, (4, 6))
self.assertEquals(msg, "'7' is not in (4, 6)")
def test_strings(self):
msg = attributes._validate_string(None, None)
self.assertEquals(msg, "'None' is not a valid string")