Example #1
0
class ClientRepositoryTest(unittest.TestCase):
    def setUp(self):
        self.client_repository = ClientRepository()
        client = Client(1, "Name1")
        self.client_repository.add_client(client)
        client = Client(2, "Name2")
        self.client_repository.add_client(client)

    def test_client_repository_add_client(self):
        client = Client(3, "Name3")
        self.client_repository.add_client(client)
        client = self.client_repository.get_all_clients()
        client = client[-1]
        assert client.id == 3
        assert client.name == "Name3"

    def test_client_repository_remove_client(self):
        clients_list = self.client_repository.get_all_clients()
        client = clients_list[-1]
        assert client.id == 2
        self.client_repository.remove_client_by_index(1)
        client = clients_list[-1]
        assert client.id == 1

    def test_client_repository_update_client(self):
        clients_list = self.client_repository.get_all_clients()
        client = clients_list[-1]
        assert client.id == 2
        assert client.name == "Name2"
        client = Client(10, "New Name")
        self.client_repository.update_client(1, client)
        clients_list = self.client_repository.get_all_clients()
        client = clients_list[-1]
        assert client.id == 10
        assert client.name == "New Name"

    def test_client_repository_get_next_client_id(self):
        assert self.client_repository.get_next_client_id() == 1
        self.client_repository.increment_last_client_id()
        assert self.client_repository.get_next_client_id() == 3

    def test_client_repository_get_all_clients(self):
        clients_list = self.client_repository.get_all_clients()
        assert len(clients_list) == 2

    def tearDown(self):
        del self.client_repository
Example #2
0
 def add_client(self, client):
     self.__read_all_from_file()
     ClientRepository.add_client(self, client)
     self.__write_all_to_file()
 def add_client(self, client):
     self.__read_all_from_database()
     ClientRepository.add_client(self, client)
     self.__add_to_database(client)
class RentalServiceTest(unittest.TestCase):
    def setUp(self):
        self.action_repository = ActionRepository()
        self.book_repository = BookRepository()
        self.client_repository = ClientRepository()
        self.rental_repository = RentalRepository()

        self.action_service = ActionService(self.action_repository, self.book_repository,
                                            self.client_repository, self.rental_repository)

        self.book_validator = BookValidator()
        self.client_validator = ClientValidator()
        self.rental_validator = RentalValidator()

        self.rental_service = RentalService(self.action_service, self.book_repository, self.client_repository,
                                            self.rental_repository, self.book_validator, self.client_validator,
                                            self.rental_validator)

        client = Client(1, "Name1")
        self.client_repository.add_client(client)
        client = Client(2, "Name2")
        self.client_repository.add_client(client)
        book = Book(1, "Title1", "Author1")
        self.book_repository.add_book(book)
        book = Book(2, "Title2", "Author2")
        self.book_repository.add_book(book)

        self.rental_service.add_rental(1, 1)

        assert (len(self.rental_repository.get_all_rentals()) == 1)

    def test_rental_service_add_rental(self):
        self.rental_service.add_rental(2, 2)
        assert (len(self.rental_repository.get_all_rentals()) == 2)
        self.assertRaises(RepoError, self.rental_service.add_rental, 1, 1)
        self.assertRaises(RepoError, self.rental_service.add_rental, 1, 5)
        self.assertRaises(RepoError, self.rental_service.add_rental, 5, 1)
        self.action_service.undo()
        assert (len(self.rental_repository.get_all_rentals()) == 1)
        self.action_service.redo()
        assert (len(self.rental_repository.get_all_rentals()) == 2)

    def test_rental_service_return_rental_by_id(self):
        self.rental_service.return_rental_by_id(1)
        assert (len(self.rental_repository.get_all_rentals()) == 1)
        assert self.rental_repository.get_all_rentals()[-1].returned_date == date.today()
        self.assertRaises(ValidError, self.rental_service.return_rental_by_id, "k")
        self.assertRaises(RepoError, self.rental_service.return_rental_by_id, 5)
        self.action_service.undo()
        assert self.rental_repository.get_all_rentals()[-1].returned_date is None
        self.action_service.redo()
        assert self.rental_repository.get_all_rentals()[-1].returned_date == date.today()

    def test_rental_service_return_rental_by_book_id(self):
        self.rental_service.return_rental_by_book_id(1)
        assert (len(self.rental_repository.get_all_rentals()) == 1)
        assert self.rental_repository.get_all_rentals()[-1].returned_date == date.today()
        self.assertRaises(RepoError, self.rental_service.return_rental_by_book_id, 5)
        self.assertRaises(RepoError, self.rental_service.return_rental_by_book_id, 1)
        self.action_service.undo()
        assert self.rental_repository.get_all_rentals()[-1].returned_date is None
        self.action_service.redo()
        assert self.rental_repository.get_all_rentals()[-1].returned_date == date.today()

    def test_rental_service_remove_rental_by_book_id(self):
        assert (len(self.rental_repository.get_all_rentals()) == 1)
        self.rental_service.remove_rental_by_book_id(1)
        assert (len(self.rental_repository.get_all_rentals()) == 0)
        self.action_service.undo()
        assert (len(self.rental_repository.get_all_rentals()) == 0)
        self.action_service.redo()

    def test_rental_service_remove_rental_by_client_id(self):
        self.rental_service.add_rental(2, 1)
        assert (len(self.rental_repository.get_all_rentals()) == 2)
        self.rental_service.remove_rental_by_client_id(1)
        assert (len(self.rental_repository.get_all_rentals()) == 0)
        assert self.rental_service.remove_rental_by_client_id(1) is None
        self.action_service.undo()
        assert (len(self.rental_repository.get_all_rentals()) == 1)
        self.action_service.redo()
        assert (len(self.rental_repository.get_all_rentals()) == 0)

    def test_rental_service_get_client_active_rentals(self):
        self.rental_service.add_rental(2, 1)
        assert (len(self.rental_repository.get_all_rentals()) == 2)
        rentals_list = self.rental_service.get_client_active_rentals(1)
        assert (len(rentals_list) == 2)

    def test_rental_service_get_book_rental_status(self):
        assert self.rental_service.get_book_rental_status(1) == 1
        assert self.rental_service.get_book_rental_status(5) is None

    def test_rental_service_get_statistics_for_most_active_clients(self):
        self.rental_service.add_rental(2, 1)
        assert (len(self.rental_repository.get_all_rentals()) == 2)
        rentals_list = self.rental_service.get_statistics_for_most_active_clients()
        assert rentals_list[-1]["ID"] == 1

    def test_rental_service_get_statistics_for_most_rented_books(self):
        self.rental_service.return_rental_by_id(1)
        self.rental_service.add_rental(1, 1)
        self.rental_service.add_rental(2, 2)
        assert (len(self.rental_repository.get_all_rentals()) == 3)
        rentals_list = self.rental_service.get_statistics_for_most_rented_books()
        most_rented_book = rentals_list[0]
        assert most_rented_book["Title"] == "Title1"
        assert most_rented_book["Author"] == "Author1"
        assert most_rented_book["Rental amount"] == 2

    def test_rental_service_get_book_by_book_id(self):
        book = self.rental_service.get_book_by_book_id(1)
        assert isinstance(book, Book)
        assert book.id == 1
        assert self.rental_service.get_book_by_book_id(10) is None

    def test_rental_service_get_most_rented_author(self):
        self.rental_service.add_rental(2, 2)
        self.rental_service.return_rental_by_id(1)
        self.rental_service.add_rental(1, 1)
        book = Book(5, "New Title Book", "Author1")
        self.book_repository.add_book(book)
        self.rental_service.add_rental(5, 1)
        author = self.rental_service.get_most_rented_author()
        assert author["Name"] == "Author1"
        books_list = self.rental_service.get_list_of_books_of_most_rented_author(author)
        assert len(books_list) == 2

    def tearDown(self):
        del self.book_repository
        del self.client_repository
        del self.rental_repository
        del self.book_validator
        del self.client_validator
        del self.rental_validator
        del self.rental_service