Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: use persisted account query fixtures (#43187)

Asuka Minatob3188a299bb9
diff --git a/api/tests/unit_tests/services/test_account_deletion_feedback_service.py b/api/tests/unit_tests/services/test_account_deletion_feedback_service.py
index 7d427c94c6d..a0346b656cf 100644
--- a/api/tests/unit_tests/services/test_account_deletion_feedback_service.py
+++ b/api/tests/unit_tests/services/test_account_deletion_feedback_service.py
@@ -1,12 +1,14 @@
-from unittest.mock import Mock
+from pytest_mock import MockerFixture
-from services.account_deletion_feedback_service import AccountDeletionFeedbackGateway, AccountDeletionFeedbackService
+from services.account.adapters import BillingAccountDeletionFeedbackGateway
+from services.account_deletion_feedback_service import AccountDeletionFeedbackService
+from services.billing_service import BillingService
-def test_submit_delegates_to_billing_gateway() -> None:
- feedback = Mock(spec=AccountDeletionFeedbackGateway)
- service = AccountDeletionFeedbackService(feedback=feedback)
+def test_submit_delegates_to_billing_gateway(mocker: MockerFixture) -> None:
+ submit = mocker.patch.object(BillingService, "update_account_deletion_feedback")
+ service = AccountDeletionFeedbackService(feedback=BillingAccountDeletionFeedbackGateway())
service.submit(email="[email protected]", feedback="No longer needed")
- feedback.submit.assert_called_once_with(email="[email protected]", feedback="No longer needed")
+ submit.assert_called_once_with("[email protected]", "No longer needed")
diff --git a/api/tests/unit_tests/services/test_account_integration_service.py b/api/tests/unit_tests/services/test_account_integration_service.py
index a461b020dae..6c6d5ff0cb5 100644
--- a/api/tests/unit_tests/services/test_account_integration_service.py
+++ b/api/tests/unit_tests/services/test_account_integration_service.py
@@ -1,21 +1,31 @@
from __future__ import annotations
from datetime import datetime
-from unittest.mock import Mock
+
+from pytest_mock import MockerFixture
+from sqlalchemy.orm import Session, sessionmaker
from machinery.context import RequestContext
+from models.account import AccountIntegrate
+from repositories.account_integration_repository import SQLAlchemyAccountIntegrationRepository
from services.account_integration_service import AccountIntegrationService
-from services.account_ports import AccountIntegrationRepository
-from services.entities.account_entities import AccountIntegrationSnapshot
+from tests.unit_tests.model_factories import make_account
-def test_list_merges_configured_providers_with_persisted_integrations() -> None:
+def test_list_merges_configured_providers_with_persisted_integrations(
+ sqlite_session_factory: sessionmaker[Session], mocker: MockerFixture
+) -> None:
created_at = datetime(2026, 1, 1)
- integrations = Mock(spec=AccountIntegrationRepository)
- integrations.list_for_account.return_value = [
- AccountIntegrationSnapshot(provider="github", created_at=created_at),
- AccountIntegrationSnapshot(provider="ignored", created_at=created_at),
- ]
+ with sqlite_session_factory.begin() as session:
+ session.add(make_account())
+ for provider in ("github", "ignored"):
+ integration = AccountIntegrate(
+ account_id="account-1", provider=provider, open_id=f"{provider}-user", encrypted_token=""
+ )
+ integration.created_at = created_at
+ session.add(integration)
+ integrations = SQLAlchemyAccountIntegrationRepository(sqlite_session_factory)
+ lookup = mocker.spy(integrations, "list_for_account")
service = AccountIntegrationService(
integrations=integrations,
providers=("github", "google"),
@@ -33,4 +43,4 @@ def test_list_merges_configured_providers_with_persisted_integrations() -> None:
("github", created_at, True),
("google", None, False),
]
- integrations.list_for_account.assert_called_once_with("account-1")
+ lookup.assert_called_once_with("account-1")
diff --git a/api/tests/unit_tests/services/test_billing_portal_service.py b/api/tests/unit_tests/services/test_billing_portal_service.py
index 9af98ed8368..20395950f84 100644
--- a/api/tests/unit_tests/services/test_billing_portal_service.py
+++ b/api/tests/unit_tests/services/test_billing_portal_service.py
@@ -1,13 +1,15 @@
-from unittest.mock import MagicMock, Mock
+from unittest.mock import MagicMock
import pytest
+from pytest_mock import MockerFixture
+from sqlalchemy.orm import Session, sessionmaker
from machinery.context import RequestContext
+from models.account import Account
+from repositories.account.repository import SQLAlchemyAccountRepository
from services.account_errors import AccountNotFoundError
-from services.account_ports import AccountRepository
from services.billing_portal_service import BillingPortalService
-from services.entities.account_entities import AccountSnapshot
-from tests.unit_tests.model_factories import make_account_snapshot
+from tests.unit_tests.model_factories import make_account
def _context() -> RequestContext:
@@ -19,10 +21,6 @@ def _context() -> RequestContext:
)
-def _account() -> AccountSnapshot:
- return make_account_snapshot(email="[email protected]")
-
-
@pytest.fixture
def get_subscription() -> MagicMock:
return MagicMock()
@@ -34,22 +32,26 @@ def get_invoices() -> MagicMock:
@pytest.fixture
-def accounts() -> Mock:
- accounts = Mock(spec=AccountRepository)
- accounts.get.return_value = _account()
- return accounts
+def accounts(sqlite_session_factory: sessionmaker[Session]) -> SQLAlchemyAccountRepository:
+ with sqlite_session_factory.begin() as session:
+ session.add(make_account(email="[email protected]"))
+ return SQLAlchemyAccountRepository(sqlite_session_factory)
@pytest.fixture
-def service(accounts: Mock, get_subscription: MagicMock, get_invoices: MagicMock) -> BillingPortalService:
+def service(
+ accounts: SQLAlchemyAccountRepository, get_subscription: MagicMock, get_invoices: MagicMock
+) -> BillingPortalService:
return BillingPortalService(accounts=accounts, get_subscription=get_subscription, get_invoices=get_invoices)
def test_get_subscription_loads_email_and_delegates(
service: BillingPortalService,
- accounts: Mock,
+ accounts: SQLAlchemyAccountRepository,
get_subscription: MagicMock,
+ mocker: MockerFixture,
) -> None:
+ lookup = mocker.spy(accounts, "get")
get_subscription.return_value = {"url": "https://billing.example.com/checkout"}
result = service.get_subscription(
@@ -59,30 +61,35 @@ def test_get_subscription_loads_email_and_delegates(
)
assert result == {"url": "https://billing.example.com/checkout"}
- accounts.get.assert_called_once_with("account-1")
+ lookup.assert_called_once_with("account-1")
get_subscription.assert_called_once_with("professional", "month", "[email protected]", "workspace-1")
def test_get_invoices_loads_email_and_delegates(
service: BillingPortalService,
- accounts: Mock,
+ accounts: SQLAlchemyAccountRepository,
get_invoices: MagicMock,
+ mocker: MockerFixture,
) -> None:
+ lookup = mocker.spy(accounts, "get")
get_invoices.return_value = {"url": "https://billing.example.com/portal"}
result = service.get_invoices(_context())
assert result == {"url": "https://billing.example.com/portal"}
- accounts.get.assert_called_once_with("account-1")
+ lookup.assert_called_once_with("account-1")
get_invoices.assert_called_once_with("[email protected]", "workspace-1")
def test_missing_account_does_not_call_billing(
service: BillingPortalService,
- accounts: Mock,
+ sqlite_session_factory: sessionmaker[Session],
get_invoices: MagicMock,
) -> None:
- accounts.get.return_value = None
+ with sqlite_session_factory.begin() as session:
+ account = session.get(Account, "account-1")
+ assert account is not None
+ session.delete(account)
with pytest.raises(AccountNotFoundError):
service.get_invoices(_context())
diff --git a/api/tests/unit_tests/services/test_step_by_step_tour_service.py b/api/tests/unit_tests/services/test_step_by_step_tour_service.py
index 25413aa3449..44d6a8f37fc 100644
--- a/api/tests/unit_tests/services/test_step_by_step_tour_service.py
+++ b/api/tests/unit_tests/services/test_step_by_step_tour_service.py
@@ -3,16 +3,16 @@ from __future__ import annotations
from collections.abc import Callable
from dataclasses import replace
from datetime import datetime
-from unittest.mock import Mock
import pytest
+from sqlalchemy.orm import Session, sessionmaker
from machinery.context import RequestContext
-from services.account_ports import AccountRepository
+from repositories.account.repository import SQLAlchemyAccountRepository
from services.entities.account_entities import AccountSnapshot
from services.entities.onboarding_entities import StepByStepTourPatch, StepByStepTourResult, StepByStepTourState
from services.step_by_step_tour_service import StepByStepTourService
-from tests.unit_tests.model_factories import make_account_snapshot
+from tests.unit_tests.model_factories import make_account, make_account_snapshot
def _context(*, workspace_id: str = "workspace-1") -> RequestContext:
@@ -59,13 +59,19 @@ def _account(*, started_at: datetime = datetime(2026, 6, 28)) -> AccountSnapshot
return make_account_snapshot(initialized_at=started_at, created_at=started_at)
-def _accounts(account: AccountSnapshot | None) -> Mock:
- accounts = Mock(spec=AccountRepository)
- accounts.get.return_value = account
- return accounts
+def _accounts(session_factory: sessionmaker[Session], account: AccountSnapshot | None) -> SQLAlchemyAccountRepository:
+ """Persist the supplied account; None leaves the repository empty."""
+ if account is not None:
+ row = make_account(account_id=account.id, name=account.name, email=account.email)
+ row.created_at = account.created_at
+ row.initialized_at = account.initialized_at
+ with session_factory.begin() as session:
+ session.add(row)
+ return SQLAlchemyAccountRepository(session_factory)
def _service(
+ session_factory: sessionmaker[Session],
*,
states: StateRepositoryStub,
account: AccountSnapshot | None = None,
@@ -73,17 +79,19 @@ def _service(
rollout_started_at: datetime | None = datetime(2026, 6, 1),
) -> StepByStepTourService:
return StepByStepTourService(
- accounts=_accounts(account or _account()),
+ accounts=_accounts(session_factory, account or _account()),
states=states,
enabled=enabled,
rollout_started_at=rollout_started_at,
)
-def test_get_state_creates_state_and_records_first_workspace_for_eligible_account() -> None:
+def test_get_state_creates_state_and_records_first_workspace_for_eligible_account(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
states = StateRepositoryStub()
- result = _service(states=states).get_state(_context())
+ result = _service(sqlite_session_factory, states=states).get_state(_context())
assert result.first_workspace_id == "workspace-1"
assert states.get_account_ids == []
@@ -91,20 +99,22 @@ def test_get_state_creates_state_and_records_first_workspace_for_eligible_accoun
assert states.mutation_account_ids == []
-def test_get_state_returns_existing_state_without_rewriting_first_workspace() -> None:
+def test_get_state_returns_existing_state_without_rewriting_first_workspace(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
state = StepByStepTourState(account_id="account-1", first_workspace_id="workspace-original")
states = StateRepositoryStub(state)
- result = _service(states=states).get_state(_context(workspace_id="workspace-current"))
+ result = _service(sqlite_session_factory, states=states).get_state(_context(workspace_id="workspace-current"))
assert result.first_workspace_id == "workspace-original"
assert states.initialize_calls == [("account-1", "workspace-current")]
assert states.mutation_account_ids == []
-def test_get_state_does_not_create_state_for_ineligible_account() -> None:
+def test_get_state_does_not_create_state_for_ineligible_account(sqlite_session_factory: sessionmaker[Session]) -> None:
states = StateRepositoryStub()
- service = _service(states=states, account=_account(started_at=datetime(2026, 5, 31)))
+ service = _service(sqlite_session_factory, states=states, account=_account(started_at=datetime(2026, 5, 31)))
result = service.get_state(_context())
@@ -113,18 +123,18 @@ def test_get_state_does_not_create_state_for_ineligible_account() -> None:
assert states.mutation_account_ids == []
-def test_get_state_does_not_create_state_when_tour_is_disabled() -> None:
+def test_get_state_does_not_create_state_when_tour_is_disabled(sqlite_session_factory: sessionmaker[Session]) -> None:
states = StateRepositoryStub()
- result = _service(states=states, enabled=False).get_state(_context())
+ result = _service(sqlite_session_factory, states=states, enabled=False).get_state(_context())
assert result == StepByStepTourResult()
assert states.get_account_ids == ["account-1"]
-def test_patch_state_persists_even_when_tour_is_disabled() -> None:
+def test_patch_state_persists_even_when_tour_is_disabled(sqlite_session_factory: sessionmaker[Session]) -> None:
states = StateRepositoryStub()
- service = _service(states=states, enabled=False)
+ service = _service(sqlite_session_factory, states=states, enabled=False)
result = service.patch_state(_context(workspace_id="workspace-2"), StepByStepTourPatch("enable_current_workspace"))
@@ -132,7 +142,7 @@ def test_patch_state_persists_even_when_tour_is_disabled() -> None:
assert states.mutation_account_ids == ["account-1"]
-def test_patch_state_skip_removes_current_workspace_enable() -> None:
+def test_patch_state_skip_removes_current_workspace_enable(sqlite_session_factory: sessionmaker[Session]) -> None:
states = StateRepositoryStub(
StepByStepTourState(
account_id="account-1",
@@ -140,13 +150,13 @@ def test_patch_state_skip_removes_current_workspace_enable() -> None:
)
)
- result = _service(states=states).patch_state(_context(), StepByStepTourPatch("skip"))
+ result = _service(sqlite_session_factory, states=states).patch_state(_context(), StepByStepTourPatch("skip"))
assert result.skipped is True
assert result.manually_enabled_workspace_ids == ("workspace-2",)
-def test_patch_state_disable_moves_current_workspace_to_disabled() -> None:
+def test_patch_state_disable_moves_current_workspace_to_disabled(sqlite_session_factory: sessionmaker[Session]) -> None:
states = StateRepositoryStub(
StepByStepTourState(
account_id="account-1",
@@ -154,7 +164,7 @@ def test_patch_state_disable_moves_current_workspace_to_disabled() -> None:
)
)
- result = _service(states=states).patch_state(
+ result = _service(sqlite_session_factory, states=states).patch_state(
_context(),
StepByStepTourPatch("disable_current_workspace"),
)
@@ -163,9 +173,9 @@ def test_patch_state_disable_moves_current_workspace_to_disabled() -> None:
assert result.manually_disabled_workspace_ids == ("workspace-1",)
-def test_patch_state_complete_and_uncomplete_task() -> None:
+def test_patch_state_complete_and_uncomplete_task(sqlite_session_factory: sessionmaker[Session]) -> None:
states = StateRepositoryStub(StepByStepTourState(account_id="account-1", completed_task_ids=("home",)))
- service = _service(states=states)
+ service = _service(sqlite_session_factory, states=states)
service.patch_state(_context(), StepByStepTourPatch("complete_task", "studio"))
result = service.patch_state(_context(), StepByStepTourPatch("uncomplete_task", "home"))
@@ -178,10 +188,10 @@ def test_rejects_unsupported_task_id() -> None:
StepByStepTourService._require_task_id("unknown")
-def test_get_state_rejects_unknown_admitted_account() -> None:
+def test_get_state_rejects_unknown_admitted_account(sqlite_session_factory: sessionmaker[Session]) -> None:
states = StateRepositoryStub()
service = StepByStepTourService(
- accounts=_accounts(None),
+ accounts=_accounts(sqlite_session_factory, None),
states=states,
enabled=True,
rollout_started_at=datetime(2026, 6, 1),