root / kamaki / cli / utils / test.py @ 0f383dcc
History | View | Annotate | Download (9.4 kB)
1 | b3cb58c2 | Stavros Sachtouris | # Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | b3cb58c2 | Stavros Sachtouris | #
|
3 | b3cb58c2 | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | b3cb58c2 | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | b3cb58c2 | Stavros Sachtouris | # conditions are met:
|
6 | b3cb58c2 | Stavros Sachtouris | #
|
7 | b3cb58c2 | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | b3cb58c2 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | b3cb58c2 | Stavros Sachtouris | # disclaimer.
|
10 | b3cb58c2 | Stavros Sachtouris | #
|
11 | b3cb58c2 | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | b3cb58c2 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | b3cb58c2 | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | b3cb58c2 | Stavros Sachtouris | # provided with the distribution.
|
15 | b3cb58c2 | Stavros Sachtouris | #
|
16 | b3cb58c2 | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | b3cb58c2 | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | b3cb58c2 | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | b3cb58c2 | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | b3cb58c2 | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | b3cb58c2 | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | b3cb58c2 | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | b3cb58c2 | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | b3cb58c2 | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | b3cb58c2 | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | b3cb58c2 | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | b3cb58c2 | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | b3cb58c2 | Stavros Sachtouris | #
|
29 | b3cb58c2 | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | b3cb58c2 | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | b3cb58c2 | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | b3cb58c2 | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | b3cb58c2 | Stavros Sachtouris | |
34 | b3cb58c2 | Stavros Sachtouris | from unittest import TestCase |
35 | b3cb58c2 | Stavros Sachtouris | #from tempfile import NamedTemporaryFile
|
36 | 0f383dcc | Stavros Sachtouris | from mock import patch, call |
37 | b3cb58c2 | Stavros Sachtouris | from itertools import product |
38 | b3cb58c2 | Stavros Sachtouris | |
39 | b3cb58c2 | Stavros Sachtouris | |
40 | b3cb58c2 | Stavros Sachtouris | class UtilsMethods(TestCase): |
41 | b3cb58c2 | Stavros Sachtouris | |
42 | b3cb58c2 | Stavros Sachtouris | def assert_dicts_are_equal(self, d1, d2): |
43 | b3cb58c2 | Stavros Sachtouris | for k, v in d1.items(): |
44 | b3cb58c2 | Stavros Sachtouris | self.assertTrue(k in d2) |
45 | b3cb58c2 | Stavros Sachtouris | if isinstance(v, dict): |
46 | b3cb58c2 | Stavros Sachtouris | self.assert_dicts_are_equal(v, d2[k])
|
47 | b3cb58c2 | Stavros Sachtouris | else:
|
48 | b3cb58c2 | Stavros Sachtouris | self.assertEqual(unicode(v), unicode(d2[k])) |
49 | b3cb58c2 | Stavros Sachtouris | |
50 | b3cb58c2 | Stavros Sachtouris | def test_guess_mime_type(self): |
51 | b3cb58c2 | Stavros Sachtouris | from kamaki.cli.utils import guess_mime_type |
52 | b3cb58c2 | Stavros Sachtouris | from mimetypes import guess_type |
53 | b3cb58c2 | Stavros Sachtouris | for args in product( |
54 | b3cb58c2 | Stavros Sachtouris | ('file.txt', 'file.png', 'file.zip', 'file.gz', None, 'X'), |
55 | b3cb58c2 | Stavros Sachtouris | ('a type', None), |
56 | b3cb58c2 | Stavros Sachtouris | ('an/encoding', None)): |
57 | b3cb58c2 | Stavros Sachtouris | filename, ctype, cencoding = args |
58 | b3cb58c2 | Stavros Sachtouris | if filename:
|
59 | b3cb58c2 | Stavros Sachtouris | exp_type, exp_enc = guess_type(filename) |
60 | b3cb58c2 | Stavros Sachtouris | self.assertEqual(
|
61 | b3cb58c2 | Stavros Sachtouris | guess_mime_type(*args), |
62 | b3cb58c2 | Stavros Sachtouris | (exp_type or ctype, exp_enc or cencoding)) |
63 | b3cb58c2 | Stavros Sachtouris | else:
|
64 | b3cb58c2 | Stavros Sachtouris | self.assertRaises(AssertionError, guess_mime_type, *args) |
65 | b3cb58c2 | Stavros Sachtouris | |
66 | 0f383dcc | Stavros Sachtouris | @patch('kamaki.cli.utils.dumps', return_value='(dumps output)') |
67 | 0f383dcc | Stavros Sachtouris | @patch('kamaki.cli.utils._print') |
68 | 0f383dcc | Stavros Sachtouris | def test_print_json(self, PR, JD): |
69 | 0f383dcc | Stavros Sachtouris | from kamaki.cli.utils import print_json, INDENT_TAB |
70 | 0f383dcc | Stavros Sachtouris | print_json('some data')
|
71 | 0f383dcc | Stavros Sachtouris | JD.assert_called_once_with('some data', indent=INDENT_TAB)
|
72 | 0f383dcc | Stavros Sachtouris | PR.assert_called_once_with('(dumps output)')
|
73 | 0f383dcc | Stavros Sachtouris | |
74 | 0f383dcc | Stavros Sachtouris | @patch('kamaki.cli.utils._print') |
75 | 0f383dcc | Stavros Sachtouris | def test_print_dict(self, PR): |
76 | 0f383dcc | Stavros Sachtouris | from kamaki.cli.utils import print_dict, INDENT_TAB |
77 | 0f383dcc | Stavros Sachtouris | call_counter = 0
|
78 | 0f383dcc | Stavros Sachtouris | self.assertRaises(AssertionError, print_dict, 'non-dict think') |
79 | 0f383dcc | Stavros Sachtouris | self.assertRaises(AssertionError, print_dict, {}, indent=-10) |
80 | 0f383dcc | Stavros Sachtouris | for args in product( |
81 | b3cb58c2 | Stavros Sachtouris | ( |
82 | 0f383dcc | Stavros Sachtouris | {'k1': 'v1'}, |
83 | 0f383dcc | Stavros Sachtouris | {'k1': 'v1', 'k2': 'v2'}, |
84 | 0f383dcc | Stavros Sachtouris | {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}, |
85 | 0f383dcc | Stavros Sachtouris | {'k1': 'v1', 'k2': {'k1': 'v1', 'k2': 'v2'}, 'k3': 'v3'}, |
86 | b3cb58c2 | Stavros Sachtouris | { |
87 | 0f383dcc | Stavros Sachtouris | 'k1': {'k1': 'v1', 'k2': 'v2'}, |
88 | 0f383dcc | Stavros Sachtouris | 'k2': [1, 2, 3], |
89 | 0f383dcc | Stavros Sachtouris | 'k3': 'v3'}, |
90 | 0f383dcc | Stavros Sachtouris | { |
91 | 0f383dcc | Stavros Sachtouris | 'k1': {'k1': 'v1', 'k2': 'v2'}, |
92 | 0f383dcc | Stavros Sachtouris | 'k2': 42, |
93 | 0f383dcc | Stavros Sachtouris | 'k3': {'k1': 1, 'k2': [1, 2, 3]}}, |
94 | b3cb58c2 | Stavros Sachtouris | { |
95 | 0f383dcc | Stavros Sachtouris | 'k1': {
|
96 | 0f383dcc | Stavros Sachtouris | 'k1': 'v1', |
97 | 0f383dcc | Stavros Sachtouris | 'k2': [1, 2, 3], |
98 | 0f383dcc | Stavros Sachtouris | 'k3': {'k1': [(1, 2)]}}, |
99 | 0f383dcc | Stavros Sachtouris | 'k2': (3, 4, 5), |
100 | 0f383dcc | Stavros Sachtouris | 'k3': {'k1': 1, 'k2': [1, 2, 3]}}), |
101 | 0f383dcc | Stavros Sachtouris | (tuple(), ('k1', ), ('k1', 'k2')), |
102 | 0f383dcc | Stavros Sachtouris | (0, 1, 2, 9), (False, True), (False, True)): |
103 | 0f383dcc | Stavros Sachtouris | d, exclude, indent, with_enumeration, recursive_enumeration = args |
104 | 0f383dcc | Stavros Sachtouris | with patch('kamaki.cli.utils.print_dict') as PD: |
105 | 0f383dcc | Stavros Sachtouris | with patch('kamaki.cli.utils.print_list') as PL: |
106 | 0f383dcc | Stavros Sachtouris | pd_calls, pl_calls = 0, 0 |
107 | 0f383dcc | Stavros Sachtouris | print_dict(*args) |
108 | 0f383dcc | Stavros Sachtouris | exp_calls = [] |
109 | 0f383dcc | Stavros Sachtouris | for i, (k, v) in enumerate(d.items()): |
110 | 0f383dcc | Stavros Sachtouris | if k in exclude: |
111 | 0f383dcc | Stavros Sachtouris | continue
|
112 | 0f383dcc | Stavros Sachtouris | str_k = ' ' * indent
|
113 | 0f383dcc | Stavros Sachtouris | str_k += '%s.' % (i + 1) if with_enumeration else '' |
114 | 0f383dcc | Stavros Sachtouris | str_k += '%s:' % k
|
115 | 0f383dcc | Stavros Sachtouris | if isinstance(v, dict): |
116 | 0f383dcc | Stavros Sachtouris | self.assertEqual(
|
117 | 0f383dcc | Stavros Sachtouris | PD.mock_calls[pd_calls], |
118 | 0f383dcc | Stavros Sachtouris | call( |
119 | 0f383dcc | Stavros Sachtouris | v, |
120 | 0f383dcc | Stavros Sachtouris | exclude, |
121 | 0f383dcc | Stavros Sachtouris | indent + INDENT_TAB, |
122 | 0f383dcc | Stavros Sachtouris | recursive_enumeration, |
123 | 0f383dcc | Stavros Sachtouris | recursive_enumeration)) |
124 | 0f383dcc | Stavros Sachtouris | pd_calls += 1
|
125 | 0f383dcc | Stavros Sachtouris | exp_calls.append(call(str_k)) |
126 | 0f383dcc | Stavros Sachtouris | elif isinstance(v, list) or isinstance(v, tuple): |
127 | 0f383dcc | Stavros Sachtouris | self.assertEqual(
|
128 | 0f383dcc | Stavros Sachtouris | PL.mock_calls[pl_calls], |
129 | 0f383dcc | Stavros Sachtouris | call( |
130 | 0f383dcc | Stavros Sachtouris | v, |
131 | 0f383dcc | Stavros Sachtouris | exclude, |
132 | 0f383dcc | Stavros Sachtouris | indent + INDENT_TAB, |
133 | 0f383dcc | Stavros Sachtouris | recursive_enumeration, |
134 | 0f383dcc | Stavros Sachtouris | recursive_enumeration)) |
135 | 0f383dcc | Stavros Sachtouris | pl_calls += 1
|
136 | 0f383dcc | Stavros Sachtouris | exp_calls.append(call(str_k)) |
137 | 0f383dcc | Stavros Sachtouris | else:
|
138 | 0f383dcc | Stavros Sachtouris | exp_calls.append(call('%s %s' % (str_k, v)))
|
139 | 0f383dcc | Stavros Sachtouris | real_calls = PR.mock_calls[call_counter:] |
140 | 0f383dcc | Stavros Sachtouris | call_counter = len(PR.mock_calls)
|
141 | 0f383dcc | Stavros Sachtouris | self.assertEqual(sorted(real_calls), sorted(exp_calls)) |
142 | 0f383dcc | Stavros Sachtouris | |
143 | 0f383dcc | Stavros Sachtouris | @patch('kamaki.cli.utils._print') |
144 | 0f383dcc | Stavros Sachtouris | def test_print_list(self, PR): |
145 | 0f383dcc | Stavros Sachtouris | from kamaki.cli.utils import print_list, INDENT_TAB |
146 | 0f383dcc | Stavros Sachtouris | call_counter = 0
|
147 | 0f383dcc | Stavros Sachtouris | self.assertRaises(AssertionError, print_list, 'non-list non-tuple') |
148 | 0f383dcc | Stavros Sachtouris | self.assertRaises(AssertionError, print_list, {}, indent=-10) |
149 | 0f383dcc | Stavros Sachtouris | for args in product( |
150 | 0f383dcc | Stavros Sachtouris | ( |
151 | 0f383dcc | Stavros Sachtouris | ['v1', ],
|
152 | 0f383dcc | Stavros Sachtouris | ('v2', 'v3'), |
153 | 0f383dcc | Stavros Sachtouris | [1, '2', 'v3'], |
154 | 0f383dcc | Stavros Sachtouris | ({'k1': 'v1'}, 2, 'v3'), |
155 | 0f383dcc | Stavros Sachtouris | [(1, 2), 'v2', [(3, 4), {'k3': [5, 6], 'k4': 7}]]), |
156 | 0f383dcc | Stavros Sachtouris | (tuple(), ('v1', ), ('v1', 1), ('v1', 'k3')), |
157 | 0f383dcc | Stavros Sachtouris | (0, 1, 2, 9), (False, True), (False, True)): |
158 | 0f383dcc | Stavros Sachtouris | l, exclude, indent, with_enumeration, recursive_enumeration = args |
159 | 0f383dcc | Stavros Sachtouris | with patch('kamaki.cli.utils.print_dict') as PD: |
160 | 0f383dcc | Stavros Sachtouris | with patch('kamaki.cli.utils.print_list') as PL: |
161 | 0f383dcc | Stavros Sachtouris | pd_calls, pl_calls = 0, 0 |
162 | 0f383dcc | Stavros Sachtouris | print_list(*args) |
163 | 0f383dcc | Stavros Sachtouris | exp_calls = [] |
164 | 0f383dcc | Stavros Sachtouris | for i, v in enumerate(l): |
165 | 0f383dcc | Stavros Sachtouris | str_v = ' ' * indent
|
166 | 0f383dcc | Stavros Sachtouris | str_v += '%s.' % (i + 1) if with_enumeration else '' |
167 | 0f383dcc | Stavros Sachtouris | if isinstance(v, dict): |
168 | 0f383dcc | Stavros Sachtouris | if with_enumeration:
|
169 | 0f383dcc | Stavros Sachtouris | exp_calls.append(call(str_v)) |
170 | 0f383dcc | Stavros Sachtouris | elif i and i < len(l): |
171 | 0f383dcc | Stavros Sachtouris | exp_calls.append(call()) |
172 | 0f383dcc | Stavros Sachtouris | self.assertEqual(
|
173 | 0f383dcc | Stavros Sachtouris | PD.mock_calls[pd_calls], |
174 | 0f383dcc | Stavros Sachtouris | call( |
175 | 0f383dcc | Stavros Sachtouris | v, |
176 | 0f383dcc | Stavros Sachtouris | exclude, |
177 | 0f383dcc | Stavros Sachtouris | indent + ( |
178 | 0f383dcc | Stavros Sachtouris | INDENT_TAB if with_enumeration else 0), |
179 | 0f383dcc | Stavros Sachtouris | recursive_enumeration, |
180 | 0f383dcc | Stavros Sachtouris | recursive_enumeration)) |
181 | 0f383dcc | Stavros Sachtouris | pd_calls += 1
|
182 | 0f383dcc | Stavros Sachtouris | elif isinstance(v, list) or isinstance(v, tuple): |
183 | 0f383dcc | Stavros Sachtouris | if with_enumeration:
|
184 | 0f383dcc | Stavros Sachtouris | exp_calls.append(call(str_v)) |
185 | 0f383dcc | Stavros Sachtouris | elif i and i < len(l): |
186 | 0f383dcc | Stavros Sachtouris | exp_calls.append(call()) |
187 | 0f383dcc | Stavros Sachtouris | self.assertEqual(
|
188 | 0f383dcc | Stavros Sachtouris | PL.mock_calls[pl_calls], |
189 | 0f383dcc | Stavros Sachtouris | call( |
190 | 0f383dcc | Stavros Sachtouris | v, |
191 | 0f383dcc | Stavros Sachtouris | exclude, |
192 | 0f383dcc | Stavros Sachtouris | indent + INDENT_TAB, |
193 | 0f383dcc | Stavros Sachtouris | recursive_enumeration, |
194 | 0f383dcc | Stavros Sachtouris | recursive_enumeration)) |
195 | 0f383dcc | Stavros Sachtouris | pl_calls += 1
|
196 | 0f383dcc | Stavros Sachtouris | elif ('%s' % v) in exclude: |
197 | 0f383dcc | Stavros Sachtouris | continue
|
198 | 0f383dcc | Stavros Sachtouris | else:
|
199 | 0f383dcc | Stavros Sachtouris | exp_calls.append(call('%s%s' % (str_v, v)))
|
200 | 0f383dcc | Stavros Sachtouris | real_calls = PR.mock_calls[call_counter:] |
201 | 0f383dcc | Stavros Sachtouris | call_counter = len(PR.mock_calls)
|
202 | 0f383dcc | Stavros Sachtouris | self.assertEqual(sorted(real_calls), sorted(exp_calls)) |
203 | b3cb58c2 | Stavros Sachtouris | |
204 | b3cb58c2 | Stavros Sachtouris | |
205 | b3cb58c2 | Stavros Sachtouris | if __name__ == '__main__': |
206 | b3cb58c2 | Stavros Sachtouris | from sys import argv |
207 | b3cb58c2 | Stavros Sachtouris | from kamaki.cli.test import runTestCase |
208 | b3cb58c2 | Stavros Sachtouris | runTestCase(UtilsMethods, 'UtilsMethods', argv[1:]) |