feat: migrate persistence from JSON to SQLModel (Phase 1)
- Integrated SQLModel with SQLite for robust data persistence - Refactored UserManager and WatchlistManager to use SQL queries - Migrated models to SQLModel with relationships and primary keys - Updated test suite with in-memory database isolation - Removed deprecated JSON storage files
This commit is contained in:
@@ -11,12 +11,66 @@ from unittest.mock import Mock, AsyncMock, patch
|
||||
import sys
|
||||
import os
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Ensure the project root is in the Python path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
# FORCE DATABASE_URL to in-memory for ALL tests before ANY app imports
|
||||
os.environ["DATABASE_URL"] = "sqlite://"
|
||||
|
||||
from app.models import DownloadTask, DownloadStatus, DownloadRequest, HostType
|
||||
from app.favorites import FavoritesManager
|
||||
from app.download_manager import DownloadManager
|
||||
from sqlmodel import SQLModel, create_engine, Session
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def init_db():
|
||||
"""Initialize the in-memory database once for the test session"""
|
||||
from app.database import engine
|
||||
SQLModel.metadata.create_all(engine)
|
||||
return engine
|
||||
|
||||
|
||||
@pytest.fixture(name="engine")
|
||||
def engine_fixture():
|
||||
"""Returns the global test engine"""
|
||||
from app.database import engine
|
||||
return engine
|
||||
|
||||
|
||||
@pytest.fixture(name="session")
|
||||
def session_fixture(engine):
|
||||
"""Create a temporary database session for testing"""
|
||||
# Clear and recreate tables for each test to ensure isolation
|
||||
SQLModel.metadata.drop_all(engine)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_db(engine):
|
||||
"""Ensure each test starts with fresh tables"""
|
||||
SQLModel.metadata.drop_all(engine)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
yield engine
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user_manager():
|
||||
"""Create a UserManager instance"""
|
||||
from app.auth import UserManager
|
||||
return UserManager()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def watchlist_manager():
|
||||
"""Create a WatchlistManager instance"""
|
||||
from app.watchlist import WatchlistManager
|
||||
return WatchlistManager()
|
||||
|
||||
|
||||
def pytest_configure(config):
|
||||
|
||||
Reference in New Issue
Block a user