- Add SQLAlchemy database module with DeclarativeBase - Implement engine and session factory management - Add context manager for database sessions - Add database initialization script - Update models/__init__.py to import Base from database - Fix Python 3.8 compatibility issues (use Optional instead of |) - Ensure SQLite database file is created on init_db Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
27 lines
587 B
Python
27 lines
587 B
Python
from pathlib import Path
|
|
|
|
def test_database_initialization(db_path):
|
|
"""测试数据库初始化."""
|
|
from app.database import init_db, get_engine, Base
|
|
|
|
init_db(db_path)
|
|
|
|
assert db_path.exists()
|
|
|
|
engine = get_engine()
|
|
assert engine is not None
|
|
|
|
# 创建所有表
|
|
Base.metadata.create_all(engine)
|
|
assert True # 如果没有异常则成功
|
|
|
|
|
|
def test_get_session(db_path):
|
|
"""测试获取数据库会话."""
|
|
from app.database import init_db, get_db
|
|
|
|
init_db(db_path)
|
|
|
|
with get_db(db_path) as session:
|
|
assert session is not None
|