Skip to content

Commit cb4f11c

Browse files
committed
More unit tests
1 parent 040084a commit cb4f11c

File tree

5 files changed

+2034
-80
lines changed

5 files changed

+2034
-80
lines changed

tests/services/test_api_clients.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""Tests for API client initialization."""
2+
3+
from unittest.mock import Mock, patch
4+
from services.api_clients import get_openai_client, get_httpx_client
5+
6+
7+
class TestGetOpenAIClient:
8+
"""Tests for get_openai_client function."""
9+
10+
@patch("services.api_clients.OpenAI")
11+
@patch("services.api_clients.get_config")
12+
def test_returns_cached_client_on_second_call(self, mock_config, mock_openai_class):
13+
"""Test that client is cached and reused."""
14+
# Setup
15+
config = Mock()
16+
config.openai_api_key = "test-key"
17+
mock_config.return_value = config
18+
19+
mock_client = Mock()
20+
mock_openai_class.return_value = mock_client
21+
22+
# First call creates client
23+
client1 = get_openai_client()
24+
25+
# Second call returns cached client
26+
client2 = get_openai_client()
27+
28+
# Verify
29+
assert client1 is client2
30+
# Check that OpenAI was called with at least the api_key
31+
call_kwargs = mock_openai_class.call_args[1]
32+
assert call_kwargs["api_key"] == "test-key"
33+
assert mock_openai_class.call_count == 1
34+
35+
@patch("services.api_clients.OpenAI")
36+
@patch("services.api_clients.get_config")
37+
def test_creates_client_with_api_key(self, mock_config, mock_openai_class):
38+
"""Test that client is created with correct API key."""
39+
# Setup
40+
config = Mock()
41+
config.openai_api_key = "my-secret-key"
42+
mock_config.return_value = config
43+
44+
mock_client = Mock()
45+
mock_openai_class.return_value = mock_client
46+
47+
# Execute
48+
# Reset the cache first
49+
import services.api_clients
50+
51+
services.api_clients._openai_client = None
52+
53+
client = get_openai_client()
54+
55+
# Verify
56+
call_kwargs = mock_openai_class.call_args[1]
57+
assert call_kwargs["api_key"] == "my-secret-key"
58+
assert mock_openai_class.call_count == 1
59+
assert client is mock_client
60+
61+
62+
class TestGetHttpxClient:
63+
"""Tests for get_httpx_client function."""
64+
65+
@patch("services.api_clients.httpx.Client")
66+
def test_returns_cached_client_on_second_call(self, mock_httpx_class):
67+
"""Test that httpx client is cached and reused."""
68+
# Reset the cache first
69+
import services.api_clients
70+
71+
services.api_clients._httpx_client = None
72+
73+
# Setup
74+
mock_client = Mock()
75+
mock_httpx_class.return_value = mock_client
76+
77+
# First call creates client
78+
client1 = get_httpx_client()
79+
80+
# Second call returns cached client
81+
client2 = get_httpx_client()
82+
83+
# Verify
84+
assert client1 is client2
85+
# Check that httpx.Client was called with at least timeout
86+
call_kwargs = mock_httpx_class.call_args[1]
87+
assert call_kwargs["timeout"] == 30.0
88+
assert mock_httpx_class.call_count == 1
89+
90+
@patch("services.api_clients.httpx.Client")
91+
def test_creates_client_with_timeout(self, mock_httpx_class):
92+
"""Test that httpx client is created with correct timeout."""
93+
# Setup
94+
mock_client = Mock()
95+
mock_httpx_class.return_value = mock_client
96+
97+
# Reset the cache first
98+
import services.api_clients
99+
100+
services.api_clients._httpx_client = None
101+
102+
# Execute
103+
client = get_httpx_client()
104+
105+
# Verify
106+
call_kwargs = mock_httpx_class.call_args[1]
107+
assert call_kwargs["timeout"] == 30.0
108+
assert mock_httpx_class.call_count == 1
109+
assert client is mock_client

0 commit comments

Comments
 (0)