1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 Justin Santa Barbara
#
# 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.
from nova import safe_utils
from nova import test
class GetCallArgsTestCase(test.NoDBTestCase):
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 = safe_utils.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 = safe_utils.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 = safe_utils.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 = safe_utils.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 = safe_utils.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 = safe_utils.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'])
|