Add tests for LlmSession and LlmSessionCaptionsAndKeywords in both integration and mocked scenarios
Tests / test_and_lint (push) Successful in 13s

This commit is contained in:
2026-10-02 11:20:16 +02:00
parent 906707de55
commit ff86f08ae7
9 changed files with 994 additions and 29 deletions
+8 -6
View File
@@ -12,9 +12,11 @@ def test_photo():
@pytest.fixture
def llm_session():
return LlmSession(
system_prompt=(
"You are a helpful assistent to photographer. "
'Answer in JSON format: `{"caption": "...", "tags": ["tag1", "tag2", ...]}`'
)
)
return LlmSession(system_prompt="You are a helpful assistant.")
@pytest.fixture
def llm_session_captions_and_keywords():
from photools3.llm_session import LlmSessionCaptionsAndKeywords
return LlmSessionCaptionsAndKeywords()
+15 -1
View File
@@ -7,7 +7,21 @@ from photools3.llm_session import LlmSession
not LlmSession(system_prompt="").is_api_reachable(), reason="API not available"
)
def test_llm_session(llm_session, test_photo):
llm_session.system_prompt = "Return only JSON format: {'keywords': ['...']}"
result = llm_session.call(
prompt="keywords for the attached picture", photos=[test_photo]
)
assert result
assert result is not None
@pytest.mark.skipif(
not LlmSession(system_prompt="").is_api_reachable(), reason="API not available"
)
def test_llm_session_captions_and_keywords(
llm_session_captions_and_keywords, test_photo
):
result = llm_session_captions_and_keywords.call(photos=[test_photo])
assert "caption" in result
assert "tags" in result
assert isinstance(result["caption"], str)
assert isinstance(result["tags"], list)
+41
View File
@@ -45,3 +45,44 @@ def test_llm_session_mocked(test_photo):
# Verify that the mock was called
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": ["mocked_tag"]}'
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 "mocked_tag" in result["tags"]
# Verify that the mock was called
assert mock_client.chat.completions.create.called