def test_user_exception():
    """If a controller raises an exception, transactions are rolled back."""
    capture_log("gearshift.database")
    app = make_app(MyRoot)
    response = app.get("/create_person?id=19&docom=0&doerr=1&name=Martin%20GAL")
    print_log()
    assert 'KARL25' in response, \
        'The exception handler should have answered us'
    p = session.query(Person).get(19)
    assert p is None, \
        'This Person should have been rolled back: %s' % p
def test_raise_sa_exception():
    """If a controller causes an SA exception, it is raised properly."""
    capture_log("gearshift.database")
    app = make_app(MyRoot)
    response = app.get("/create_person?id=20")
    print_log()
    assert 'No exceptions occurred' in response
    response = app.get("/create_person?id=20", status=500)
    assert 'KARL25' not in response, \
        'Exception should NOT have been handled by our handler'
    assert 'DBAPIError' in response, \
        'The page should have displayed an SQLAlchemy exception'
def test_implicit_trans_no_error():
    """If a controller runs sucessfully, the transaction is commited."""
    capture_log("gearshift.database")
    app = make_app(MyRoot)
    response = app.get("/no_error?name=A.%20Dent")
    print_log()
    q = session.query(Person)
    arthur = q.filter_by(name="A. Dent").first()
    assert 'someconfirmhandler' in response, \
        'The no error should have redirected to someconfirmhandler'
    assert arthur is not None, 'Person arthur should have been saved!'
    assert arthur.name == "A. Dent", 'Person arthur should be named "A. Dent"'