def setUp(self): unittest.TestCase.setUp(self) undoController = UndoController() self.rentalList = Repository() self.bookList = Repository() self.clientList = Repository() self.rentalList.add( Rental(0, 0, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")) self.rentalList.add( Rental(1, 1, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")) self.bookList.add(Book(0, "book0", "desc0", "author0")) self.bookList.add(Book(1, "book1", "desc1", "author1")) self.bookList.add(Book(2, "book2", "desc2", "author2")) self.clientList.add(Client(0, "name0")) self.clientList.add(Client(1, "name1")) self.clientList.add(Client(2, "name2")) self.rentalController = RentalController(self.rentalList, self.bookList, self.clientList, undoController) self.bookController = BookController(self.bookList, undoController, self.rentalController) self.clientController = ClientController(self.clientList, undoController, self.rentalController)
def __test_repo(): """ Function to test repository :return: """ val = BookValidator() repo = Repository(val) b = Book("Ion", "Liviu Rebreanu", "Drama", 1912) b.set_id(0) repo.store_book(b) assert len(repo.get_all()) == 1 b.set_id(2) repo.store_book(b) assert len(repo.get_all()) == 2 assert repo.search_book(1) is not None assert repo.search_book(2) is None assert repo.delete_book(1) is not None assert repo.delete_book(1) is None b.set_id(2) repo.store_book(b) assert len(repo.get_all()) == 2 b.set_year(2018) assert repo.update_book(1, b) is not None assert repo.update_book(2, b) is None b1 = repo.search_book(1) assert b1.get_year() == 2018 try: repo.store_book(b) assert False except RepositoryException: assert True
def testBook(): b_repo = Repository() b_repo.add(Book(1, "Charlie and the chocolate factory", "nice storry", "Roald Dalh")) b_repo.add(Book(3, "Ciresarii", "tat felu de aventuri", "Constantin Chirita")) return b_repo
def testInitBooks(repo): repo.add( Book(0, "Harry Potter and the Philosopher's Stone ", "first book of HP series", "J.K. Rowling")) repo.add( Book(1, "Harry Potter and the Chamber of Secrets ", "second book of HP series", "J.K. Rowling")) repo.add( Book(2, "Harry Potter and the Prisoner of Azkaban ", "third book of HP series", "J.K. Rowling")) repo.add( Book(3, "Harry Potter and the Goblet of Fire ", "4th book of HP series", "J.K. Rowling")) repo.add( Book(4, "Harry Potter and the Order of Phoenix ", "5th book of HP series", "J.K. Rowling")) repo.add( Book(5, "Harry Potter and the Half-Blood Prince ", "6th book of HP series", "J.K. Rowling")) repo.add( Book(6, "Harry Potter and the Deathly Hallows ", "7th book of HP series", "J.K. Rowling")) repo.add(Book(7, "The Great Gatsby", "-", "F. Scott Fitzgerald")) repo.add(Book(8, "The Alchimist", "", "Paulo Coelho")) repo.add( Book(9, "A Flew Over the Cuckoo's Nest", "mental health", "Ken Kesey"))
def testGetBooks(self): self.assertEqual(self.bookController.getBooks(), [ Book(0, "book0", "desc0", "author0"), Book(1, "book1", "desc1", "author1"), Book(2, "book2", "desc2", "author2") ])
def testAddBook(self): self.bookController.addBook(3, "book3", "desc3", "author3") self.assertEqual(self.bookList.getAll(), [ Book(0, "book0", "desc0", "author0"), Book(1, "book1", "desc1", "author1"), Book(2, "book2", "desc2", "author2"), Book(3, "book3", "desc3", "author3") ])
def testRepository(self): bookRepo = Repository() testList = [ Book(0, "book0", "desc0", "author0"), Book(1, "book1", "desc1", "author1") ] for i in range(0, len(testList)): bookRepo.add(testList[i]) self.assertEqual(bookRepo.get(i), testList[i]) bookRepo.update(1, Book(0, "Book1", "Author1", "Description1")) self.assertEqual(bookRepo.get(1), Book(1, "Book1", "Author1", "Description1"))
def testRemoveBook(self): self.bookController.removeBook(0) self.assertEqual(self.bookList.getAll(), [ Book(1, "book1", "desc1", "author1"), Book(2, "book2", "desc2", "author2") ]) self.bookController.removeBook(2) self.bookController.removeBook(1) self.assertEqual(self.bookList.getAll(), []) with self.assertRaises(Exception): self.bookController.removeBook(10)
def testSortObject(self): self.clientList.sort(key=lambda x: x.getName()) self.assertEqual(self.clientList[0], Client(1, "name0")) self.assertEqual(self.clientList[1], Client(0, "name1")) self.assertEqual(self.clientList[2], Client(2, "name2")) self.bookList.sort(key=lambda x: x.getTitle(), reverse=True) self.assertEqual(self.bookList[0], Book(2, "book2", "desc2", "author2")) self.assertEqual(self.bookList[1], Book(1, "book1", "desc1", "author0")) self.assertEqual(self.bookList[2], Book(0, "book0", "desc0", "author1"))
def testUpdateBook(self): with self.assertRaises(InvalidIdException): self.bookController.updateBook(20, "book2", "author3", "desc4") self.bookController.updateBook(1, "Book1", "Author1", "Description1") self.assertEqual(self.bookList.get(1), Book(1, "Book1", "Author1", "Description1"))
def updateBook(self, id, title, description, author, recordForUndo=True): """ Updates the book with the given id with a new book :param id: int :param title: the new title :param description: the new description :param author: the new author :return:the book before it was updated """ okId = False for i in self._bookRepo.getAll(): if id == i.getId(): okId = True if not okId: raise InvalidIdException lastBook = copy.deepcopy(self.findBookByBookId(id)) index = self.findIndexInRepo(id) book = Book(id, title, description, author) if recordForUndo == True: undo = FunctionCall(self.updateBook, id, lastBook.getTitle(), lastBook.getDescription(), lastBook.getAuthor(), False) redo = FunctionCall(self.updateBook, id, title, description, author, False) operation = Operation(redo, undo) self._undoController.recordOperation(operation) self._bookRepo.update(index, book) return lastBook
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")
def add(self, bookID, title, author, description): b = Book(bookID, title, author, description) self._validator.validate(b) self._repository.add(b) undo = FunctionCall(self.remove, bookID) redo = FunctionCall(self.add, bookID, title, author, description) oper = Operation(undo, redo) self._undoCtrl.addOperation(oper) return True
def testFilter(self): res = self.clientList.filter(key=lambda x: x.getName(), value="name1") self.assertEqual(res[0], Client(0, "name1")) res1 = self.bookList.filter(key=lambda x: x.getId(), value=0) self.assertEqual(res1[0], Book(0, "book0", "desc0", "author1")) res2 = self.clientList.filter() self.assertEqual(res2, self.clientList) self.clientList.append(Client(3, "name0")) res3 = self.clientList.filter(key=lambda x: x.getName(), value="name0") self.assertEqual(res3[0], Client(1, "name0")) self.assertEqual(res3[1], Client(3, "name0"))
def removeBook(self, id, recForUndo=True): b = Book(int(id), "default", "default", "default") self._validator.validateBook(b) b = self._repo.rem(b) if recForUndo == True: undo = FunctionCall(self.addBook, [b.id, b.title, b.desc, b.author], False) redo = FunctionCall(self.removeBook, b.id, False) operation = Operation(redo, undo) self._undo_ctrl.recordOp(operation)
def addBook(self, args, recForUndo=True): b = Book(int(args[0]), args[1], args[2], args[3]) self._validator.validateBook(b) self._repo.add(b) if recForUndo == True: undo = FunctionCall(self.removeBook, b.id, False) redo = FunctionCall(self.addBook, [b.id, b.title, b.desc, b.author], False) operation = Operation(redo, undo) self._undo_ctrl.recordOp(operation)
def update(self, id, title, author, description): b = Book(id, title, author, description) self._validator.validate(b) oldB = self._repository.searchByID(id) params = [oldB.getTitle(), oldB.getAuthor(), oldB.getDescription()] self._repository.update(id, b) undo = FunctionCall(self.update, id, params[0], params[1], params[2]) redo = FunctionCall(self.update, id, title, author, description) oper = Operation(undo, redo) self._undoCtrl.addOperation(oper) return True
def __loadFromFile(self): try: f = open(self.__f_name, "r") line = f.readline().strip() while line != "": attrs = line.split(',') b = Book(int(attrs[0]), attrs[1], attrs[2], attrs[3]) Repository.add(self, b) line = f.readline().strip() except IOError: raise RepositoryException("Can't load data from file " + self.__f_name + "!") f.close()
def test_setGet(self): b = Book(1, "Fizica", "fiction", "Ilea Carmen") self.assertTrue(b.title == "Fizica") b.id = 2 b.title = "Matematica" b.desc = "somehow human" b.author = "Ganga" self.assertTrue(b == Book(2, "Matematica", "somehow human", "Ganga"))
def __test_file_repo(): """ Function to test file based repository :return: """ val = BookValidator() repo = FileRepository(val, "repository/test_file.txt") repo.clear_file() b = Book("Ion", "Liviu Rebreanu", "Drama", 1912) repo.store_book(b) b = Book("Prislea", "Petre Ispirescu", "Basm", 1970) repo.store_book(b) assert len(repo.get_all()) == 2 b = Book("Limite de functii", "Catalin Pana", "Stiinta", 2016) repo.store_book(b) repo.delete_book(1) assert len(repo.get_all()) == 2 mod_b = Book("Integrale", "Catalin Pana", "Stiinta", 2017) repo.update_book(2, mod_b) assert len(repo.get_all()) == 2 assert repo.delete_book(5) is None assert repo.get_all()[1].get_year() == 2017
def test_search(self): book_repo = Repository() client_repo = Repository() rental_repo = Repository() book_repo.add(Book(1, "Iona", "teatru postbelic", "Marin Sorescu")) book_repo.add(Book(2, "Amintiri din copilarie", "proza", "Ion Creanga")) book_repo.add(Book(3, "1984", "roman", "George Orwell")) st_ctrl = StatisticsController(book_repo, client_repo, rental_repo) l = [] st_ctrl.search(book_repo, "author", "re", l) self.assertTrue(len(l) == 2) rental_repo.add( Rental(2, 2, 10, date.today(), date.today() + timedelta(days=10), False)) most_rented = st_ctrl.mostRentedBooks() self.assertTrue( most_rented[0] == [Book(2, "Amintiri din copilarie", "proza", "Ion Creanga"), 1])
def setUp(self): unittest.TestCase.setUp(self) self.rentalList = MyList() self.bookList = MyList() self.clientList = MyList() self.rentalList.append( Rental(0, 0, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")) self.rentalList.append( Rental(1, 1, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")) self.bookList.append(Book(0, "book0", "desc0", "author1")) self.bookList.append(Book(1, "book1", "desc1", "author0")) self.bookList.append(Book(2, "book2", "desc2", "author2")) self.clientList.append(Client(0, "name1")) self.clientList.append(Client(1, "name0")) self.clientList.append(Client(2, "name2"))
def add_book(self, title, writer, genre, year): """ Function to add a book into the repository :param title: title of the book :param writer: writer of the book :param genre: genre of the book :param year: year of release :return: the newly added book """ book = Book(title, writer, genre, year) self.__undo.append(UndoAdd(self.__repo, book)) self.__repo.store_book(book) return book
def __load_from_file(self): """ Function which loads all the books from the file :return: a list containing all the books from the file """ try: f = open(self.__f_name, "r") except IOError: print(self.__f_name) #file not exists return line = f.readline().strip() books = [] while line != "": comp = line.split(":") book = Book(comp[1], comp[2], comp[3], int(comp[4])) book.set_id(int(comp[0])) books.append(book) line = f.readline().strip() f.close() return books
def _loadFromFile(self): try: f = open(self._fName, "r") except IOError: print("ERROR while trying to open " + self._fName) ID = f.readline().strip() self._nextID = ID ln = f.readline().strip() while ln != "": t = ln.split(" @ ") book = Book(ID=int(t[0]), title=t[1], author=t[2], descr=t[3]) BookRepository.add(self, book) ln = f.readline().strip() f.close
def updateBook(self, args, recForUndo=True): b = Book(int(args[0]), args[1], args[2], args[3]) self._validator.validateBook(b) old_b = deepcopy(self._repo.find(b)) self._repo.upd(b) if recForUndo == True: undo = FunctionCall( self.updateBook, [old_b.id, old_b.title, old_b.desc, old_b.author], False) redo = FunctionCall(self.updateBook, [b.id, b.title, b.desc, b.author], False) operation = Operation(redo, undo) self._undo_ctrl.recordOp(operation)
def modify_book(self, book_id, title, writer, genre, year): """ Function to modify a book by giving the old book's id and the new book :param book_id: id of the old book :param title: title of the book :param writer: writer of the book :param genre: genre of the book :param year: year of release :return: the new book if the old book exists, None otherwise """ book = Book(title, writer, genre, year) to_ret = self.__repo.update_book(book_id, book) if to_ret is not None: self.__undo.append(UndoUpd(self.__repo, book_id, to_ret)) return to_ret
def mostRentedBooks(self): l = {} rentals = self.__rental_repo.getAll() for x in rentals: if x.bookId not in l: l[x.bookId] = 1 else: l[x.bookId] += 1 nr_rentals = [] for x in l.keys(): nr_rentals.append( [self.__book_repo.find(Book(x, "def", "def", "def")), l[x]]) nr_rentals.sort(key=lambda x: x[1], reverse=True) return nr_rentals
def addBook(self, id, title, description, author, recordForUndo=True): """ Adds a new book :param book: class Book :return: book - for undo/redo """ if self.checkId(id) == False: raise InvalidIdException book = Book(id, title, description, author) self._bookRepo.add(book) if recordForUndo == True: undo = FunctionCall(self.removeBook, id, False) redo = FunctionCall(self.addBook, id, title, description, author, False) operation = Operation(redo, undo) self._undoController.recordOperation(operation) return book
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) self._repo.add(r) else: raise LibraryException("This book is not available!")