def test_save_add_to_db(self): bk = Book('AAA', 'BBB', True) bk.save() self.assertIsNotNone(bk.id) # Check book has ID store = BookStore() self.assertEqual(bk, store.get_book_by_id(bk.id)) self.assertTrue(store.exact_match(bk))
def test_save_update_changes_to_db(self): bk = Book('CCC', 'DDD', True) bk.save() # Change some attributes and save bk.author = 'EEE' bk.title = 'FFF' bk.read = False bk.save() store = BookStore() # Check DB has same data as bk Book object self.assertEqual(bk, store.get_book_by_id(bk.id)) self.assertTrue(bk, store.exact_match(bk))
def setUp(self): self.BS = BookStore() self.clear_bookstore()
class TestBookstore(TestCase): def setUp(self): self.BS = BookStore() self.clear_bookstore() def add_test_data(self): self.bk1 = Book('An Interesting Book', 'Ann Author', True) self.bk2 = Book('Booky Book Book', 'B. Bookwriter', False) self.bk3 = Book('Collection of words', 'Creative Creator') self.clear_bookstore() self.BS.add_book(self.bk1) self.BS.add_book(self.bk2) self.BS.add_book(self.bk3) def clear_bookstore(self): self.BS.delete_all_books() def test_singleton(self): bs = BookStore() bs2 = BookStore() self.assertEqual(bs, bs2) def test_add_book_empty_store(self): self.BS.delete_all_books() bk = Book('aa', 'aaa') self.BS.add_book(bk) self.assertTrue(self.BS.get_book_by_id(bk.id)) self.assertEqual(1, self.BS.book_count()) def test_add_book(self): self.add_test_data() book_count = self.BS.book_count() bk = Book('aa', 'bbbbb') self.BS.add_book(bk) self.assertTrue(self.BS.get_book_by_id(bk.id)) self.assertEqual(book_count + 1, self.BS.book_count()) def test_add_book_duplicate_errors(self): bk = Book('aa', 'aaa') self.BS.add_book(bk) with self.assertRaises(BookError): bk_dupe = Book('aa', 'aaa') self.BS.add_book(bk_dupe) def test_delete_book(self): self.add_test_data() count = self.BS.book_count() delete_book(self.bk2) self.assertEqual(count - 1, self.BS.book_count()) self.assertIsNone(self.BS.get_book_by_id(self.bk2.id)) def test_delete_book_not_in_store_errors(self): self.add_test_data() bk = Book('Not in store', 'Not in store') with self.assertRaises(BookError): delete_book(bk) def test_delete_book_empty_list_errors(self): self.clear_bookstore() bk = Book('Not in store', 'Not in store') with self.assertRaises(BookError): delete_book(bk) def test_delete_all_books(self): self.clear_bookstore() bk1 = Book('Not in store', 'Not in store') bk2 = Book('Whatever', 'Whatever') self.BS.add_book(bk1) self.BS.add_book(bk2) self.BS.delete_all_books() self.assertEqual(0, self.BS.book_count()) def test_delete_all_books_empty(self): self.clear_bookstore() self.BS.delete_all_books() self.assertEqual(0, self.BS.book_count()) def test_set_read_book_read(self): self.add_test_data() self.BS.set_book_read(self.bk1.id, True) def test_set_unread_book_read(self): self.add_test_data() self.BS.set_book_read(self.bk2.id, True) def test_set_read_book_unread(self): self.add_test_data() self.BS.set_book_read(self.bk1.id, False) def test_set_unread_book_unread(self): self.add_test_data() self.BS.set_book_read(self.bk2.id, False) def test_set_book_read_not_found_errors(self): bk = Book('Not in store', 'Not in store') with self.assertRaises(BookError): self.BS.set_book_read(bk.id, True) def test_search_book_author_match(self): self.add_test_data() self.assertCountEqual([self.bk1], self.BS.book_search('Ann')) def test_search_book_title_match(self): self.add_test_data() self.assertCountEqual([self.bk1, self.bk2], self.BS.book_search('Book')) def test_search_book_not_found(self): self.add_test_data() self.assertEqual([], self.BS.book_search('Not in list')) def test_search_book_empty_store(self): self.clear_bookstore() self.assertEqual([], self.BS.book_search('Not in list')) def test_search_book_case_insensitive_title_match(self): self.add_test_data() self.assertCountEqual([self.bk1, self.bk2], self.BS.book_search('bOoK')) def test_search_book_case_insensitive_author_match(self): self.add_test_data() self.assertCountEqual([self.bk3], self.BS.book_search('cReAtOr')) def test_get_books_by_read_read(self): self.add_test_data() read_books = self.BS.get_books_by_read_value(True) self.assertCountEqual([self.bk1], read_books) def test_get_books_by_read_unread(self): self.add_test_data() read_books = self.BS.get_books_by_read_value(False) self.assertCountEqual([self.bk2, self.bk3], read_books)
def test_singleton(self): bs = BookStore() bs2 = BookStore() self.assertEqual(bs, bs2)
""" Program to create and manage a list of books that the user wishes to read, and books that the user has read. """ from bookstore import Book, BookStore from menu import Menu import ui store = BookStore() def main(): menu = create_menu() while True: choice = ui.display_menu_get_choice(menu) action = menu.get_action(choice) action() if choice == 'Q': break def create_menu(): menu = Menu() menu.add_option('1', 'Add Book', add_book) menu.add_option('2', 'Search For Book', search_book) menu.add_option('3', 'Show Unread Books', show_unread_books) menu.add_option('4', 'Show Read Books', show_read_books) menu.add_option('5', 'Show All Books', show_all_books) menu.add_option('6', 'Change Book Read Status', change_read) menu.add_option('7', 'Delete Book', delete_book) menu.add_option('Q', 'Quit', quit_program)
class TestBookstore(TestCase): @classmethod def setUpClass(cls): bookstore.db = os.path.join('database', 'test_books.db') BookStore.instance = None def setUp(self): self.BS = BookStore() self.clear_bookstore() def add_test_data(self): self.clear_bookstore() self.bk1 = Book('An Interesting Book', 'Ann Author', True) self.bk2 = Book('Booky Book Book', 'B. Bookwriter', False) self.bk3 = Book('Collection of words', 'Creative Creator') self.bk1.save() self.bk2.save() self.bk3.save() def clear_bookstore(self): self.BS.delete_all_books() def test_singleton(self): bs = BookStore() bs2 = BookStore() self.assertEqual(bs, bs2) def test_add_book_empty_store(self): bk = Book('aa', 'aaa') bk.save() self.assertTrue(self.BS.exact_match(bk)) self.assertEqual(1, self.BS.book_count()) def test_add_book_store_with_books_in(self): self.add_test_data() book_count = self.BS.book_count() bk = Book('aa', 'bbbbb') bk.save() self.assertTrue(self.BS.exact_match(bk)) self.assertEqual(book_count + 1, self.BS.book_count()) def test_add_book_duplicate_errors(self): bk = Book('aa', 'aaa') bk.save() with self.assertRaises(BookError): bk_dupe = Book('aa', 'aaa') bk_dupe.save() def test_add_book_duplicate_errors_case_insensitive(self): bk = Book('a', 'a') bk.save() with self.assertRaises(BookError): bk_dupe = Book('a', 'A') bk_dupe.save() def test_get_book_by_id_found(self): self.add_test_data() result = self.BS.get_book_by_id(self.bk1.id) self.assertEqual(result, self.bk1) def test_get_book_by_id_not_found(self): # This test fails - student should fix self.add_test_data() result = self.BS.get_book_by_id(-1) self.assertIsNone(result) def test_delete_book(self): self.add_test_data() count = self.BS.book_count() self.BS.delete_book(self.bk2.id) self.assertEqual(count - 1, self.BS.book_count()) self.assertFalse(self.BS.exact_match(self.bk2)) def test_delete_book_not_in_store_errors(self): self.add_test_data() bk = Book('Not in store', 'Not in store') with self.assertRaises(BookError): self.BS.delete_book(-10) def test_delete_book_empty_list_errors(self): bk = Book('Not in store', 'Not in store') with self.assertRaises(BookError): self.BS.delete_book(-100) def test_delete_all_books(self): bk1 = Book('Not in store', 'Not in store') bk2 = Book('Whatever', 'Whatever') bk1.save() bk2.save() self.BS.delete_all_books() self.assertEqual(0, self.BS.book_count()) def test_delete_all_books_empty(self): self.BS.delete_all_books() self.assertEqual(0, self.BS.book_count()) def test_count_books(self): self.add_test_data() count = self.BS.book_count() self.assertEqual(3, count) def test_set_read_book_read(self): self.add_test_data() self.bk1.read = True self.bk1.save() bk1_from_store = self.BS.get_book_by_id(self.bk1.id) self.assertTrue(bk1_from_store.read) def test_set_unread_book_read(self): self.add_test_data() self.bk2.read = True self.bk2.save() bk2_from_store = self.BS.get_book_by_id(self.bk2.id) self.assertTrue(bk2_from_store.read) def test_set_read_book_unread(self): self.add_test_data() self.bk1.read = False self.bk1.save() bk1_from_store = self.BS.get_book_by_id(self.bk1.id) self.assertFalse(bk1_from_store.read) def test_set_unread_book_unread(self): self.add_test_data() self.bk2.read = False self.bk2.save() bk2_from_store = self.BS.get_book_by_id(self.bk2.id) self.assertFalse(bk2_from_store.read) def test_get_all_books(self): self.add_test_data() self.assertCountEqual([self.bk1, self.bk2, self.bk3], self.BS.get_all_books()) def test_is_book_in_store_present(self): self.add_test_data() self.assertTrue(self.BS.exact_match(self.bk1)) self.assertTrue(self.BS.exact_match(self.bk2)) self.assertTrue(self.BS.exact_match(self.bk3)) def test_is_book_in_store_not_present(self): not_in_store = Book('aaaa', 'bbbb') self.add_test_data() self.assertFalse(self.BS.exact_match(not_in_store)) def test_is_book_in_store_empty_list(self): self.clear_bookstore() not_in_store = Book('aaaa', 'bbbb') self.assertFalse(self.BS.exact_match(not_in_store)) def test_search_book_author_match(self): self.add_test_data() self.assertCountEqual([self.bk1], self.BS.book_search('Ann')) def test_search_book_title_match(self): self.add_test_data() self.assertCountEqual([self.bk1, self.bk2], self.BS.book_search('Book')) def test_search_book_not_found(self): self.add_test_data() self.assertEqual([], self.BS.book_search('Not in list')) def test_search_book_empty_store(self): self.clear_bookstore() self.assertEqual([], self.BS.book_search('Not in list')) def test_search_book_case_insensitive_title_match(self): self.add_test_data() self.assertCountEqual([self.bk1, self.bk2], self.BS.book_search('bOoK')) def test_search_book_case_insensitive_author_match(self): self.add_test_data() self.assertCountEqual([self.bk3], self.BS.book_search('cReAtOr')) def test_exact_match_found(self): self.add_test_data() bk = Book('Collection of words', 'Creative Creator') self.assertTrue(self.BS.exact_match(bk)) def test_exact_match_not_found_author(self): self.add_test_data() bk = Book('Collection of words', 'Someone Else') self.assertFalse(self.BS.exact_match(bk)) def test_exact_match_not_found_title(self): self.add_test_data() bk = Book('Collection of Stories', 'Creative Creator') self.assertFalse(self.BS.exact_match(bk)) def test_exact_match_not_found_title_author(self): self.add_test_data() bk = Book('Collection of Cheese', 'Beyonce') self.assertFalse(self.BS.exact_match(bk)) def test_exact_match_not_found_empty_store(self): bk = Book('Whatever', 'Whatever') self.clear_bookstore() self.assertFalse(self.BS.exact_match(bk)) def test_get_books_by_read_read(self): self.add_test_data() read_books = self.BS.get_books_by_read_value(True) self.assertCountEqual([self.bk1], read_books) def test_get_books_by_read_unread(self): self.add_test_data() read_books = self.BS.get_books_by_read_value(False) self.assertCountEqual([self.bk2, self.bk3], read_books)
class TestWishList(TestCase): """ menu.add_option('1', 'Add Book', add_book) menu.add_option('2', 'Search For Book', search_book) menu.add_option('3', 'Show Unread Books', show_unread_books) menu.add_option('4', 'Show Read Books', show_read_books) menu.add_option('5', 'Show All Books', show_all_books) menu.add_option('6', 'Change Book Read Status', change_read) """ def setUp(self): self.store = BookStore() self.store.delete_all_books() Counter.reset_counter() def add_test_data(self): self.bk1 = Book('the title', 'the author', False) self.bk2 = Book('what the book is called', 'the writer', True) self.bk3 = Book('fascinating', 'the author', True) self.bk4 = Book('brilliant', 'schrodinger', False) self.store.add_book(self.bk1) self.store.add_book(self.bk2) self.store.add_book(self.bk3) self.store.add_book(self.bk4) @patch('builtins.input', side_effect=['1', 'Title', 'Author', 'Q']) @patch('builtins.print') def test_add_book(self, mock_print, mock_input): main.main() # reset counter and make book that mimics the one expected to be created Counter.reset_counter() expected_book = Book('Title', 'Author', False) all_books = self.store.get_all_books() self.assertEqual(expected_book, all_books[0]) @patch('builtins.input', side_effect=['1', 'Title', 'Author', '1', 'title', 'author', 'Q']) @patch('builtins.print') def test_add_book_prevent_duplicates(self, mock_print, mock_input): main.main() # reset counter and make book that mimics the first one expected to be created Counter.reset_counter() expected_book = Book('Title', 'Author', False) all_books = self.store.get_all_books() self.assertEqual(expected_book, all_books[0]) self.assertEqual(1, self.store.book_count()) @patch('builtins.input', side_effect=['2', 'call', 'Q']) # match bk2 @patch('builtins.print') def test_search_for_book_found(self, mock_print, mock_input): self.add_test_data() main.main() mock_print.assert_any_call(self.bk2) @patch('builtins.input', side_effect=['2', 'the author', 'Q']) # Partial match bk1 and bk3 @patch('builtins.print') def test_search_for_book_multiple_books_found(self, mock_print, mock_input): self.add_test_data() # assert bk1 and bk3 is printed main.main() mock_print.assert_any_call(self.bk1) mock_print.assert_any_call(self.bk3) @patch('builtins.input', side_effect=['2', 'jk rowling', 'Q']) # No match @patch('builtins.print') def test_search_for_book_not_found(self, mock_print, mock_input): self.add_test_data() main.main() mock_print.assert_any_call('No books to display') @patch('builtins.input', side_effect=['3', 'Q']) @patch('builtins.print') def test_show_unread_books(self, mock_print, mock_input): self.add_test_data() main.main() mock_print.assert_any_call(self.bk1) mock_print.assert_any_call(self.bk4) @patch('builtins.input', side_effect=['4', 'Q']) @patch('builtins.print') def test_show_read_books(self, mock_print, mock_input): self.add_test_data() main.main() mock_print.assert_any_call(self.bk2) mock_print.assert_any_call(self.bk3) @patch('builtins.input', side_effect=['5', 'Q']) @patch('builtins.print') def test_show_all_books(self, mock_print, mock_input): self.add_test_data() main.main() mock_print.assert_any_call(self.bk1) mock_print.assert_any_call(self.bk2) mock_print.assert_any_call(self.bk3) mock_print.assert_any_call(self.bk4) @patch('builtins.input', side_effect=['6', '4', 'read', 'Q']) # Change book Id 4 to read @patch('builtins.print') def test_change_book_read_status(self, mock_print, mock_input): self.add_test_data() main.main() self.assertTrue(self.bk4.read) @patch('builtins.input', side_effect=['6', '3', 'not read', 'Q']) # Change book Id 3 to unread @patch('builtins.print') def test_change_book_read_status_unread(self, mock_print, mock_input): self.add_test_data() main.main() self.assertFalse(self.bk3.read) @patch('builtins.input', side_effect=['6', '42', 'not read', 'Q']) # Change book Id 42 (not found) to unread, @patch('builtins.print') def test_change_book_read_status_book_not_found(self, mock_print, mock_input): self.add_test_data() main.main() # Not crash? All's well. @patch('builtins.input', side_effect=['7', '3', 'Q']) # Delete book ID 3 @patch('builtins.print') def test_delete_book_in_list(self, mock_print, mock_input): self.add_test_data() main.main() self.assertNotIn(self.bk3, self.store.get_all_books()) @patch('builtins.input', side_effect=['7', '42', 'Q']) # Delete book ID 42, not a book in list @patch('builtins.print') def test_delete_book_not_found_in_list(self, mock_print, mock_input): self.add_test_data() main.main() self.assertEqual(4, self.store.book_count())
def setUp(self): self.store = BookStore() self.store.delete_all_books() Counter.reset_counter()
def delete_book(): try: which_book = ui.get_book_id() BookStore._delete_book(which_book) except: print('Error: Book Not Found')
def setUp(self): self.store = BookStore() self.store.delete_all_books()