Skip to content

Commit

Permalink
Updated test fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Movchan committed Jul 30, 2024
1 parent 37fe813 commit 3409a74
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions aana_chat_with_video/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# ruff: noqa: S101
import importlib
import os
import tempfile
from pathlib import Path

import pytest
from sqlalchemy.orm import Session

from aana.configs.db import DbSettings, SQLiteConfig
from aana.configs.settings import settings as aana_settings
from aana.exceptions.runtime import EmptyMigrationsException
from aana.storage.op import DbType
from aana.storage.op import DbType, run_alembic_migrations
from aana.tests.conftest import app_factory, call_endpoint # noqa: F401
from aana.utils.json import jsonify
from aana_chat_with_video.configs.settings import settings
from aana_chat_with_video.storage.op import (
run_alembic_migrations as run_app_alembic_migrations,
)
Expand All @@ -19,33 +20,30 @@
@pytest.fixture(scope="function")
def db_session():
"""Creates a new database file and session for each test."""
tmp_database_path = Path(tempfile.mkstemp(suffix=".db")[1])
db_config = DbSettings(
datastore_type=DbType.SQLITE,
datastore_config=SQLiteConfig(path=tmp_database_path),
)
os.environ["DB_CONFIG"] = jsonify(db_config)

# Reload the settings to update the database path
import aana.configs.settings
import aana_chat_with_video.configs.settings

importlib.reload(aana.configs.settings)
importlib.reload(aana_chat_with_video.configs.settings)

from aana_chat_with_video.configs.settings import settings

# Run migrations to set up the schema
try:
run_app_alembic_migrations(settings)
except EmptyMigrationsException:
print("No versions found in the custom migrations. Using default migrations.")
run_app_alembic_migrations(settings)

# Create a new session
engine = settings.db_config.get_engine()
with Session(engine) as session:
yield session
with tempfile.NamedTemporaryFile(dir=settings.tmp_data_dir) as tmp:
db_config = DbSettings(
datastore_type=DbType.SQLITE,
datastore_config=SQLiteConfig(path=tmp.name),
)
os.environ["DB_CONFIG"] = jsonify(db_config)

settings.db_config = db_config
settings.db_config._engine = None
aana_settings.db_config = db_config
aana_settings.db_config._engine = None

try:
run_app_alembic_migrations(settings)
except EmptyMigrationsException:
print(
"No versions found in the custom migrations. Using default migrations."
)
run_alembic_migrations(settings)

# Create a new session
engine = settings.db_config.get_engine()
with Session(engine) as session:
yield session


@pytest.fixture(scope="module")
Expand Down

0 comments on commit 3409a74

Please sign in to comment.