1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
# Copyright (c) 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
import sys
from awscli import text
from awscli.compat import StringIO
from awscli.testutils import mock, unittest
class TestSection(unittest.TestCase):
def format_text(self, data, stream=None):
if stream is None:
stream = StringIO()
text.format_text(data, stream=stream)
return stream.getvalue()
def assert_text_renders_to(self, data, expected_rendering):
rendered = self.format_text(data)
self.assertEqual(rendered, expected_rendering)
def test_dict_format(self):
self.assert_text_renders_to(dict(a=1, b=2, c=3), "1\t2\t3\n")
def test_list_format(self):
self.assert_text_renders_to([1, 2, 3], "1\t2\t3\n")
def test_list_of_dicts(self):
self.assert_text_renders_to(
{'foo': [dict(a=1, b=2, c=3), dict(a=4, b=5, c=6)]},
'FOO\t1\t2\t3\n' 'FOO\t4\t5\t6\n',
)
def test_multiple_list_of_dicts(self):
self.assert_text_renders_to(
{
'foo': [dict(a=1, b=2, c=3), dict(a=4, b=5, c=6)],
'zoo': [dict(a=7, b=8, c=9), dict(a=0, b=1, c=2)],
},
'FOO\t1\t2\t3\n'
'FOO\t4\t5\t6\n'
'ZOO\t7\t8\t9\n'
'ZOO\t0\t1\t2\n',
)
def test_single_scalar_number(self):
self.assert_text_renders_to(10, '10\n')
def test_list_of_single_number(self):
self.assert_text_renders_to([10], '10\n')
def test_list_of_multiple_numbers(self):
self.assert_text_renders_to([10, 10, 10], '10\t10\t10\n')
def test_different_keys_in_sublists(self):
self.assert_text_renders_to(
# missing "b" adds "d"
{'foo': [dict(a=1, b=2, c=3), dict(a=4, c=5), dict(a=6, d=7)]},
'FOO\t1\t2\t3\t\n' 'FOO\t4\t\t5\t\n' 'FOO\t6\t\t\t7\n',
)
def test_different_keys_in_nested_sublists(self):
self.assert_text_renders_to(
{
'bar': [
{'foo': [dict(a=1, b=2, c=3), dict(a=4, c=5)]},
{'foo': [dict(b=6, d=7), dict(b=8, c=9)]},
]
},
'FOO\t1\t2\t3\n' 'FOO\t4\t\t5\n' 'FOO\t6\t\t7\n' 'FOO\t8\t9\t\n',
)
def test_different_keys_in_deeply_nested_sublists(self):
self.assert_text_renders_to(
{
'bar': [
{'foo': [[[dict(a=1, b=2, c=3), dict(a=4, c=5)]]]},
{'foo': [[[dict(b=6, d=7), dict(b=8, c=9)]]]},
]
},
'FOO\t1\t2\t3\n' 'FOO\t4\t\t5\n' 'FOO\t6\t\t7\n' 'FOO\t8\t9\t\n',
)
def test_scalars_and_complex_types(self):
self.assert_text_renders_to(
{
'foo': [
dict(a=1, b=dict(y='y', z='z'), c=3),
dict(a=4, b=dict(y='y', z='z'), c=6),
]
},
'FOO\t1\t3\n' 'B\ty\tz\n' 'FOO\t4\t6\n' 'B\ty\tz\n',
)
def test_nested_list_of_lists(self):
self.assert_text_renders_to(
[['1', '2', '3'], ['4', '5', '6']], '1\t2\t3\n' '4\t5\t6\n'
)
def test_deeply_nested_lists(self):
self.assert_text_renders_to(
[
[['1', '2', '3'], ['4', '5', '6']],
[['7', '8', '9'], ['0', '1', '2']],
],
'1\t2\t3\n' '4\t5\t6\n' '7\t8\t9\n' '0\t1\t2\n',
)
def test_unicode_text(self):
self.assert_text_renders_to([['1', '2', '\u2713']], '1\t2\t\u2713\n')
def test_single_scalar_value(self):
self.assert_text_renders_to('foobarbaz', 'foobarbaz\n')
def test_empty_list(self):
self.assert_text_renders_to([], '')
def test_empty_inner_list(self):
self.assert_text_renders_to([[]], '')
def test_deeploy_nested_empty_list(self):
self.assert_text_renders_to([[[[]]]], '')
def test_deeploy_nested_single_scalar(self):
self.assert_text_renders_to([[[['a']]]], 'a\n')
def test_empty_list_mock_calls(self):
# We also need this test as well as test_empty_list
# because we want to ensure that write() is never called with
# a list object.
fake_stream = mock.Mock()
self.format_text(data=[], stream=fake_stream)
# We should not call .write() at all for an empty list.
self.assertFalse(fake_stream.write.called)
def test_list_of_strings_in_dict(self):
self.assert_text_renders_to(
{'KeyName': ['a', 'b', 'c']},
'KEYNAME\ta\n' 'KEYNAME\tb\n' 'KEYNAME\tc\n',
)
def test_inconsistent_sublists(self):
self.assert_text_renders_to(
[[['1', '2'], ['3', '4', '5', '6']], [['7', '8', '9'], ['0']]],
'1\t2\n' '3\t4\t5\t6\n' '7\t8\t9\n' '0\n',
)
def test_lists_mixed_with_scalars(self):
self.assert_text_renders_to(
[['a', 'b', ['c', 'd']], ['e', 'f', ['g', 'h']]],
'a\tb\n' 'c\td\n' 'e\tf\n' 'g\th\n',
)
def test_deeply_nested_with_scalars(self):
self.assert_text_renders_to(
[
['a', 'b', ['c', 'd', ['e', 'f', ['g', 'h']]]],
['i', 'j', ['k', 'l', ['m', 'n', ['o', 'p']]]],
],
'a\tb\n'
'c\td\n'
'e\tf\n'
'g\th\n'
'i\tj\n'
'k\tl\n'
'm\tn\n'
'o\tp\n',
)
def test_deeply_nested_with_identifier(self):
self.assert_text_renders_to(
{'foo': [['a', 'b', ['c', 'd']], ['e', 'f', ['g', 'h']]]},
'FOO\ta\n'
'FOO\tb\n'
'FOO\tc\n'
'FOO\td\n'
'FOO\te\n'
'FOO\tf\n'
'FOO\tg\n'
'FOO\th\n',
)
if __name__ == '__main__':
unittest.main()
|