File size: 2,657 Bytes
f1b0176
 
 
 
 
 
 
 
 
 
 
 
 
 
95cf15d
f1b0176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95cf15d
 
 
 
 
 
 
 
 
 
 
 
 
 
f1b0176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import unittest
from unittest.mock import patch

from training_coach.models import CheckIn, ParsedCheckIn
from training_coach.parser_service import parser_backend, parse_check_in_with_configured_backend


class ParserServiceTest(unittest.TestCase):
    def test_parser_backend_defaults_to_ollama_locally(self):
        with patch.dict("os.environ", {}, clear=True):
            self.assertEqual(parser_backend(), "ollama")

    def test_parser_backend_defaults_to_transformers_on_space(self):
        with patch.dict("os.environ", {"SPACE_ID": "LucasMah/app"}, clear=True):
            self.assertEqual(parser_backend(), "llama_cpp")

    def test_parser_backend_env_override_wins(self):
        with patch.dict(
            "os.environ",
            {"SPACE_ID": "LucasMah/app", "PARSER_BACKEND": "ollama"},
            clear=True,
        ):
            self.assertEqual(parser_backend(), "ollama")

    def test_configured_parser_uses_ollama_backend(self):
        parsed = ParsedCheckIn(check_in=CheckIn(raw_text="60 min"))

        with patch.dict("os.environ", {"PARSER_BACKEND": "ollama"}, clear=True):
            with patch(
                "training_coach.parser_service.parse_check_in_with_ollama",
                return_value=parsed,
            ) as parse_ollama:
                result = parse_check_in_with_configured_backend("60 min")

        self.assertEqual(result, parsed)
        parse_ollama.assert_called_once_with("60 min", model_name="qwen3:1.7B")

    def test_configured_parser_uses_llama_cpp_backend(self):
        parsed = ParsedCheckIn(check_in=CheckIn(raw_text="60 min"))

        with patch.dict("os.environ", {"PARSER_BACKEND": "llama_cpp"}, clear=True):
            with patch(
                "training_coach.parser_service.parse_check_in_with_llama_cpp",
                return_value=parsed,
            ) as parse_llama_cpp:
                result = parse_check_in_with_configured_backend("60 min")

        self.assertEqual(result, parsed)
        parse_llama_cpp.assert_called_once_with("60 min")

    def test_configured_parser_uses_transformers_backend(self):
        parsed = ParsedCheckIn(check_in=CheckIn(raw_text="60 min"))

        with patch.dict("os.environ", {"PARSER_BACKEND": "transformers"}, clear=True):
            with patch(
                "training_coach.parser_service.parse_check_in_with_model",
                return_value=parsed,
            ) as parse_transformers:
                result = parse_check_in_with_configured_backend("60 min")

        self.assertEqual(result, parsed)
        parse_transformers.assert_called_once_with("60 min")


if __name__ == "__main__":
    unittest.main()