summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2013-05-31 12:35:53 -0700
committerJoshua Harlow <harlowja@gmail.com>2013-06-09 00:56:17 -0700
commitd7970343cfd880817ac7985e328c9a8fe5cf6b73 (patch)
treeea95a48af1b89fa74ae967fa2146f4b12a99b9d0 /tests
parent106376d59f9a25fef94baf413316b22c7d4218d2 (diff)
downloadoslo-d7970343cfd880817ac7985e328c9a8fe5cf6b73.tar.gz
oslo-d7970343cfd880817ac7985e328c9a8fe5cf6b73.tar.xz
oslo-d7970343cfd880817ac7985e328c9a8fe5cf6b73.zip
Add a funcutils file for working with functions.
Multiple projects are starting to copy around the various function utilities that exist in nova and elsewhere. It would seem appropriate that there exist a helper file in oslo that was used instead as a central place for this type of code. Change-Id: Ia83f26da16b0c868506ecf90e1aaf8affecf3617
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/test_funcutils.py139
1 files changed, 139 insertions, 0 deletions
diff --git a/tests/unit/test_funcutils.py b/tests/unit/test_funcutils.py
new file mode 100644
index 0000000..439d825
--- /dev/null
+++ b/tests/unit/test_funcutils.py
@@ -0,0 +1,139 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2011 OpenStack Foundation.
+# Copyright 2011 Justin Santa Barbara
+#
+# All Rights Reserved.
+#
+# 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 functools
+
+from openstack.common import funcutils
+
+from tests import utils
+
+
+class FuncutilsTestCase(utils.BaseTestCase):
+ def _test_func(self, instance, red=None, blue=None):
+ pass
+
+ def test_all_kwargs(self):
+ args = ()
+ kwargs = {'instance': {'uuid': 1}, 'red': 3, 'blue': 4}
+ callargs = funcutils.getcallargs(self._test_func, *args, **kwargs)
+
+ #implicit self counts as an arg
+ self.assertEqual(4, len(callargs))
+ self.assertTrue('instance' in callargs)
+ self.assertEqual({'uuid': 1}, callargs['instance'])
+ self.assertTrue('red' in callargs)
+ self.assertEqual(3, callargs['red'])
+ self.assertTrue('blue' in callargs)
+ self.assertEqual(4, callargs['blue'])
+
+ def test_all_args(self):
+ args = ({'uuid': 1}, 3, 4)
+ kwargs = {}
+ callargs = funcutils.getcallargs(self._test_func, *args, **kwargs)
+
+ #implicit self counts as an arg
+ self.assertEqual(4, len(callargs))
+ self.assertTrue('instance' in callargs)
+ self.assertEqual({'uuid': 1}, callargs['instance'])
+ self.assertTrue('red' in callargs)
+ self.assertEqual(3, callargs['red'])
+ self.assertTrue('blue' in callargs)
+ self.assertEqual(4, callargs['blue'])
+
+ def test_mixed_args(self):
+ args = ({'uuid': 1}, 3)
+ kwargs = {'blue': 4}
+ callargs = funcutils.getcallargs(self._test_func, *args, **kwargs)
+
+ #implicit self counts as an arg
+ self.assertEqual(4, len(callargs))
+ self.assertTrue('instance' in callargs)
+ self.assertEqual({'uuid': 1}, callargs['instance'])
+ self.assertTrue('red' in callargs)
+ self.assertEqual(3, callargs['red'])
+ self.assertTrue('blue' in callargs)
+ self.assertEqual(4, callargs['blue'])
+
+ def test_partial_kwargs(self):
+ args = ()
+ kwargs = {'instance': {'uuid': 1}, 'red': 3}
+ callargs = funcutils.getcallargs(self._test_func, *args, **kwargs)
+
+ #implicit self counts as an arg
+ self.assertEqual(4, len(callargs))
+ self.assertTrue('instance' in callargs)
+ self.assertEqual({'uuid': 1}, callargs['instance'])
+ self.assertTrue('red' in callargs)
+ self.assertEqual(3, callargs['red'])
+ self.assertTrue('blue' in callargs)
+ self.assertEqual(None, callargs['blue'])
+
+ def test_partial_args(self):
+ args = ({'uuid': 1}, 3)
+ kwargs = {}
+ callargs = funcutils.getcallargs(self._test_func, *args, **kwargs)
+
+ #implicit self counts as an arg
+ self.assertEqual(4, len(callargs))
+ self.assertTrue('instance' in callargs)
+ self.assertEqual({'uuid': 1}, callargs['instance'])
+ self.assertTrue('red' in callargs)
+ self.assertEqual(3, callargs['red'])
+ self.assertTrue('blue' in callargs)
+ self.assertEqual(None, callargs['blue'])
+
+ def test_partial_mixed_args(self):
+ args = (3,)
+ kwargs = {'instance': {'uuid': 1}}
+ callargs = funcutils.getcallargs(self._test_func, *args, **kwargs)
+
+ self.assertEqual(4, len(callargs))
+ self.assertTrue('instance' in callargs)
+ self.assertEqual({'uuid': 1}, callargs['instance'])
+ self.assertTrue('red' in callargs)
+ self.assertEqual(3, callargs['red'])
+ self.assertTrue('blue' in callargs)
+ self.assertEqual(None, callargs['blue'])
+
+ def _wrapper(self, function):
+
+ @functools.wraps(function)
+ def decorated_function(self, *args, **kwargs):
+ function(self, *args, **kwargs)
+
+ return decorated_function
+
+ def test_wrapped_X(self):
+
+ def wrapped(self, instance, red=None, blue=None):
+ pass
+
+ old_wrapped = wrapped
+
+ # Wrap it many times and ensure that its still the right one.
+ for _i in range(0, 10):
+ wrapped = self._wrapper(wrapped)
+ func = funcutils.get_wrapped_function(wrapped)
+ func_code = func.func_code
+ self.assertEqual(4, len(func_code.co_varnames))
+ self.assertTrue('self' in func_code.co_varnames)
+ self.assertTrue('instance' in func_code.co_varnames)
+ self.assertTrue('red' in func_code.co_varnames)
+ self.assertTrue('blue' in func_code.co_varnames)
+ self.assertEqual(old_wrapped, func)