From 108f97f5d7854cfed2c006112a91873ac4f2ed1a Mon Sep 17 00:00:00 2001 From: Joshua McKenty Date: Thu, 24 Jun 2010 04:11:56 +0100 Subject: First pass at validation unit tests. Haven't figured out class methods yet. --- nova/tests/validator_unittest.py | 41 ++++++++++++++++++++++++++++++++++++++++ nova/validate.py | 14 +++++++------- 2 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 nova/tests/validator_unittest.py diff --git a/nova/tests/validator_unittest.py b/nova/tests/validator_unittest.py new file mode 100644 index 000000000..e605f86cb --- /dev/null +++ b/nova/tests/validator_unittest.py @@ -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 \ No newline at end of file diff --git a/nova/validate.py b/nova/validate.py index d1358d402..e96a47059 100644 --- a/nova/validate.py +++ b/nova/validate.py @@ -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 -- cgit