Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SesstionTransaction._connection_for_bind call. Fixes #53 #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pytest_flask_sqlalchemy/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import contextlib
import os

import pytest
import sqlalchemy as sa
Expand Down Expand Up @@ -144,6 +144,12 @@ def raw_connection():

engine.raw_connection = raw_connection

# Fix SessionTransaction._connection_for_bind caching
@sa.event.listens_for(session, 'after_begin')
def after_begin(session, transaction, conn):
if engine not in transaction._connections:
transaction._connections[engine] = transaction._connections[conn]

for mocked_engine in pytestconfig._mocked_engines:
mocker.patch(mocked_engine, new=engine)

Expand Down
29 changes: 29 additions & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,32 @@ def test_delete_message(account_address, db_session):

result = db_testdir.runpytest()
result.assert_outcomes(passed=1)


def test_rollback_nested(db_testdir):
'''
Test that creating objects and emitting SQL in the ORM won't bleed into
other tests.
'''
# Load tests from file
db_testdir.makepyfile("""
def test_rollback_nested(person, db_session, caplog):
assert db_session.query(person).count() == 0
n1 = db_session.begin_nested()
db_session.add(person())
assert db_session.query(person).count() == 1

n2 = db_session.begin_nested()
db_session.add(person())
assert db_session.query(person).count() == 2

n2.rollback()
print(db_session.bind.mock_calls)
assert db_session.query(person).count() == 1
n1.rollback()
assert db_session.query(person).count() == 0
""")

# Run tests
result = db_testdir.runpytest()
result.assert_outcomes(passed=1)