class TestDB(unittest.TestCase): 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_insert_patron_not_Patron(self): self.assertIsNone(self.CuT.insert_patron(None)) def test_insert_patron_already_inserted(self): self.CuT.retrieve_patron = Mock() self.CuT.retrieve_patron.return_value = self.patron2 self.assertIsNone(self.CuT.insert_patron(self.patron2)) def test_insert_patron_success(self): self.CuT.retrieve_patron = Mock() self.CuT.retrieve_patron.return_value = None self.assertIsNotNone(self.CuT.insert_patron(self.patron2)) def test_get_patron_count(self): self.CuT.db.all.return_value = [] self.assertEqual(0, self.CuT.get_patron_count()) def test_get_all_patrons(self): self.CuT.db.all.return_value = ["test", "filler"] self.assertEqual(["test", "filler"], self.CuT.get_all_patrons()) def test_update_patron_not_Patron(self): self.assertIsNone(self.CuT.update_patron(None)) def test_update_patron_success(self): self.patron2.add_borrowed_book("test") self.CuT.update_patron(self.patron2) self.CuT.db.update.assert_called_once() def test_retrieve_patron_not_found(self): self.CuT.db.search.return_value = None self.assertIsNone(self.CuT.retrieve_patron(None)) def test_retrieve_patron_success(self): self.CuT.db.search.return_value = [{ "fname": "Reggie", "lname": "Miller", "age": 49, "memberID": "987654321", "borrowed_books": [] }] patron = self.CuT.retrieve_patron(self.patron1) self.assertEqual(patron.memberID, self.patron1.memberID) def test_close_db(self): self.CuT.close_db() self.assertTrue(self.CuT.db.close.called) def test_convert_patron_to_db_format(self): self.assertIsNotNone(self.CuT.convert_patron_to_db_format( self.patron1))
def test_close_db(self): lib_db = Library_DB() with patch('tinydb.TinyDB') as mock_db: mock_db.close = True self.assertIsNone(lib_db.close_db())
class TestLibraryDBInterface(unittest.TestCase): CuT = Library_DB() CuT.close_db() def tearDown(self): if os.path.exists('db.json'): os.remove('db.json') def test_insert_patron_not_patron(self): self.assertEqual(None, self.CuT.insert_patron(0)) def test_insert_patron_mock_patron_not_in_db(self): mock_patron = Mock(Patron) mock_patron.get_memberid = Mock() mock_patron.get_memberid.return_value = ('1') self.CuT = Library_DB() self.CuT.close_db() self.CuT.db = Mock() self.CuT.db.search = Mock() self.CuT.db.search.return_value = None # patron is not in db self.CuT.db.insert = Mock() self.CuT.db.insert.return_value = 1 self.CuT.insert_patron(mock_patron) self.CuT.db.insert.assert_called_once( ) # patron is inserted if this happens def test_insert_patron_mock_patron_in_db(self): mock_patron = Mock(Patron) mock_patron.get_memberid = Mock() mock_patron.get_memberid.return_value = ('1') self.CuT.db = Mock() self.CuT.db.search = Mock() self.CuT.db.search.return_value = mock_patron # patron is in db self.CuT.db.insert = Mock() self.CuT.db.insert.return_value = 1 self.CuT.retrieve_patron = Mock() self.CuT.retrieve_patron.return_value = mock_patron # Can't subscript a mock so do this for now # and test retrieve_patron method later self.CuT.insert_patron(mock_patron) self.CuT.db.insert.assert_not_called( ) # patron is not inserted because patron is in db def test_get_patron_count(self): self.CuT.db = Mock() self.CuT.db.all = Mock() self.CuT.db.all.return_value = [] # list of patrons in db self.assertEqual(0, self.CuT.get_patron_count()) def test_get_patron_count_multiple(self): mock_patron = Mock(Patron) mock_patron.get_memberid = Mock() self.CuT.db = Mock() self.CuT.db.all = Mock() self.CuT.db.all.return_value = [mock_patron, mock_patron] # list of patrons in db self.assertEqual(2, self.CuT.get_patron_count()) def test_get_all_patrons(self): self.CuT.db = Mock() self.CuT.db.all = Mock() self.CuT.db.all.return_value = [] # list of patrons in db self.assertEqual([], self.CuT.get_all_patrons()) def test_get_all_patrons_multiple(self): mock_patron = Mock(Patron) mock_patron.get_memberid = Mock() self.CuT.db = Mock() self.CuT.db.all = Mock() self.CuT.db.all.return_value = [mock_patron, mock_patron] # list of patrons in db self.assertEqual([mock_patron, mock_patron], self.CuT.get_all_patrons()) def test_update_patron_not_patron(self): self.assertEqual(None, self.CuT.update_patron(0)) def test_update_patron(self): mock_patron = Mock(Patron) mock_patron.get_memberid = Mock() self.CuT.db = Mock() self.CuT.convert_patron_to_db_format = Mock() self.CuT.convert_patron_to_db_format.return_value = mock_patron self.CuT.db.update = Mock() self.CuT.update_patron(mock_patron) self.CuT.db.update.assert_called_once() def test_retrieve_patron(self): self.CuT = Library_DB() self.CuT.close_db() self.CuT.db = Mock() self.CuT.db.search = Mock() self.CuT.db.search.return_value = False self.assertEqual(None, self.CuT.retrieve_patron(1)) def test_close_db(self): self.CuT.db = Mock() self.CuT.db.close = Mock() self.CuT.close_db() self.CuT.db.close.assert_called_once()