self.assertEqual(info.status_int, 500)
py.test style rewrite:
# conftest.py
from pyramid import testing
import pytest
import transaction
from pokerherd.models import DBSession
@pytest.fixture
def req():
"""Pyramid DummyRequest."""
return testing.DummyRequest()
@pytest.fixture(scope='function')
def db(request):
"""SQLAlchemy session."""
config = testing.setUp()
from sqlalchemy import create_engine
engine = create_engine('sqlite://', echo=True)
from pokerherd.models import Base, MyModel
DBSession.configure(bind=engine)
Base.metadata.create_all(engine)
with transaction.manager:
model = MyModel(name='one', value=55)
DBSession.add(model)
def fin():
DBSession.remove()
testing.tearDown()
request.addfinalizer(fin)
return DBSession
# test_views.py
from myproject.models import DBSession, MyModel
class TestMyView:
def test_passing_view(self, db, req):
from myproject.views import my_view
response = my_view(req)
assert response['one'].name == 'one'
assert response['project'] == 'pokerherd'
def test_failing_view(self, req):
from myproject.views import my_view
response = my_view(req)
assert response.status_int == 500
The first example is working fine, but the second test fails, because sqlite DBSession from memory don't get deleted.
I don't understand the difference.
If I call Base.metadata.drop_all(engine) in the finalizer, the test passes, but in the original test, only the session is removed and still working fine.
Also in SQLAlchemy manual, scoped DBSession.remove() is described as:
"Dispose of the current Session, if present. This will first call Session.close() method on the current Session, which releases any existing transactional/connection resources still being held; transactions specifically are rolled back. The Session is then discarded. Upon next usage within the same scope, the scoped_session will produce a new Session object."