Hi All,

I'm working with the pattern described at https://docs.sqlalchemy.org/en/13/orm/session_transaction.html#joining-a-session-into-an-external-transaction-such-as-for-test-suites along with pytest and FastAPI, an async web app framework with good support for running blocking code.

So, I have my fixtures:

@pytest.fixture(scope='session')
def db(client):
    engine = Session.kw['bind']
    conn = engine.connect()
    transaction = conn.begin()
    try:
        Base.metadata.create_all(bind=conn,checkfirst=False)
        yield conn
    finally:
        transaction.rollback()
        Session.configure(bind=engine)

@pytest.fixture()
def transaction(db):
    transaction = db.begin_nested()
    try:
        Session.configure(bind=db)
        yield transaction
    finally:
        transaction.rollback()

@pytest.fixture()
def session(transaction):
    return Session()

And a test:

def test_create_full_data(transaction, session, client):
    response = client.post('/events/',json={
        'date':'2019-06-02',
        'type':'DONE',
        'text':'some stuff got done' })
    transaction.rollback()
    actual = session.query(Event).one()
    compare(actual.date,expected=date(2019,6,2))
    compare(type(actual),expected=Done)
    compare(response.json(),expected={
        'id': actual.id,
        'date':'2019-06-02',
        'type':'DONE',
        'text':'some stuff got done' })
    compare(response.status_code,expected=201)

And some code:

@router.post("/",response_model=EventRead,status_code=201)
def create_object(
    *,
    session: Session = Depends(db_session),
    event: EventCreate,
):
    """ Create new Event. """ with session.transaction:
        event = Event(**event.dict())
        session.add(event)
        session.flush()
        session.expunge(event)
    return event


Now, what I'm trying to test is that I haven't forgotten to include the "with session.transaction". The problem is that, without the transaction.rollback(), the test passes regardless of whether the "with session.transaction" is there, and with it there, it always fails.

What's the best way for writing tests that have the database setup DDL run in a transaction that's rolled back at the end of the session, each test in a subtransaction that gets rolled back at the end of each test, and also test that code under test is committing or rolling back as required?

cheers,

Chris

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/430d4156-d9ff-4349-5be5-62bee6ea4627%40withers.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to