import pytest

from .app import setup, teardown


@pytest.fixture(autouse=True, scope='session')
def db_migration(request):
    setup()
    request.addfinalizer(teardown)

from __future__ import absolute_import

import pytest

from .app import setup, teardown

from app import create_app
from app.models import db, Framework


@pytest.fixture(autouse=True, scope='session')
def db_migration(request):
    setup()
    request.addfinalizer(teardown)


@pytest.fixture(scope='session')
def app(request):
    return create_app('test')


@pytest.fixture()
def live_framework(request, app, status='live'):
    with app.app_context():
        framework = Framework.query.filter(
            Framework.slug == 'digital-outcomes-and-specialists'
        ).first()
        original_framework_status = framework.status
        framework.status = status

        db.session.add(framework)
        db.session.commit()

    def teardown():
        with app.app_context():
            framework = Framework.query.filter(
                Framework.slug == 'digital-outcomes-and-specialists'
            ).first()
            framework.status = original_framework_status

            db.session.add(framework)
            db.session.commit()

    request.addfinalizer(teardown)


@pytest.fixture()
def expired_framework(request, app):
    return live_framework(request, app, status='expired')

Add live and expired framework pytest fixtures
Add live and expired framework pytest fixtures

This is a tiny attempt to move away from relying on database migrations
to set up framework records for tests. Using migration framework records
means we need to use actual (sometimes expired) frameworks to write tests
ties us to existing frameworks and require manual rollbacks for any record
changes since frameworks/lots tables are not reset between tests.

Using fixtures for frameworks makes it possible to depend explicitly
on database state required to run the test. This works better than
creating objects in setup/teardown since we can create more complicated
objects by using fixture dependencies (eg a service fixture relying on
framework and supplier fixtures etc.).

The fixture is still using DOS for now, but we could change it to create
a test framework with test lots as long as any additional test objects
are created using the fixture dependencies.
