forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_api_tests.py
More file actions
155 lines (122 loc) · 5.71 KB
/
call_api_tests.py
File metadata and controls
155 lines (122 loc) · 5.71 KB
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
"""
SoftLayer.tests.CLI.modules.call_api_tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:license: MIT, see LICENSE for more details.
"""
from SoftLayer.CLI import call_api
from SoftLayer.CLI import formatting
from SoftLayer import testing
import json
class CallCliTests(testing.TestCase):
def test_options(self):
mock = self.set_mock('SoftLayer_Service', 'method')
mock.return_value = 'test'
result = self.run_command(['call-api', 'Service', 'method',
'--mask=some.mask',
'--limit=20',
'--offset=40',
'--id=100'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(json.loads(result.output), 'test')
self.assert_called_with('SoftLayer_Service', 'method',
mask='mask[some.mask]',
limit=20,
offset=40,
identifier='100')
def test_object(self):
mock = self.set_mock('SoftLayer_Service', 'method')
mock.return_value = {'string': 'string',
'int': 10,
'float': 1.0,
'None': None,
'Bool': True}
result = self.run_command(['call-api', 'Service', 'method'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(json.loads(result.output),
{'string': 'string',
'int': 10,
'float': 1.0,
'None': None,
'Bool': True})
def test_object_table(self):
mock = self.set_mock('SoftLayer_Service', 'method')
mock.return_value = {'string': 'string',
'int': 10,
'float': 1.0,
'None': None,
'Bool': True}
result = self.run_command(['call-api', 'Service', 'method'],
fmt='table')
self.assertEqual(result.exit_code, 0)
# NOTE(kmcdonald): Order is not guaranteed
self.assertIn(":........:........:", result.output)
self.assertIn(": Name : Value :", result.output)
self.assertIn(": int : 10 :", result.output)
self.assertIn(": None : None :", result.output)
self.assertIn(": float : 1.0 :", result.output)
self.assertIn(": Bool : True :", result.output)
self.assertIn(": string : string :", result.output)
self.assertIn(":........:........:", result.output)
def test_object_nested(self):
mock = self.set_mock('SoftLayer_Service', 'method')
mock.return_value = {'this': {'is': [{'pretty': 'nested'}]}}
result = self.run_command(['call-api', 'Service', 'method'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(json.loads(result.output),
{'this': {'is': [{'pretty': 'nested'}]}})
def test_list(self):
mock = self.set_mock('SoftLayer_Service', 'method')
mock.return_value = [{'string': 'string',
'int': 10,
'float': 1.0,
'None': None,
'Bool': True}]
result = self.run_command(['call-api', 'Service', 'method'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(json.loads(result.output),
[{'string': 'string',
'int': 10,
'float': 1.0,
'None': None,
'Bool': True}])
def test_list_table(self):
mock = self.set_mock('SoftLayer_Service', 'method')
mock.return_value = [{'string': 'string',
'int': 10,
'float': 1.0,
'None': None,
'Bool': True}]
result = self.run_command(['call-api', 'Service', 'method'],
fmt='table')
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output,
""":......:......:.......:.....:........:
: Bool : None : float : int : string :
:......:......:.......:.....:........:
: True : None : 1.0 : 10 : string :
:......:......:.......:.....:........:
""")
def test_parameters(self):
mock = self.set_mock('SoftLayer_Service', 'method')
mock.return_value = {}
result = self.run_command(['call-api', 'Service', 'method',
'arg1', '1234'])
self.assertEqual(result.exit_code, 0)
self.assert_called_with('SoftLayer_Service', 'method',
args=('arg1', '1234'))
class CallCliHelperTests(testing.TestCase):
def test_format_api_dict(self):
result = call_api.format_api_dict({'key': 'value'})
self.assertIsInstance(result, formatting.Table)
self.assertEqual(result.columns, ['Name', 'Value'])
self.assertEqual(result.rows, [['key', 'value']])
def test_format_api_list(self):
result = call_api.format_api_list([{'key': 'value'}])
self.assertIsInstance(result, formatting.Table)
self.assertEqual(result.columns, ['key'])
self.assertEqual(result.rows, [['value']])
def test_format_api_list_non_objects(self):
result = call_api.format_api_list(['a', 'b', 'c'])
self.assertIsInstance(result, formatting.Table)
self.assertEqual(result.columns, ['Value'])
self.assertEqual(result.rows, [['a'], ['b'], ['c']])