def __init__(self, read_rental, write_rental): self.connection = sqlite3.connect('library.db') self.__create_database() RentalRepository.__init__(self) self.__read_rental = read_rental self.__write_rental = write_rental self.set_next_rental_id()
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 setUp(self): self.book_repository = BookRepository() self.client_repository = ClientRepository() self.rental_repository = RentalRepository() self.action_repository = ActionRepository() self.action_service = ActionService(self.action_repository, self.book_repository, self.client_repository, self.rental_repository) self.book_validator = BookValidator() self.book_service = BookService(self.action_service, self.book_repository, self.book_validator) self.client_validator = ClientValidator() self.rental_validator = RentalValidator() self.client_service = ClientService(self.action_service, self.client_repository, self.client_validator) 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)
class PopulateRepositoriesTest(unittest.TestCase): def setUp(self): self.book_repository = BookRepository() self.client_repository = ClientRepository() self.rental_repository = RentalRepository() self.action_repository = ActionRepository() self.action_service = ActionService(self.action_repository, self.book_repository, self.client_repository, self.rental_repository) self.book_validator = BookValidator() self.book_service = BookService(self.action_service, self.book_repository, self.book_validator) self.client_validator = ClientValidator() self.rental_validator = RentalValidator() self.client_service = ClientService(self.action_service, self.client_repository, self.client_validator) 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) def test_populate_book_repository(self): populate_book_repository(self.book_service) assert len(self.book_repository.get_all_books()) > 0 def test_populate_client_repository(self): populate_client_repository(self.client_service) assert len(self.client_repository.get_all_clients()) > 0 def test_populate_rental_repository(self): self.test_populate_book_repository() self.test_populate_client_repository() populate_rental_repository(self.rental_service) assert len(self.rental_repository.get_all_rentals()) > 0
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.client_validator = ClientValidator() self.client_service = ClientService(self.action_service, self.client_repository, self.client_validator) self.client_service.add_client("Name") assert (len(self.client_repository.get_all_clients()) == 1)
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.book_service = BookService(self.action_service, self.book_repository, self.book_validator) self.book_service.add_book("Title", "Author") assert (len(self.book_repository.get_all_books()) == 1)
def configure_repositories(self): if self.repository_type == "inmemory": self.book_repository = BookRepository() self.client_repository = ClientRepository() self.rental_repository = RentalRepository() elif self.repository_type == "textfiles": self.book_repository = BookTextRepository(self.book_repository, Book.string_read_book, Book.string_write_book) self.client_repository = ClientTextRepository( self.client_repository, Client.read_client, Client.write_client) self.rental_repository = RentalTextRepository( self.rental_repository, Rental.string_read_rental, Rental.string_write_rental) elif self.repository_type == "binaryfiles": self.book_repository = BookBinaryRepository(self.book_repository) self.client_repository = ClientBinaryRepository( self.client_repository) self.rental_repository = RentalBinaryRepository( self.rental_repository) elif self.repository_type == "jsonfiles": self.book_repository = BookJsonRepository(self.book_repository, Book.json_read_book, Book.json_write_book) self.client_repository = ClientJsonRepository( self.client_repository, Client.json_read_client, Client.json_write_client) self.rental_repository = RentalJsonRepository( self.rental_repository, Rental.json_read_rental, Rental.json_write_rental) elif self.repository_type == "database": self.book_repository = BookDatabaseRepository( Book.read_book_database, Book.write_book_database) self.client_repository = ClientDatabaseRepository( Client.read_client_database, Client.write_client_database) self.rental_repository = RentalDatabaseRepository( Rental.read_rental_database, Rental.write_rental_database) self.action_repository = ActionRepository()
def add_rental(self, rental): self.__read_all_from_file() RentalRepository.add_rental(self, rental) self.__write_all_to_file()
def __init__(self, filename, read_rental, write_rental): RentalRepository.__init__(self) self.__filename = filename self.__read_rental = read_rental self.__write_rental = write_rental self.set_next_rental_id()
def setUp(self): self.rental_repository = RentalRepository() rental = Rental(1, 1, 1, date.today(), date.today()) self.rental_repository.add_rental(rental) rental = Rental(2, 2, 2, date.today(), None) self.rental_repository.add_rental(rental)
def get_all_rentals(self): self.__read_all_from_file() return RentalRepository.get_all_rentals(self)
def update_rental_return_by_index(self, index, new_date): self.__read_all_from_database() RentalRepository.update_rental_return_by_index(self, index, new_date) self.__update_to_database(index, new_date)
def remove_rental_by_index(self, index): self.__read_all_from_database() rental = self.get_all_rentals()[index] RentalRepository.remove_rental_by_index(self, index) self.__remove_from_database(rental.id)
def add_rental(self, rental): self.__read_all_from_database() RentalRepository.add_rental(self, rental) self.__add_to_database(rental)
def remove_rental_by_index(self, index): self.__read_all_from_file() RentalRepository.remove_rental_by_index(self, index) self.__write_all_to_file()
def update_rental_return_by_index(self, index, new_date): self.__read_all_from_file() RentalRepository.update_rental_return_by_index(self, index, new_date) self.__write_all_to_file()
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
def __init__(self, filename): RentalRepository.__init__(self) self.__filename = filename self.set_next_rental_id()
class RentalRepositoryTest(unittest.TestCase): def setUp(self): self.rental_repository = RentalRepository() rental = Rental(1, 1, 1, date.today(), date.today()) self.rental_repository.add_rental(rental) rental = Rental(2, 2, 2, date.today(), None) self.rental_repository.add_rental(rental) def test_rental_repository_add_rental(self): rental = Rental(3, 3, 3, date.today(), None) self.rental_repository.add_rental(rental) rental = self.rental_repository.get_all_rentals() rental = rental[-1] assert rental.id == 3 assert rental.book_id == 3 assert rental.client_id == 3 assert rental.rented_date == date.today() assert rental.returned_date is None def test_rental_repository_delete_rental_by_index(self): rental = self.rental_repository.get_all_rentals() rental = rental[-1] assert rental.id == 2 self.rental_repository.remove_rental_by_index(1) rental = self.rental_repository.get_all_rentals() rental = rental[-1] assert rental.id == 1 def test_rental_repository_return_rental_by_index(self): rental = self.rental_repository.get_all_rentals() rental = rental[-1] assert rental.id == 2 assert rental.returned_date is None self.rental_repository.update_rental_return_by_index(1, date.today()) assert rental.returned_date == date.today() def test_rental_repository_get_next_rental_id(self): assert self.rental_repository.get_next_rental_id() == 1 self.rental_repository.increment_last_rental_id() assert self.rental_repository.get_next_rental_id() == 3 def test_rental_repository_get_all_rentals(self): rentals_list = self.rental_repository.get_all_rentals() assert len(rentals_list) == 2 def tearDown(self): del self.rental_repository