fix: mock LLM clients in test_llm_factory to avoid API key requirement

- Add mock_chat_anthropic and mock_chat_deepseek fixtures to conftest.py
- Update test_llm_factory to use monkeypatch and mock fixtures
- Import ChatAnthropic and ChatDeepSeek in conftest.py for spec creation
- Tests now pass without requiring ANTHROPIC_API_KEY or DEEPSEEK_API_KEY
- Execution time reduced from failing after 0.12s to passing in 0.01s

This change allows factory tests to run in isolation without external
dependencies, making them consistent with the API test approach and
suitable for any development environment or CI/CD pipeline.
This commit is contained in:
2026-06-10 16:16:14 -05:00
parent 34053b3fc1
commit d328d801f4
2 changed files with 43 additions and 7 deletions

View File

@@ -9,6 +9,8 @@ from httpx import ASGITransport, AsyncClient
from fastapi import FastAPI from fastapi import FastAPI
from langgraph.checkpoint.memory import MemorySaver from langgraph.checkpoint.memory import MemorySaver
from langchain_core.messages import AIMessage from langchain_core.messages import AIMessage
from langchain_anthropic import ChatAnthropic
from langchain_deepseek import ChatDeepSeek
from naliiabot.bot.prompts.load_prompt import get_prompt_template from naliiabot.bot.prompts.load_prompt import get_prompt_template
from naliiabot.bot.tools.naliia_tools import NaliiaTools from naliiabot.bot.tools.naliia_tools import NaliiaTools
import naliiabotapi.api.dependencies as deps import naliiabotapi.api.dependencies as deps
@@ -241,3 +243,19 @@ def send_message_payload():
"server_url": "http://localhost:8080", "server_url": "http://localhost:8080",
"apikey": "98B6B820AFC3-4544-9A4C-011B0719C2D1" "apikey": "98B6B820AFC3-4544-9A4C-011B0719C2D1"
} }
@pytest.fixture
def mock_chat_anthropic():
"""Mock para ChatAnthropic que no requiere API key."""
mock = Mock(spec=ChatAnthropic)
mock.model_name = "claude-sonnet-4-5-20250929"
return mock
@pytest.fixture
def mock_chat_deepseek():
"""Mock para ChatDeepSeek que no requiere API key."""
mock = Mock(spec=ChatDeepSeek)
mock.model_name = "deepseek-chat"
return mock

View File

@@ -1,17 +1,35 @@
import pytest import pytest
from unittest.mock import Mock
from naliiabot.bot.factories.llm_factory import LLMFactory from naliiabot.bot.factories.llm_factory import LLMFactory
from naliiabot.bot.exceptions import NotFoundProviderError from naliiabot.bot.exceptions import NotFoundProviderError
from langchain_anthropic import ChatAnthropic from langchain_anthropic import ChatAnthropic
from langchain_deepseek import ChatDeepSeek from langchain_deepseek import ChatDeepSeek
def test_llm_factory(): def test_llm_factory(monkeypatch, mock_chat_anthropic, mock_chat_deepseek):
"""Test que LLMFactory retorna el tipo correcto de modelo según el provider."""
antrophicModel = LLMFactory("anthropic").get_model() # Mock ChatAnthropic para evitar necesitar API key
assert isinstance(antrophicModel, ChatAnthropic) is True monkeypatch.setattr(
"naliiabot.bot.factories.llm_factory.ChatAnthropic",
Mock(return_value=mock_chat_anthropic)
)
deepseekModel = LLMFactory("deepseek").get_model() # Mock ChatDeepSeek para evitar necesitar API key
assert isinstance(deepseekModel, ChatDeepSeek) monkeypatch.setattr(
"naliiabot.bot.factories.llm_factory.ChatDeepSeek",
Mock(return_value=mock_chat_deepseek)
)
# Test Anthropic
anthropic_model = LLMFactory("anthropic").get_model()
assert isinstance(anthropic_model, Mock)
assert anthropic_model == mock_chat_anthropic
# Test DeepSeek
deepseek_model = LLMFactory("deepseek").get_model()
assert isinstance(deepseek_model, Mock)
assert deepseek_model == mock_chat_deepseek
def test_get_model_raises_exception_when_provider_not_found(): def test_get_model_raises_exception_when_provider_not_found():