2021-01-23 01:18:44 +00:00
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import pytest
|
|
|
|
|
2021-01-25 12:12:04 +00:00
|
|
|
from flaschengeist import database
|
2021-12-21 23:34:32 +00:00
|
|
|
from flaschengeist.app import create_app
|
2021-01-23 01:18:44 +00:00
|
|
|
|
2021-01-25 12:12:04 +00:00
|
|
|
# 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 = ""
|
|
|
|
|
|
|
|
|
2021-01-23 01:18:44 +00:00
|
|
|
@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 23:34:32 +00:00
|
|
|
"DATABASE": {"engine": "sqlite", "database": f"/{db_path}"},
|
2021-11-18 11:54:37 +00:00
|
|
|
"LOGGING": {"level": "DEBUG"},
|
|
|
|
}
|
|
|
|
)
|
2021-01-23 01:18:44 +00:00
|
|
|
with app.app_context():
|
2021-12-21 23:34:32 +00:00
|
|
|
database.db.create_all()
|
|
|
|
database.db.session.commit()
|
|
|
|
|
2021-01-25 12:12:04 +00:00
|
|
|
engine = database.db.engine
|
|
|
|
with engine.connect() as connection:
|
|
|
|
for statement in _data_sql:
|
|
|
|
connection.execute(statement)
|
2021-01-23 01:18:44 +00:00
|
|
|
yield app
|
|
|
|
os.close(db_fd)
|
2021-01-25 12:12:04 +00:00
|
|
|
# os.unlink(db_path)
|
|
|
|
print(db_path)
|
2021-01-23 01:18:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def client(app):
|
|
|
|
return app.test_client()
|