forked from IBM-Cloud/logistics-wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_demos_service.py
238 lines (193 loc) · 10 KB
/
test_demos_service.py
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import unittest
from datetime import datetime
from json import loads
from multiprocessing import Pool
import server.tests.utils as utils
import server.services.demos as demo_service
import server.services.users as user_service
import server.services.shipments as shipment_service
import server.services.distribution_centers as distribution_center_service
import server.services.retailers as retailer_service
from server.utils import async_helper
from server.exceptions import (UnprocessableEntityException,
ResourceDoesNotExistException)
def suite():
test_suite = unittest.TestSuite()
test_suite.addTest(CreateDemoTestCase('test_demo_create_success'))
test_suite.addTest(CreateDemoTestCase('test_demo_create_email'))
test_suite.addTest(RetrieveDemoTestCase('test_demo_retrieve_success'))
test_suite.addTest(RetrieveDemoTestCase('test_demo_retrieve_invalid_input'))
test_suite.addTest(RetrieveDemoTestCase('test_demo_retrieve_retailers_success'))
test_suite.addTest(RetrieveDemoTestCase('test_admin_data_async_success'))
test_suite.addTest(DeleteDemoTestCase('test_demo_delete_success'))
test_suite.addTest(DeleteDemoTestCase('test_demo_delete_invalid_input'))
return test_suite
###########################
# Unit Tests #
###########################
class CreateDemoTestCase(unittest.TestCase):
"""Tests for `services/demos.py - create_demo()`."""
def test_demo_create_success(self):
"""With correct values, is a valid demo returned?"""
# Create demo
demo_name = datetime.now().isoformat("T")
demo = demo_service.create_demo(demo_name)
# TODO: Update to use assertIsInstance(a,b)
# Check all expected object values are present
demo_json = loads(demo)
self.assertTrue(demo_json.get('id'))
self.assertTrue(demo_json.get('guid'))
self.assertTrue(demo_json.get('name') == demo_name)
self.assertTrue(demo_json.get('createdAt'))
self.assertTrue(demo_json.get('users'))
# Check that the default supplychainmanager user was created
created_user_json = demo_json.get('users')[0]
self.assertTrue(created_user_json.get('id'))
self.assertTrue(created_user_json.get('demoId'))
self.assertTrue(created_user_json.get('username'))
self.assertTrue(created_user_json.get('email'))
self.assertTrue(created_user_json.get('roles'))
# Check that the proper role was created
scm_role_json = created_user_json.get('roles')[0]
self.assertTrue(scm_role_json.get('id'))
self.assertTrue(scm_role_json.get('name') == "supplychainmanager")
self.assertTrue(scm_role_json.get('created'))
self.assertTrue(scm_role_json.get('modified'))
# Destroy demo
demo_service.delete_demo_by_guid(demo_json.get('guid'))
def test_demo_create_email(self):
"""Is an invalid email detected correctly?"""
# Test invalid email throws ValidationException
demo_name = datetime.now().isoformat("T")
invalid_email = "email@[email protected]"
self.assertRaises(UnprocessableEntityException,
demo_service.create_demo,
demo_name, invalid_email)
# Test valid email completes
demo_name = datetime.now().isoformat("T")
valid_email = "[email protected]"
demo = demo_service.create_demo(demo_name, valid_email)
self.assertTrue(loads(demo).get('id'))
# Destroy demo
demo_service.delete_demo_by_guid(loads(demo).get('guid'))
class RetrieveDemoTestCase(unittest.TestCase):
"""Tests for `services/demos.py - get_demo_by_guid(), get_demo_retailers()`.
Tests for `web/utils.py - async_helper()`."""
def setUp(self):
# Create demo
self.demo = utils.create_demo()
demo_json = loads(self.demo)
demo_guid = demo_json.get('guid')
demo_user_id = demo_json.get('users')[0].get('id')
# Log in user
auth_data = user_service.login(demo_guid, demo_user_id)
self.loopback_token = auth_data.get('loopback_token')
def test_demo_retrieve_success(self):
"""With correct values, is a valid demo returned?"""
# Retrieve demo
retrieved_demo = demo_service.get_demo_by_guid(loads(self.demo).get('guid'))
# TODO: Update to use assertIsInstance(a,b)
# Check all expected object values are present
created_demo_json = loads(self.demo)
demo_json = loads(retrieved_demo)
self.assertTrue(demo_json.get('id') == created_demo_json.get('id'))
self.assertTrue(demo_json.get('guid') == created_demo_json.get('guid'))
self.assertTrue(demo_json.get('name') == created_demo_json.get('name'))
self.assertTrue(demo_json.get('createdAt') == created_demo_json.get('createdAt'))
self.assertTrue(demo_json.get('users'))
# Check that the users are valid
for user_json in demo_json.get('users'):
self.assertTrue(user_json.get('id'))
self.assertTrue(user_json.get('demoId'))
self.assertTrue(user_json.get('username'))
self.assertTrue(user_json.get('email'))
# Check that user roles are valid, if present
if user_json.get('roles'):
for role_json in user_json.get('roles'):
self.assertTrue(role_json.get('id'))
self.assertTrue(role_json.get('name'))
self.assertTrue(role_json.get('created'))
self.assertTrue(role_json.get('modified'))
def test_demo_retrieve_invalid_input(self):
"""With invalid guid, is correct error thrown?"""
self.assertRaises(ResourceDoesNotExistException,
demo_service.get_demo_by_guid,
'ABC123')
def test_demo_retrieve_retailers_success(self):
"""With correct values, are valid demo retailers returned?"""
# Retrieve demo retailers
demo_guid = loads(self.demo).get('guid')
retailers = demo_service.get_demo_retailers(demo_guid)
retailers_json = loads(retailers)
# TODO: Update to use assertIsInstance(a,b)
# Check that the retailers are valid
for retailer_json in retailers_json:
self.assertTrue(retailer_json.get('id'))
self.assertTrue(retailer_json.get('address'))
address_json = retailer_json.get('address')
self.assertTrue(address_json.get('city'))
self.assertTrue(address_json.get('state'))
self.assertTrue(address_json.get('country'))
self.assertTrue(address_json.get('latitude'))
self.assertTrue(address_json.get('longitude'))
def test_admin_data_async_success(self):
"""With correct values, is valid data returned asynchronously?"""
# Specify functions and their corresponding arguments to be called
erp_calls = [(shipment_service.get_shipments, self.loopback_token),
(distribution_center_service.get_distribution_centers, self.loopback_token),
(retailer_service.get_retailers, self.loopback_token)]
pool = Pool(processes=len(erp_calls))
# Asynchronously make calls and then wait on all processes to finish
results = pool.map(async_helper, erp_calls)
pool.close()
pool.join()
# Check that the shipment is valid
shipment = loads(results[0])[0]
self.assertTrue(shipment.get('id'))
self.assertTrue(shipment.get('status'))
self.assertTrue(shipment.get('createdAt'))
self.assertTrue(shipment.get('estimatedTimeOfArrival'))
self.assertTrue(shipment.get('fromId'))
self.assertTrue(shipment.get('toId'))
if shipment.get('currentLocation'):
self.assertTrue(shipment.get('currentLocation').get('city'))
self.assertTrue(shipment.get('currentLocation').get('state'))
self.assertTrue(shipment.get('currentLocation').get('country'))
self.assertTrue(shipment.get('currentLocation').get('latitude'))
self.assertTrue(shipment.get('currentLocation').get('longitude'))
# Check that the retailer is valid
retailer = loads(results[1])[0]
self.assertTrue(retailer.get('id'))
if retailer.get('address'):
self.assertTrue(retailer.get('address').get('city'))
self.assertTrue(retailer.get('address').get('state'))
self.assertTrue(retailer.get('address').get('country'))
self.assertTrue(retailer.get('address').get('latitude'))
self.assertTrue(retailer.get('address').get('longitude'))
# Check that the distribution center is valid
distribution_center = loads(results[2])[0]
self.assertTrue(distribution_center.get('id'))
if distribution_center.get('address'):
self.assertTrue(distribution_center.get('address').get('city'))
self.assertTrue(distribution_center.get('address').get('state'))
self.assertTrue(distribution_center.get('address').get('country'))
self.assertTrue(distribution_center.get('address').get('latitude'))
self.assertTrue(distribution_center.get('address').get('longitude'))
def tearDown(self):
utils.delete_demo(loads(self.demo).get('guid'))
class DeleteDemoTestCase(unittest.TestCase):
"""Tests for `services/demos.py - delete_demo_by_guid()`."""
def setUp(self):
# Create demo
self.demo = utils.create_demo()
def test_demo_delete_success(self):
"""With correct values, is a valid demo deleted?"""
self.assertTrue(demo_service.delete_demo_by_guid(loads(self.demo).get('guid')) is None)
def test_demo_delete_invalid_input(self):
"""With invalid guid, is correct error thrown?"""
# Attempt to delete demo with invalid guid
self.assertRaises(ResourceDoesNotExistException,
demo_service.delete_demo_by_guid,
'ABC123')
if __name__ == '__main__':
unittest.main()