61db5d8521add50800f13514c25a0a55b0d4974a
[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 six
16 import yaml
17
18 from cliff.formatters import yaml_format
19 from cliff.tests import test_columns
20
21 import mock
22
23
24 def test_yaml_format_one():
25     sf = yaml_format.YAMLFormatter()
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     output = six.StringIO()
35     args = mock.Mock()
36     sf.emit_one(c, d, output, args)
37     actual = yaml.safe_load(output.getvalue())
38     assert expected == actual
39
40
41 def test_yaml_format_formattablecolumn_one():
42     sf = yaml_format.YAMLFormatter()
43     c = ('a', 'b', 'c', 'd')
44     d = ('A', 'B', 'C', test_columns.FauxColumn(['the', 'value']))
45     expected = {
46         'a': 'A',
47         'b': 'B',
48         'c': 'C',
49         'd': ['the', 'value'],
50     }
51     args = mock.Mock()
52     sf.add_argument_group(args)
53
54     args.noindent = True
55     output = six.StringIO()
56     sf.emit_one(c, d, output, args)
57     value = output.getvalue()
58     print(len(value.splitlines()))
59     actual = yaml.safe_load(output.getvalue())
60     assert expected == actual
61
62
63 def test_yaml_format_list():
64     sf = yaml_format.YAMLFormatter()
65     c = ('a', 'b', 'c')
66     d = (
67         ('A1', 'B1', 'C1'),
68         ('A2', 'B2', 'C2'),
69         ('A3', 'B3', 'C3')
70     )
71     expected = [
72         {'a': 'A1', 'b': 'B1', 'c': 'C1'},
73         {'a': 'A2', 'b': 'B2', 'c': 'C2'},
74         {'a': 'A3', 'b': 'B3', 'c': 'C3'}
75     ]
76     output = six.StringIO()
77     args = mock.Mock()
78     sf.add_argument_group(args)
79     sf.emit_list(c, d, output, args)
80     actual = yaml.safe_load(output.getvalue())
81     assert expected == actual
82
83
84 def test_yaml_format_formattablecolumn_list():
85     sf = yaml_format.YAMLFormatter()
86     c = ('a', 'b', 'c')
87     d = (
88         ('A1', 'B1', test_columns.FauxColumn(['the', 'value'])),
89     )
90     expected = [
91         {'a': 'A1', 'b': 'B1', 'c': ['the', 'value']},
92     ]
93     args = mock.Mock()
94     sf.add_argument_group(args)
95
96     args.noindent = True
97     output = six.StringIO()
98     sf.emit_list(c, d, output, args)
99     actual = yaml.safe_load(output.getvalue())
100     assert expected == actual