-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
281 lines (182 loc) · 10.1 KB
/
tests.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import unittest
from common_logger import log
from SimulationItems import SimulationItemFactory, ExchangePointProxy, PublicLibraryAdapter, PublicLibrary
from StoredItemPrototype import StoredUserPrototype, StoredBookPrototype
from SimulationSingleton import Simulation
import TransactionsLogic
from BaseClasses.ObserverBaseClasses import ObserverBase
from BaseClasses.Resourse import ComputerResource, ComputerResourceImplementor, BookResource, BookResourceImplementor
from BookCircleFacade import BookCircle
from BaseClasses.Composite import ExchangePointsHierarchyComposite, ExchangePointsHierarchyLeaf
from BaseClasses import SimulationState, Mediator
simulation_item_factory = SimulationItemFactory()
class PrototypeTests(unittest.TestCase):
def test_user_clone(self):
simulation_user = simulation_item_factory.create_simulation_user()
stored_user = StoredUserPrototype(simulation_user).clone()
self.assertEqual(simulation_user.name, stored_user.username)
def test_book_clone(self):
simulation_book = simulation_item_factory.create_simulation_book()
stored_book = StoredBookPrototype(simulation_book).clone()
self.assertEqual(simulation_book.title, stored_book.title)
class SimulationUserTests(unittest.TestCase):
def test_own_books(self):
sim_book = simulation_item_factory.create_simulation_book()
books_list = [sim_book]
sim_user = simulation_item_factory.create_simulation_user()
sim_user.own_books = books_list
self.assertTrue(sim_book in sim_user.own_books)
class SimulationBookTests(unittest.TestCase):
def test_owning(self):
test_user = simulation_item_factory.create_simulation_user()
test_book = simulation_item_factory.create_simulation_book()
test_book.owner = test_user
self.assertEqual(test_book.owner, test_user)
class SimulationTests(unittest.TestCase):
def test_simulation_singleton(self):
simulation_a = Simulation()
simulation_b = Simulation()
simulation_a.common_data = 'test data'
self.assertEqual(simulation_a.common_data, simulation_b.common_data)
def test_generate_items(self):
simulation = Simulation(users_count=3, max_books_per_user=3, exchange_points_count=5)
simulation.generate_items()
self.assertTrue(len(simulation.all_books) > 0)
self.assertTrue(len(simulation.all_users) > 0)
self.assertTrue(len(simulation.all_exchange_points) > 0)
class SimulationExchangePointTests(unittest.TestCase):
def test_point_creation(self):
exchange_point = simulation_item_factory.create_simulation_exchange_point()
self.assertTrue(exchange_point.address)
def test_put_get_book(self):
exchange_point = simulation_item_factory.create_simulation_exchange_point()
book = simulation_item_factory.create_simulation_book()
exchange_point.put_book(book)
self.assertTrue(len(exchange_point.stored_books) == 1)
gotten_book = exchange_point.get_book()
self.assertEqual(gotten_book , book)
self.assertTrue(len(exchange_point.stored_books) == 0)
class ExchangePointProxyTests(unittest.TestCase):
def test_points_chaining(self):
sim = Simulation(users_count=2, max_books_per_user=2, exchange_points_count=3)
sim.exchange_point_capacity = 5
sim.generate_items()
test_point = sim.get_last_exchange_point()
for _ in range(0,15):
test_point.put_book(simulation_item_factory.create_simulation_book())
for point in sim.all_exchange_points:
self.assertTrue(len(point.stored_books)>0)
def test_put_get_book(self):
exchange_point = simulation_item_factory.create_simulation_exchange_point()
point_proxy = ExchangePointProxy(proxied_point=exchange_point)
book = simulation_item_factory.create_simulation_book()
point_proxy.put_book(book)
self.assertTrue(len(point_proxy.proxied_point.stored_books) == 1)
gotten_book = point_proxy.get_book()
self.assertEqual(gotten_book , book)
self.assertTrue(len(point_proxy.proxied_point.stored_books) == 0)
def test_full_point(self):
exchange_point = simulation_item_factory.create_simulation_exchange_point()
point_proxy = ExchangePointProxy(proxied_point=exchange_point)
point_proxy.capacity = 10
books_pool = [simulation_item_factory.create_simulation_book() for book in range(0, int(point_proxy.capacity/2))]
for book in books_pool:
point_proxy.put_book(book)
self.assertTrue(point_proxy.point_is_not_full())
class TransactionsTests(unittest.TestCase):
def test_otp_execution(self):
simulation = Simulation(users_count=2, max_books_per_user=2, exchange_points_count=1)
simulation.generate_items()
TransactionsLogic.move_books_from_owners_to_points(simulation)
point = simulation.get_last_exchange_point()
self.assertTrue(len(point.stored_books) > 0) # point received some books as result of transaction
moved_book = point.stored_books[0]
one_user = simulation.all_users[0]
self.assertTrue(moved_book not in one_user.own_books) # user has no book which is moved to exchange point
class ObserverTests(unittest.TestCase):
TestNotification = 'TestNotification'
class UserObserver(ObserverBase):
def notification_received(self, sender, notification):
if notification == ObserverTests.TestNotification:
sender.observer_setted_test_flag = True
def test_concrete_observer(self):
new_user = simulation_item_factory.create_simulation_user()
user_observer = ObserverTests.UserObserver()
new_user.attach(user_observer)
new_user.notify(ObserverTests.TestNotification)
self.assertTrue(new_user.observer_setted_test_flag)
self.assertTrue(ObserverTests.TestNotification in user_observer.received_notifications[new_user])
new_user.detach(user_observer)
self.assertTrue(user_observer not in new_user.observers)
class AdapterTests(unittest.TestCase):
def test_public_library_adapter(self):
lib = PublicLibrary()
book = simulation_item_factory.create_simulation_book()
lib_as_exchange_point = PublicLibraryAdapter(adaptee=lib)
lib_as_exchange_point.put_book(book)
gotten_book = lib_as_exchange_point.get_book()
self.assertTrue(gotten_book, book)
class BridgeTests(unittest.TestCase):
def test_resources(self):
book_implementor = BookResourceImplementor()
book = BookResource(implementor=book_implementor, title='Книга про UTF8', owner='Python3 User')
book_description = book.get_resource_description()
log(book_description)
self.assertTrue(len(book_description)>0)
self.assertTrue(book.implementor == book_implementor)
computer_implementor = ComputerResourceImplementor()
computer = ComputerResource(model='BRDG3', vendor='Pattern Computers', implementor=computer_implementor)
computer_description = computer.get_resource_description()
log(computer_description)
self.assertTrue(len(computer_description)>0)
self.assertTrue(computer.implementor == computer_implementor)
class CompositeTests(unittest.TestCase):
def test_composite(self):
point1 = simulation_item_factory.create_simulation_exchange_point()
points_of_country = ExchangePointsHierarchyComposite()
all_points_of_city_A = ExchangePointsHierarchyComposite()
all_points_of_city_B = ExchangePointsHierarchyComposite()
points_of_country.add_child(all_points_of_city_A)
points_of_country.add_child(all_points_of_city_B)
# Some books at different points in city A
for _ in range(0, 3):
new_exchange_point = simulation_item_factory.create_simulation_exchange_point()
new_book = simulation_item_factory.create_simulation_book()
new_exchange_point.put_book(new_book)
all_points_of_city_A.add_child(ExchangePointsHierarchyLeaf(exchange_point=new_exchange_point))
# Some books at different points in city B
one_leaf_point_in_city_B = None
for _ in range(0, 2):
new_exchange_point = simulation_item_factory.create_simulation_exchange_point()
new_book = simulation_item_factory.create_simulation_book()
new_exchange_point.put_book(new_book)
one_leaf_point_in_city_B = ExchangePointsHierarchyLeaf(exchange_point=new_exchange_point)
all_points_of_city_B.add_child(one_leaf_point_in_city_B)
# Pattern: Composite
# Different components of hierarchy using same interface and able to be accessed with the same method
log(points_of_country.get_subordinated_books_count())
log(all_points_of_city_A.get_subordinated_books_count())
log(all_points_of_city_B.get_subordinated_books_count())
log(one_leaf_point_in_city_B.get_subordinated_books_count())
class StateTests(unittest.TestCase):
def test_simulation_state(self):
book_circle = BookCircle()
book_circle.create_simulation(users_count=2, max_books_per_user=3, exchange_points_count=5)
self.assertEqual(type(book_circle.simulation.state), type(SimulationState.ReadyToRunState()))
book_circle.run_simulation()
self.assertEqual(type(book_circle.simulation.state), type(SimulationState.ExecutedSimulationState()))
class MediatorTests(unittest.TestCase):
def test_users_mediation(self):
# simulation inherit MediatorMixin
simulation = Simulation(users_count=2, max_books_per_user=2, exchange_points_count=1)
simulation.generate_items()
# each user inherit ColleagueMixin
for user in simulation.all_users:
user.set_mediator(simulation)
simulation.broadcast_message_to_colleagues('Broadcasted message')
user_colleague = simulation.all_users[0]
user_colleague.send_message_to_mediator('new message')
self.assertEqual(user_colleague.mediator, simulation)
self.assertTrue(user_colleague in simulation.colleagues)
if __name__ == "__main__":
unittest.main()