Files
dify/api/tests/unit_tests/services/test_account_profile_service.py
T
Mesrop Harutyunyanandautofix-ci[bot] 104f3b239b refactor(api): merge duplicated model builders in unit tests (#41549)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-09-18 05:10:25 +00:00

73 lines
2.3 KiB
Python

from __future__ import annotations
from unittest.mock import Mock
import pytest
from machinery.context import RequestContext
from services.account_errors import AccountNotFoundError
from services.account_ports import AccountRepository
from services.account_profile_service import AccountProfileService
from services.entities.account_entities import AccountProfileChanges, AccountSnapshot
from tests.unit_tests.model_factories import make_account_snapshot
def _context() -> RequestContext:
return RequestContext(
request_id="request-1",
trace_id="trace-1",
account_id="account-1",
active_workspace_id="workspace-1",
)
def _account() -> AccountSnapshot:
return make_account_snapshot()
def test_get_returns_framework_neutral_account_snapshot() -> None:
accounts = Mock(spec=AccountRepository)
accounts.get.return_value = _account()
service = AccountProfileService(accounts=accounts)
result = service.get(_context())
assert result == _account()
accounts.get.assert_called_once_with("account-1")
def test_update_applies_profile_changes() -> None:
accounts = Mock(spec=AccountRepository)
accounts.update_profile.return_value = _account()
service = AccountProfileService(accounts=accounts)
changes = AccountProfileChanges(name="Updated", timezone="Asia/Singapore")
result = service.update(_context(), changes)
assert result == _account()
accounts.update_profile.assert_called_once_with("account-1", changes)
def test_update_treats_empty_changes_as_noop() -> None:
accounts = Mock(spec=AccountRepository)
accounts.get.return_value = _account()
service = AccountProfileService(accounts=accounts)
result = service.update(_context(), AccountProfileChanges())
assert result == _account()
accounts.get.assert_called_once_with("account-1")
accounts.update_profile.assert_not_called()
def test_update_rejects_missing_account() -> None:
accounts = Mock(spec=AccountRepository)
accounts.update_profile.return_value = None
service = AccountProfileService(accounts=accounts)
changes = AccountProfileChanges(name="Updated")
with pytest.raises(AccountNotFoundError):
service.update(_context(), changes)
accounts.update_profile.assert_called_once_with("account-1", changes)