|
|
"""Tests for the Gemini AI client.""" |
|
|
|
|
|
import os |
|
|
import unittest |
|
|
from unittest.mock import MagicMock, patch |
|
|
|
|
|
import pytest |
|
|
|
|
|
from src.folio.gemini_client import GeminiClient |
|
|
|
|
|
|
|
|
class TestGeminiClient(unittest.TestCase): |
|
|
"""Tests for the Gemini AI client.""" |
|
|
|
|
|
def setUp(self): |
|
|
"""Set up test environment.""" |
|
|
|
|
|
self.original_env = os.environ.copy() |
|
|
|
|
|
os.environ["GEMINI_API_KEY"] = "test_api_key" |
|
|
|
|
|
def tearDown(self): |
|
|
"""Clean up test environment.""" |
|
|
|
|
|
os.environ.clear() |
|
|
os.environ.update(self.original_env) |
|
|
|
|
|
@patch("google.generativeai.configure") |
|
|
@patch("google.generativeai.GenerativeModel") |
|
|
def test_init_with_api_key(self, mock_model, mock_configure): |
|
|
"""Test that the client initializes correctly with an API key.""" |
|
|
|
|
|
mock_model.return_value = MagicMock() |
|
|
|
|
|
|
|
|
client = GeminiClient() |
|
|
|
|
|
|
|
|
mock_configure.assert_called_once_with(api_key="test_api_key") |
|
|
|
|
|
mock_model.assert_called_once() |
|
|
|
|
|
assert hasattr(client, "model") |
|
|
|
|
|
@patch("google.generativeai.configure") |
|
|
@patch("google.generativeai.GenerativeModel") |
|
|
def test_init_without_api_key(self, mock_model, mock_configure): |
|
|
"""Test that the client raises an error when no API key is provided.""" |
|
|
|
|
|
if "GEMINI_API_KEY" in os.environ: |
|
|
del os.environ["GEMINI_API_KEY"] |
|
|
|
|
|
|
|
|
with pytest.raises( |
|
|
ValueError, match="GEMINI_API_KEY environment variable not set" |
|
|
): |
|
|
GeminiClient() |
|
|
|
|
|
|
|
|
mock_configure.assert_not_called() |
|
|
|
|
|
mock_model.assert_not_called() |
|
|
|
|
|
@patch("google.generativeai.configure") |
|
|
@patch("google.generativeai.GenerativeModel") |
|
|
def test_chat_sync_basic(self, mock_model, _mock_configure): |
|
|
"""Test that the chat_sync method works correctly.""" |
|
|
|
|
|
mock_model_instance = MagicMock() |
|
|
mock_model.return_value = mock_model_instance |
|
|
|
|
|
|
|
|
mock_chat = MagicMock() |
|
|
mock_model_instance.start_chat.return_value = mock_chat |
|
|
|
|
|
|
|
|
mock_response = MagicMock() |
|
|
mock_response.text = "Test response" |
|
|
mock_chat.send_message.return_value = mock_response |
|
|
|
|
|
|
|
|
client = GeminiClient() |
|
|
|
|
|
|
|
|
result = client.chat_sync("Test message", []) |
|
|
|
|
|
|
|
|
mock_model_instance.start_chat.assert_called_once() |
|
|
|
|
|
|
|
|
mock_chat.send_message.assert_called_once() |
|
|
|
|
|
|
|
|
assert result["response"] == "Test response" |
|
|
assert result["complete"] is True |
|
|
|
|
|
@patch("google.generativeai.configure") |
|
|
@patch("google.generativeai.GenerativeModel") |
|
|
def test_chat_sync_with_error(self, mock_model, _mock_configure): |
|
|
"""Test that the chat_sync method handles errors correctly.""" |
|
|
|
|
|
mock_model_instance = MagicMock() |
|
|
mock_model.return_value = mock_model_instance |
|
|
|
|
|
|
|
|
mock_model_instance.start_chat.side_effect = Exception("Test error") |
|
|
|
|
|
|
|
|
client = GeminiClient() |
|
|
|
|
|
|
|
|
result = client.chat_sync("Test message", []) |
|
|
|
|
|
|
|
|
assert "error" in result |
|
|
assert result["error"] is True |
|
|
assert "Test error" in result["response"] |
|
|
assert result["complete"] is False |
|
|
|