refactor: human input node decouple db (#32900)

This commit is contained in:
wangxiaolei
2026-03-04 13:18:32 +08:00
committed by GitHub
parent b584434e28
commit e14b09d4db
12 changed files with 69 additions and 58 deletions

View File

@ -5,7 +5,6 @@ from __future__ import annotations
import dataclasses
from datetime import datetime
from types import SimpleNamespace
from unittest.mock import MagicMock
import pytest
@ -35,7 +34,7 @@ from models.human_input import (
def _build_repository() -> HumanInputFormRepositoryImpl:
return HumanInputFormRepositoryImpl(session_factory=MagicMock(), tenant_id="tenant-id")
return HumanInputFormRepositoryImpl(tenant_id="tenant-id")
def _patch_recipient_factory(monkeypatch: pytest.MonkeyPatch) -> list[SimpleNamespace]:
@ -389,8 +388,21 @@ def _session_factory(session: _FakeSession):
return _factory
def _patch_repo_session_factory(monkeypatch: pytest.MonkeyPatch, session: _FakeSession) -> None:
"""Patch repository's global session factory to return our fake session.
The repositories under test now use a global session factory; patch its
create_session method so unit tests don't hit a real database.
"""
monkeypatch.setattr(
"core.repositories.human_input_repository.session_factory.create_session",
_session_factory(session),
raising=True,
)
class TestHumanInputFormRepositoryImplPublicMethods:
def test_get_form_returns_entity_and_recipients(self):
def test_get_form_returns_entity_and_recipients(self, monkeypatch: pytest.MonkeyPatch):
form = _DummyForm(
id="form-1",
workflow_run_id="run-1",
@ -408,7 +420,8 @@ class TestHumanInputFormRepositoryImplPublicMethods:
access_token="token-123",
)
session = _FakeSession(scalars_results=[form, [recipient]])
repo = HumanInputFormRepositoryImpl(_session_factory(session), tenant_id="tenant-id")
_patch_repo_session_factory(monkeypatch, session)
repo = HumanInputFormRepositoryImpl(tenant_id="tenant-id")
entity = repo.get_form(form.workflow_run_id, form.node_id)
@ -418,13 +431,14 @@ class TestHumanInputFormRepositoryImplPublicMethods:
assert len(entity.recipients) == 1
assert entity.recipients[0].token == "token-123"
def test_get_form_returns_none_when_missing(self):
def test_get_form_returns_none_when_missing(self, monkeypatch: pytest.MonkeyPatch):
session = _FakeSession(scalars_results=[None])
repo = HumanInputFormRepositoryImpl(_session_factory(session), tenant_id="tenant-id")
_patch_repo_session_factory(monkeypatch, session)
repo = HumanInputFormRepositoryImpl(tenant_id="tenant-id")
assert repo.get_form("run-1", "node-1") is None
def test_get_form_returns_unsubmitted_state(self):
def test_get_form_returns_unsubmitted_state(self, monkeypatch: pytest.MonkeyPatch):
form = _DummyForm(
id="form-1",
workflow_run_id="run-1",
@ -436,7 +450,8 @@ class TestHumanInputFormRepositoryImplPublicMethods:
expiration_time=naive_utc_now(),
)
session = _FakeSession(scalars_results=[form, []])
repo = HumanInputFormRepositoryImpl(_session_factory(session), tenant_id="tenant-id")
_patch_repo_session_factory(monkeypatch, session)
repo = HumanInputFormRepositoryImpl(tenant_id="tenant-id")
entity = repo.get_form(form.workflow_run_id, form.node_id)
@ -445,7 +460,7 @@ class TestHumanInputFormRepositoryImplPublicMethods:
assert entity.selected_action_id is None
assert entity.submitted_data is None
def test_get_form_returns_submission_when_completed(self):
def test_get_form_returns_submission_when_completed(self, monkeypatch: pytest.MonkeyPatch):
form = _DummyForm(
id="form-1",
workflow_run_id="run-1",
@ -460,7 +475,8 @@ class TestHumanInputFormRepositoryImplPublicMethods:
submitted_at=naive_utc_now(),
)
session = _FakeSession(scalars_results=[form, []])
repo = HumanInputFormRepositoryImpl(_session_factory(session), tenant_id="tenant-id")
_patch_repo_session_factory(monkeypatch, session)
repo = HumanInputFormRepositoryImpl(tenant_id="tenant-id")
entity = repo.get_form(form.workflow_run_id, form.node_id)
@ -471,7 +487,7 @@ class TestHumanInputFormRepositoryImplPublicMethods:
class TestHumanInputFormSubmissionRepository:
def test_get_by_token_returns_record(self):
def test_get_by_token_returns_record(self, monkeypatch: pytest.MonkeyPatch):
form = _DummyForm(
id="form-1",
workflow_run_id="run-1",
@ -490,7 +506,8 @@ class TestHumanInputFormSubmissionRepository:
form=form,
)
session = _FakeSession(scalars_result=recipient)
repo = HumanInputFormSubmissionRepository(_session_factory(session))
_patch_repo_session_factory(monkeypatch, session)
repo = HumanInputFormSubmissionRepository()
record = repo.get_by_token("token-123")
@ -499,7 +516,7 @@ class TestHumanInputFormSubmissionRepository:
assert record.recipient_type == RecipientType.STANDALONE_WEB_APP
assert record.submitted is False
def test_get_by_form_id_and_recipient_type_uses_recipient(self):
def test_get_by_form_id_and_recipient_type_uses_recipient(self, monkeypatch: pytest.MonkeyPatch):
form = _DummyForm(
id="form-1",
workflow_run_id="run-1",
@ -518,7 +535,8 @@ class TestHumanInputFormSubmissionRepository:
form=form,
)
session = _FakeSession(scalars_result=recipient)
repo = HumanInputFormSubmissionRepository(_session_factory(session))
_patch_repo_session_factory(monkeypatch, session)
repo = HumanInputFormSubmissionRepository()
record = repo.get_by_form_id_and_recipient_type(
form_id=form.id,
@ -553,7 +571,8 @@ class TestHumanInputFormSubmissionRepository:
forms={form.id: form},
recipients={recipient.id: recipient},
)
repo = HumanInputFormSubmissionRepository(_session_factory(session))
_patch_repo_session_factory(monkeypatch, session)
repo = HumanInputFormSubmissionRepository()
record: HumanInputFormRecord = repo.mark_submitted(
form_id=form.id,