testsAndMisc/python_pkg/code_tutor/tests/test_llm.py
Krzysztof kuhy Rudnicki e3214c813c
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
Python tests / test (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Clear the seven over-cap code_tutor test modules
Partitioned at the section banners each file already used, into ten new
modules named for what they cover. python_pkg/code_tutor is now entirely
under the cap, from thirteen violations.

The splitter copies the whole original header into each new file and
lets ruff's F401 pass prune it - 59 imports removed correctly, versus a
hand-written import block failing on every earlier test split today.
That does not cover module-level helpers: _make_verifier sat between two
banners and was left behind by its callers, so it moved to conftest.py,
taking test_verifier.py's shadowing copy of _make_live_mock with it.

No patch string changed. Pass count is 217 before and after.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-16 18:06:38 +02:00

75 lines
2.4 KiB
Python

"""Tests for python_pkg.code_tutor._llm."""
from __future__ import annotations
import json
from unittest.mock import MagicMock, patch
from python_pkg.code_tutor._llm import (
OllamaBackend,
)
# ---------------------------------------------------------------------------
# OllamaBackend.complete
# ---------------------------------------------------------------------------
def test_ollama_complete_success() -> None:
mock_resp = MagicMock()
mock_resp.json.return_value = {"response": "hello world"}
with patch("python_pkg.code_tutor._llm.requests.post", return_value=mock_resp):
backend = OllamaBackend()
result = backend.complete("sys", "user")
assert result == "hello world"
mock_resp.raise_for_status.assert_called_once()
def test_ollama_complete_missing_key() -> None:
mock_resp = MagicMock()
mock_resp.json.return_value = {}
with patch("python_pkg.code_tutor._llm.requests.post", return_value=mock_resp):
backend = OllamaBackend()
result = backend.complete("sys", "user")
assert result == ""
# ---------------------------------------------------------------------------
# OllamaBackend.stream
# ---------------------------------------------------------------------------
def test_ollama_stream_basic() -> None:
lines = [
json.dumps({"response": "hello", "done": False}).encode(),
b"", # empty line -> skipped
json.dumps({"response": " world", "done": False}).encode(),
json.dumps({"response": "", "done": True}).encode(),
]
mock_resp = MagicMock()
mock_resp.iter_lines.return_value = iter(lines)
tokens: list[str] = []
with patch("python_pkg.code_tutor._llm.requests.post", return_value=mock_resp):
backend = OllamaBackend()
result = backend.stream("sys", "user", tokens.append)
assert result == "hello world"
assert tokens == ["hello", " world"]
def test_ollama_stream_empty_token_not_appended() -> None:
lines = [
json.dumps({"response": "", "done": False}).encode(),
json.dumps({"response": "ok", "done": True}).encode(),
]
mock_resp = MagicMock()
mock_resp.iter_lines.return_value = iter(lines)
tokens: list[str] = []
with patch("python_pkg.code_tutor._llm.requests.post", return_value=mock_resp):
backend = OllamaBackend()
result = backend.stream("sys", "user", tokens.append)
assert result == "ok"
assert tokens == ["ok"]