27c6f84c06ddd3d7f446bb328dba6adfe312c2aa
[sdc/sdc-distribution-client.git] /
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 #  Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #  not use this file except in compliance with the License. You may obtain
6 #  a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #  Unless required by applicable law or agreed to in writing, software
11 #  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #  License for the specific language governing permissions and limitations
14 #  under the License.
15
16 import mock
17 import argparse
18 import six
19
20 from cliff.formatters import commaseparated
21 from cliff.tests import test_columns
22
23
24 def test_commaseparated_list_formatter():
25     sf = commaseparated.CSVLister()
26     c = ('a', 'b', 'c')
27     d1 = ('A', 'B', 'C')
28     d2 = ('D', 'E', 'F')
29     data = [d1, d2]
30     expected = 'a,b,c\nA,B,C\nD,E,F\n'
31     output = six.StringIO()
32     parsed_args = mock.Mock()
33     parsed_args.quote_mode = 'none'
34     sf.emit_list(c, data, output, parsed_args)
35     actual = output.getvalue()
36     assert expected == actual
37
38
39 def test_commaseparated_list_formatter_quoted():
40     sf = commaseparated.CSVLister()
41     c = ('a', 'b', 'c')
42     d1 = ('A', 'B', 'C')
43     d2 = ('D', 'E', 'F')
44     data = [d1, d2]
45     expected = '"a","b","c"\n"A","B","C"\n"D","E","F"\n'
46     output = six.StringIO()
47     # Parse arguments as if passed on the command-line
48     parser = argparse.ArgumentParser(description='Testing...')
49     sf.add_argument_group(parser)
50     parsed_args = parser.parse_args(['--quote', 'all'])
51     sf.emit_list(c, data, output, parsed_args)
52     actual = output.getvalue()
53     assert expected == actual
54
55
56 def test_commaseparated_list_formatter_formattable_column():
57     sf = commaseparated.CSVLister()
58     c = ('a', 'b', 'c')
59     d1 = ('A', 'B', test_columns.FauxColumn(['the', 'value']))
60     data = [d1]
61     expected = 'a,b,c\nA,B,[\'the\'\\, \'value\']\n'
62     output = six.StringIO()
63     parsed_args = mock.Mock()
64     parsed_args.quote_mode = 'none'
65     sf.emit_list(c, data, output, parsed_args)
66     actual = output.getvalue()
67     assert expected == actual
68
69
70 def test_commaseparated_list_formatter_unicode():
71     sf = commaseparated.CSVLister()
72     c = (u'a', u'b', u'c')
73     d1 = (u'A', u'B', u'C')
74     happy = u'高兴'
75     d2 = (u'D', u'E', happy)
76     data = [d1, d2]
77     expected = u'a,b,c\nA,B,C\nD,E,%s\n' % happy
78     output = six.StringIO()
79     parsed_args = mock.Mock()
80     parsed_args.quote_mode = 'none'
81     sf.emit_list(c, data, output, parsed_args)
82     actual = output.getvalue()
83     if six.PY2:
84         actual = actual.decode('utf-8')
85     assert expected == actual