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)
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_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
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
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)
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)
class BookCircle: def __init__(self): self.simulation = None def create_simulation(self, users_count, max_books_per_user, exchange_points_count): """ Initialize and run simulation with given conditions """ self.simulation = Simulation(users_count, max_books_per_user, exchange_points_count) def run_simulation(self): self.simulation.run() log(self.simulation) def run_web_app(self): flask_app.run() def save_simulation_data(self, **kwargs): # Save simulation objects to persistent storage sql_persistence = SimulationPersistenceStrategy.SimulationPersistenceFlaskSQL() self.simulation.set_persistence_strategy(sql_persistence) self.simulation.save_data(**kwargs)
class BookCircle: def __init__(self): self.simulation = None def create_simulation(self, users_count, max_books_per_user, exchange_points_count): """ Initialize and run simulation with given conditions """ self.simulation = Simulation(users_count, max_books_per_user, exchange_points_count) def run_simulation(self): self.simulation.run() log(self.simulation) def run_web_app(self): flask_app.run() def save_simulation_data(self, **kwargs): # Save simulation objects to persistent storage sql_persistence = SimulationPersistenceStrategy.SimulationPersistenceFlaskSQL( ) self.simulation.set_persistence_strategy(sql_persistence) self.simulation.save_data(**kwargs)
def create_simulation(self, users_count, max_books_per_user, exchange_points_count): """ Initialize and run simulation with given conditions """ self.simulation = Simulation(users_count, max_books_per_user, exchange_points_count)