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

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