First pass at validation unit tests. Haven't figured out class methods yet.
This commit is contained in:
41
nova/tests/validator_unittest.py
Normal file
41
nova/tests/validator_unittest.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
# Copyright [2010] [Anso Labs, LLC]
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
from nova import vendor
|
||||
|
||||
from nova import flags
|
||||
from nova import test
|
||||
from nova import validate
|
||||
|
||||
|
||||
class ValidationTestCase(test.TrialTestCase):
|
||||
def setUp(self):
|
||||
super(ValidationTestCase, self).setUp()
|
||||
|
||||
def tearDown(self):
|
||||
super(ValidationTestCase, self).tearDown()
|
||||
|
||||
def test_type_validation(self):
|
||||
self.assertTrue(type_case("foo", 5, 1))
|
||||
self.assertRaises(TypeError, type_case, "bar", "5", 1)
|
||||
self.assertRaises(TypeError, type_case, None, 5, 1)
|
||||
|
||||
@validate.typetest(instanceid=str, size=int, number_of_instances=int)
|
||||
def type_case(instanceid, size, number_of_instances):
|
||||
print ("type_case was successfully executed")
|
||||
return True
|
||||
@@ -63,17 +63,17 @@ def typetest(**argchecks):
|
||||
|
||||
def onCall(*pargs, **kargs):
|
||||
positionals = list(allargs)[:len(pargs)]
|
||||
for (argname, type) in argchecks.items():
|
||||
if argname in kargs:
|
||||
if not isinstance(kargs[argname], type):
|
||||
for (argname, typeof) in argchecks.items():
|
||||
if argname in kargs:
|
||||
if not isinstance(kargs[argname], typeof):
|
||||
errmsg = '{0} argument "{1}" not of type {2}'
|
||||
errmsg = errmsg.format(funcname, argname, type)
|
||||
errmsg = errmsg.format(funcname, argname, typeof)
|
||||
raise TypeError(errmsg)
|
||||
elif argname in positionals:
|
||||
position = positionals.index(argname)
|
||||
if not isinstance(pargs[position], type):
|
||||
errmsg = '{0} argument "{1}" not of type {2}'
|
||||
errmsg = errmsg.format(funcname, argname, type)
|
||||
if not isinstance(pargs[position], typeof):
|
||||
errmsg = '{0} argument "{1}" with value of {2} not of type {3}'
|
||||
errmsg = errmsg.format(funcname, argname, pargs[position], typeof)
|
||||
raise TypeError(errmsg)
|
||||
else:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user