生产是 MySQL 8、测试是 SQLite,两者的方言差异在本地完全测不出来。今天为此 付出了两次代价: 1. `ORDER BY ck_checked_at ASC NULLS FIRST` 在 SQLite 上完全正常,本地 258 条 全绿,一部署到生产第一次巡检就 1064 语法错误(MySQL 不支持 NULLS FIRST); 2. P1b 自动重登的结算复用调度器事务读取,在 SQLite 下看不出问题,但在 MySQL 的 REPEATABLE READ 下读到的是批次开始前的快照 —— 10 条实际成功的重登全部被记成 失败。SQLite 的读一致性与 MySQL 不同,这类问题跑一万遍也复现不出来。 改动: - 新增 tests/support/db.py:测试专用引擎/会话工具,连接串从 .env 读取, 默认指向独立的 douyu_login_test 库; - 新增 tests/conftest.py:会话级建好表结构,每个用例前后清空所有表做隔离; - 根 conftest.py 接管连接串设置:它在 pytest 收集阶段最先执行,在这里把 DATABASE_URL 指向测试库,应用模块首次 import 拿到就是 MySQL,不依赖任何 import 顺序(原先这里是 sqlite:// 兜底,改造后会变成「先建 SQLite engine 再打补丁换成 MySQL」,顺序一变就静默跑错库); - 21 个测试文件的 setup 样板统一改用共享测试引擎; - 两个迁移测试改为在临时库 douyu_login_mig_<随机> 上跑完整 alembic upgrade head, 跑完 drop —— 迁移脚本本身也终于在 MySQL 上被验证; - dev.sh 与 README 的过时说明一并更新(原先写着「不需要 MySQL / .env 配置」; 「测试各自建 SQLite 内存库」)。 安全与易用: - 测试库名与开发库同名时**直接拒绝运行**(clear_tables 会 DELETE 所有表, 一旦 TEST_MYSQL_DATABASE 被指到 douyu_login 就是数据灾难); - 连不上 MySQL 时给出可操作提示(先 ./dev.sh 起库 / 用 TEST_MYSQL_DATABASE 覆盖), 而不是抛一段 SQLAlchemy 原始堆栈。 验收: - 全量 301 条通过,连跑两次结果一致(隔离有效),耗时约 12.5 秒(原 SQLite 约 3.6 秒); - 单文件独立运行通过(不依赖全量执行顺序); - `grep -rn sqlite tests/ conftest.py` 已无实际用法; - **方言确实生效**:临时把一条查询改回 `NULLS FIRST`,13 条测试立刻变红(含运行期 失败),还原即恢复;这条由 test_generated_sql_is_mysql_compatible 长期守着。 代价(已在 README 与规划附录 C.2 记明):跑测试前必须先有 MySQL,pre-push 钩子 同理;CI 需要 service container。
130 lines
4.6 KiB
Python
130 lines
4.6 KiB
Python
import pytest
|
|
|
|
from tests.support.db import get_test_engine, make_test_session
|
|
from web.backend.models import HuyaAccount, HuyaWorkbench, HuyaWorkbenchAccount, User
|
|
from web.backend.routers.huya import (
|
|
delete_account,
|
|
list_huya_account_ids,
|
|
list_huya_workbench_accounts,
|
|
list_tasks,
|
|
update_huya_workbench_accounts,
|
|
)
|
|
from web.backend.schemas import HuyaTaskBatchRequest, HuyaWorkbenchAccountsUpdate
|
|
from web.backend.services.huya_service import create_planned_tasks
|
|
|
|
|
|
class TestHuyaWorkbenchScope:
|
|
def setup_method(self):
|
|
self.engine = get_test_engine()
|
|
self.session = make_test_session(self.engine)
|
|
self.user = User(username="operator", password_hash="hash", role="super_admin")
|
|
self.account = HuyaAccount(
|
|
uid="100", yyuid="100", cookie="yyuid=100; udb_uid=100"
|
|
)
|
|
self.session.add_all([self.user, self.account])
|
|
self.session.commit()
|
|
|
|
def teardown_method(self):
|
|
self.session.close()
|
|
|
|
def test_accounts_and_tasks_are_scoped(self):
|
|
update_huya_workbench_accounts(
|
|
HuyaWorkbenchAccountsUpdate(
|
|
handbook_scope="elite", account_ids=[self.account.id]
|
|
),
|
|
db=self.session,
|
|
current=self.user,
|
|
)
|
|
result = list_huya_workbench_accounts(
|
|
"elite", db=self.session, current=self.user
|
|
)
|
|
assert result == {"account_ids": [self.account.id], "configured": True}
|
|
assert self.session.query(HuyaWorkbench).count() == 1
|
|
|
|
elite_batch, count = create_planned_tasks(
|
|
self.session,
|
|
[self.account.id],
|
|
"query_act_tasks",
|
|
self.user.id,
|
|
handbook_scope="elite",
|
|
)
|
|
assert count == 1
|
|
tasks = list_tasks(
|
|
handbook_scope="elite",
|
|
include_images=False,
|
|
db=self.session,
|
|
current=self.user,
|
|
)
|
|
assert [task.batch_id for task in tasks] == [elite_batch]
|
|
assert tasks[0].handbook_scope == "elite"
|
|
|
|
with pytest.raises(ValueError, match="无效的工作台"):
|
|
create_planned_tasks(
|
|
self.session,
|
|
[self.account.id],
|
|
"query_points",
|
|
self.user.id,
|
|
handbook_scope="invalid",
|
|
)
|
|
|
|
def test_browser_exchange_scope_and_account_ids(self):
|
|
no_cookie = HuyaAccount(uid="200", yyuid="200", cookie="")
|
|
self.session.add(no_cookie)
|
|
self.session.commit()
|
|
|
|
update_huya_workbench_accounts(
|
|
HuyaWorkbenchAccountsUpdate(
|
|
handbook_scope="browser_exchange", account_ids=[self.account.id]
|
|
),
|
|
db=self.session,
|
|
current=self.user,
|
|
)
|
|
assert list_huya_workbench_accounts(
|
|
"browser_exchange", db=self.session, current=self.user
|
|
) == {
|
|
"account_ids": [self.account.id],
|
|
"configured": True,
|
|
}
|
|
# browser_exchange 与 elite 两个 scope 的账号集合互不影响
|
|
assert list_huya_workbench_accounts(
|
|
"elite", db=self.session, current=self.user
|
|
) == {
|
|
"account_ids": [],
|
|
"configured": False,
|
|
}
|
|
|
|
# ids 接口:has_cookie=True 只返回有 Cookie 的账号
|
|
result = list_huya_account_ids(
|
|
has_cookie=True, db=self.session, current=self.user
|
|
)
|
|
assert result == {"account_ids": [self.account.id], "total": 1}
|
|
all_ids = list_huya_account_ids(db=self.session, current=self.user)
|
|
assert sorted(all_ids["account_ids"]) == sorted([self.account.id, no_cookie.id])
|
|
assert all_ids["total"] == 2
|
|
|
|
# ids 接口:search 匹配 uid
|
|
searched = list_huya_account_ids(
|
|
search="200", db=self.session, current=self.user
|
|
)
|
|
assert searched["account_ids"] == [no_cookie.id]
|
|
|
|
def test_account_delete_cleans_workbench_membership(self):
|
|
self.session.add(
|
|
HuyaWorkbenchAccount(
|
|
user_id=self.user.id,
|
|
handbook_scope="elite",
|
|
account_id=self.account.id,
|
|
)
|
|
)
|
|
self.session.commit()
|
|
|
|
result = delete_account(self.account.id, db=self.session, current=self.user)
|
|
assert result["success"] is True
|
|
assert self.session.query(HuyaAccount).count() == 0
|
|
assert self.session.query(HuyaWorkbenchAccount).count() == 0
|
|
|
|
|
|
def test_huya_batch_schema_defaults_to_legacy():
|
|
request = HuyaTaskBatchRequest(account_ids=[1], task_type="query_points")
|
|
assert request.handbook_scope == "legacy"
|