def setUp(self): self.__bookId = 5 self.__title = "Fratii Karamazov" self.__author = "Dostoievski" self.__description = "pam pam" self.__clientId = 7 self.__name = "Ion Iliescu" self.__rentalId = 16 self.__rentedDate = 20 self.__dueDate = 13 self.__returnedDate = 30 self.__book = Book(self.__bookId, self.__title, self.__description, self.__author) self.__client = Client(self.__clientId, self.__name) self.__rental = Rental(self.__rentalId, self.__bookId, self.__clientId, self.__rentedDate, self.__dueDate, self.__returnedDate) self.__repoBook = RepoBook() self.__repoClient = RepoClient() self.__undoList = UndoRedo() self.__repoRental = RepoRental(self.__repoBook, self.__repoClient) self.__validBook = BookValidator() self.__validClient = ClientValidator() self.__validRental = RepoValidator(self.__repoBook, self.__repoClient) self.__bookService = ServiceBook(self.__repoBook, self.__validBook, self.__undoList) self.__clientService = ServiceClient(self.__repoClient, self.__validClient, self.__undoList) self.__rentalService = ServiceRental(self.__repoBook, self.__repoClient, self.__validRental, self.__repoRental, self.__undoList)
def rentBook(self, rentalId, bookId, clientId, rentedDate, dueDate, undo=True): ''' Function that rents a book(it adds a new rental in the rental list and it modifies the numberOfRentals of the rental entity) Input: rentalId, bookId, clientId, rentedDate, dueDate Precondition: rentalId, bookId, clientId - integers rentedDate, dueDate - strings Output: - Postcondition: - ''' index = 2 rentalId = int(rentalId) self.__valid.validRent(rentedDate) self.__valid.validRent(dueDate) new_rent = Rental(rentalId, bookId, clientId, rentedDate, dueDate) self.__valid.validRent1(new_rent) self.__valid.validRepo(new_rent) if self.__repo.searchElement1(new_rent) == -1: old_rent = self.__repo.searchId(bookId) if type(old_rent) != int: self.__valid.validNewRent(new_rent, old_rent) self.__repo.addElement(new_rent, 0) else: raise RepoError("invalid rental id") if undo == True: self.__undoService.addNewOperation(self.__repo.addElement, self.__repo.removeElementUndo, [new_rent, rentalId], index)
def __uiAddBook(self): bookId = input("Id:") title = input("Title:") author = input("Author:") description = input("Description:") self.__bookList.addNewBook(bookId, title, author, description) Id = self.__rentalList.findCurrentRentalId() new_rental = Rental(Id, bookId, 0, 0, 0, 0) self.__rentalList.addNewRental(new_rental, 0)
def rentBook(self, rentalId, bookId, clientId, rentedDate, dueDate, undo=True): ''' Function that rents a book(it adds a new rental in the rental list and it modifies the numberOfRentals of the rental entity) Input: rentalId, bookId, clientId, rentedDate, dueDate Precondition: rentalId, bookId, clientId - integers rentedDate, dueDate - strings Output: - Postcondition: - ''' index = 2 rentalId = int(rentalId) self.__valid.validRent(rentedDate) self.__valid.validRent(dueDate) new_rent = Rental(rentalId, bookId, clientId, rentedDate, dueDate) self.__valid.validRepo(new_rent) if self.__repo.searchElement1(new_rent) == -1: new_rent = Rental(rentalId, bookId, clientId, rentedDate, dueDate, 1) self.__repo.addElement(new_rent, 0) if undo == True: self.__undoService.addNewOperation( self.__repo.addElement, self.__repo.removeElementUndo, [new_rent, rentalId], index) else: i = self.__repo.searchElement1(new_rent) if self.__repo.rentElement(new_rent, i): number = self.__repo.numberOfRents(bookId) number = int(number) new_rent = Rental(rentalId, bookId, clientId, rentedDate, dueDate, number) self.__repo.addElement(new_rent) if undo == True: self.__undoService.addNewOperation( self.__repo.rentBook, self.__repo.removeElementUndo, [new_rent, rentalId], index) else: raise ValueError("This book can't be rented")
def returnBook(self, rentalId, bookId, clientId, returnedDate): ''' Function that returns a rented book and updates its numberOfRentals Input: rentalId, bookId, clientId, returnedDate Precondition: rentalId, bookId, clientId - integers returnedDate - string Output: - Postcondition: - ''' rentalId = int(rentalId) returnedBook = Rental(rentalId, bookId, clientId) i = self.__repo.searchElement1(returnedBook) if i == -1: raise ValueError("This book was not rented") rentedDate = self.__repo.returnElement(i)[0] dueDate = self.__repo.returnElement(i)[1] rental1 = Rental(rentalId, bookId, clientId, rentedDate, dueDate, returnedDate) self.__repo.updateElement(rental1) return rental1
def removeElementUndo(self, element, param): ''' Function that removes a given element from the list if it existed in the list before Input: element Precondition: - Output: - Postcondition: - ''' element = int(element) element = Rental(element) if element not in self.__list: raise ValueError("inexisting element") i = 0 while i < len(self.__list): if self.__list[i] == element: del self.__list[i] i -= 1 i += 1
def removeAparition(self, bookId, command, undo=True): if command == "book": bookId = int(bookId) book = self.__repoBooks.searchElement(bookId, 1) new_book_Id = book.get_book_id() new_book_title = book.get_title() new_book_author = book.get_author() new_book_description = book.get_description() new_book_number_of_rentals = book.get_number_of_rentals() new_book_number_of_days = book.get_number_of_days() new_book = Book(new_book_Id, new_book_title, new_book_author, new_book_description, new_book_number_of_rentals, new_book_number_of_days) i = 2 rental = self.__repo.findRentalId(bookId, "book") self.__repoBooks.removeElement(bookId) self.__undoService.addNewOperation(self.__repoBooks.removeElement, self.__repoBooks.addElement, [bookId, new_book], i) if type(rental) != int: index = int(self.__repo.searchElement1(rental)) while index != -1: old_rental_id = rental.get_rental_id() old_rental_book_id = rental.get_book_id() old_rental_client_id = rental.get_client_id() old_rental_rented_date = rental.get_rented_date() old_rental_due_date = rental.get_due_date() old_rental_returned_date = rental.get_returned_date() self.__repo.removeElementInCascade(index) old_rental = Rental(old_rental_id, old_rental_book_id, old_rental_client_id, old_rental_rented_date, old_rental_due_date, old_rental_returned_date) self.__undoService.addNewOperation(self.removeAparition, self.__repo.addRental, [0, old_rental], i) i += 1 rental = self.__repo.findRentalId(bookId, "book") if type(rental) != int: index = int(self.__repo.searchElement1(rental)) else: index = -1 elif command == "client": i = 2 bookId = int(bookId) client = self.__repoClients.searchElement(bookId, 2) new_client_Id = client.get_client_id() new_client_name = client.get_name() new_client_number_of_days = client.get_number_of_days() new_client = Client(new_client_Id, new_client_name, new_client_number_of_days) rental = self.__repo.findRentalId(bookId, "client") self.__repoClients.removeElement(bookId) self.__undoService.addNewOperation( self.__repoClients.removeElement, self.__repoClients.addElement, [bookId, new_client], i) if type(rental) != int: index = int(self.__repo.searchElement1(rental)) while index != -1: old_rental_id = rental.get_rental_id() old_rental_book_id = rental.get_book_id() old_rental_client_id = rental.get_client_id() old_rental_rented_date = rental.get_rented_date() old_rental_due_date = rental.get_due_date() old_rental_returned_date = rental.get_returned_date() self.__repo.removeElementInCascade(index) rental = Rental(old_rental_id, old_rental_book_id, old_rental_client_id, old_rental_rented_date, old_rental_due_date, old_rental_returned_date) self.__undoService.addNewOperation(self.removeAparition, self.addNewRental, [0, rental], i) i += 1 rental = self.__repo.findRentalId(bookId, "client") if type(rental) != int: index = int(self.__repo.searchElement1(rental)) else: index = -1
class Tests(unittest.TestCase): def setUp(self): self.__bookId = 5 self.__title = "Fratii Karamazov" self.__author = "Dostoievski" self.__description = "pam pam" self.__clientId = 7 self.__name = "Ion Iliescu" self.__rentalId = 16 self.__rentedDate = 20 self.__dueDate = 13 self.__returnedDate = 30 self.__book = Book(self.__bookId, self.__title, self.__description, self.__author) self.__client = Client(self.__clientId, self.__name) self.__rental = Rental(self.__rentalId, self.__bookId, self.__clientId, self.__rentedDate, self.__dueDate, self.__returnedDate) self.__repoBook = RepoBook() self.__repoClient = RepoClient() self.__undoList = UndoRedo() self.__repoRental = RepoRental(self.__repoBook, self.__repoClient) self.__validBook = BookValidator() self.__validClient = ClientValidator() self.__validRental = RepoValidator(self.__repoBook, self.__repoClient) self.__bookService = ServiceBook(self.__repoBook, self.__validBook, self.__undoList) self.__clientService = ServiceClient(self.__repoClient, self.__validClient, self.__undoList) self.__rentalService = ServiceRental(self.__repoBook, self.__repoClient, self.__validRental, self.__repoRental, self.__undoList) def testModels(self): self.assertEquals(self.__book.get_book_id(), self.__bookId) self.assertEquals(self.__client.get_name(), self.__name) self.assertEquals(self.__rental.get_rental_id(), self.__rentalId) #self.assertEquals self.__rental.get_number_of_rentals() == self.__numberOfRentals def testRepository(self): self.assertEquals(len(self.__repoBook), 0) self.__repoBook.addElement(self.__book, 0) self.assertEquals(len(self.__repoBook), 1) new_book = Book(3, "Ion", "glasul pamantului/glasul iubirii", "Liviu Rebreanu") self.__repoBook.addElement(new_book, 0) self.assertEquals(len(self.__repoBook), 2) try: self.__repoBook.addElement(self.__book, 0) self.assertEquals(False) except ValueError as re: self.assertEquals(str(re), "existing element") self.assertEquals(len(self.__repoClient), 0) self.__repoClient.addElement(self.__client, 0) self.assertEquals(len(self.__repoClient), 1) new_client = Client(2, "Justin Trudeau") self.__repoClient.addElement(new_client, 0) self.assertEquals(len(self.__repoClient), 2) try: self.__repoClient.addElement(self.__client, 0) self.assertEquals(False) except ValueError as re: self.assertEquals(str(re), "existing element") self.__repoClient.removeElement(new_client.get_client_id()) self.assertEquals(len(self.__repoClient), 1) try: self.__repoClient.removeElement(new_client.get_client_id()) self.assertEquals(False) except ValueError as re: self.assertEquals(str(re), "inexisting element") #self.__repo.printBooks() new_book = Book(3, "HP", "JK Rowling", "magic") self.__repoBook.updateElement(new_book, 0) cnt = self.__repoBook.searchElement1(new_book) cnt = int(cnt) def testBusiness(self): self.__repoBook = RepoBook() self.__business = ServiceBook(self.__repoBook, self.__validBook, self.__undoList) self.assertEquals(len(self.__business), 0) self.__business.addNewBook(self.__bookId, self.__title, self.__author, self.__description) self.assertEquals(len(self.__business), 1) self.__business.addNewBook(15, "GoT", "George RR Martin", "abcd") self.assertEquals(len(self.__business), 2) self.__repoClient = RepoClient() self.__business = ServiceClient(self.__repoClient, self.__validClient, self.__undoList) self.__business.addNewClient(self.__clientId, self.__name) self.assertEquals(len(self.__business), 1) self.__business.addNewClient(5, "Nicolae Ceausescu") self.assertEquals(len(self.__business), 2) element = Client(5, "Traian Basescu") self.__repoClient.updateElement(element, 0) search = self.__repoClient.searchElement(5, 2) self.assertEquals(search.get_name(), "Traian Basescu") self.__business.removeClient(5, "Nicolae Ceausescu") self.assertEquals(len(self.__business), 1) self.__repoBook = RepoBook() self.__business = ServiceBook(self.__repoBook, self.__validBook, self.__undoList) self.__business.addNewBook(23, "De veghe in lanul de secara", "JD Salinger", "Roman") self.__business.addNewBook(15, "Martianul", "Andy Weir", "I-a placut lui Leo") self.__business.removeBook(15, 0) self.assertEquals(len(self.__business), 1) element = Book(15, "1984", "George Orwell", "Tot lui Leo i-a placut") self.__repoBook.addElement(element, 0) element = Book(15, "1984", "Pam pam", "Tot lui Leo i-a placut") self.__repoBook.updateElement(element, 0) search = self.__repoBook.searchElement(15, 1) self.assertEquals(search.get_author(), "Pam pam") def testRent(self): pass