Hugoifier

hugoifier / tests / test_config.py
Blame History Raw 73 lines
1
"""Tests for config.py."""
2
import os
3
import unittest
4
from unittest.mock import patch
5
6
7
class TestCallAiRouting(unittest.TestCase):
8
def _get_config(self):
9
# Reload config to pick up env var changes
10
import importlib
11
12
import hugoifier.config as config
13
importlib.reload(config)
14
return config
15
16
def test_raises_on_unknown_backend(self):
17
with patch.dict(os.environ, {"HUGOIFIER_BACKEND": "unknown"}):
18
cfg = self._get_config()
19
with self.assertRaises(ValueError):
20
cfg.call_ai("test prompt")
21
22
def test_anthropic_raises_without_key(self):
23
env = {"HUGOIFIER_BACKEND": "anthropic", "ANTHROPIC_API_KEY": ""}
24
with patch.dict(os.environ, env, clear=False):
25
cfg = self._get_config()
26
cfg.ANTHROPIC_API_KEY = ""
27
with self.assertRaises(EnvironmentError):
28
cfg._call_anthropic("prompt", "system")
29
30
def test_openai_raises_without_key(self):
31
env = {"HUGOIFIER_BACKEND": "openai", "OPENAI_API_KEY": ""}
32
with patch.dict(os.environ, env, clear=False):
33
cfg = self._get_config()
34
cfg.OPENAI_API_KEY = ""
35
with self.assertRaises(EnvironmentError):
36
cfg._call_openai("prompt", "system")
37
38
def test_google_raises_without_key(self):
39
env = {"HUGOIFIER_BACKEND": "google", "GOOGLE_API_KEY": ""}
40
with patch.dict(os.environ, env, clear=False):
41
cfg = self._get_config()
42
cfg.GOOGLE_API_KEY = ""
43
with self.assertRaises(EnvironmentError):
44
cfg._call_google("prompt", "system")
45
46
def test_anthropic_backend_calls_anthropic(self):
47
import hugoifier.config as config
48
with patch.object(config, '_call_anthropic', return_value="response") as mock_fn:
49
config.BACKEND = 'anthropic'
50
result = config.call_ai("hello")
51
mock_fn.assert_called_once()
52
self.assertEqual(result, "response")
53
54
def test_openai_backend_calls_openai(self):
55
import hugoifier.config as config
56
with patch.object(config, '_call_openai', return_value="response") as mock_fn:
57
config.BACKEND = 'openai'
58
result = config.call_ai("hello")
59
mock_fn.assert_called_once()
60
self.assertEqual(result, "response")
61
62
def test_google_backend_calls_google(self):
63
import hugoifier.config as config
64
with patch.object(config, '_call_google', return_value="response") as mock_fn:
65
config.BACKEND = 'google'
66
result = config.call_ai("hello")
67
mock_fn.assert_called_once()
68
self.assertEqual(result, "response")
69
70
71
if __name__ == "__main__":
72
unittest.main()
73

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button