-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app_data.py
191 lines (129 loc) · 6.45 KB
/
test_app_data.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
from models.route import Route
from models.package import Package
from models.truck import Truck
from models.customer import Customer
from core.app_data import AppData
from datetime import datetime, timedelta
import unittest
customer_1 = Customer('Gosho', 'Goshev', '[email protected]')
truck_1 = Truck(1, 'Scania', 42000, 8000)
route_1 = Route(1, [909, 877], 0, datetime.now(), truck_1, [], ['BRI', 'SYD', 'MEL'])
package_1 = Package(1, 'BRI', 'SYD', 50, customer_1, None, None)
#TODO - add more unit tests
class AppData_Should(unittest.TestCase):
def test_properties_returnTuple(self):
appdata = AppData()
self.assertEqual(tuple, type(appdata.trucks))
self.assertEqual(tuple, type(appdata.routes))
self.assertEqual(tuple, type(appdata.packages))
def test_addPackageFunction_whenParameterIsValid(self):
appdata = AppData()
appdata.add_package(package_1)
added_package = appdata.packages[0]
self.assertEqual(added_package, package_1)
def test_addPackageFunction_raisesValueErrorWhenParameterIsNotInstanceOfPackage(self):
appdata = AppData()
with self.assertRaises(ValueError):
package = 'hey'
appdata.add_package(package)
def test_addRouteFunction_whenParameterIsValid(self):
appdata = AppData()
appdata.add_route(route_1)
added_route = appdata.routes[0]
self.assertEqual(added_route, route_1)
def test_addRouteFunction_raisesValueErrorWhenParameterIsNotInstanceOfPackage(self):
appdata = AppData()
with self.assertRaises(ValueError):
route = 'hey'
appdata.add_route(route)
def test_addTruckFunction_whenParameterIsValid(self):
appdata = AppData()
appdata.add_truck(truck_1)
added_truck = appdata.trucks[0]
self.assertEqual(added_truck, truck_1)
def test_addTruckFunction_raisesValueErrorWhenParameterIsNotInstanceOfPackage(self):
with self.assertRaises(ValueError):
appdata = AppData()
truck = 'hey'
appdata.add_truck(truck)
def test_findSuitableTruckFunction_whenAllParametersAreValid(self):
# add truck to appdata.trucks collection
appdata = AppData()
appdata.add_truck(truck_1)
# find a suitable truck from appdata.trucks collection
found_truck = appdata.find_suitable_truck(1, 2, datetime.now(), datetime.now() + timedelta(1))
self.assertEqual(truck_1, found_truck)
def test_findSuitableTruckFunction_raiseValueErrorWhenNotFoundSuitableTruck(self):
# appdata.trucks collection is empty
with self.assertRaises(ValueError):
appdata = AppData()
start_time = datetime.now()
end_time = datetime.now() + timedelta(1)
appdata.find_suitable_truck(1, 22, start_time, end_time)
def test_assignPackageToRouteFunction_whenParameterIsValid(self):
appdata = AppData()
appdata.add_package(package_1)
appdata.add_route(route_1)
appdata.add_truck(truck_1)
result_str = appdata.assign_package_to_route(1)
self.assertEqual(True, package_1.is_assigned)
self.assertEqual(result_str, appdata.assign_package_to_route(1))
def test_assignPackageToRouteFunction_whenPackageIdIsNotvalid(self):
appdata = AppData()
invalid_id_assign_package = appdata.assign_package_to_route(-1)
result_str = 'No package with such ID!'
self.assertEqual(result_str, invalid_id_assign_package)
def test_assignPackageToRouteFunction_whenRouteNotExist(self):
appdata = AppData()
appdata.add_package(package_1)
result_str = 'No suitable route for package with ID [1]! The package is in pending mode!'
self.assertEqual(result_str, appdata.assign_package_to_route(1))
def test_getRouteByIdFunction_whenParameterIdIsValid(self):
appdata = AppData()
appdata.add_route(route_1)
self.assertEqual(route_1, appdata.get_route_by_id(1))
def test_getRouteByIdFunction_returnNonewhenParameterIdIsNotValid(self):
appdata = AppData()
appdata.add_route(route_1)
self.assertEqual(None, appdata.get_route_by_id(-1))
def test_getPackageByIdFunction_whenParameterIdIsValid(self):
appdata = AppData()
appdata.add_package(package_1)
self.assertEqual(package_1, appdata.get_package_by_id(1))
def test_getPackageByIdFunction_returnNonewhenParameterIdIsNotValid(self):
appdata = AppData()
appdata.add_package(package_1)
self.assertEqual(None, appdata.get_package_by_id(-1))
def test_findSuitableRouteFunction_whenAllParametersAreValid(self):
appdata = AppData()
appdata.add_route(route_1)
suitable_route = appdata.find_suitable_route('BRI', 'MEL')
self.assertEqual(route_1, suitable_route)
def test_findSuitableRouteFunction_whenAllParametersAreNotValid(self):
appdata = AppData()
invalid_suitable_route = appdata.find_suitable_route('BRI', 'SYD')
self.assertEqual(None, invalid_suitable_route)
def test_getAllPendingPackagesEndLocationsFunction_whenParameterIsValid(self):
appdata = AppData()
appdata.add_package(package_1)
package_end_location = appdata.get_all_pending_packages_end_locations(appdata.packages)
self.assertEqual(['SYD'], package_end_location)
def test_checkBacklogForLocationFunction_whenParameterIsValid(self):
appdata = AppData()
package = Package(1, 'BRI', 'SYD', 50, customer_1, None, None)
package_2 = Package(2, 'BRI', 'SYD', 50, customer_1, None, None)
package_3 = Package(3, 'BRI', 'MEL', 50, customer_1, None, None)
package_4 = Package(4, 'BRI', 'MEL', 50, customer_1, None, None)
appdata.add_package(package)
appdata.add_package(package_2)
appdata.add_package(package_3)
appdata.add_package(package_4)
self.assertEqual(([package, package_2, package_3, package_4], ['SYD', 'MEL']),
appdata.check_backlog_for_location('BRI'))
def test_checkBacklogForLocationFunction_whenPackagesAreLessThanThree(self):
appdata = AppData()
package = Package(1, 'BRI', 'SYD', 50, customer_1, None, None)
package_2 = Package(2, 'BRI', 'SYD', 50, customer_1, None, None)
appdata.add_package(package)
appdata.add_package(package_2)
self.assertEqual((None, None), appdata.check_backlog_for_location('BRI'))