Files
photools3/test/test_llm_session_mocked.py
T
mathias2784 401488fa7e
Tests / test_and_lint (push) Successful in 14s
Llm client quality (#14)
Reviewed-on: #14
Co-authored-by: Mathias Scheider <mathias2784@web.de>
2026-10-02 21:31:21 +00:00

87 lines
3.2 KiB
Python

from unittest.mock import MagicMock, patch
from photools3.llm_session import LlmSession
def test_llm_session_mocked(test_photo):
# We want to mock OpenAI client's chat.completions.create
with patch("photools3.llm_session.OpenAI") as mocked_openai:
# Setup the mock response structure
mock_client = MagicMock()
mocked_openai.return_value = mock_client
# Mock the models list to return a proper object with an ID string
# This prevents Pydantic ValidationError when LlmSession tries to use model_id
mock_model = MagicMock()
mock_model.id = "gpt-4o"
mock_client.models.list.return_value.data = [mock_model]
# Create a response object: response.choices[0].message.content
mock_response = MagicMock()
mock_choice = MagicMock()
mock_message = MagicMock()
mock_message.content = '{"text": "some text"}'
mock_choice.message = mock_message
mock_response.choices = [mock_choice]
# Configure the client to return our mock response
mock_client.chat.completions.create.return_value = mock_response
# Create a new LlmSession instance that will use the mocked OpenAI client
mocked_session = LlmSession(system_prompt="You are a helper.")
# Run the test
result = mocked_session.call(
prompt="keywords for the attached picture", photos=[test_photo]
)
# Verify the results
assert result.text == "some text"
# Verify that the mock was enough
assert mock_client.chat.completions.create.called
def test_llm_session_captions_and_keywords_mocked(test_photo):
# We want to mock OpenAI client's chat.completions.create
with patch("photools3.llm_session.OpenAI") as mocked_openai:
# Setup the mock response structure
mock_client = MagicMock()
mocked_openai.return_value = mock_client
# Mock the models list to return a proper object with an ID string
# This prevents Pydantic ValidationError when LlmSession tries to use model_id
mock_model = MagicMock()
mock_model.id = "gpt-4o"
mock_client.models.list.return_value.data = [mock_model]
# Create a response object: response.choices[0].message.content
mock_response = MagicMock()
mock_choice = MagicMock()
mock_message = MagicMock()
mock_message.content = (
'{"caption": "A mocked caption", "tags": ["architektur"]}'
)
mock_choice.message = mock_message
mock_response.choices = [mock_choice]
# Configure the client to return our mock response
mock_client.chat.completions.create.return_value = mock_response
# Create a new LlmSessionCaptionsAndKeywords instance that will use the mocked OpenAI client
from photools3.llm_session import LlmSessionCaptionsAndKeywords
mocked_session = LlmSessionCaptionsAndKeywords()
# Run the test
result = mocked_session.call(photos=[test_photo])
# Verify the results
assert result.caption == "A mocked caption"
assert "architektur" in result.tags
# Verify that the mock was called
assert mock_client.chat.completions.create.called