def rem_book(stdscr, bdb: bb.BookDataBase, adb: ab.AuthorDataBase): current_row = 0 book_dict, id_list = bdb.return_dict_by_author() aut_list = adb.return_list() list_copy = list() for i in book_dict: list_copy.append(f"{book_dict[i]} by {aut_list[int(i)]} ") list_copy.append('Exit') while 1: print_list(stdscr, current_row, list_copy, "Books") key = stdscr.getch() if key == curses.KEY_UP and current_row > 0: current_row -= 1 elif key == curses.KEY_DOWN and current_row < len(list_copy) - 1: current_row += 1 elif key == curses.KEY_ENTER or key in [10, 13]: if current_row == len(list_copy) - 1: break else: if bdb.remove_book(id_list[current_row]): bdb.save_books() print_center(stdscr, "Success - press key to return") stdscr.getch() else: print_center(stdscr, "Error - press key to return") stdscr.getch() break
def test_remove_missing_record(self): database = BookDataBase(self.file) book = Book("What", "Steven Law", 2000, "Thriller", 0.2, 3) with open(self.file, 'w') as file: file.write("What+Steven Maw+2000+Thriller+0.2+20\n") file.write("Go+Steven Law+2000+Thriller+0.2+20\n") with self.assertRaises(MissingBookError): database.remove_record(book)
def test_booklist(self): database = BookDataBase(self.file) book = Book("What", "Steven Law", 2000, "Thriller", 0.2, 3) book1 = Book("What", "Steven Maw", 2000, "Thriller", 0.2, 20) book2 = Book("Go", "Steven Law", 2000, "Thriller", 0.2, 20) booklist = [book, book1, book2] with open(self.file, 'w') as file: file.write("What+Steven Law+2000+Thriller+0.2+3\n") file.write("What+Steven Maw+2000+Thriller+0.2+20\n") file.write("Go+Steven Law+2000+Thriller+0.2+20\n") self.assertEqual(booklist, database.booklist()) os.remove(os.path.realpath(self.file))
def test_remove_record(self): database = BookDataBase(self.file) book = Book("What", "Steven Law", 2000, "Thriller", 0.2, 3) book1 = Book("What", "Steven Maw", 2000, "Thriller", 0.2, 20) book2 = Book("Go", "Steven Law", 2000, "Thriller", 0.2, 20) booklist = [book, book1, book2] recordlist = [book.special_record() for book in booklist] with open(self.file, 'w') as file: file.write("What+Steven Law+2000+Thriller+0.2+3\n") file.write("What+Steven Maw+2000+Thriller+0.2+20\n") file.write("Go+Steven Law+2000+Thriller+0.2+20\n") database.remove_record(book2) del recordlist[2] with open(self.file, 'r') as file: records_after_removal = file.readlines() self.assertEqual(recordlist, records_after_removal) database.remove_record(book) del recordlist[0] with open(self.file, 'r') as file: records_after_removal = file.readlines() self.assertEqual(recordlist, records_after_removal) os.remove(os.path.realpath(self.file))
def add_book(stdscr, bdb: bb.BookDataBase): h, w = stdscr.getmaxyx() stdscr.clear() stdscr.addstr(0, w // 2 - len("Title") // 2, "Title") title = read_word(stdscr, h, w) stdscr.clear() stdscr.addstr(0, w // 2 - len("Isbn") // 2, "Isbn") isbn = read_word(stdscr, h, w) stdscr.clear() stdscr.addstr(0, w // 2 - len("Category") // 2, "Category") category = read_word(stdscr, h, w) stdscr.clear() stdscr.addstr(0, w // 2 - len("Author id") // 2, "Author id") aut_id = read_word(stdscr, h, w) book = bk.MyBook(aut_id, title, isbn, category) if bdb.add_book(book): bdb.save_books() print_center(stdscr, "Success - press key to return") stdscr.getch() else: print_center(stdscr, "Error - press key to return") stdscr.getch()
def test_add_record(self): database = BookDataBase(self.file) book = Book("Somewhere", "Steven Law", 2000, "Fantasy", 0.2, 3) book1 = Book("What", "Steven Maw", 2000, "Triller", 0.2, 20) book2 = Book("Go", "Steven Law", 2000, "Thriller", 0.2, 20) book3 = Book("Go", "Steven Law", 2000, "Thriller", 0.8, 12) booklist = [book, book1, book2] expected_records = [book.special_record() for book in booklist] database.add_record(book) database.add_record(book1) database.add_record(book2) with open(self.file, 'r') as file: actual_records = file.readlines() self.assertEqual(expected_records, actual_records) database.add_record(book3) book3.increase_number_of_copies(20) booklist = [book, book1, book3] new_expected_records = [book.special_record() for book in booklist] with open(self.file, 'r') as file: updated_records = file.readlines() self.assertEqual(new_expected_records, updated_records) os.remove(os.path.realpath(self.file))