From f8c5852ee35fca017c44e0e9845b009a2fcaa414 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Feb 26 2017 19:33:47 +0000 Subject: tests: simplify db fixtures a bit We don't need to manage the session stuff manually, Flask-SQLAlchemy already handles that if we hook it up correctly: See: http://flask-sqlalchemy.pocoo.org/2.1/contexts/ We also don't need to drop all tables (it's already in a temp directory) and leaving the data behind can help when investigating failing tests. --- diff --git a/tests/conftest.py b/tests/conftest.py index db596eb..d4ad971 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,12 +14,15 @@ import os import pytest from waiverdb.app import create_app, init_db +from waiverdb.models import db @pytest.fixture(scope='session') def app(tmpdir_factory, request): app = create_app('waiverdb.config.TestingConfig') db_file = tmpdir_factory.mktemp('waiverdb').join('db.sqlite') app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///%s' % db_file + db.init_app(app) + init_db(app) # Establish an application context before running the tests. ctx = app.app_context() ctx.push() @@ -30,39 +33,6 @@ def app(tmpdir_factory, request): request.addfinalizer(teardown) return app -@pytest.fixture(scope='session') -def db(app, request): - """Session-wide test database.""" - db = init_db(app) - def teardown(): - db.drop_all() - request.addfinalizer(teardown) - return db - -@pytest.fixture(scope='function') -def session(db, request): - """Creates a new database session for a test.""" - connection = db.engine.connect() - transaction = connection.begin() - - # https://github.com/mitsuhiko/flask-sqlalchemy/issues/345 - class _dict(dict): - def __nonzero__(self): - return True - - options = dict(bind=connection, binds=_dict()) - session = db.create_scoped_session(options=options) - - db.session = session - - def teardown(): - transaction.rollback() - connection.close() - session.remove() - - request.addfinalizer(teardown) - return session - @pytest.yield_fixture def client(app): """A Flask test client. An instance of :class:`flask.testing.TestClient` diff --git a/tests/test_api_v10.py b/tests/test_api_v10.py index e9bb62b..eeb75bd 100644 --- a/tests/test_api_v10.py +++ b/tests/test_api_v10.py @@ -12,7 +12,7 @@ import pytest import json -def test_create_waiver(client, session): +def test_create_waiver(client): data = { 'result_id': 123, 'product_version': 'fool-1',