flaschengeist/tests/conftest.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.3 KiB
Python
Raw Normal View History

import os
import tempfile
import pytest
from flaschengeist import database
from flaschengeist.app import create_app, install_all
2021-12-21 21:56:03 +00:00
from flask_migrate import upgrade
# read in SQL for populating test data
with open(os.path.join(os.path.dirname(__file__), "data.sql"), "r") as f:
_data_sql = []
__sql_command = ""
for line in f.readlines():
if not line.startswith("--"):
line = line.strip("\n")
__sql_command += line.strip("\n")
if __sql_command.endswith(";"):
_data_sql.append(__sql_command)
__sql_command = ""
@pytest.fixture
def app():
db_fd, db_path = tempfile.mkstemp()
2021-11-18 11:54:37 +00:00
app = create_app(
{
"TESTING": True,
2021-12-21 21:56:03 +00:00
"DATABASE": {
"engine": "sqlite",
"database": f"/{db_path}"
},
2021-11-18 11:54:37 +00:00
"LOGGING": {"level": "DEBUG"},
}
)
with app.app_context():
2021-12-21 21:56:03 +00:00
upgrade(directory='migrations', revision='heads')
# install_all()
engine = database.db.engine
with engine.connect() as connection:
for statement in _data_sql:
connection.execute(statement)
yield app
os.close(db_fd)
# os.unlink(db_path)
print(db_path)
@pytest.fixture
def client(app):
return app.test_client()