forked from IBM-Cloud/logistics-wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_distribution_centers_service.py
196 lines (152 loc) · 8.93 KB
/
test_distribution_centers_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
import unittest
from json import loads
from types import IntType
import server.tests.utils as utils
import server.services.users as user_service
import server.services.distribution_centers as distribution_center_service
from server.exceptions import (AuthenticationException,
ResourceDoesNotExistException)
def suite():
test_suite = unittest.TestSuite()
test_suite.addTest(GetDistributionCentersTestCase('test_get_distribution_centers_success'))
test_suite.addTest(GetDistributionCentersTestCase('test_get_distribution_centers_invalid_token'))
test_suite.addTest(GetDistributionCenterTestCase('test_get_distribution_center_success'))
test_suite.addTest(GetDistributionCenterTestCase('test_get_distribution_center_invalid_input'))
test_suite.addTest(GetDistributionCenterTestCase('test_get_distribution_center_invalid_token'))
test_suite.addTest(GetDistributionCenterInventoryTestCase('test_get_distribution_center_inventory_success'))
test_suite.addTest(GetDistributionCenterInventoryTestCase('test_get_distribution_center_inventory_invalid_input'))
test_suite.addTest(GetDistributionCenterInventoryTestCase('test_get_distribution_center_inventory_invalid_token'))
return test_suite
###########################
# Unit Tests #
###########################
class GetDistributionCentersTestCase(unittest.TestCase):
"""Tests for `services/distribution_centers.py - get_distribution_centers()`."""
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_get_distribution_centers_success(self):
"""With correct values, are valid distribution centers returned?"""
# Get distribution centers
distribution_centers = distribution_center_service.get_distribution_centers(self.loopback_token)
# TODO: Update to use assertIsInstance(a,b)
# Check all expected object values are present
distribution_centers_json = loads(distribution_centers)
# Check that the distribution centers are valid
for distribution_center_json in distribution_centers_json:
self.assertTrue(distribution_center_json.get('id'))
# Check that distribution center address is valid, if present
if distribution_center_json.get('address'):
self.assertTrue(distribution_center_json.get('address').get('city'))
self.assertTrue(distribution_center_json.get('address').get('state'))
self.assertTrue(distribution_center_json.get('address').get('country'))
self.assertTrue(distribution_center_json.get('address').get('latitude'))
self.assertTrue(distribution_center_json.get('address').get('longitude'))
# Check that distribution center contact is valid, if present
if distribution_center_json.get('contact'):
self.assertTrue(distribution_center_json.get('contact').get('name'))
def test_get_distribution_centers_invalid_token(self):
"""With an invalid token, are correct errors thrown?"""
self.assertRaises(AuthenticationException,
distribution_center_service.get_distribution_centers,
utils.get_bad_token())
def tearDown(self):
utils.delete_demo(loads(self.demo).get('guid'))
class GetDistributionCenterTestCase(unittest.TestCase):
"""Tests for `services/distribution_centers.py - get_distribution_center()`."""
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_get_distribution_center_success(self):
"""With correct values, is a valid distribution center returned?"""
# Get distribution center
distribution_centers = distribution_center_service.get_distribution_centers(self.loopback_token)
dc_id = loads(distribution_centers)[0].get('id')
distribution_center = distribution_center_service.get_distribution_center(self.loopback_token, dc_id)
# TODO: Update to use assertIsInstance(a,b)
# Check all expected object values are present
distribution_center_json = loads(distribution_center)
# Check that the distribution center is valid
self.assertTrue(distribution_center_json.get('id'))
# Check that distribution center address is valid, if present
if distribution_center_json.get('address'):
self.assertTrue(distribution_center_json.get('address').get('city'))
self.assertTrue(distribution_center_json.get('address').get('state'))
self.assertTrue(distribution_center_json.get('address').get('country'))
self.assertTrue(distribution_center_json.get('address').get('latitude'))
self.assertTrue(distribution_center_json.get('address').get('longitude'))
# Check that distribution center contact is valid, if present
if distribution_center_json.get('contact'):
self.assertTrue(distribution_center_json.get('contact').get('name'))
def test_get_distribution_center_invalid_input(self):
"""With invalid inputs, are correct errors thrown?"""
self.assertRaises(ResourceDoesNotExistException,
distribution_center_service.get_distribution_center,
self.loopback_token, '123321')
def test_get_distribution_center_invalid_token(self):
"""With an invalid token, are correct errors thrown?"""
# Get distribution centers
distribution_centers = distribution_center_service.get_distribution_centers(self.loopback_token)
dc_id = loads(distribution_centers)[0].get('id')
# Attempt to get a distribution center with invalid token
self.assertRaises(AuthenticationException,
distribution_center_service.get_distribution_center,
utils.get_bad_token(), dc_id)
def tearDown(self):
utils.delete_demo(loads(self.demo).get('guid'))
class GetDistributionCenterInventoryTestCase(unittest.TestCase):
"""Tests for `services/distribution_centers.py - get_distribution_center_inventory()`."""
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_get_distribution_center_inventory_success(self):
"""With correct values, is valid inventory returned?"""
# Get distribution center
distribution_centers = distribution_center_service.get_distribution_centers(self.loopback_token)
dc_id = loads(distribution_centers)[0].get('id')
inventory = distribution_center_service.get_distribution_center_inventory(self.loopback_token, dc_id)
# TODO: Update to use assertIsInstance(a,b)
# Check all expected object values are present
inventories_json = loads(inventory)
for inventory_json in inventories_json:
self.assertTrue(inventory_json.get('id'))
self.assertIsInstance(inventory_json.get('quantity'), IntType)
self.assertTrue(inventory_json.get('productId'))
self.assertTrue(inventory_json.get('locationId'))
self.assertTrue(inventory_json.get('locationType'))
def test_get_distribution_center_inventory_invalid_input(self):
"""With invalid inputs, are correct errors thrown?"""
self.assertRaises(ResourceDoesNotExistException,
distribution_center_service.get_distribution_center_inventory,
self.loopback_token, '123321')
def test_get_distribution_center_inventory_invalid_token(self):
"""With an invalid token, are correct errors thrown?"""
# Get distribution centers
distribution_centers = distribution_center_service.get_distribution_centers(self.loopback_token)
dc_id = loads(distribution_centers)[0].get('id')
# Attempt to get retailer inventory with invalid token
self.assertRaises(AuthenticationException,
distribution_center_service.get_distribution_center_inventory,
utils.get_bad_token(), dc_id)
def tearDown(self):
utils.delete_demo(loads(self.demo).get('guid'))
if __name__ == '__main__':
unittest.main()