Files
live-hub-py/tests/test_cookie_custom_order.py
T
yml2213 3ce1c7a51b test: pytest 工程化落地 — 本地门禁、容器测试镜像、迁移链冒烟测试
- 门禁: 本地 pre-push 钩子(.git/hooks,推送前强制跑全量 pytest)
- Docker: 新增 test 镜像阶段(含 dev 依赖与 tests),compose 提供
  --profile test run --rm test 入口;dev.sh 支持 ./dev.sh test
- 测试基建: 根目录 conftest.py 统一 DATABASE_URL/APP_ENCRYPTION_KEY,
  移除 9 个测试文件内的重复 setdefault(含多余 import os)
- 迁移冒烟: tests/test_migrations.py 校验链线性、脚本可编译、空库整链
  upgrade head 后与 Base.metadata 表/列/索引对齐
- 修复冒烟测试发现的漂移: YybRechargeTask.task_id 冗余 index=True
  (唯一索引已覆盖,迁移链未建普通索引,模型与真实 schema 对齐)
- alembic.ini: path_separator=os 消除弃用告警;README 补测试章节
2026-08-30 18:54:00 +08:00

68 lines
2.2 KiB
Python

import unittest
from datetime import datetime, timedelta, timezone
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from web.backend.database import Base
from web.backend.models import Account, LoginTask, User
from web.backend.routers.cookies import _order_cookie_tasks
class CustomCookieOrderTests(unittest.TestCase):
def setUp(self):
self.engine = create_engine("sqlite://")
Base.metadata.create_all(self.engine)
self.db = Session(self.engine)
self.user = User(username="admin", password_hash="hash", role="super_admin")
self.db.add(self.user)
self.db.flush()
now = datetime.now(timezone.utc)
accounts = {
name: Account(username=name, password="p", email="e", email_password="ep")
for name in ("account-a", "account-b", "account-c")
}
self.db.add_all(accounts.values())
self.db.flush()
# 完成时间特意与用户输入顺序相反。
for index, name in enumerate(("account-a", "account-b", "account-c")):
self.db.add(LoginTask(
batch_id=f"batch-{name}",
account_id=accounts[name].id,
status="success",
created_by=self.user.id,
finished_at=now + timedelta(minutes=index),
))
self.db.commit()
def tearDown(self):
self.db.close()
Base.metadata.drop_all(self.engine)
self.engine.dispose()
def test_custom_cookie_order_follows_input_not_finished_time(self):
selected_names = ["account-c", "account-a", "account-b"]
tasks = (
_order_cookie_tasks(
self.db.query(LoginTask)
.join(Account, LoginTask.account_id == Account.id)
.filter(Account.username.in_(selected_names)),
selected_names,
)
.all()
)
account_names = {
account.id: account.username
for account in self.db.query(Account).all()
}
self.assertEqual(
[account_names[task.account_id] for task in tasks],
selected_names,
)
if __name__ == "__main__":
unittest.main()