def test_manual_enter_and_exit_out_of_order_exit_raises_assertion(cxn):
    t1, t2 = Transaction(cxn), Transaction(cxn)
    t1.__enter__()
    t2.__enter__()
    with pytest.raises(
            AssertionError,
            match=re.escape(
                'Out-of-order Transaction context exits. Are you calling '
                '__exit__() manually and getting it wrong?')):
        t1.__exit__(None, None, None)
def test_explicit_rollback_required_after_handling_sql_exception_otherwise_exception_is_raised(
        cxn):
    txn = Transaction(cxn)
    txn.__enter__()
    with pytest.raises(psycopg2.ProgrammingError):
        cxn.cursor().execute('SELECT * FROM this_table_does_not_exist')
    with pytest.raises(
            Exception,
            match=re.escape(
                'SQL error occurred within current transaction. '
                'Transaction.rollback() must be called before exiting '
                'transaction context.')):
        txn.__exit__(None, None, None)