51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
import os
|
|
import tempfile
|
|
import pytest
|
|
|
|
from flaschengeist import database
|
|
from flaschengeist.app import create_app, install_all
|
|
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()
|
|
app = create_app(
|
|
{
|
|
"TESTING": True,
|
|
"DATABASE": {
|
|
"engine": "sqlite",
|
|
"database": f"/{db_path}"
|
|
},
|
|
"LOGGING": {"level": "DEBUG"},
|
|
}
|
|
)
|
|
with app.app_context():
|
|
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()
|