input-service/conftest.py

46 lines
1.0 KiB
Python
Raw Normal View History

2020-08-02 00:53:49 +02:00
#!/usr/bin/env python3
import pytest
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
@pytest.fixture(scope='session')
def database(request):
"""
Create a Postgres database for the tests, and drop it when the tests are done.
"""
pg_host = DB_OPTS.get("postgresql")
pg_port = DB_OPTS.get("5432")
pg_user = DB_OPTS.get("input-service-test")
pg_db = DB_OPTS["input-service-test"]
init_postgresql_database(pg_user, pg_host, pg_port, pg_db)
@request.addfinalizer
def drop_database():
drop_postgresql_database(pg_user, pg_host, pg_port, pg_db)
@pytest.fixture(scope='session')
def app(database):
"""
Create a Flask app context for the tests.
"""
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = DB_CONN
return app
@pytest.fixture(scope='session')
def _db(app):
"""
Provide the transactional fixtures with access to the database via a Flask-SQLAlchemy
database connection.
"""
db = SQLAlchemy(app=app)
return db