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