mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 06:23:34 -07:00
Add test database with alembic
This commit is contained in:
parent
d310180d8c
commit
9e5549adc9
2 changed files with 26 additions and 10 deletions
|
@ -60,11 +60,13 @@ def run_migrations_online():
|
|||
|
||||
"""
|
||||
config.set_section_option(config.config_ini_section, "DB_URL", settings.DB_URL)
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
connectable = config.attributes.get('connection', None)
|
||||
if not connectable:
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
|
|
|
@ -6,17 +6,33 @@ from mealie.app import app
|
|||
from mealie.core.config import app_dirs, settings
|
||||
from mealie.db.db_setup import generate_session, sql_global_init
|
||||
from pytest import fixture
|
||||
from alembic import command, config
|
||||
import sqlalchemy
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from tests.app_routes import AppRoutes
|
||||
from tests.test_config import TEST_DATA
|
||||
from tests.utils.recipe_data import build_recipe_store, get_raw_no_image, get_raw_recipe
|
||||
|
||||
SQLITE_FILE = app_dirs.SQLITE_DIR.joinpath("test.db")
|
||||
# sqlite test db
|
||||
SQLITE_FILE = app_dirs.DATA_DIR.joinpath("test.db")
|
||||
SQLITE_FILE.unlink(missing_ok=True)
|
||||
DB_URL = "sqlite:///" + str(SQLITE_FILE.absolute())
|
||||
engine = create_engine(
|
||||
DB_URL,
|
||||
echo=False,
|
||||
connect_args={"check_same_thread": False},
|
||||
)
|
||||
|
||||
# alembic migrations
|
||||
config = config.Config("alembic.ini")
|
||||
config.set_main_option('sqlalchemy.url', DB_URL)
|
||||
config.attributes['connection'] = engine
|
||||
command.upgrade(config, "head")
|
||||
|
||||
TestSessionLocal = sql_global_init(SQLITE_FILE)
|
||||
|
||||
# test session
|
||||
TestSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
def override_get_db():
|
||||
try:
|
||||
|
@ -33,8 +49,6 @@ def api_client():
|
|||
|
||||
yield TestClient(app)
|
||||
|
||||
SQLITE_FILE.unlink()
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def api_routes():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue