root / kamaki / cli / utils / test.py @ 1716a15d
History | View | Annotate | Download (22.7 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 | 23963422 | 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 | 46d130c9 | Stavros Sachtouris | @patch('__builtin__.raw_input') |
205 | 46d130c9 | Stavros Sachtouris | def test_page_hold(self, RI): |
206 | 46d130c9 | Stavros Sachtouris | from kamaki.cli.utils import page_hold |
207 | 46d130c9 | Stavros Sachtouris | ri_counter = 0
|
208 | 46d130c9 | Stavros Sachtouris | for args, expected in ( |
209 | 46d130c9 | Stavros Sachtouris | ((0, 0, 0), False), |
210 | 46d130c9 | Stavros Sachtouris | ((1, 3, 10), True), |
211 | 46d130c9 | Stavros Sachtouris | ((3, 3, 10), True), |
212 | 46d130c9 | Stavros Sachtouris | ((5, 3, 10), True), |
213 | 46d130c9 | Stavros Sachtouris | ((6, 3, 10), True), |
214 | 46d130c9 | Stavros Sachtouris | ((10, 3, 10), False), |
215 | 46d130c9 | Stavros Sachtouris | ((11, 3, 10), False)): |
216 | 46d130c9 | Stavros Sachtouris | self.assertEqual(page_hold(*args), expected)
|
217 | 46d130c9 | Stavros Sachtouris | index, limit, maxlen = args |
218 | 46d130c9 | Stavros Sachtouris | if index and index < maxlen and index % limit == 0: |
219 | 46d130c9 | Stavros Sachtouris | self.assertEqual(ri_counter + 1, len(RI.mock_calls)) |
220 | 46d130c9 | Stavros Sachtouris | self.assertEqual(RI.mock_calls[-1], call( |
221 | 46d130c9 | Stavros Sachtouris | '(%s listed - %s more - "enter" to continue)' % (
|
222 | 46d130c9 | Stavros Sachtouris | index, maxlen - index))) |
223 | 46d130c9 | Stavros Sachtouris | else:
|
224 | 46d130c9 | Stavros Sachtouris | self.assertEqual(ri_counter, len(RI.mock_calls)) |
225 | 46d130c9 | Stavros Sachtouris | ri_counter = len(RI.mock_calls)
|
226 | 46d130c9 | Stavros Sachtouris | |
227 | 46d130c9 | Stavros Sachtouris | @patch('kamaki.cli.utils._print') |
228 | 46d130c9 | Stavros Sachtouris | @patch('kamaki.cli.utils._write') |
229 | 46d130c9 | Stavros Sachtouris | @patch('kamaki.cli.utils.print_dict') |
230 | 46d130c9 | Stavros Sachtouris | @patch('kamaki.cli.utils.print_list') |
231 | 46d130c9 | Stavros Sachtouris | @patch('kamaki.cli.utils.page_hold') |
232 | 46d130c9 | Stavros Sachtouris | @patch('kamaki.cli.utils.bold', return_value='bold') |
233 | 46d130c9 | Stavros Sachtouris | def test_print_items(self, bold, PH, PL, PD, WR, PR): |
234 | 46d130c9 | Stavros Sachtouris | from kamaki.cli.utils import print_items, INDENT_TAB |
235 | 46d130c9 | Stavros Sachtouris | for args in product( |
236 | 46d130c9 | Stavros Sachtouris | ( |
237 | 46d130c9 | Stavros Sachtouris | 42, None, 'simple outputs', |
238 | 46d130c9 | Stavros Sachtouris | [1, 2, 3], {1: 1, 2: 2}, (3, 4), |
239 | 46d130c9 | Stavros Sachtouris | ({'k': 1, 'id': 2}, [5, 6, 7], (8, 9), '10')), |
240 | 46d130c9 | Stavros Sachtouris | (('id', 'name'), ('something', 2), ('lala', )), |
241 | 46d130c9 | Stavros Sachtouris | (False, True), |
242 | 46d130c9 | Stavros Sachtouris | (False, True), |
243 | 46d130c9 | Stavros Sachtouris | (0, 1, 2, 10)): |
244 | 46d130c9 | Stavros Sachtouris | items, title, with_enumeration, with_redundancy, page_size = args |
245 | 46d130c9 | Stavros Sachtouris | wr_counter, pr_counter = len(WR.mock_calls), len(PR.mock_calls) |
246 | 46d130c9 | Stavros Sachtouris | pl_counter, pd_counter = len(PL.mock_calls), len(PD.mock_calls) |
247 | 46d130c9 | Stavros Sachtouris | bold_counter, ph_counter = len(bold.mock_calls), len(PH.mock_calls) |
248 | 46d130c9 | Stavros Sachtouris | print_items(*args) |
249 | 46d130c9 | Stavros Sachtouris | if not (isinstance(items, dict) or isinstance( |
250 | 46d130c9 | Stavros Sachtouris | items, list) or isinstance(items, tuple)): |
251 | 2d1202ee | Stavros Sachtouris | if items:
|
252 | 2d1202ee | Stavros Sachtouris | self.assertEqual(PR.mock_calls[-1], call('%s' % items)) |
253 | 46d130c9 | Stavros Sachtouris | else:
|
254 | 46d130c9 | Stavros Sachtouris | for i, item in enumerate(items): |
255 | 46d130c9 | Stavros Sachtouris | if with_enumeration:
|
256 | 46d130c9 | Stavros Sachtouris | self.assertEqual(
|
257 | 46d130c9 | Stavros Sachtouris | WR.mock_calls[wr_counter], |
258 | 46d130c9 | Stavros Sachtouris | call('%s. ' % (i + 1))) |
259 | 46d130c9 | Stavros Sachtouris | wr_counter += 1
|
260 | 46d130c9 | Stavros Sachtouris | if isinstance(item, dict): |
261 | 46d130c9 | Stavros Sachtouris | title = sorted(set(title).intersection(item)) |
262 | 46d130c9 | Stavros Sachtouris | pick = item.get if with_redundancy else item.pop |
263 | 46d130c9 | Stavros Sachtouris | header = ' '.join('%s' % pick(key) for key in title) |
264 | 46d130c9 | Stavros Sachtouris | self.assertEqual(
|
265 | 46d130c9 | Stavros Sachtouris | bold.mock_calls[bold_counter], call(header)) |
266 | 46d130c9 | Stavros Sachtouris | self.assertEqual(
|
267 | 46d130c9 | Stavros Sachtouris | PR.mock_calls[pr_counter], call('bold'))
|
268 | 46d130c9 | Stavros Sachtouris | self.assertEqual(
|
269 | 46d130c9 | Stavros Sachtouris | PD.mock_calls[pd_counter], |
270 | 46d130c9 | Stavros Sachtouris | call(item, indent=INDENT_TAB)) |
271 | 46d130c9 | Stavros Sachtouris | pr_counter += 1
|
272 | 46d130c9 | Stavros Sachtouris | pd_counter += 1
|
273 | 46d130c9 | Stavros Sachtouris | bold_counter += 1
|
274 | 46d130c9 | Stavros Sachtouris | elif isinstance(item, list) or isinstance(item, tuple): |
275 | 46d130c9 | Stavros Sachtouris | self.assertEqual(
|
276 | 46d130c9 | Stavros Sachtouris | PL.mock_calls[pl_counter], |
277 | 46d130c9 | Stavros Sachtouris | call(item, indent=INDENT_TAB)) |
278 | 46d130c9 | Stavros Sachtouris | pl_counter += 1
|
279 | 46d130c9 | Stavros Sachtouris | else:
|
280 | 46d130c9 | Stavros Sachtouris | self.assertEqual(
|
281 | 46d130c9 | Stavros Sachtouris | PR.mock_calls[pr_counter], call(' %s' % item))
|
282 | 46d130c9 | Stavros Sachtouris | pr_counter += 1
|
283 | 46d130c9 | Stavros Sachtouris | page_size = page_size if page_size > 0 else len(items) |
284 | 46d130c9 | Stavros Sachtouris | self.assertEqual(
|
285 | 46d130c9 | Stavros Sachtouris | PH.mock_calls[ph_counter], |
286 | 46d130c9 | Stavros Sachtouris | call(i + 1, page_size, len(items))) |
287 | 46d130c9 | Stavros Sachtouris | ph_counter += 1
|
288 | 46d130c9 | Stavros Sachtouris | |
289 | 23963422 | Stavros Sachtouris | def test_format_size(self): |
290 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import format_size |
291 | 23963422 | Stavros Sachtouris | from kamaki.cli import CLIError |
292 | 23963422 | Stavros Sachtouris | for v in ('wrong', {1: '1', 2: '2'}, ('tuples', 'not OK'), [1, 2]): |
293 | 23963422 | Stavros Sachtouris | self.assertRaises(CLIError, format_size, v)
|
294 | 23963422 | Stavros Sachtouris | for step, B, K, M, G, T in ( |
295 | 23963422 | Stavros Sachtouris | (1000, 'B', 'KB', 'MB', 'GB', 'TB'), |
296 | 23963422 | Stavros Sachtouris | (1024, 'B', 'KiB', 'MiB', 'GiB', 'TiB')): |
297 | 23963422 | Stavros Sachtouris | Ki, Mi, Gi = step, step * step, step * step * step |
298 | 23963422 | Stavros Sachtouris | for before, after in ( |
299 | 23963422 | Stavros Sachtouris | (0, '0' + B), (512, '512' + B), ( |
300 | 23963422 | Stavros Sachtouris | Ki - 1, '%s%s' % (step - 1, B)), |
301 | 23963422 | Stavros Sachtouris | (Ki, '1' + K), (42 * Ki, '42' + K), ( |
302 | 23963422 | Stavros Sachtouris | Mi - 1, '%s.99%s' % (step - 1, K)), |
303 | 23963422 | Stavros Sachtouris | (Mi, '1' + M), (42 * Mi, '42' + M), ( |
304 | 23963422 | Stavros Sachtouris | Ki * Mi - 1, '%s.99%s' % (step - 1, M)), |
305 | 23963422 | Stavros Sachtouris | (Gi, '1' + G), (42 * Gi, '42' + G), ( |
306 | 23963422 | Stavros Sachtouris | Mi * Mi - 1, '%s.99%s' % (step - 1, G)), |
307 | 23963422 | Stavros Sachtouris | (Mi * Mi, '1' + T), (42 * Mi * Mi, '42' + T), ( |
308 | 23963422 | Stavros Sachtouris | Mi * Gi - 1, '%s.99%s' % (step - 1, T)), ( |
309 | 23963422 | Stavros Sachtouris | 42 * Mi * Gi, '%s%s' % (42 * Ki, T))): |
310 | 23963422 | Stavros Sachtouris | self.assertEqual(format_size(before, step == 1000), after) |
311 | 23963422 | Stavros Sachtouris | |
312 | 23963422 | Stavros Sachtouris | def test_to_bytes(self): |
313 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import to_bytes |
314 | 23963422 | Stavros Sachtouris | for v in ('wrong', 'KABUM', 'kbps', 'kibps'): |
315 | 23963422 | Stavros Sachtouris | self.assertRaises(ValueError, to_bytes, v, 'B') |
316 | 23963422 | Stavros Sachtouris | self.assertRaises(ValueError, to_bytes, 42, v) |
317 | 23963422 | Stavros Sachtouris | for v in ([1, 2, 3], ('kb', 'mb'), {'kb': 1, 'byte': 2}): |
318 | 23963422 | Stavros Sachtouris | self.assertRaises(TypeError, to_bytes, v, 'B') |
319 | 23963422 | Stavros Sachtouris | self.assertRaises(AttributeError, to_bytes, 42, v) |
320 | 23963422 | Stavros Sachtouris | kl, ki = 1000, 1024 |
321 | 23963422 | Stavros Sachtouris | for size, (unit, factor) in product( |
322 | 23963422 | Stavros Sachtouris | (0, 42, 3.14, 1023, 10000), |
323 | 23963422 | Stavros Sachtouris | ( |
324 | 23963422 | Stavros Sachtouris | ('B', 1), ('b', 1), |
325 | 23963422 | Stavros Sachtouris | ('KB', kl), ('KiB', ki), |
326 | 23963422 | Stavros Sachtouris | ('mb', kl * kl), ('mIb', ki * ki), |
327 | 23963422 | Stavros Sachtouris | ('gB', kl * kl * kl), ('GIB', ki * ki * ki), |
328 | 23963422 | Stavros Sachtouris | ('TB', kl * kl * kl * kl), ('tiB', ki * ki * ki * ki))): |
329 | 23963422 | Stavros Sachtouris | self.assertEqual(to_bytes(size, unit), int(size * factor)) |
330 | 23963422 | Stavros Sachtouris | |
331 | 23963422 | Stavros Sachtouris | def test_dict2file(self): |
332 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import dict2file, INDENT_TAB |
333 | 23963422 | Stavros Sachtouris | for d, depth in product(( |
334 | 23963422 | Stavros Sachtouris | {'k': 42}, |
335 | 23963422 | Stavros Sachtouris | {'k1': 'v1', 'k2': [1, 2, 3], 'k3': {'k': 'v'}}, |
336 | 23963422 | Stavros Sachtouris | {'k1': {
|
337 | 23963422 | Stavros Sachtouris | 'k1.1': 'v1.1', |
338 | 23963422 | Stavros Sachtouris | 'k1.2': [1, 2, 3], |
339 | 23963422 | Stavros Sachtouris | 'k1.3': {'k': 'v'}}}), |
340 | 23963422 | Stavros Sachtouris | (-42, 0, 42)): |
341 | 23963422 | Stavros Sachtouris | exp = ''
|
342 | 23963422 | Stavros Sachtouris | exp_d = [] |
343 | 23963422 | Stavros Sachtouris | exp_l = [] |
344 | 23963422 | Stavros Sachtouris | exp, exp_d, exp_l = '', [], []
|
345 | 23963422 | Stavros Sachtouris | with NamedTemporaryFile() as f: |
346 | 23963422 | Stavros Sachtouris | for k, v in d.items(): |
347 | 23963422 | Stavros Sachtouris | sfx = '\n'
|
348 | 23963422 | Stavros Sachtouris | if isinstance(v, dict): |
349 | 23963422 | Stavros Sachtouris | exp_d.append(call(v, f, depth + 1))
|
350 | 23963422 | Stavros Sachtouris | elif isinstance(v, tuple) or isinstance(v, list): |
351 | 23963422 | Stavros Sachtouris | exp_l.append(call(v, f, depth + 1))
|
352 | 23963422 | Stavros Sachtouris | else:
|
353 | 23963422 | Stavros Sachtouris | sfx = '%s\n' % v
|
354 | 23963422 | Stavros Sachtouris | exp += '%s%s: %s' % (
|
355 | 23963422 | Stavros Sachtouris | ' ' * (depth * INDENT_TAB), k, sfx)
|
356 | 23963422 | Stavros Sachtouris | with patch('kamaki.cli.utils.dict2file') as D2F: |
357 | 23963422 | Stavros Sachtouris | with patch('kamaki.cli.utils.list2file') as L2F: |
358 | 23963422 | Stavros Sachtouris | dict2file(d, f, depth) |
359 | 23963422 | Stavros Sachtouris | f.seek(0)
|
360 | 23963422 | Stavros Sachtouris | self.assertEqual(f.read(), exp)
|
361 | 23963422 | Stavros Sachtouris | self.assertEqual(L2F.mock_calls, exp_l)
|
362 | 23963422 | Stavros Sachtouris | self.assertEqual(D2F.mock_calls, exp_d)
|
363 | 23963422 | Stavros Sachtouris | |
364 | 23963422 | Stavros Sachtouris | def test_list2file(self): |
365 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import list2file, INDENT_TAB |
366 | 23963422 | Stavros Sachtouris | for l, depth in product( |
367 | 23963422 | Stavros Sachtouris | ( |
368 | 23963422 | Stavros Sachtouris | (1, 2, 3), |
369 | 23963422 | Stavros Sachtouris | [1, 2, 3], |
370 | 23963422 | Stavros Sachtouris | ('v', [1, 2, 3], (1, 2, 3), {'1': 1, 2: '2', 3: 3}), |
371 | 23963422 | Stavros Sachtouris | ['v', {'k1': 'v1', 'k2': [1, 2, 3], 'k3': {1: '1'}}]), |
372 | 23963422 | Stavros Sachtouris | (-42, 0, 42)): |
373 | 23963422 | Stavros Sachtouris | with NamedTemporaryFile() as f: |
374 | 23963422 | Stavros Sachtouris | exp, exp_d, exp_l = '', [], []
|
375 | 23963422 | Stavros Sachtouris | for v in l: |
376 | 23963422 | Stavros Sachtouris | if isinstance(v, dict): |
377 | 23963422 | Stavros Sachtouris | exp_d.append(call(v, f, depth + 1))
|
378 | 23963422 | Stavros Sachtouris | elif isinstance(v, list) or isinstance(v, tuple): |
379 | 23963422 | Stavros Sachtouris | exp_l.append(call(v, f, depth + 1))
|
380 | 23963422 | Stavros Sachtouris | else:
|
381 | 23963422 | Stavros Sachtouris | exp += '%s%s\n' % (' ' * INDENT_TAB * depth, v) |
382 | 23963422 | Stavros Sachtouris | with patch('kamaki.cli.utils.dict2file') as D2F: |
383 | 23963422 | Stavros Sachtouris | with patch('kamaki.cli.utils.list2file') as L2F: |
384 | 23963422 | Stavros Sachtouris | list2file(l, f, depth) |
385 | 23963422 | Stavros Sachtouris | f.seek(0)
|
386 | 23963422 | Stavros Sachtouris | self.assertEqual(f.read(), exp)
|
387 | 23963422 | Stavros Sachtouris | self.assertEqual(L2F.mock_calls, exp_l)
|
388 | 23963422 | Stavros Sachtouris | self.assertEqual(D2F.mock_calls, exp_d)
|
389 | 23963422 | Stavros Sachtouris | |
390 | 23963422 | Stavros Sachtouris | def test__parse_with_regex(self): |
391 | 23963422 | Stavros Sachtouris | from re import compile as r_compile |
392 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import _parse_with_regex |
393 | 23963422 | Stavros Sachtouris | for args in product( |
394 | 23963422 | Stavros Sachtouris | ( |
395 | 23963422 | Stavros Sachtouris | 'this is a line',
|
396 | 23963422 | Stavros Sachtouris | 'this_is_also_a_line',
|
397 | 23963422 | Stavros Sachtouris | 'This "text" is quoted',
|
398 | 23963422 | Stavros Sachtouris | 'This "quoted" "text" is more "complicated"',
|
399 | 23963422 | Stavros Sachtouris | 'Is this \'quoted\' text "double \'quoted\' or not?"',
|
400 | 23963422 | Stavros Sachtouris | '"What \'about\' the" oposite?',
|
401 | 23963422 | Stavros Sachtouris | ' Try with a " single double quote',
|
402 | 23963422 | Stavros Sachtouris | 'Go "down \'deep " deeper \'bottom \' up" go\' up" !'),
|
403 | 23963422 | Stavros Sachtouris | ( |
404 | 23963422 | Stavros Sachtouris | '\'.*?\'|".*?"|^[\S]*$',
|
405 | 23963422 | Stavros Sachtouris | r'"([A-Za-z0-9_\./\\-]*)"',
|
406 | 23963422 | Stavros Sachtouris | r'\"(.+?)\"',
|
407 | 23963422 | Stavros Sachtouris | '\\^a\\.\\*\\$')):
|
408 | 23963422 | Stavros Sachtouris | r_parser = r_compile(args[1])
|
409 | 23963422 | Stavros Sachtouris | self.assertEqual(
|
410 | 23963422 | Stavros Sachtouris | _parse_with_regex(*args), |
411 | 23963422 | Stavros Sachtouris | (r_parser.split(args[0]), r_parser.findall(args[0]))) |
412 | 23963422 | Stavros Sachtouris | |
413 | 23963422 | Stavros Sachtouris | def test_split_input(self): |
414 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import split_input |
415 | 23963422 | Stavros Sachtouris | for line, expected in ( |
416 | 23963422 | Stavros Sachtouris | ('unparsable', ['unparsable']), |
417 | 23963422 | Stavros Sachtouris | ('"parsable"', ['parsable']), |
418 | 23963422 | Stavros Sachtouris | ('"parse" out', ['parse', 'out']), |
419 | 23963422 | Stavros Sachtouris | ('"one', ['"one']), |
420 | 23963422 | Stavros Sachtouris | ('two" or" more"', ['two', ' or', 'more"']), |
421 | 23963422 | Stavros Sachtouris | ('Go "down \'deep " deeper \'bottom \' up" go\' up" !', [
|
422 | 23963422 | Stavros Sachtouris | 'Go', "down 'deep ", 'deeper', 'bottom ', |
423 | 23963422 | Stavros Sachtouris | 'up', " go' up", '!']), |
424 | 23963422 | Stavros Sachtouris | ('Is "this" a \'parsed\' string?', [
|
425 | 23963422 | Stavros Sachtouris | 'Is', 'this', 'a', 'parsed', 'string?'])): |
426 | 23963422 | Stavros Sachtouris | self.assertEqual(split_input(line), expected)
|
427 | 23963422 | Stavros Sachtouris | |
428 | 23963422 | Stavros Sachtouris | @patch('kamaki.cli.utils._readline', return_value='read line') |
429 | 23963422 | Stavros Sachtouris | @patch('kamaki.cli.utils._flush') |
430 | 23963422 | Stavros Sachtouris | @patch('kamaki.cli.utils._write') |
431 | 23963422 | Stavros Sachtouris | def test_ask_user(self, WR, FL, RL): |
432 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import ask_user |
433 | 23963422 | Stavros Sachtouris | msg = 'some question'
|
434 | 23963422 | Stavros Sachtouris | self.assertFalse(ask_user(msg))
|
435 | 23963422 | Stavros Sachtouris | WR.assert_called_once_with('%s [y/N]: ' % msg)
|
436 | 23963422 | Stavros Sachtouris | FL.assert_called_once_with() |
437 | 23963422 | Stavros Sachtouris | RL.assert_called_once_with() |
438 | 23963422 | Stavros Sachtouris | |
439 | 23963422 | Stavros Sachtouris | self.assertTrue(ask_user(msg, ('r', ))) |
440 | 23963422 | Stavros Sachtouris | self.assertEqual(WR.mock_calls[-1], call('%s [r/N]: ' % msg)) |
441 | 23963422 | Stavros Sachtouris | self.assertEqual(FL.mock_calls, 2 * [call()]) |
442 | 23963422 | Stavros Sachtouris | self.assertEqual(RL.mock_calls, 2 * [call()]) |
443 | 23963422 | Stavros Sachtouris | |
444 | 23963422 | Stavros Sachtouris | self.assertTrue(ask_user(msg, ('Z', 'r', 'k'))) |
445 | 23963422 | Stavros Sachtouris | self.assertEqual(WR.mock_calls[-1], call('%s [Z, r, k/N]: ' % msg)) |
446 | 23963422 | Stavros Sachtouris | self.assertEqual(FL.mock_calls, 3 * [call()]) |
447 | 23963422 | Stavros Sachtouris | self.assertEqual(RL.mock_calls, 3 * [call()]) |
448 | 23963422 | Stavros Sachtouris | |
449 | 23963422 | Stavros Sachtouris | @patch('kamaki.cli.utils._flush') |
450 | 23963422 | Stavros Sachtouris | @patch('kamaki.cli.utils._write') |
451 | 23963422 | Stavros Sachtouris | def test_spiner(self, WR, FL): |
452 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import spiner |
453 | 23963422 | Stavros Sachtouris | spins = ('/', '-', '\\', '|') |
454 | 23963422 | Stavros Sachtouris | prev = 1
|
455 | 23963422 | Stavros Sachtouris | for i, SP in enumerate(spiner(6)): |
456 | 23963422 | Stavros Sachtouris | if not i: |
457 | 23963422 | Stavros Sachtouris | self.assertEqual(WR.mock_calls[-2], call(' ')) |
458 | 23963422 | Stavros Sachtouris | elif i > 5: |
459 | 23963422 | Stavros Sachtouris | break
|
460 | 23963422 | Stavros Sachtouris | self.assertEqual(SP, None) |
461 | 23963422 | Stavros Sachtouris | self.assertEqual(WR.mock_calls[-1], call('\b%s' % spins[i % 4])) |
462 | 23963422 | Stavros Sachtouris | self.assertEqual(FL.mock_calls, prev * [call()])
|
463 | 23963422 | Stavros Sachtouris | prev += 1
|
464 | 23963422 | Stavros Sachtouris | |
465 | 23963422 | Stavros Sachtouris | def test_remove_from_items(self): |
466 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import remove_from_items |
467 | 23963422 | Stavros Sachtouris | for v in ('wrong', [1, 2, 3], [{}, 2, {}]): |
468 | 23963422 | Stavros Sachtouris | self.assertRaises(AssertionError, remove_from_items, v, 'none') |
469 | 23963422 | Stavros Sachtouris | d = dict(k1=1, k2=dict(k2=2, k3=3), k3=3, k4=4) |
470 | 23963422 | Stavros Sachtouris | for k in (d.keys() + ['kN']): |
471 | 23963422 | Stavros Sachtouris | tmp1, tmp2 = dict(d), dict(d) |
472 | 23963422 | Stavros Sachtouris | remove_from_items([tmp1, ], k) |
473 | 23963422 | Stavros Sachtouris | tmp1.pop(k, None)
|
474 | 23963422 | Stavros Sachtouris | self.assert_dicts_are_equal(tmp1, tmp2)
|
475 | 23963422 | Stavros Sachtouris | for k in (d.keys() + ['kN']): |
476 | 23963422 | Stavros Sachtouris | tmp1, tmp2 = dict(d), dict(d) |
477 | 23963422 | Stavros Sachtouris | remove_from_items([tmp1, tmp2], k) |
478 | 23963422 | Stavros Sachtouris | self.assert_dicts_are_equal(tmp1, tmp2)
|
479 | 23963422 | Stavros Sachtouris | |
480 | 1716a15d | Stavros Sachtouris | def test_filter_dicts_by_dict(self): |
481 | 1716a15d | Stavros Sachtouris | from kamaki.cli.utils import filter_dicts_by_dict |
482 | 1716a15d | Stavros Sachtouris | |
483 | 1716a15d | Stavros Sachtouris | dlist = [ |
484 | 1716a15d | Stavros Sachtouris | dict(k1='v1', k2='v2', k3='v3'), |
485 | 1716a15d | Stavros Sachtouris | dict(k1='v1'), |
486 | 1716a15d | Stavros Sachtouris | dict(k2='v2', k3='v3'), |
487 | 1716a15d | Stavros Sachtouris | dict(k1='V1', k3='V3'), |
488 | 1716a15d | Stavros Sachtouris | dict()]
|
489 | 1716a15d | Stavros Sachtouris | for l, f, em, cs, exp in ( |
490 | 1716a15d | Stavros Sachtouris | (dlist, dlist[2], True, False, dlist[0:1] + dlist[2:3]), |
491 | 1716a15d | Stavros Sachtouris | (dlist, dlist[1], True, False, dlist[0:2] + dlist[3:4]), |
492 | 1716a15d | Stavros Sachtouris | (dlist, dlist[1], True, True, dlist[0:2]), |
493 | 1716a15d | Stavros Sachtouris | (dlist, {'k3': 'v'}, True, False, []), |
494 | 1716a15d | Stavros Sachtouris | (dlist, {'k3': 'v'}, False, False, dlist[0:1] + dlist[2:4]), |
495 | 1716a15d | Stavros Sachtouris | (dlist, {'k3': 'v'}, False, True, dlist[0:1] + dlist[2:3]), |
496 | 1716a15d | Stavros Sachtouris | (dlist, {'k3': 'v'}, True, True, []), |
497 | 1716a15d | Stavros Sachtouris | ): |
498 | 1716a15d | Stavros Sachtouris | self.assertEqual(exp, filter_dicts_by_dict(l, f, em, cs))
|
499 | 1716a15d | Stavros Sachtouris | |
500 | b3cb58c2 | Stavros Sachtouris | |
501 | b3cb58c2 | Stavros Sachtouris | if __name__ == '__main__': |
502 | b3cb58c2 | Stavros Sachtouris | from sys import argv |
503 | b3cb58c2 | Stavros Sachtouris | from kamaki.cli.test import runTestCase |
504 | b3cb58c2 | Stavros Sachtouris | runTestCase(UtilsMethods, 'UtilsMethods', argv[1:]) |