Fix integration tests to use lazy client injection and remove unused PropertyMock import
Co-authored-by: Kenearos <86194771+Kenearos@users.noreply.github.com>
This commit is contained in:
parent
b72cd9db1c
commit
216c7401d7
2 changed files with 27 additions and 21 deletions
|
|
@ -3,7 +3,7 @@ Tests for PerplexityProvider AI API class
|
|||
"""
|
||||
import pytest
|
||||
import httpx
|
||||
from unittest.mock import AsyncMock, Mock, patch, PropertyMock
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
from ai_provider import PerplexityProvider
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -135,21 +135,24 @@ class TestFullWorkflow:
|
|||
memory.add_message(username, "user", "Hello")
|
||||
memory.add_message(username, "assistant", "Hi there!")
|
||||
|
||||
# Simulate API failure
|
||||
with patch('httpx.AsyncClient') as mock_client:
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 500
|
||||
mock_response.text = "Internal Server Error"
|
||||
# Mock API client for lazy initialization (simulating 500 error)
|
||||
mock_client = Mock()
|
||||
mock_client.is_closed = False
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 500
|
||||
mock_response.text = "Internal Server Error"
|
||||
|
||||
mock_post = AsyncMock(return_value=mock_response)
|
||||
mock_client.return_value.__aenter__.return_value.post = mock_post
|
||||
mock_client.post = AsyncMock(return_value=mock_response)
|
||||
|
||||
# API call fails
|
||||
response = await ai.get_response([{"role": "user", "content": "test"}])
|
||||
assert response is None
|
||||
# Inject the mock client
|
||||
ai._client = mock_client
|
||||
|
||||
# Verify error was tracked
|
||||
assert ai.total_errors == 1
|
||||
# API call fails
|
||||
response = await ai.get_response([{"role": "user", "content": "test"}])
|
||||
assert response is None
|
||||
|
||||
# Verify error was tracked
|
||||
assert ai.total_errors == 1
|
||||
|
||||
# Verify memory still intact despite API failure
|
||||
history = memory.get_user_history(username)
|
||||
|
|
@ -239,16 +242,19 @@ class TestComponentInteraction:
|
|||
logger = Logger(log_dir=str(temp_dir / "logs"), debug_mode=True)
|
||||
ai = PerplexityProvider(api_key="test-key", logger=logger)
|
||||
|
||||
# Mock API call
|
||||
with patch('httpx.AsyncClient') as mock_client:
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = mock_perplexity_response
|
||||
# Mock API client for lazy initialization
|
||||
mock_client = Mock()
|
||||
mock_client.is_closed = False
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = mock_perplexity_response
|
||||
|
||||
mock_post = AsyncMock(return_value=mock_response)
|
||||
mock_client.return_value.__aenter__.return_value.post = mock_post
|
||||
mock_client.post = AsyncMock(return_value=mock_response)
|
||||
|
||||
await ai.get_response([{"role": "user", "content": "test"}])
|
||||
# Inject the mock client
|
||||
ai._client = mock_client
|
||||
|
||||
await ai.get_response([{"role": "user", "content": "test"}])
|
||||
|
||||
# Verify log was created
|
||||
assert (temp_dir / "logs" / "eugen.log").exists()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue