"""Выбор реализации CRM-клиента по `CRM_USE_STUB`.""" from __future__ import annotations from collections.abc import Iterator import pytest from app.core.config import settings from app.services.crm.client import get_crm_client from app.services.crm.exo_client import ExoCrmClient from app.services.crm.stub_client import StubCrmClient @pytest.fixture(autouse=True) def _fresh_factory() -> Iterator[None]: get_crm_client.cache_clear() yield get_crm_client.cache_clear() def test_real_client_by_default(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(settings, "crm_use_stub", False) assert isinstance(get_crm_client(), ExoCrmClient) def test_stub_when_enabled(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(settings, "crm_use_stub", True) monkeypatch.setattr(settings, "environment", "local") assert isinstance(get_crm_client(), StubCrmClient) def test_stub_forbidden_in_production(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(settings, "crm_use_stub", True) monkeypatch.setattr(settings, "environment", "production") with pytest.raises(RuntimeError, match="CRM_USE_STUB"): get_crm_client()