Пример #1
0
 def ui_undo(self):
     try:
         UndoService.do_undo()
         print("Undo executed")
     except UndoException as ue:
         print(ue)
Пример #2
0
class TestUndoService(unittest.TestCase):
    def setUp(self):
        """
        Prepare tests
        """
        self.rental_repository = Repository({})
        self.validator2 = RentalValidator()
        self.book_repository = Repository({})
        self.validator = BookValidator()
        self.client_repository = Repository({})
        self.validatorc = ClientValidator()
        self.undo_service = UndoService()
        self.redo_service = RedoService()
        self.book_service = BookService(self.book_repository, self.validator)
        self.client_service = ClientService(self.client_repository, self.validatorc)
        self.rental_service = RentalService(self.rental_repository, self.validator2, self.book_repository,
                                            self.client_repository)

    def test_undo_controller(self):
        """
        Tests for undo controller
        """
        self.assertRaises(UndoException, self.undo_service.do_undo)
        self.assertRaises(RedoException, self.redo_service.do_redo)

        self.book_service.add_book('12', "the name", 'By me')
        self.undo_service.store_operation(self.book_service, UndoHandler.UNDO_ADD_BOOK, '12')
        self.undo_service.do_undo()
        self.assertTrue(len(self.book_service.get_entities()) == 0)
        self.redo_service.store_operation(self.book_service, RedoHandler.REDO_ADD_BOOK, '12', "the name", "By me")

        self.redo_service.do_redo()
        self.assertTrue(len(self.book_service.get_entities()) == 1)
        self.book_service.add_book('2', "the name", 'By me')
        self.book_service.remove_book('2')
        self.undo_service.store_operation(self.book_service, UndoHandler.UNDO_REMOVE_BOOK, '2', "The Mandalorian", "Didu")
        self.undo_service.do_undo()

        self.undo_service.store_operation(self.book_service, UndoHandler.UNDO_UPDATE_BOOK, '2', "The Mandalorian","Didu")
        self.undo_service.do_undo()
        self.redo_service.store_operation(self.book_service, RedoHandler.REDO_UPDATE_BOOK, '2', "The Mandalorian","Didu")
        self.redo_service.do_redo()

        self.client_service.add_client("10", "andrei")
        self.undo_service.store_operation(self.client_service, UndoHandler.UNDO_ADD_CLIENT, '10')
        self.undo_service.do_undo()
        self.assertTrue(len(self.client_service.get_entities()) == 0)
        self.redo_service.store_operation(self.client_service, RedoHandler.REDO_ADD_CLIENT, "10", "andrei")
        self.redo_service.do_redo()
        self.assertTrue(len(self.client_service.get_entities()) == 1)

        self.client_service.remove_client('10')
        self.undo_service.store_operation(self.client_service, UndoHandler.UNDO_REMOVE_CLIENT, '10', 'andrei')
        self.undo_service.do_undo()
        self.redo_service.store_operation(self.client_service, RedoHandler.REDO_REMOVE_CLIENT, '10')
        self.redo_service.do_redo()

        self.client_service.add_client('2', "Didu")
        self.undo_service.store_operation(self.client_service, UndoHandler.UNDO_UPDATE_CLIENT, '2', "Didu")
        self.undo_service.do_undo()
        self.redo_service.store_operation(self.client_service, RedoHandler.REDO_UPDATE_CLIENT, '2', 'Didu')
        self.redo_service.do_redo()

        self.rental_service.add_rental('1', '2', '2', '20/10/2020')
        self.undo_service.store_operation(self.rental_service, UndoHandler.UNDO_ADD_RENTAL, '1')
        self.undo_service.do_undo()
        self.redo_service.store_operation(self.rental_service, RedoHandler.REDO_ADD_RENTAL, '1', '2', '2', '20/10/2020','')
        self.redo_service.do_redo()

        self.book_service.add_book('20', "book", 'By me')
        self.book_service.remove_book('20')
        self.undo_service.store_operation(self.book_service, UndoHandler.UNDO_REMOVE_BOOK, '20', "book", "By me")
        self.undo_service.do_undo()
        self.redo_service.store_operation(self.book_service, RedoHandler.REDO_REMOVE_BOOK, '20')
        self.redo_service.do_redo()

        self.redo_service.clear()