9e46c54e8f56e478070a325803a7953a6ad3f611
[sdc/sdc-distribution-client.git] /
1 #!/usr/bin/env python
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #  not use this file except in compliance with the License. You may obtain
5 #  a copy of the License at
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #  License for the specific language governing permissions and limitations
13 #  under the License.
14
15 import weakref
16
17 from cliff import show
18
19 import mock
20
21
22 class FauxFormatter(object):
23
24     def __init__(self):
25         self.args = []
26         self.obj = weakref.proxy(self)
27
28     def emit_one(self, columns, data, stdout, args):
29         self.args.append((columns, data))
30
31
32 class ExerciseShowOne(show.ShowOne):
33
34     def _load_formatter_plugins(self):
35         return {
36             'test': FauxFormatter(),
37         }
38
39     def take_action(self, parsed_args):
40         return (
41             parsed_args.columns,
42             [('a', 'A'), ('b', 'B')],
43         )
44
45
46 def test_formatter_args():
47     app = mock.Mock()
48     test_show = ExerciseShowOne(app, [])
49
50     parsed_args = mock.Mock()
51     parsed_args.columns = ('Col1', 'Col2')
52     parsed_args.formatter = 'test'
53
54     test_show.run(parsed_args)
55     f = test_show._formatter_plugins['test']
56     assert len(f.args) == 1
57     args = f.args[0]
58     assert args[0] == list(parsed_args.columns)
59     data = list(args[1])
60     assert data == [('a', 'A'), ('b', 'B')]
61
62
63 def test_dict2columns():
64     app = mock.Mock()
65     test_show = ExerciseShowOne(app, [])
66     d = {'a': 'A', 'b': 'B', 'c': 'C'}
67     expected = [('a', 'b', 'c'), ('A', 'B', 'C')]
68     actual = list(test_show.dict2columns(d))
69     assert expected == actual
70
71
72 def test_no_exist_column():
73     test_show = ExerciseShowOne(mock.Mock(), [])
74     parsed_args = mock.Mock()
75     parsed_args.columns = ('no_exist_column',)
76     parsed_args.formatter = 'test'
77     with mock.patch.object(test_show, 'take_action') as mock_take_action:
78         mock_take_action.return_value = (('Col1', 'Col2', 'Col3'), [])
79         try:
80             test_show.run(parsed_args)
81         except ValueError:
82             pass
83         else:
84             assert False, 'Should have had an exception'