f2e4a0d7f0e306be07964922e8085920e8b0f7c5
[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 lister
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_list(self, columns, data, stdout, args):
29         self.args.append((columns, data))
30
31
32 class ExerciseLister(lister.Lister):
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_lister = ExerciseLister(app, [])
49
50     parsed_args = mock.Mock()
51     parsed_args.columns = ('Col1', 'Col2')
52     parsed_args.formatter = 'test'
53
54     test_lister.run(parsed_args)
55     f = test_lister._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_no_exist_column():
64     test_lister = ExerciseLister(mock.Mock(), [])
65     parsed_args = mock.Mock()
66     parsed_args.columns = ('no_exist_column',)
67     parsed_args.formatter = 'test'
68     with mock.patch.object(test_lister, 'take_action') as mock_take_action:
69         mock_take_action.return_value = (('Col1', 'Col2', 'Col3'), [])
70         try:
71             test_lister.run(parsed_args)
72         except ValueError:
73             pass
74         else:
75             assert False, 'Should have had an exception'