mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-10 22:27:01 +01:00
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
75 lines
2.4 KiB
Python
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"]
|