mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 07:27:15 +08:00
refactor: make model sessions explicit
This commit is contained in:
@@ -10,11 +10,11 @@ from sqlalchemy.exc import IntegrityError
|
||||
from app.agent.orchestrator import AgentManager
|
||||
from app.agent.tools.impl.query_agent_tasks import QueryAgentTasksTool
|
||||
from app.db.engine import get_engine
|
||||
from app.db import base as db_base
|
||||
from app.db.oper.agenttask import AgentTaskOper
|
||||
from app.db.models.agenttask import AgentTask
|
||||
from app.db.models.agenttaskrun import AgentTaskRun
|
||||
from app.db.session import SessionFactory
|
||||
from app.db import decorators
|
||||
|
||||
|
||||
Engine = get_engine()
|
||||
@@ -141,9 +141,11 @@ def test_agenttaskrun_oper_reuses_explicit_query_session(db, monkeypatch):
|
||||
run = AgentTaskOper().begin_run(task.id)
|
||||
assert run
|
||||
monkeypatch.setattr(
|
||||
decorators,
|
||||
"ScopedSession",
|
||||
lambda: (_ for _ in ()).throw(AssertionError("不应创建额外同步会话")),
|
||||
db_base,
|
||||
"run_sync_transaction",
|
||||
lambda _operation: (_ for _ in ()).throw(
|
||||
AssertionError("不应创建额外同步事务")
|
||||
),
|
||||
)
|
||||
|
||||
oper = AgentTaskOper(db.session)
|
||||
@@ -151,19 +153,6 @@ def test_agenttaskrun_oper_reuses_explicit_query_session(db, monkeypatch):
|
||||
assert oper.list_runs(task.id)
|
||||
|
||||
|
||||
def test_agenttaskrun_model_legacy_query_keeps_keyword_abi(monkeypatch):
|
||||
"""旧插件以关键字直调 AgentTaskRun 时仍自动补入短会话。"""
|
||||
opened = []
|
||||
monkeypatch.setattr(
|
||||
decorators,
|
||||
"ScopedSession",
|
||||
lambda: (opened.append(True) or SessionFactory()),
|
||||
)
|
||||
|
||||
assert AgentTaskRun.get_by_run_id(run_id="missing-legacy") is None
|
||||
assert opened == [True]
|
||||
|
||||
|
||||
def test_begin_run_rolls_back_task_claim_when_run_insert_fails() -> None:
|
||||
"""运行记录插入失败时,任务的 running 投影必须随事务回滚。"""
|
||||
first_task = _add_task("run-rollback-first")
|
||||
|
||||
Reference in New Issue
Block a user