-
Notifications
You must be signed in to change notification settings - Fork 5
Add Ollama client tests #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ElyssaN
wants to merge
1
commit into
main
Choose a base branch
from
11-add-ollama-client-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import httpx | ||
| import pytest | ||
|
|
||
| from src.config import settings | ||
| from src.infrastructure import llm | ||
|
|
||
|
|
||
| def mock_async_client(json_data=None, raise_for_status_side_effect=None): | ||
| response = MagicMock() | ||
| response.json.return_value = json_data or {} | ||
| response.raise_for_status.side_effect = raise_for_status_side_effect | ||
|
|
||
| client = AsyncMock() | ||
| client.post = AsyncMock(return_value=response) | ||
|
|
||
| patcher = patch("src.infrastructure.llm.httpx.AsyncClient") | ||
| mocked_async_client_cls = patcher.start() | ||
|
|
||
| mocked_context_manager = AsyncMock() | ||
| mocked_context_manager.__aenter__.return_value = client | ||
| mocked_context_manager.__aexit__.return_value = None | ||
|
|
||
| mocked_async_client_cls.return_value = mocked_context_manager | ||
|
|
||
| return patcher, client, response | ||
|
|
||
|
|
||
| def test_embed_document_prefix_value_is_pinned(): | ||
| assert llm.EMBED_DOCUMENT_PREFIX == "search_document: " | ||
|
|
||
|
|
||
| async def test_embed_uses_query_prefix_and_embedding_model(): | ||
| fake_embedding = [0.0] * settings.embedding_dim | ||
| patcher, client, _ = mock_async_client(json_data={"embedding": fake_embedding}) | ||
|
|
||
| try: | ||
| result = await llm.embed("hello", is_query=True) | ||
|
|
||
| assert result == fake_embedding | ||
| args, kwargs = client.post.call_args | ||
|
|
||
| assert args[0].endswith("/api/embeddings") | ||
| assert kwargs["json"]["model"] == settings.ollama_embedding_model | ||
| assert kwargs["json"]["prompt"] == f"{llm.EMBED_QUERY_PREFIX}hello" | ||
| finally: | ||
| patcher.stop() | ||
|
|
||
|
|
||
| async def test_embed_uses_document_prefix(): | ||
| fake_embedding = [0.0] * settings.embedding_dim | ||
| patcher, client, _ = mock_async_client(json_data={"embedding": fake_embedding}) | ||
|
|
||
| try: | ||
| await llm.embed("hello", is_query=False) | ||
|
|
||
| _, kwargs = client.post.call_args | ||
| assert kwargs["json"]["prompt"] == f"{llm.EMBED_DOCUMENT_PREFIX}hello" | ||
| finally: | ||
| patcher.stop() | ||
|
|
||
|
|
||
| async def test_embed_raises_value_error_on_dimension_mismatch(): | ||
| wrong_embedding = [0.0] * (settings.embedding_dim - 1) | ||
| patcher, _, _ = mock_async_client(json_data={"embedding": wrong_embedding}) | ||
|
|
||
| try: | ||
| with pytest.raises(ValueError, match="mismatch"): | ||
| await llm.embed("hello", is_query=True) | ||
| finally: | ||
| patcher.stop() | ||
|
|
||
|
|
||
| async def test_chat_uses_chat_model_and_returns_message_content(): | ||
| patcher, client, _ = mock_async_client(json_data={"message": {"content": "hi back"}}) | ||
|
|
||
| messages = [{"role": "user", "content": "hello"}] | ||
|
|
||
| try: | ||
| result = await llm.chat(messages) | ||
|
|
||
| assert result == "hi back" | ||
| args, kwargs = client.post.call_args | ||
|
|
||
| assert args[0].endswith("/api/chat") | ||
| assert kwargs["json"]["model"] == settings.ollama_chat_model | ||
| assert kwargs["json"]["messages"] == messages | ||
| assert kwargs["json"]["stream"] is False | ||
| finally: | ||
| patcher.stop() | ||
|
|
||
|
|
||
| async def test_http_errors_propagate_for_embed_and_chat(): | ||
| request = httpx.Request("POST", "http://test/api") | ||
| response = httpx.Response(500, request=request) | ||
| error = httpx.HTTPStatusError("boom", request=request, response=response) | ||
|
|
||
| patcher, _, _ = mock_async_client( | ||
| json_data={}, | ||
| raise_for_status_side_effect=error, | ||
| ) | ||
|
|
||
| try: | ||
| with pytest.raises(httpx.HTTPStatusError): | ||
| await llm.embed("hello", is_query=True) | ||
|
|
||
| with pytest.raises(httpx.HTTPStatusError): | ||
| await llm.chat([{"role": "user", "content": "hello"}]) | ||
| finally: | ||
| patcher.stop() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test behaviour looks correct! One code style suggestion:
The mock_async_client helper currently uses
patcher.start()and every test needs its owntry/finally: patcher.stop()to clean up. If you make the helper a@contextmanager, cleanup becomes automatic and the per-test boilerplate goes away. The response/client setup at the top is unchanged — only thepatcher.start()lines and the return change:from contextlib import contextmanagerat the top@contextmanagerabove the helper definitionclient.post = AsyncMock(return_value=response). After that, replace:with
and put the rest of the lines indented inside the with block
yield client, responseThen each test can drop the
patcher, client, _ = ...andtry/finallylines and just have something like:If a test doesn't use client/response (like the one on line 64 currently), you can drop the
as (client, _)entirely and just write withmock_async_client(...):These changes help trim some boilerplate code in the tests and help prevent a hypothetical future test from forgetting the stop() for cleanup.