1+ """
2+ SoftLayer.tests.CLI.formatting_table_tests
3+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+ :license: MIT, see LICENSE for more details.
6+ """
7+ import json
8+ import os
9+ import sys
10+ import tempfile
11+
12+ import click
13+ from rich .table import Table
14+ from unittest import mock as mock
15+
16+ from SoftLayer .CLI import core
17+ from SoftLayer .CLI import exceptions
18+ from SoftLayer .CLI import formatting
19+ from SoftLayer .CLI import helpers
20+ from SoftLayer .CLI import template
21+ from SoftLayer import testing
22+
23+ class TestTable (testing .TestCase ):
24+
25+ def test_table_with_duplicated_columns (self ):
26+ self .assertRaises (exceptions .CLIHalt , formatting .Table , ['col' , 'col' ])
27+
28+ def test_boolean_table (self ):
29+ table = formatting .Table (["column1" ], title = "Test Title" )
30+ self .assertFalse (table )
31+ table .add_row (["entry1" ])
32+ self .assertTrue (table )
33+
34+
35+ class IterToTableTests (testing .TestCase ):
36+
37+ def test_format_api_dict (self ):
38+ result = formatting ._format_dict ({'key' : 'value' })
39+
40+ self .assertIsInstance (result , formatting .Table )
41+ self .assertEqual (result .columns , ['name' , 'value' ])
42+ self .assertEqual (result .rows , [['key' , 'value' ]])
43+
44+ def test_format_api_list (self ):
45+ result = formatting ._format_list ([{'key' : 'value' }])
46+
47+ self .assertIsInstance (result , formatting .Table )
48+ self .assertEqual (result .columns , ['key' ])
49+ self .assertEqual (result .rows , [['value' ]])
50+
51+ def test_format_api_list_non_objects (self ):
52+ result = formatting ._format_list (['a' , 'b' , 'c' ])
53+
54+ self .assertIsInstance (result , formatting .Table )
55+ self .assertEqual (result .columns , ['value' ])
56+ self .assertEqual (result .rows , [['a' ], ['b' ], ['c' ]])
57+
58+ def test_format_api_list_with_none_value (self ):
59+ result = formatting ._format_list ([{'key' : [None , 'value' ]}, None ])
60+
61+ self .assertIsInstance (result , formatting .Table )
62+ self .assertEqual (result .columns , ['key' ])
63+
64+ def test_format_api_list_with_empty_array (self ):
65+ result = formatting .iter_to_table ([{'id' : 130224450 , 'activeTickets' : []}])
66+ self .assertIsInstance (result , formatting .Table )
67+ self .assertIn ('id' , result .columns )
68+ self .assertIn ('activeTickets' , result .columns )
69+ formatted = formatting .format_output (result , "table" )
70+ # No good ways to test whats actually in a Rich.Table without going through the hassel of
71+ # printing it out. As long as this didn't throw and exception it should be fine.
72+ self .assertEqual (formatted .row_count , 1 )
0 commit comments