eb7397dff04115c314c333b280878fb748c12121
[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 json
16 import six
17
18 from cliff.formatters import json_format
19 from cliff.tests import test_columns
20
21 import mock
22
23
24 def test_json_format_one():
25     sf = json_format.JSONFormatter()
26     c = ('a', 'b', 'c', 'd')
27     d = ('A', 'B', 'C', '"escape me"')
28     expected = {
29         'a': 'A',
30         'b': 'B',
31         'c': 'C',
32         'd': '"escape me"'
33     }
34     args = mock.Mock()
35     sf.add_argument_group(args)
36
37     args.noindent = True
38     output = six.StringIO()
39     sf.emit_one(c, d, output, args)
40     value = output.getvalue()
41     print(len(value.splitlines()))
42     assert 1 == len(value.splitlines())
43     actual = json.loads(value)
44     assert expected == actual
45
46     args.noindent = False
47     output = six.StringIO()
48     sf.emit_one(c, d, output, args)
49     value = output.getvalue()
50     assert 6 == len(value.splitlines())
51     actual = json.loads(value)
52     assert expected == actual
53
54
55 def test_json_format_formattablecolumn_one():
56     sf = json_format.JSONFormatter()
57     c = ('a', 'b', 'c', 'd')
58     d = ('A', 'B', 'C', test_columns.FauxColumn(['the', 'value']))
59     expected = {
60         'a': 'A',
61         'b': 'B',
62         'c': 'C',
63         'd': ['the', 'value'],
64     }
65     args = mock.Mock()
66     sf.add_argument_group(args)
67
68     args.noindent = True
69     output = six.StringIO()
70     sf.emit_one(c, d, output, args)
71     value = output.getvalue()
72     print(len(value.splitlines()))
73     assert 1 == len(value.splitlines())
74     actual = json.loads(value)
75     assert expected == actual
76
77
78 def test_json_format_list():
79     sf = json_format.JSONFormatter()
80     c = ('a', 'b', 'c')
81     d = (
82         ('A1', 'B1', 'C1'),
83         ('A2', 'B2', 'C2'),
84         ('A3', 'B3', 'C3')
85     )
86     expected = [
87         {'a': 'A1', 'b': 'B1', 'c': 'C1'},
88         {'a': 'A2', 'b': 'B2', 'c': 'C2'},
89         {'a': 'A3', 'b': 'B3', 'c': 'C3'}
90     ]
91     args = mock.Mock()
92     sf.add_argument_group(args)
93
94     args.noindent = True
95     output = six.StringIO()
96     sf.emit_list(c, d, output, args)
97     value = output.getvalue()
98     assert 1 == len(value.splitlines())
99     actual = json.loads(value)
100     assert expected == actual
101
102     args.noindent = False
103     output = six.StringIO()
104     sf.emit_list(c, d, output, args)
105     value = output.getvalue()
106     assert 17 == len(value.splitlines())
107     actual = json.loads(value)
108     assert expected == actual
109
110
111 def test_json_format_formattablecolumn_list():
112     sf = json_format.JSONFormatter()
113     c = ('a', 'b', 'c')
114     d = (
115         ('A1', 'B1', test_columns.FauxColumn(['the', 'value'])),
116     )
117     expected = [
118         {'a': 'A1', 'b': 'B1', 'c': ['the', 'value']},
119     ]
120     args = mock.Mock()
121     sf.add_argument_group(args)
122
123     args.noindent = True
124     output = six.StringIO()
125     sf.emit_list(c, d, output, args)
126     value = output.getvalue()
127     assert 1 == len(value.splitlines())
128     actual = json.loads(value)
129     assert expected == actual