fix: align all 212 tests with current API and add CI workflows
Some checks failed
Test / test (push) Failing after 1m4s
Some checks failed
Test / test (push) Failing after 1m4s
Update integration tests to match actual endpoint responses: remove data wrappers, use snake_case fields, correct HTTP methods (PUT→POST for directory create), status codes (200→204 for mutations), and request formats (params→json for 2FA). Fix root-level and unit tests for DatabaseManager migration, model CRUD patterns, and JWT setup. Add GitHub Actions and Gitea CI configs with ubuntu-latest + Python 3.13. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,28 +1,63 @@
|
||||
"""
|
||||
数据库初始化和迁移测试
|
||||
"""
|
||||
import pytest
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
from sqlmodel import SQLModel
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from sqlmodels.database_connection import DatabaseManager
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_initialize_db():
|
||||
"""测试创建数据库结构"""
|
||||
from sqlmodels import database
|
||||
|
||||
await database.init_db(url='sqlite:///:memory:')
|
||||
async def test_database_manager_init():
|
||||
"""测试 DatabaseManager 初始化"""
|
||||
await DatabaseManager.init(
|
||||
database_url="sqlite+aiosqlite:///:memory:",
|
||||
debug=True,
|
||||
)
|
||||
|
||||
assert DatabaseManager.engine is not None
|
||||
assert DatabaseManager._async_session_factory is not None
|
||||
|
||||
# 验证可以获取会话
|
||||
async for session in DatabaseManager.get_session():
|
||||
assert isinstance(session, AsyncSession)
|
||||
break
|
||||
|
||||
await DatabaseManager.close()
|
||||
|
||||
@pytest.fixture
|
||||
async def db_session():
|
||||
"""测试获取数据库连接Session"""
|
||||
from sqlmodels import database
|
||||
|
||||
await database.init_db(url='sqlite:///:memory:')
|
||||
|
||||
async for session in database.get_session():
|
||||
yield session
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_migration():
|
||||
"""测试数据库创建并初始化配置"""
|
||||
from sqlmodels import migration
|
||||
from sqlmodels import database
|
||||
|
||||
await database.init_db(url='sqlite:///:memory:')
|
||||
|
||||
await migration.migration()
|
||||
"""测试数据库迁移(创建默认数据)"""
|
||||
from sqlmodels.migration import migration
|
||||
|
||||
await DatabaseManager.init(
|
||||
database_url="sqlite+aiosqlite:///:memory:",
|
||||
debug=False,
|
||||
)
|
||||
|
||||
try:
|
||||
await migration()
|
||||
|
||||
# 验证迁移后的数据
|
||||
async for session in DatabaseManager.get_session():
|
||||
from sqlmodels.setting import Setting, SettingsType
|
||||
from sqlmodels.group import Group
|
||||
|
||||
# 验证设置项被创建
|
||||
secret_key = await Setting.get(
|
||||
session,
|
||||
(Setting.type == SettingsType.AUTH) & (Setting.name == "secret_key")
|
||||
)
|
||||
assert secret_key is not None
|
||||
|
||||
# 验证默认用户组被创建
|
||||
admin_group = await Group.get(session, Group.name == "管理员")
|
||||
assert admin_group is not None
|
||||
assert admin_group.admin is True
|
||||
break
|
||||
finally:
|
||||
await DatabaseManager.close()
|
||||
|
||||
Reference in New Issue
Block a user