Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/test_table.py
1566 views
1
# Copyright (c) 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved
2
#
3
# Permission is hereby granted, free of charge, to any person obtaining a
4
# copy of this software and associated documentation files (the
5
# "Software"), to deal in the Software without restriction, including
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
8
# persons to whom the Software is furnished to do so, subject to the fol-
9
# lowing conditions:
10
#
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
13
#
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
# IN THE SOFTWARE.
21
#
22
import unittest
23
24
from awscli.table import Section, MultiTable, convert_to_vertical_table
25
26
27
class TestSection(unittest.TestCase):
28
def setUp(self):
29
self.section = Section()
30
31
def test_add_row_tracks_max_widths(self):
32
self.section.add_row(['one', 'two', 'three'])
33
self.assertEqual(self.section.calculate_column_widths(), [3, 3, 5])
34
self.section.add_row(['1234567', '1234567', '1234567'])
35
self.assertEqual(self.section.calculate_column_widths(), [7, 7, 7])
36
self.section.add_row(['a', 'a', 'a'])
37
self.assertEqual(self.section.calculate_column_widths(), [7, 7, 7])
38
self.section.add_row(['123456789', '1', '1'])
39
self.assertEqual(self.section.calculate_column_widths(), [9, 7, 7])
40
self.assertEqual(self.section.calculate_column_widths(1), [10, 8, 8])
41
self.assertEqual(self.section.total_width(), 23)
42
43
def test_add_row_also_tracks_header(self):
44
self.section.add_header(['123456789', '12345', '1234567'])
45
self.section.add_row(['a', 'a', 'a'])
46
self.section.add_row(['aa', 'aa', 'aa'])
47
self.section.add_row(['aaa', 'aaa', 'aaa'])
48
self.assertEqual(self.section.calculate_column_widths(), [9, 5, 7])
49
self.assertEqual(self.section.total_width(), 21)
50
self.section.add_row(['aaa', '123456789', 'aaa'])
51
self.assertEqual(self.section.calculate_column_widths(), [9, 9, 7])
52
self.assertEqual(self.section.total_width(padding=3, with_border=True),
53
36)
54
55
def test_max_width_with_scaling_perfect_scaling(self):
56
self.section.add_row(['one', 'two', 'three'])
57
self.section.add_row(['1234567', '1234567', '1234567'])
58
# Perfect scaling, exactly double.
59
widths = self.section.calculate_column_widths(max_width=42)
60
self.assertEqual(widths, [14, 14, 14])
61
62
def test_max_width_scaling_one_unit_short(self):
63
self.section.add_row(['one', 'two', 'three'])
64
self.section.add_row(['1234567', '1234567', '1234567'])
65
widths = self.section.calculate_column_widths(max_width=41)
66
self.assertEqual(widths, [13, 14, 14])
67
68
def test_width_with_full_width_characters(self):
69
self.section.add_row([u'\u4e00', u'one'])
70
self.assertEqual(self.section.calculate_column_widths(), [2, 3])
71
72
def test_max_width_scaling_is_negative(self):
73
self.section.add_row(['12345', '12345'])
74
widths = self.section.calculate_column_widths(max_width=17)
75
self.assertEqual(widths, [8, 9])
76
77
def test_allow_sections_to_be_padded(self):
78
self.section.add_row(['one', 'two', 'three'])
79
self.section.add_row(['1234567', '1234567', '1234567'])
80
self.assertEqual(self.section.total_width(padding=2), 27)
81
self.assertEqual(
82
self.section.total_width(padding=2, outer_padding=1), 29)
83
84
def test_title_accounts_for_outer_padding(self):
85
self.section.add_row(['a', 'b', 'c'])
86
self.section.add_title('123456789')
87
self.assertEqual(
88
self.section.total_width(padding=2, outer_padding=3), 17)
89
90
def test_unicode_text_row(self):
91
self.section.add_row([1])
92
self.section.add_row(['check'])
93
self.section.add_row([u'\u2713'])
94
self.assertEqual(
95
self.section.rows,
96
[[u'1'], [u'check'], [u'\u2713']])
97
98
99
class TestMultiTable(unittest.TestCase):
100
def setUp(self):
101
self.table = MultiTable()
102
103
def test_max_width_calculation(self):
104
self.table.add_title('foo')
105
self.table.add_row_header(['one', 'two', 'three'])
106
self.table.add_row(['one', 'two', 'three'])
107
self.table.new_section('bar')
108
self.table.add_row_header(['one', 'two'])
109
self.table.add_row(['12345', '1234567'])
110
111
112
class TestVerticalTableConversion(unittest.TestCase):
113
def setUp(self):
114
self.table = MultiTable()
115
116
def test_convert_section_to_vertical(self):
117
self.table.add_title('foo')
118
self.table.add_row_header(['key1', 'key2', 'key3'])
119
self.table.add_row(['val1', 'val2', 'val3'])
120
convert_to_vertical_table(self.table._sections)
121
# To convert to a vertical table, there should be no headers:
122
section = self.table._sections[0]
123
self.assertEqual(section.headers, [])
124
# Then we should create a two column row with key val pairs.
125
self.assertEqual(
126
section.rows,
127
[['key1', 'val1'], ['key2', 'val2'], ['key3', 'val3']])
128
129
130
if __name__ == '__main__':
131
unittest.main()
132
133