def undoExampleMedium(): undoController = UndoController() clientRepo = Repository() carRepo = Repository() ''' Start rental Controller ''' rentRepo = Repository() rentValidator = RentalValidator() rentController = RentalController(undoController, rentValidator, rentRepo, carRepo, clientRepo) ''' Start client Controller ''' clientValidator = ClientValidator() clientController = ClientController(undoController, rentController, clientValidator, clientRepo) ''' Start car Controller ''' carValidator = CarValidator() carController = CarController(undoController, rentController, carValidator, carRepo) ''' We add 3 clients ''' clientSophia = clientController.create(103, "2990511035588", "Sophia") clientCarol = clientController.create(104, "2670511035588", "Carol") clientBob = clientController.create(105, "2590411035588", "Bob") printReposWithMessage("We added 3 clients", clientRepo, None, None) ''' We delete 2 of the clients ''' clientController.delete(103) clientController.delete(105) printReposWithMessage("Deleted Sophia and Bob", clientRepo, None, None) ''' We undo twice ''' undoController.undo() printReposWithMessage("1 undo, so Bob is back", clientRepo, None, None) undoController.undo() printReposWithMessage("Another undo, so Sophia is back too", clientRepo, None, None) ''' We redo once ''' undoController.redo() printReposWithMessage("1 redo, so Sophia is again deleted", clientRepo, None, None)
class TestClientController(unittest.TestCase): def setUp(self): self.repo = Repository() self.rentalRepo = Repository() self.Ucontroller = UndoController() self.client = Client(11, 'alex') self.client2 = Client(12, 'ana') self.controller = ClientController(self.repo, self.Ucontroller, self.rentalRepo) def test_something(self): self.assertRaises(Exception, self.Ucontroller.undo) self.assertRaises(Exception, self.Ucontroller.redo) self.Ucontroller.newOperation() self.controller.addClient(self.client) self.assertEqual(len(self.repo), 1) self.Ucontroller.newOperation() self.controller.removeClient(11) self.assertEqual(len(self.repo), 0) self.Ucontroller.newOperation() self.controller.addClient(self.client) self.Ucontroller.newOperation() self.controller.updateClient(self.client) self.Ucontroller.newOperation() self.controller.addClient(self.client2) self.assertEqual(len(self.controller.getAllClients()), 2) self.assertRaises(ControllerException, self.controller.searchClient, 'nu') self.assertRaises(ControllerException, self.controller.searchClient, '') self.assertEqual(len(self.controller.searchClient('a')), 2) self.Ucontroller.newOperation() self.controller.removeClient(12) self.Ucontroller.undo() self.assertEqual(len(self.controller.getAllClients()), 2) self.Ucontroller.redo() self.assertEqual(len(self.controller.getAllClients()), 1)
def undoExampleHardest(): undoController = UndoController() clientRepo = Repository() carRepo = Repository() ''' Start rental Controller ''' rentRepo = Repository() rentController = RentalController(undoController, rentRepo, carRepo, clientRepo) ''' Start client Controller ''' clientController = ClientController(undoController, rentController, clientRepo) ''' Start car Controller ''' carController = CarController(undoController, rentController, carRepo) ''' We add 1 client, 1 car and 2 rentals ''' clientSophia = clientController.create(103, "2990511035588", "Sophia") carHyundaiTucson = carController.create(201, "CJ 02 TWD", "Hyundai", "Tucson") rentController.createRental(301, clientSophia, carHyundaiTucson, date(2016, 11, 1), date(2016, 11, 30)) rentController.createRental(302, clientSophia, carHyundaiTucson, date(2016, 12, 1), date(2016, 12, 31)) printReposWithMessage("We added Sophia, the Hyundai and its 2 rentals", clientRepo, carRepo, rentRepo) carController.delete(201) printReposWithMessage("Delete the Hyundai (also deletes its rentals)", clientRepo, carRepo, rentRepo) ''' Now undo the performed operations, one by one ''' undoController.undo() printReposWithMessage("1 undo, the Hyundai and its rentals are back", clientRepo, carRepo, rentRepo) undoController.undo() printReposWithMessage("1 undo deletes the second rental", clientRepo, carRepo, rentRepo) undoController.undo() printReposWithMessage("1 undo deletes the first rental", clientRepo, carRepo, rentRepo) undoController.undo() printReposWithMessage("1 undo deletes the Hyundai", clientRepo, carRepo, rentRepo) ''' After 5 undos, all repos should be empty, as we did 5 operations in total ''' undoController.undo() printReposWithMessage("1 undo deletes Sophia (no more undos)", clientRepo, carRepo, rentRepo) ''' Redos start here ''' undoController.redo() printReposWithMessage("1 redo and Sophia is added", clientRepo, carRepo, rentRepo) undoController.redo() printReposWithMessage("1 redo adds the Hyundai", clientRepo, carRepo, rentRepo) undoController.redo() printReposWithMessage("1 redo adds the first rental", clientRepo, carRepo, rentRepo) undoController.redo() printReposWithMessage("1 redo adds the second rental", clientRepo, carRepo, rentRepo) undoController.redo() printReposWithMessage("1 redo deletes the Hyundai and its rentals (again)", clientRepo, carRepo, rentRepo) ''' Let's do a few undos again... ''' undoController.undo() undoController.undo() undoController.undo() printReposWithMessage("3 undos later, we have Sophia and the Hyundai", clientRepo, carRepo, rentRepo) ''' Now we try something new - let's add another car! NB! A new operation must invalidate the history for redo() operations ''' carController.create(202, "CJ 02 SSE", "Dacia", "Sandero") print("\n Do we have a redo? -", undoController.redo(), "\n") ''' Now we should have 2 cars ''' printReposWithMessage("After adding the Dacia, there is no redo ", clientRepo, carRepo, rentRepo) ''' However, undos is still available ! ''' undoController.undo() printReposWithMessage("1 undo deletes the Dacia", clientRepo, carRepo, rentRepo) undoController.undo() printReposWithMessage("1 undo deletes the Hyundai", clientRepo, carRepo, rentRepo)
def undoExampleHard(): undoController = UndoController() clientRepo = Repository() carRepo = Repository() ''' Start rental Controller ''' rentRepo = Repository() rentValidator = RentalValidator() rentController = RentalController(undoController, rentValidator, rentRepo, carRepo, clientRepo) ''' Start client Controller ''' clientValidator = ClientValidator() clientController = ClientController(undoController, rentController, clientValidator, clientRepo) ''' Start car Controller ''' carValidator = CarValidator() carController = CarController(undoController, rentController, carValidator, carRepo) ''' We add 2 clients ''' clientSophia = clientController.create(103, "2990511035588", "Sophia") clientCarol = clientController.create(104, "2670511035588", "Carol") ''' We add 2 cars ''' carHyundaiTucson = carController.create(201, "CJ 02 TWD", "Hyundai", "Tucson") carToyotaCorolla = carController.create(202, "CJ 02 FWD", "Toyota", "Corolla") printReposWithMessage("Added 2 clients and 2 cars", clientRepo, carRepo, None) ''' We delete 1 client and 1 car ''' clientController.delete(103) carController.delete(202) printReposWithMessage("Deleted Sophia and the Corolla", clientRepo, carRepo, None) ''' We undo twice ''' undoController.undo() printReposWithMessage("1 undo, the Corolla is back", clientRepo, carRepo, None) undoController.undo() printReposWithMessage("1 undo, Sophia is back", clientRepo, carRepo, None) ''' Redo twice ''' undoController.redo() undoController.redo() printReposWithMessage("2 redos, Sophia and the Corolla are again deleted", clientRepo, carRepo, None) ''' Last redo ''' undoController.redo() printReposWithMessage("1 redo - but there are no more redos", clientRepo, carRepo, None)