Add Ollama client tests#36
Conversation
| from src.infrastructure import llm | ||
|
|
||
|
|
||
| def mock_async_client(json_data=None, raise_for_status_side_effect=None): |
There was a problem hiding this comment.
Test behaviour looks correct! One code style suggestion:
The mock_async_client helper currently uses patcher.start() and every test needs its own try/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 the patcher.start() lines and the return change:
- Add
from contextlib import contextmanagerat the top - Add
@contextmanagerabove the helper definition - Setup stays the same up to
client.post = AsyncMock(return_value=response). After that, replace:
patcher = patch("src.infrastructure.llm.httpx.AsyncClient")
mocked_async_client_cls = patcher.start()
with
with patch("src.infrastructure.llm.httpx.AsyncClient") as mocked_async_client_cls:
and put the rest of the lines indented inside the with block
- The final return would just become
yield client, response
Then each test can drop the patcher, client, _ = ... and try/finally lines and just have something like:
with mock_async_client(json_data={"embedding": fake_embedding}) as (client, _):
<test code...>
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 with mock_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.
No description provided.