def testBookValidator(self): b = Book(1, "Arma invizibila", "", "Paul Feval") validator = Validator() try: validator.validateBook(b) self.assertTrue(False) except ValidationException as ve: self.assertTrue(str(ve) == "Invalid description!\n")
class Controller(): def __init__(self, book_repo, client_repo, rental_repo): self.__book_repo = book_repo self.__client_repo = client_repo self.__rental_repo = rental_repo self.__validator = Validator() def addBook(self, args): b = Book(int(args[0]), args[1], args[2], args[3]) self.__validator.validateBook(b) self.__book_repo.add(b) def removeBook(self, id): b = Book(int(id), "default", "default", "default") self.__validator.validateBook(b) self.__book_repo.rem(b) def updateBook(self, args): b = Book(int(args[0]), args[1], args[2], args[3]) self.__validator.validateBook(b) self.__book_repo.upd(b) def getBooks(self): return self.__book_repo.getAll() def addClient(self, args): c = Client(int(args[0]), args[1]) self.__validator.validateClient(c) self.__client_repo.add(c) def getClients(self): return self.__client_repo.getAll() def removeClient(self, id): c = Client(int(id), "default") self.__validator.validateClient(c) self.__client_repo.rem(c) def updateClient(self, args): c = Client(int(args[0]), args[1]) self.__validator.validateClient(c) self.__client_repo.upd(c) def avlBook(self, book_id): rentals = self.__rental_repo.getAll() for x in rentals: if x.bookId == book_id: state = x.rentState() if state["ret"] == False: return False return True def addRental(self, args): if self.avlBook(int(args[1])): b = Book(int(args[1]), "default", "default", "default") try: self.__book_repo.find(b) except LibraryException: raise LibraryException("Inexistent book!") c = Client(int(args[2]), "default") try: self.__client_repo.find(c) except LibraryException: raise LibraryException("Unregistred client!") r = Rental(int(args[0]), int(args[1]), int(args[2]), date.today(), date.today() + timedelta(days=10), False) self.__validator.rentalValidator(r) try: self.__rental_repo.add(r) except LibraryException: raise LibraryException("There already is a rental with this id!") else: raise LibraryException("This book is not available!") def getRentals(self): return self.__rental_repo.getAll() def updateRental(self, args): r = Rental(int(args[0]), int(args[1]), int(args[2]), args[3], args[4], args[5]) self.__validator.rentalValidator(r) self.__rental_repo.upd(r) def returnBook(self, args): r = Rental(int(args[0]), 1, 1, date.today(), date.today(), False) r = self.__rental_repo.find(r) self.__validator.rentalValidator(r) r.retDate = date.today()