root / kamaki / cli / utils / test.py @ 6778681e
History | View | Annotate | Download (20.5 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 | fa9c0c38 | Stavros Sachtouris | from io import StringIO |
39 | b3cb58c2 | Stavros Sachtouris | |
40 | b3cb58c2 | Stavros Sachtouris | |
41 | b3cb58c2 | Stavros Sachtouris | class UtilsMethods(TestCase): |
42 | b3cb58c2 | Stavros Sachtouris | |
43 | b3cb58c2 | Stavros Sachtouris | def assert_dicts_are_equal(self, d1, d2): |
44 | b3cb58c2 | Stavros Sachtouris | for k, v in d1.items(): |
45 | b3cb58c2 | Stavros Sachtouris | self.assertTrue(k in d2) |
46 | b3cb58c2 | Stavros Sachtouris | if isinstance(v, dict): |
47 | b3cb58c2 | Stavros Sachtouris | self.assert_dicts_are_equal(v, d2[k])
|
48 | b3cb58c2 | Stavros Sachtouris | else:
|
49 | b3cb58c2 | Stavros Sachtouris | self.assertEqual(unicode(v), unicode(d2[k])) |
50 | b3cb58c2 | Stavros Sachtouris | |
51 | b3cb58c2 | Stavros Sachtouris | def test_guess_mime_type(self): |
52 | b3cb58c2 | Stavros Sachtouris | from kamaki.cli.utils import guess_mime_type |
53 | b3cb58c2 | Stavros Sachtouris | from mimetypes import guess_type |
54 | b3cb58c2 | Stavros Sachtouris | for args in product( |
55 | b3cb58c2 | Stavros Sachtouris | ('file.txt', 'file.png', 'file.zip', 'file.gz', None, 'X'), |
56 | b3cb58c2 | Stavros Sachtouris | ('a type', None), |
57 | b3cb58c2 | Stavros Sachtouris | ('an/encoding', None)): |
58 | b3cb58c2 | Stavros Sachtouris | filename, ctype, cencoding = args |
59 | b3cb58c2 | Stavros Sachtouris | if filename:
|
60 | b3cb58c2 | Stavros Sachtouris | exp_type, exp_enc = guess_type(filename) |
61 | b3cb58c2 | Stavros Sachtouris | self.assertEqual(
|
62 | b3cb58c2 | Stavros Sachtouris | guess_mime_type(*args), |
63 | b3cb58c2 | Stavros Sachtouris | (exp_type or ctype, exp_enc or cencoding)) |
64 | b3cb58c2 | Stavros Sachtouris | else:
|
65 | b3cb58c2 | Stavros Sachtouris | self.assertRaises(AssertionError, guess_mime_type, *args) |
66 | b3cb58c2 | Stavros Sachtouris | |
67 | 0f383dcc | Stavros Sachtouris | @patch('kamaki.cli.utils.dumps', return_value='(dumps output)') |
68 | fa9c0c38 | Stavros Sachtouris | def test_print_json(self, JD): |
69 | 0f383dcc | Stavros Sachtouris | from kamaki.cli.utils import print_json, INDENT_TAB |
70 | fa9c0c38 | Stavros Sachtouris | out = StringIO() |
71 | fa9c0c38 | Stavros Sachtouris | print_json('some data', out)
|
72 | 0f383dcc | Stavros Sachtouris | JD.assert_called_once_with('some data', indent=INDENT_TAB)
|
73 | fa9c0c38 | Stavros Sachtouris | self.assertEqual(out.getvalue(), u'(dumps output)\n') |
74 | 0f383dcc | Stavros Sachtouris | |
75 | fa9c0c38 | Stavros Sachtouris | def test_print_dict(self): |
76 | 0f383dcc | Stavros Sachtouris | from kamaki.cli.utils import print_dict, INDENT_TAB |
77 | fa9c0c38 | Stavros Sachtouris | out = StringIO() |
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 | fa9c0c38 | Stavros Sachtouris | print_dict(*args, out=out) |
108 | fa9c0c38 | Stavros Sachtouris | exp_calls = u''
|
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 | fa9c0c38 | Stavros Sachtouris | str_k = u' ' * indent
|
113 | fa9c0c38 | Stavros Sachtouris | str_k += u'%s.' % (i + 1) if with_enumeration else u'' |
114 | fa9c0c38 | Stavros Sachtouris | str_k += u'%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 | fa9c0c38 | Stavros Sachtouris | recursive_enumeration, |
124 | fa9c0c38 | Stavros Sachtouris | out)) |
125 | 0f383dcc | Stavros Sachtouris | pd_calls += 1
|
126 | fa9c0c38 | Stavros Sachtouris | exp_calls += str_k + '\n'
|
127 | 0f383dcc | Stavros Sachtouris | elif isinstance(v, list) or isinstance(v, tuple): |
128 | 0f383dcc | Stavros Sachtouris | self.assertEqual(
|
129 | 0f383dcc | Stavros Sachtouris | PL.mock_calls[pl_calls], |
130 | 0f383dcc | Stavros Sachtouris | call( |
131 | 0f383dcc | Stavros Sachtouris | v, |
132 | 0f383dcc | Stavros Sachtouris | exclude, |
133 | 0f383dcc | Stavros Sachtouris | indent + INDENT_TAB, |
134 | 0f383dcc | Stavros Sachtouris | recursive_enumeration, |
135 | fa9c0c38 | Stavros Sachtouris | recursive_enumeration, |
136 | fa9c0c38 | Stavros Sachtouris | out)) |
137 | 0f383dcc | Stavros Sachtouris | pl_calls += 1
|
138 | fa9c0c38 | Stavros Sachtouris | exp_calls += str_k + '\n'
|
139 | 0f383dcc | Stavros Sachtouris | else:
|
140 | fa9c0c38 | Stavros Sachtouris | exp_calls += u'%s %s\n' % (str_k, v)
|
141 | fa9c0c38 | Stavros Sachtouris | self.assertEqual(exp_calls, out.getvalue())
|
142 | fa9c0c38 | Stavros Sachtouris | out = StringIO() |
143 | 0f383dcc | Stavros Sachtouris | |
144 | fa9c0c38 | Stavros Sachtouris | def test_print_list(self): |
145 | 0f383dcc | Stavros Sachtouris | from kamaki.cli.utils import print_list, INDENT_TAB |
146 | fa9c0c38 | Stavros Sachtouris | out = StringIO() |
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 | fa9c0c38 | Stavros Sachtouris | print_list(*args, out=out) |
163 | fa9c0c38 | Stavros Sachtouris | exp_calls = ''
|
164 | 0f383dcc | Stavros Sachtouris | for i, v in enumerate(l): |
165 | fa9c0c38 | Stavros Sachtouris | str_v = u' ' * indent
|
166 | fa9c0c38 | Stavros Sachtouris | str_v += u'%s.' % (i + 1) if with_enumeration else u'' |
167 | 0f383dcc | Stavros Sachtouris | if isinstance(v, dict): |
168 | 0f383dcc | Stavros Sachtouris | if with_enumeration:
|
169 | fa9c0c38 | Stavros Sachtouris | exp_calls += str_v + '\n'
|
170 | 0f383dcc | Stavros Sachtouris | elif i and i < len(l): |
171 | fa9c0c38 | Stavros Sachtouris | exp_calls += u'\n'
|
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 | fa9c0c38 | Stavros Sachtouris | recursive_enumeration, |
181 | fa9c0c38 | Stavros Sachtouris | out)) |
182 | 0f383dcc | Stavros Sachtouris | pd_calls += 1
|
183 | 0f383dcc | Stavros Sachtouris | elif isinstance(v, list) or isinstance(v, tuple): |
184 | 0f383dcc | Stavros Sachtouris | if with_enumeration:
|
185 | fa9c0c38 | Stavros Sachtouris | exp_calls += str_v + '\n'
|
186 | 0f383dcc | Stavros Sachtouris | elif i and i < len(l): |
187 | fa9c0c38 | Stavros Sachtouris | exp_calls += u'\n'
|
188 | 0f383dcc | Stavros Sachtouris | self.assertEqual(
|
189 | 0f383dcc | Stavros Sachtouris | PL.mock_calls[pl_calls], |
190 | 0f383dcc | Stavros Sachtouris | call( |
191 | 0f383dcc | Stavros Sachtouris | v, |
192 | 0f383dcc | Stavros Sachtouris | exclude, |
193 | 0f383dcc | Stavros Sachtouris | indent + INDENT_TAB, |
194 | 0f383dcc | Stavros Sachtouris | recursive_enumeration, |
195 | fa9c0c38 | Stavros Sachtouris | recursive_enumeration, |
196 | fa9c0c38 | Stavros Sachtouris | out)) |
197 | 0f383dcc | Stavros Sachtouris | pl_calls += 1
|
198 | 0f383dcc | Stavros Sachtouris | elif ('%s' % v) in exclude: |
199 | 0f383dcc | Stavros Sachtouris | continue
|
200 | 0f383dcc | Stavros Sachtouris | else:
|
201 | fa9c0c38 | Stavros Sachtouris | exp_calls += u'%s%s\n' % (str_v, v)
|
202 | fa9c0c38 | Stavros Sachtouris | self.assertEqual(out.getvalue(), exp_calls)
|
203 | fa9c0c38 | Stavros Sachtouris | out = StringIO() |
204 | 46d130c9 | Stavros Sachtouris | |
205 | 46d130c9 | Stavros Sachtouris | @patch('kamaki.cli.utils.print_dict') |
206 | 46d130c9 | Stavros Sachtouris | @patch('kamaki.cli.utils.print_list') |
207 | 46d130c9 | Stavros Sachtouris | @patch('kamaki.cli.utils.bold', return_value='bold') |
208 | fa9c0c38 | Stavros Sachtouris | def test_print_items(self, bold, PL, PD): |
209 | 46d130c9 | Stavros Sachtouris | from kamaki.cli.utils import print_items, INDENT_TAB |
210 | 46d130c9 | Stavros Sachtouris | for args in product( |
211 | 46d130c9 | Stavros Sachtouris | ( |
212 | 46d130c9 | Stavros Sachtouris | 42, None, 'simple outputs', |
213 | 46d130c9 | Stavros Sachtouris | [1, 2, 3], {1: 1, 2: 2}, (3, 4), |
214 | 46d130c9 | Stavros Sachtouris | ({'k': 1, 'id': 2}, [5, 6, 7], (8, 9), '10')), |
215 | 46d130c9 | Stavros Sachtouris | (('id', 'name'), ('something', 2), ('lala', )), |
216 | 46d130c9 | Stavros Sachtouris | (False, True), |
217 | fa9c0c38 | Stavros Sachtouris | (False, True)): |
218 | fa9c0c38 | Stavros Sachtouris | items, title, with_enumeration, with_redundancy = args |
219 | 46d130c9 | Stavros Sachtouris | pl_counter, pd_counter = len(PL.mock_calls), len(PD.mock_calls) |
220 | fa9c0c38 | Stavros Sachtouris | bold_counter, out_counter = len(bold.mock_calls), 0 |
221 | fa9c0c38 | Stavros Sachtouris | out = StringIO() |
222 | fa9c0c38 | Stavros Sachtouris | print_items(*args, out=out) |
223 | fa9c0c38 | Stavros Sachtouris | out.seek(0)
|
224 | 46d130c9 | Stavros Sachtouris | if not (isinstance(items, dict) or isinstance( |
225 | 46d130c9 | Stavros Sachtouris | items, list) or isinstance(items, tuple)): |
226 | 2d1202ee | Stavros Sachtouris | if items:
|
227 | fa9c0c38 | Stavros Sachtouris | self.assertEqual(out.getvalue(), '%s\n' % items) |
228 | 46d130c9 | Stavros Sachtouris | else:
|
229 | 46d130c9 | Stavros Sachtouris | for i, item in enumerate(items): |
230 | 46d130c9 | Stavros Sachtouris | if with_enumeration:
|
231 | fa9c0c38 | Stavros Sachtouris | exp_str = '%s. ' % (i + 1) |
232 | fa9c0c38 | Stavros Sachtouris | self.assertEqual(out.read(len(exp_str)), exp_str) |
233 | 46d130c9 | Stavros Sachtouris | if isinstance(item, dict): |
234 | 46d130c9 | Stavros Sachtouris | title = sorted(set(title).intersection(item)) |
235 | 46d130c9 | Stavros Sachtouris | pick = item.get if with_redundancy else item.pop |
236 | 46d130c9 | Stavros Sachtouris | header = ' '.join('%s' % pick(key) for key in title) |
237 | 12be2bd2 | Stavros Sachtouris | if header:
|
238 | 12be2bd2 | Stavros Sachtouris | self.assertEqual(
|
239 | 12be2bd2 | Stavros Sachtouris | bold.mock_calls[bold_counter], call(header)) |
240 | 12be2bd2 | Stavros Sachtouris | self.assertEqual(out.read(5), 'bold\n') |
241 | 12be2bd2 | Stavros Sachtouris | bold_counter += 1
|
242 | 6778681e | Stavros Sachtouris | else:
|
243 | 6778681e | Stavros Sachtouris | out.read(1)
|
244 | 46d130c9 | Stavros Sachtouris | self.assertEqual(
|
245 | 46d130c9 | Stavros Sachtouris | PD.mock_calls[pd_counter], |
246 | fa9c0c38 | Stavros Sachtouris | call(item, indent=INDENT_TAB, out=out)) |
247 | 46d130c9 | Stavros Sachtouris | pd_counter += 1
|
248 | 46d130c9 | Stavros Sachtouris | elif isinstance(item, list) or isinstance(item, tuple): |
249 | 46d130c9 | Stavros Sachtouris | self.assertEqual(
|
250 | 46d130c9 | Stavros Sachtouris | PL.mock_calls[pl_counter], |
251 | fa9c0c38 | Stavros Sachtouris | call(item, indent=INDENT_TAB, out=out)) |
252 | 46d130c9 | Stavros Sachtouris | pl_counter += 1
|
253 | 46d130c9 | Stavros Sachtouris | else:
|
254 | fa9c0c38 | Stavros Sachtouris | exp_str = u' %s\n' % item
|
255 | fa9c0c38 | Stavros Sachtouris | self.assertEqual(out.read(len(exp_str)), exp_str) |
256 | 46d130c9 | Stavros Sachtouris | |
257 | 23963422 | Stavros Sachtouris | def test_format_size(self): |
258 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import format_size |
259 | 23963422 | Stavros Sachtouris | from kamaki.cli import CLIError |
260 | 23963422 | Stavros Sachtouris | for v in ('wrong', {1: '1', 2: '2'}, ('tuples', 'not OK'), [1, 2]): |
261 | 23963422 | Stavros Sachtouris | self.assertRaises(CLIError, format_size, v)
|
262 | 23963422 | Stavros Sachtouris | for step, B, K, M, G, T in ( |
263 | 23963422 | Stavros Sachtouris | (1000, 'B', 'KB', 'MB', 'GB', 'TB'), |
264 | 23963422 | Stavros Sachtouris | (1024, 'B', 'KiB', 'MiB', 'GiB', 'TiB')): |
265 | 23963422 | Stavros Sachtouris | Ki, Mi, Gi = step, step * step, step * step * step |
266 | 23963422 | Stavros Sachtouris | for before, after in ( |
267 | 23963422 | Stavros Sachtouris | (0, '0' + B), (512, '512' + B), ( |
268 | 23963422 | Stavros Sachtouris | Ki - 1, '%s%s' % (step - 1, B)), |
269 | 23963422 | Stavros Sachtouris | (Ki, '1' + K), (42 * Ki, '42' + K), ( |
270 | 23963422 | Stavros Sachtouris | Mi - 1, '%s.99%s' % (step - 1, K)), |
271 | 23963422 | Stavros Sachtouris | (Mi, '1' + M), (42 * Mi, '42' + M), ( |
272 | 23963422 | Stavros Sachtouris | Ki * Mi - 1, '%s.99%s' % (step - 1, M)), |
273 | 23963422 | Stavros Sachtouris | (Gi, '1' + G), (42 * Gi, '42' + G), ( |
274 | 23963422 | Stavros Sachtouris | Mi * Mi - 1, '%s.99%s' % (step - 1, G)), |
275 | 23963422 | Stavros Sachtouris | (Mi * Mi, '1' + T), (42 * Mi * Mi, '42' + T), ( |
276 | 23963422 | Stavros Sachtouris | Mi * Gi - 1, '%s.99%s' % (step - 1, T)), ( |
277 | 23963422 | Stavros Sachtouris | 42 * Mi * Gi, '%s%s' % (42 * Ki, T))): |
278 | 23963422 | Stavros Sachtouris | self.assertEqual(format_size(before, step == 1000), after) |
279 | 23963422 | Stavros Sachtouris | |
280 | 23963422 | Stavros Sachtouris | def test_to_bytes(self): |
281 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import to_bytes |
282 | 23963422 | Stavros Sachtouris | for v in ('wrong', 'KABUM', 'kbps', 'kibps'): |
283 | 23963422 | Stavros Sachtouris | self.assertRaises(ValueError, to_bytes, v, 'B') |
284 | 23963422 | Stavros Sachtouris | self.assertRaises(ValueError, to_bytes, 42, v) |
285 | 23963422 | Stavros Sachtouris | for v in ([1, 2, 3], ('kb', 'mb'), {'kb': 1, 'byte': 2}): |
286 | 23963422 | Stavros Sachtouris | self.assertRaises(TypeError, to_bytes, v, 'B') |
287 | 23963422 | Stavros Sachtouris | self.assertRaises(AttributeError, to_bytes, 42, v) |
288 | 23963422 | Stavros Sachtouris | kl, ki = 1000, 1024 |
289 | 23963422 | Stavros Sachtouris | for size, (unit, factor) in product( |
290 | 23963422 | Stavros Sachtouris | (0, 42, 3.14, 1023, 10000), |
291 | 23963422 | Stavros Sachtouris | ( |
292 | 23963422 | Stavros Sachtouris | ('B', 1), ('b', 1), |
293 | 23963422 | Stavros Sachtouris | ('KB', kl), ('KiB', ki), |
294 | 23963422 | Stavros Sachtouris | ('mb', kl * kl), ('mIb', ki * ki), |
295 | 23963422 | Stavros Sachtouris | ('gB', kl * kl * kl), ('GIB', ki * ki * ki), |
296 | 23963422 | Stavros Sachtouris | ('TB', kl * kl * kl * kl), ('tiB', ki * ki * ki * ki))): |
297 | 23963422 | Stavros Sachtouris | self.assertEqual(to_bytes(size, unit), int(size * factor)) |
298 | 23963422 | Stavros Sachtouris | |
299 | 23963422 | Stavros Sachtouris | def test_dict2file(self): |
300 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import dict2file, INDENT_TAB |
301 | 23963422 | Stavros Sachtouris | for d, depth in product(( |
302 | 23963422 | Stavros Sachtouris | {'k': 42}, |
303 | 23963422 | Stavros Sachtouris | {'k1': 'v1', 'k2': [1, 2, 3], 'k3': {'k': 'v'}}, |
304 | 23963422 | Stavros Sachtouris | {'k1': {
|
305 | 23963422 | Stavros Sachtouris | 'k1.1': 'v1.1', |
306 | 23963422 | Stavros Sachtouris | 'k1.2': [1, 2, 3], |
307 | 23963422 | Stavros Sachtouris | 'k1.3': {'k': 'v'}}}), |
308 | 23963422 | Stavros Sachtouris | (-42, 0, 42)): |
309 | 23963422 | Stavros Sachtouris | exp = ''
|
310 | 23963422 | Stavros Sachtouris | exp_d = [] |
311 | 23963422 | Stavros Sachtouris | exp_l = [] |
312 | 23963422 | Stavros Sachtouris | exp, exp_d, exp_l = '', [], []
|
313 | 23963422 | Stavros Sachtouris | with NamedTemporaryFile() as f: |
314 | 23963422 | Stavros Sachtouris | for k, v in d.items(): |
315 | 23963422 | Stavros Sachtouris | sfx = '\n'
|
316 | 23963422 | Stavros Sachtouris | if isinstance(v, dict): |
317 | 23963422 | Stavros Sachtouris | exp_d.append(call(v, f, depth + 1))
|
318 | 23963422 | Stavros Sachtouris | elif isinstance(v, tuple) or isinstance(v, list): |
319 | 23963422 | Stavros Sachtouris | exp_l.append(call(v, f, depth + 1))
|
320 | 23963422 | Stavros Sachtouris | else:
|
321 | 23963422 | Stavros Sachtouris | sfx = '%s\n' % v
|
322 | 23963422 | Stavros Sachtouris | exp += '%s%s: %s' % (
|
323 | 23963422 | Stavros Sachtouris | ' ' * (depth * INDENT_TAB), k, sfx)
|
324 | 23963422 | Stavros Sachtouris | with patch('kamaki.cli.utils.dict2file') as D2F: |
325 | 23963422 | Stavros Sachtouris | with patch('kamaki.cli.utils.list2file') as L2F: |
326 | 23963422 | Stavros Sachtouris | dict2file(d, f, depth) |
327 | 23963422 | Stavros Sachtouris | f.seek(0)
|
328 | 23963422 | Stavros Sachtouris | self.assertEqual(f.read(), exp)
|
329 | 23963422 | Stavros Sachtouris | self.assertEqual(L2F.mock_calls, exp_l)
|
330 | 23963422 | Stavros Sachtouris | self.assertEqual(D2F.mock_calls, exp_d)
|
331 | 23963422 | Stavros Sachtouris | |
332 | 23963422 | Stavros Sachtouris | def test_list2file(self): |
333 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import list2file, INDENT_TAB |
334 | 23963422 | Stavros Sachtouris | for l, depth in product( |
335 | 23963422 | Stavros Sachtouris | ( |
336 | 23963422 | Stavros Sachtouris | (1, 2, 3), |
337 | 23963422 | Stavros Sachtouris | [1, 2, 3], |
338 | 23963422 | Stavros Sachtouris | ('v', [1, 2, 3], (1, 2, 3), {'1': 1, 2: '2', 3: 3}), |
339 | 23963422 | Stavros Sachtouris | ['v', {'k1': 'v1', 'k2': [1, 2, 3], 'k3': {1: '1'}}]), |
340 | 23963422 | Stavros Sachtouris | (-42, 0, 42)): |
341 | 23963422 | Stavros Sachtouris | with NamedTemporaryFile() as f: |
342 | 23963422 | Stavros Sachtouris | exp, exp_d, exp_l = '', [], []
|
343 | 23963422 | Stavros Sachtouris | for v in l: |
344 | 23963422 | Stavros Sachtouris | if isinstance(v, dict): |
345 | 23963422 | Stavros Sachtouris | exp_d.append(call(v, f, depth + 1))
|
346 | 23963422 | Stavros Sachtouris | elif isinstance(v, list) or isinstance(v, tuple): |
347 | 23963422 | Stavros Sachtouris | exp_l.append(call(v, f, depth + 1))
|
348 | 23963422 | Stavros Sachtouris | else:
|
349 | 23963422 | Stavros Sachtouris | exp += '%s%s\n' % (' ' * INDENT_TAB * depth, v) |
350 | 23963422 | Stavros Sachtouris | with patch('kamaki.cli.utils.dict2file') as D2F: |
351 | 23963422 | Stavros Sachtouris | with patch('kamaki.cli.utils.list2file') as L2F: |
352 | 23963422 | Stavros Sachtouris | list2file(l, f, depth) |
353 | 23963422 | Stavros Sachtouris | f.seek(0)
|
354 | 23963422 | Stavros Sachtouris | self.assertEqual(f.read(), exp)
|
355 | 23963422 | Stavros Sachtouris | self.assertEqual(L2F.mock_calls, exp_l)
|
356 | 23963422 | Stavros Sachtouris | self.assertEqual(D2F.mock_calls, exp_d)
|
357 | 23963422 | Stavros Sachtouris | |
358 | 23963422 | Stavros Sachtouris | def test__parse_with_regex(self): |
359 | 23963422 | Stavros Sachtouris | from re import compile as r_compile |
360 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import _parse_with_regex |
361 | 23963422 | Stavros Sachtouris | for args in product( |
362 | 23963422 | Stavros Sachtouris | ( |
363 | 23963422 | Stavros Sachtouris | 'this is a line',
|
364 | 23963422 | Stavros Sachtouris | 'this_is_also_a_line',
|
365 | 23963422 | Stavros Sachtouris | 'This "text" is quoted',
|
366 | 23963422 | Stavros Sachtouris | 'This "quoted" "text" is more "complicated"',
|
367 | 23963422 | Stavros Sachtouris | 'Is this \'quoted\' text "double \'quoted\' or not?"',
|
368 | 23963422 | Stavros Sachtouris | '"What \'about\' the" oposite?',
|
369 | 23963422 | Stavros Sachtouris | ' Try with a " single double quote',
|
370 | 23963422 | Stavros Sachtouris | 'Go "down \'deep " deeper \'bottom \' up" go\' up" !'),
|
371 | 23963422 | Stavros Sachtouris | ( |
372 | 23963422 | Stavros Sachtouris | '\'.*?\'|".*?"|^[\S]*$',
|
373 | 23963422 | Stavros Sachtouris | r'"([A-Za-z0-9_\./\\-]*)"',
|
374 | 23963422 | Stavros Sachtouris | r'\"(.+?)\"',
|
375 | 23963422 | Stavros Sachtouris | '\\^a\\.\\*\\$')):
|
376 | 23963422 | Stavros Sachtouris | r_parser = r_compile(args[1])
|
377 | 23963422 | Stavros Sachtouris | self.assertEqual(
|
378 | 23963422 | Stavros Sachtouris | _parse_with_regex(*args), |
379 | 23963422 | Stavros Sachtouris | (r_parser.split(args[0]), r_parser.findall(args[0]))) |
380 | 23963422 | Stavros Sachtouris | |
381 | 23963422 | Stavros Sachtouris | def test_split_input(self): |
382 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import split_input |
383 | 23963422 | Stavros Sachtouris | for line, expected in ( |
384 | 89ea97e1 | Stavros Sachtouris | ('set key="v1"', ['set', 'key=v1']), |
385 | 23963422 | Stavros Sachtouris | ('unparsable', ['unparsable']), |
386 | 23963422 | Stavros Sachtouris | ('"parsable"', ['parsable']), |
387 | 23963422 | Stavros Sachtouris | ('"parse" out', ['parse', 'out']), |
388 | 23963422 | Stavros Sachtouris | ('"one', ['"one']), |
389 | b9eebf2a | Stavros Sachtouris | ('two" or" more"', ['two or', 'more"']), |
390 | 23963422 | Stavros Sachtouris | ('Go "down \'deep " deeper \'bottom \' up" go\' up" !', [
|
391 | 23963422 | Stavros Sachtouris | 'Go', "down 'deep ", 'deeper', 'bottom ', |
392 | b9eebf2a | Stavros Sachtouris | 'up go\' up', '!']), |
393 | 23963422 | Stavros Sachtouris | ('Is "this" a \'parsed\' string?', [
|
394 | 23963422 | Stavros Sachtouris | 'Is', 'this', 'a', 'parsed', 'string?'])): |
395 | 23963422 | Stavros Sachtouris | self.assertEqual(split_input(line), expected)
|
396 | 23963422 | Stavros Sachtouris | |
397 | fa9c0c38 | Stavros Sachtouris | def test_ask_user(self): |
398 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import ask_user |
399 | fa9c0c38 | Stavros Sachtouris | msg = u'some question'
|
400 | fa9c0c38 | Stavros Sachtouris | out = StringIO() |
401 | fa9c0c38 | Stavros Sachtouris | user_in = StringIO(u'n')
|
402 | fa9c0c38 | Stavros Sachtouris | self.assertFalse(ask_user(msg, out=out, user_in=user_in))
|
403 | fa9c0c38 | Stavros Sachtouris | self.assertEqual(out.getvalue(), u'%s [y/N]: ' % msg) |
404 | 23963422 | Stavros Sachtouris | |
405 | fa9c0c38 | Stavros Sachtouris | user_in.seek(0)
|
406 | fa9c0c38 | Stavros Sachtouris | out.seek(0)
|
407 | fa9c0c38 | Stavros Sachtouris | self.assertTrue(ask_user(msg, ('n', ), out=out, user_in=user_in)) |
408 | fa9c0c38 | Stavros Sachtouris | self.assertEqual(out.getvalue(), u'%s [n/<not n>]: ' % msg) |
409 | 23963422 | Stavros Sachtouris | |
410 | fa9c0c38 | Stavros Sachtouris | user_in = StringIO(unicode('N')) |
411 | fa9c0c38 | Stavros Sachtouris | out.seek(0)
|
412 | fa9c0c38 | Stavros Sachtouris | self.assertTrue(ask_user(msg, ('r', 'N'), out=out, user_in=user_in)) |
413 | fa9c0c38 | Stavros Sachtouris | self.assertEqual(out.getvalue(), u'%s [r, N/<not r, N>]: ' % msg) |
414 | 23963422 | Stavros Sachtouris | |
415 | 23963422 | Stavros Sachtouris | def test_remove_from_items(self): |
416 | 23963422 | Stavros Sachtouris | from kamaki.cli.utils import remove_from_items |
417 | 23963422 | Stavros Sachtouris | for v in ('wrong', [1, 2, 3], [{}, 2, {}]): |
418 | 23963422 | Stavros Sachtouris | self.assertRaises(AssertionError, remove_from_items, v, 'none') |
419 | 23963422 | Stavros Sachtouris | d = dict(k1=1, k2=dict(k2=2, k3=3), k3=3, k4=4) |
420 | 23963422 | Stavros Sachtouris | for k in (d.keys() + ['kN']): |
421 | 23963422 | Stavros Sachtouris | tmp1, tmp2 = dict(d), dict(d) |
422 | 23963422 | Stavros Sachtouris | remove_from_items([tmp1, ], k) |
423 | 23963422 | Stavros Sachtouris | tmp1.pop(k, None)
|
424 | 23963422 | Stavros Sachtouris | self.assert_dicts_are_equal(tmp1, tmp2)
|
425 | 23963422 | Stavros Sachtouris | for k in (d.keys() + ['kN']): |
426 | 23963422 | Stavros Sachtouris | tmp1, tmp2 = dict(d), dict(d) |
427 | 23963422 | Stavros Sachtouris | remove_from_items([tmp1, tmp2], k) |
428 | 23963422 | Stavros Sachtouris | self.assert_dicts_are_equal(tmp1, tmp2)
|
429 | 23963422 | Stavros Sachtouris | |
430 | 1716a15d | Stavros Sachtouris | def test_filter_dicts_by_dict(self): |
431 | 1716a15d | Stavros Sachtouris | from kamaki.cli.utils import filter_dicts_by_dict |
432 | 1716a15d | Stavros Sachtouris | |
433 | 1716a15d | Stavros Sachtouris | dlist = [ |
434 | 1716a15d | Stavros Sachtouris | dict(k1='v1', k2='v2', k3='v3'), |
435 | 1716a15d | Stavros Sachtouris | dict(k1='v1'), |
436 | 1716a15d | Stavros Sachtouris | dict(k2='v2', k3='v3'), |
437 | 1716a15d | Stavros Sachtouris | dict(k1='V1', k3='V3'), |
438 | 1716a15d | Stavros Sachtouris | dict()]
|
439 | 1716a15d | Stavros Sachtouris | for l, f, em, cs, exp in ( |
440 | 1716a15d | Stavros Sachtouris | (dlist, dlist[2], True, False, dlist[0:1] + dlist[2:3]), |
441 | 1716a15d | Stavros Sachtouris | (dlist, dlist[1], True, False, dlist[0:2] + dlist[3:4]), |
442 | 1716a15d | Stavros Sachtouris | (dlist, dlist[1], True, True, dlist[0:2]), |
443 | 1716a15d | Stavros Sachtouris | (dlist, {'k3': 'v'}, True, False, []), |
444 | 1716a15d | Stavros Sachtouris | (dlist, {'k3': 'v'}, False, False, dlist[0:1] + dlist[2:4]), |
445 | 1716a15d | Stavros Sachtouris | (dlist, {'k3': 'v'}, False, True, dlist[0:1] + dlist[2:3]), |
446 | 1716a15d | Stavros Sachtouris | (dlist, {'k3': 'v'}, True, True, []), |
447 | 854222c7 | Stavros Sachtouris | (dlist, dlist[4], True, False, dlist), |
448 | 1716a15d | Stavros Sachtouris | ): |
449 | 1716a15d | Stavros Sachtouris | self.assertEqual(exp, filter_dicts_by_dict(l, f, em, cs))
|
450 | 1716a15d | Stavros Sachtouris | |
451 | b3cb58c2 | Stavros Sachtouris | |
452 | b3cb58c2 | Stavros Sachtouris | if __name__ == '__main__': |
453 | b3cb58c2 | Stavros Sachtouris | from sys import argv |
454 | b3cb58c2 | Stavros Sachtouris | from kamaki.cli.test import runTestCase |
455 | b3cb58c2 | Stavros Sachtouris | runTestCase(UtilsMethods, 'UtilsMethods', argv[1:]) |