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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
17 from cliff import show
22 class FauxFormatter(object):
26 self.obj = weakref.proxy(self)
28 def emit_one(self, columns, data, stdout, args):
29 self.args.append((columns, data))
32 class ExerciseShowOne(show.ShowOne):
34 def _load_formatter_plugins(self):
36 'test': FauxFormatter(),
39 def take_action(self, parsed_args):
42 [('a', 'A'), ('b', 'B')],
46 def test_formatter_args():
48 test_show = ExerciseShowOne(app, [])
50 parsed_args = mock.Mock()
51 parsed_args.columns = ('Col1', 'Col2')
52 parsed_args.formatter = 'test'
54 test_show.run(parsed_args)
55 f = test_show._formatter_plugins['test']
56 assert len(f.args) == 1
58 assert args[0] == list(parsed_args.columns)
60 assert data == [('a', 'A'), ('b', 'B')]
63 def test_dict2columns():
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
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'), [])
80 test_show.run(parsed_args)
84 assert False, 'Should have had an exception'