def test_delete_hold(app): """ Test that we can delete a hold correctly. """ with app.app_context(): db.drop_all() db.create_all() # Create everything book = utils._get_book() hold = utils._get_hold() patron = utils._get_patron() hold.book = book hold.patron = patron db.session.add(book) db.session.add(patron) db.session.add(hold) db.session.commit() # Check that everything exists assert Book.query.count() == 1 assert Patron.query.count() == 1 assert Hold.query.count() == 1 # Delete hold db.session.delete(hold) db.session.commit() # Check that the right thing has changed assert Book.query.count() == 1 assert Patron.query.count() == 1 assert Hold.query.count() == 0
def test_hold_ondelete_patron(app): """ Tests that hold's patron foreign key is set to null when the patron is deleted. """ with app.app_context(): db.drop_all() db.create_all() # Create everything hold = utils._get_hold() patron = utils._get_patron() book = utils._get_book() hold.patron = patron hold.book = book db.session.add(hold) db.session.add(book) db.session.add(patron) db.session.commit() # Delete patron associated with the hold db.session.delete(patron) db.session.commit() # Check that foreign key is now null assert hold.patron is None
def test_hold_ondelete_item(app): """ Tests that holds are deleted when their book is deleted """ with app.app_context(): db.drop_all() db.create_all() # Create everything hold1 = utils._get_hold() hold2 = utils._get_hold() patron1 = utils._get_patron() patron2 = utils._get_patron(barcode=123789, email="*****@*****.**") book = utils._get_book() hold1.patron = patron1 hold1.book = book hold2.patron = patron2 hold2.book = book db.session.add(hold1) db.session.add(hold2) db.session.add(book) db.session.add(patron1) db.session.add(patron2) db.session.commit() # Check that everything exists assert Patron.query.count() == 2 assert Book.query.count() == 1 assert Hold.query.count() == 2 # Delete book db.session.delete(book) db.session.commit() # Check that book was deleted and as a consequence, the holds were deleted assert Patron.query.count() == 2 assert Book.query.count() == 0 assert Hold.query.count() == 0
def test_create_instances(app): """ Tests that we can create one instance of each model and save them to the database using valid values for all columns. After creation, test that everything can be found from database, and that all relationships have been saved correctly. """ with app.app_context(): db.drop_all() db.create_all() # Create everything patron = utils._get_patron() book = utils._get_book() loan = utils._get_loan() hold = utils._get_hold() loan.patron = patron loan.book = book hold.patron = patron hold.book = book db.session.add(patron) db.session.add(book) db.session.add(hold) db.session.add(loan) db.session.commit() # Check that everything exists assert Patron.query.count() == 1 assert Book.query.count() == 1 assert Loan.query.count() == 1 assert Hold.query.count() == 1 db_patron = Patron.query.first() db_item = Book.query.first() db_loan = Loan.query.first() db_hold = Hold.query.first() # Check all relationships (both sides) assert db_loan.patron == patron assert db_loan.book == book assert db_hold.patron == patron assert db_hold.book == book assert db_hold in db_patron.holds assert db_loan in db_patron.loans assert db_hold in db_item.holds assert db_loan in db_item.loan
def test_update_hold_patron(app): """ Test that we can update the patron of a hold. """ with app.app_context(): db.drop_all() db.create_all() # Create everything book = utils._get_book() hold = utils._get_hold() patron1 = utils._get_patron() hold.book = book hold.patron = patron1 db.session.add(book) db.session.add(patron1) db.session.add(hold) db.session.commit() # Check that everything exists assert Book.query.count() == 1 assert Patron.query.count() == 1 assert Hold.query.count() == 1 db_hold = Hold.query.first() old_patron_id = db_hold.patron_id # Change patron associated with the hold and commit patron2 = utils._get_patron(barcode="123852", email="*****@*****.**") hold.patron = patron2 db.session.add(patron2) db.session.commit() # Check that patron has changed db_hold = Hold.query.first() assert db_hold.patron_id != old_patron_id
def test_query_filters(app): """ Test that we can query everything using multiple filters and find all, one, or some of the instances. """ with app.app_context(): db.drop_all() db.create_all() # Filter Patron queries by firstname, barcode and email patron1 = utils._get_patron(barcode="123456") patron2 = utils._get_patron(barcode="456789", email="*****@*****.**") patron3 = utils._get_patron(barcode="789456", email="*****@*****.**", firstname="Jussi") db.session.add(patron1) db.session.add(patron2) db.session.add(patron3) db.session.commit() assert Patron.query.count() == 3 assert Patron.query.filter_by(firstname="Testi").count() == 2 assert Patron.query.filter_by(firstname="Jussi").count() == 1 assert Patron.query.filter_by(barcode="456789").count() == 1 assert Patron.query.filter(Patron.email!="*****@*****.**").count() == 2 # Filter Book queries book1 = utils._get_book() book2 = utils._get_book(barcode="234789") book3 = utils._get_book(barcode="256789", pubyear=2018) db.session.add(book1) db.session.add(book2) db.session.add(book3) db.session.commit() assert Book.query.count() == 3 assert Book.query.filter_by(barcode="234789").count() == 1 assert Book.query.filter_by(title="Testikirja").count() == 3 assert Book.query.filter(Book.pubyear>2019).count() == 2 # Filter Loan queries loan1 = utils._get_loan() loan2 = utils._get_loan() loan1.book = book1 loan1.patron = patron1 loan2.book = book2 loan2.patron = patron1 db.session.add(loan1) db.session.add(loan2) db.session.commit() assert Loan.query.count() == 2 assert Loan.query.filter_by(patron_id=patron1.id).count() == 2 assert Loan.query.filter_by(book_id=book1.id).count() == 1 # Filter Hold queries hold1 = utils._get_hold() hold2 = utils._get_hold() hold1.book = book1 hold1.patron = patron3 hold2.book = book1 hold2.patron = patron2 db.session.add(hold1) db.session.add(hold2) db.session.commit() assert Hold.query.count() == 2 assert Hold.query.filter_by(patron_id=patron1.id).count() == 0 assert Hold.query.filter_by(patron_id=patron2.id).count() == 1 assert Hold.query.filter_by(book_id=book1.id).count() == 2