Skip to content

Commit b7fd730

Browse files
fixing testing issues related to new tables
1 parent 1238377 commit b7fd730

File tree

6 files changed

+37
-26
lines changed

6 files changed

+37
-26
lines changed

SoftLayer/CLI/call_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def cli(env, service, method, parameters, _id, _filters, mask, limit, offset, or
172172
}
173173

174174
if output_python:
175-
env.out(_build_python_example(args, kwargs))
175+
env.python_output(_build_python_example(args, kwargs))
176176
else:
177177
result = env.client.call(*args, **kwargs)
178178
env.fout(formatting.iter_to_table(result))

SoftLayer/CLI/environment.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import click
1111
import pkg_resources
1212
from rich.console import Console
13+
from rich.syntax import Syntax
1314

1415
import SoftLayer
1516
from SoftLayer.CLI import formatting
@@ -33,6 +34,7 @@ def __init__(self):
3334
self.vars = {}
3435

3536
self.client = None
37+
self.console = Console()
3638
self.format = 'table'
3739
self.skip_confirmations = False
3840
self.config_file = None
@@ -41,16 +43,19 @@ def __init__(self):
4143

4244
def out(self, output):
4345
"""Outputs a string to the console (stdout)."""
44-
console = Console()
45-
if self.format in ('json', 'jsonraw'):
46-
console.print_json(output)
46+
if self.format == 'json':
47+
self.console.print_json(output)
48+
elif self.format == 'jsonraw':
49+
# Using Rich here is problematic because in the unit tests it thinks the terminal is 80 characters wide
50+
# and only prints out that many characters.
51+
click.echo(output)
4752
else:
4853
# If we want to print a list of tables, Rich doens't handle that well.
4954
if isinstance(output, list):
5055
for line in output:
51-
console.print(line)
56+
self.console.print(line)
5257
else:
53-
console.print(output)
58+
self.console.print(output)
5459

5560
def err(self, output, newline=True):
5661
"""Outputs an error string to the console (stderr)."""
@@ -75,6 +80,10 @@ def fout(self, output):
7580
# If we hit an undecodeable entry, just try outputting as json.
7681
self.out(self.fmt(output, 'json'))
7782

83+
def python_output(self, output):
84+
"""Prints out python code"""
85+
self.console.print(Syntax(output, "python"))
86+
7887
def input(self, prompt, default=None, show_default=True):
7988
"""Provide a command prompt."""
8089
return click.prompt(prompt, default=default, show_default=show_default)

SoftLayer/CLI/formatting.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912
2525
:param data: One of: String, Table, FormattedItem, List, Tuple, SequentialOutput
2626
:param string fmt (optional): One of: table, raw, json, python
2727
"""
28-
if fmt in ('json', 'jsonraw'):
28+
if fmt == 'json':
2929
return json.dumps(data, indent=4, cls=CLIJSONEncoder)
30+
elif fmt == 'jsonraw':
31+
return json.dumps(data, cls=CLIJSONEncoder)
3032

3133
if isinstance(data, str) or isinstance(data, rTable):
3234
return data
@@ -65,7 +67,7 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912
6567
return output
6668

6769
# fallback, convert this odd object to a string
68-
print(f"Casting this to string {data}")
70+
# print(f"Casting this to string {data}")
6971
return str(data)
7072

7173

tests/CLI/modules/account_tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ def test_event_jsonraw_output(self):
4444
command = '--format jsonraw account events'
4545
command_params = command.split()
4646
result = self.run_command(command_params)
47+
4748
json_text_tables = result.stdout.split('\n')
49+
print("RESULT: {}".format(result.output))
4850
# removing an extra item due to an additional Newline at the end of the output
4951
json_text_tables.pop()
5052
# each item in the json_text_tables should be a list
5153
for json_text_table in json_text_tables:
54+
print("TESTING THIS: \n{}\n".format(json_text_table))
5255
json_table = json.loads(json_text_table)
5356
self.assertIsInstance(json_table, list)
5457

tests/CLI/modules/block_tests.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,11 @@ def test_volume_list_reduced_notes_format_output_table(self, list_mock):
166166
list_mock.return_value = [
167167
{'notes': note_mock}
168168
]
169-
expected_table = formatting.Table(['notes'])
170-
expected_table.add_row([expected_reduced_note])
171-
expected_output = formatting.format_output(expected_table)+'\n'
172169
result = self.run_command(['--format', 'table', 'block', 'volume-list', '--columns', 'notes'])
173170

174171
self.assert_no_fail(result)
175-
self.assertEqual(expected_output, result.output)
172+
self.assertIn(expected_reduced_note, result.output)
173+
self.assertNotIn(note_mock, result.output)
176174

177175
def test_volume_list_order(self):
178176
result = self.run_command(['block', 'volume-list', '--order=1234567'])

tests/CLI/modules/call_api_tests.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ def test_python_output(self):
9393
'--id=100',
9494
'-f nested.property=5432',
9595
'--output-python'])
96-
96+
print("OUTPUT: \n{}".format(result.exception))
9797
self.assert_no_fail(result)
98+
9899
self.assertIsNotNone(result.output, """import SoftLayer
99100
100101
client = SoftLayer.create_client_from_env()
@@ -162,14 +163,12 @@ def test_object_table(self):
162163

163164
self.assert_no_fail(result)
164165
# NOTE(kmcdonald): Order is not guaranteed
165-
self.assertIn(":........:........:", result.output)
166-
self.assertIn(": name : value :", result.output)
167-
self.assertIn(": int : 10 :", result.output)
168-
self.assertIn(": None : None :", result.output)
169-
self.assertIn(": float : 1.0 :", result.output)
170-
self.assertIn(": Bool : True :", result.output)
171-
self.assertIn(": string : string :", result.output)
172-
self.assertIn(":........:........:", result.output)
166+
self.assertIn("│ name │ value │", result.output)
167+
self.assertIn("│ int │ 10 │", result.output)
168+
self.assertIn("│ None │ None │", result.output)
169+
self.assertIn("│ float │ 1.0 │", result.output)
170+
self.assertIn("│ Bool │ True │", result.output)
171+
self.assertIn("│ string │ string │", result.output)
173172

174173
def test_object_nested(self):
175174
mock = self.set_mock('SoftLayer_Service', 'method')
@@ -210,11 +209,11 @@ def test_list_table(self):
210209

211210
self.assert_no_fail(result)
212211
self.assertEqual(result.output,
213-
""":......:......:.......:.....:........:
214-
: Bool : None : float : int : string :
215-
:......:......:.......:.....:........:
216-
: True : None : 1.0 : 10 : string :
217-
:......:......:.......:.....:........:
212+
"""┌──────┬──────┬───────┬─────┬────────┐
213+
Bool None float int string
214+
├──────┼──────┼───────┼─────┼────────┤
215+
True None 1.0 10 string
216+
└──────┴──────┴───────┴─────┴────────┘
218217
""")
219218

220219
def test_parameters(self):

0 commit comments

Comments
 (0)