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 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))
# --------------------------------------------------------- # # Do not change this file. # Hints are provided at the bottom of this file. # # --------------------------------------------------------- from book_database import Book turing_book = Book.search("enigma") assert type(turing_book) is Book assert turing_book.publisher == "Princeton University." assert len(Book.publishers) == 15 ##### print("* All assertions passed. Nice work!") ### HINTS: ### ------- ### 1. Before starting to code, run this file and read the error message. ### 2. Do the Simplest Thing That Can Possibly Work to resolve the error. ### 3. Run the file again. Use Error-Driven Development until all assertions pass. ### 4. To read a CSV-formatted file with Python's CSV facilities: # # with open("books.csv", encoding='utf-8') as file: # reader = csv.reader(file)
# --------------------------------------------------------- # # Do not change this file. # Hints are provided at the bottom of this file. # # --------------------------------------------------------- from book_database import Book turing_book = Book.search('enigma') print(turing_book.title) # this prints <book_database.Book object at 0x1021046a0> assert type(turing_book) is Book assert turing_book.publisher == "Princeton University." assert len(Book.publishers) == 15 ##### print("* All assertions passed. Nice work!") ### HINTS: ### ------- ### 1. Before starting to code, run this file and read the error message. ### 2. Do the Simplest Thing That Can Possibly Work to resolve the error. ### 3. Run the file again. Use Error-Driven Development until all assertions pass. ### 4. To read a CSV-formatted file with Python's CSV facilities: # # with open("books.csv", encoding='utf-8') as file: # reader = csv.reader(file) # for row in reader: