def setUp(self): self.CuT = Library_DB() self.CuT.close_db() self.CuT.db = Mock() self.patron1 = Patron("Reggie", "Miller", 49, "987654321") self.patron2 = Patron("Todd", "Dodd", 20, "12345") self.patron1.add_borrowed_book("Old Man and the Sea")
def test_constructor_numberName(self): try: Patron("yupp", "5678", 49, "yert4321") except InvalidNameException: self.assertTrue(True) return self.assertTrue(False, "Expected InvalidNumberException")
def setUp(self, mockBooks_API, mockLibrary_DB): self.library = Library() self.patron = Patron("bob", "bobson", 41, 1) self.library.api = mockBooks_API self.library.db = mockLibrary_DB mockBooks_API.get_ebooks.return_value = [{ 'title': 'The Count of Monte Cristo', "ebook_count": 1 }, { 'title': 'A Tale of Two Cities', "ebook_count": 1 }, { 'title': '1984', "ebook_count": 1 }, { 'title': 'Slaughterhouse 5', "ebook_count": 1 }, { 'title': 'Breakfast of Champions', "ebook_count": 1 }] mockBooks_API.books_by_author.return_value = [ 'Slaughterhouse 5', 'Breakfast of Champions' ] mockBooks_API.get_book_info.return_value = [{ 'title': 'The Count of Monte Cristo', "ebook_count": 1, 'publisher': 'Penguin', 'publish_year': '1844', 'language': "french" }] mockLibrary_DB.patrons = [self.patron] mockLibrary_DB.update_patron = self.mock_update_patron mockLibrary_DB.insert_patron.return_value = 1 mockLibrary_DB.retrieve_patron = self.mock_retrieve_patron
def retrieve_patron(self, memberID): query = Query() # assuming no two people in the db have the same memberID results = self.db.search(query.memberID == memberID) if results: return Patron(results[0]['fname'], results[0]['lname'], results[0]['age'], results[0]['memberID']) return None
def register_patron(self, fname, lname, age, memberID): """Registers a Patron with the library and adds them to the database. :param fname: the Patron's first name :param lname: the Patron's last name :param age: the Patron's age :param memberID: the ID of the Patron :returns: None if the Patron is already in the database, else their ID """ patron = Patron(fname, lname, age, memberID) return self.db.insert_patron(patron)
def retrieve_patron(self, memberID): """Gets a Patron from the database. :param memberID: the ID for the Patron to retrieve :returns: the Patron with the given ID, or None """ query = Query() # assuming no two people in the db have the same memberID results = self.db.search(query.memberID == memberID) if results: return Patron(results[0]['fname'], results[0]['lname'], results[0]['age'], results[0]['memberID']) return None
def test_patron_is_not_registered_in_db(self): # Assume self.CuT.db = Mock() self.CuT.db.insert_patron.return_value = 1 self.CuT.db.retrieve_patron.return_value = False patron = Patron(self.first_name, self.last_name, self.age, self.member_id) patron.get_memberID = Mock(return_value=1) # Action self.CuT.register_patron(self.first_name, self.last_name, self.age, self.member_id) is_patron_registered = self.CuT.is_patron_registered(patron) # Removes the instance of the db.json created if path.exists('db.json'): os.remove('db.json') # Assert self.assertFalse(is_patron_registered)
def test_get_books(self): patron = Patron("first", "last", 23, "98765432") books = patron.get_borrowed_books() self.assertFalse(books, "Expected empty list")
def test_ne(self): patron1 = self.get_patron() patron2 = Patron('Stephen', 'Hook', 23, 2) self.assertTrue(patron1.__ne__(patron2))
def register_patron(self, fname, lname, age, memberID): patron = Patron(fname, lname, age, memberID) return self.db.insert_patron(patron)
def test_ne(self): patron1 = Patron("burst", "ghast", 33, "23456789") patron2 = Patron("first", "last", 23, "98765432") self.assertTrue(patron1 != patron2)
def test_constructor(self): Patron("mister", "twister", 49, "yert4321") # constructor executed without error self.assertTrue(True)
def test_return_book(self): book = "Lord of the Flies" patron = Patron("first", "last", 23, "98765432") patron.add_borrowed_book(book) patron.return_borrowed_book(book) self.assertEqual(len(patron.get_borrowed_books()), 0)
def test_eq(self): patron1 = Patron("first", "last", 23, "98765432") patron2 = Patron("first", "last", 23, "98765432") self.assertTrue(patron1 == patron2)
def test_is_patron_registered(self): library = self.library patron = self.patron p2 = Patron("bob", "bobson", 41, 2) self.assertTrue(library.is_patron_registered(patron)) self.assertFalse(library.is_patron_registered(p2))
def test_add_new_book(self): book = "Lord of the Flies" patron = Patron("first", "last", 23, "98765432") patron.add_borrowed_book(book) self.assertEqual(patron.get_borrowed_books()[0], "Lord of the Flies".lower())
def test_get_lname(self): lname = "twister" patron = Patron("mister", lname, 49, "yert4321") self.assertEqual(patron.get_lname(), "twister")
def test_get_fname(self): fname = "mister" patron = Patron(fname, "twister", 49, "yert4321") self.assertEqual(patron.get_fname(), "mister")
def test_get_age(self): age = 49 patron = Patron("mister", "twister", age, "yert4321") self.assertEqual(patron.get_age(), 49)
def test_get_memberID(self): ID = "yert4321" patron = Patron("mister", "twister", 49, ID) self.assertEqual(patron.get_memberID(), "yert4321")
def get_patron(): return Patron("Stephen", "Cook", 22, 1)