-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtest_core.py
More file actions
144 lines (115 loc) · 4.75 KB
/
test_core.py
File metadata and controls
144 lines (115 loc) · 4.75 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""Tests for core components."""
import pytest
from unittest.mock import patch
import httpx
from fishaudio.core import (
OMIT,
ClientWrapper,
AsyncClientWrapper,
RequestOptions,
WebSocketOptions,
)
class TestOMIT:
"""Test OMIT sentinel."""
def test_omit_is_falsy(self):
assert not OMIT
def test_omit_repr(self):
assert repr(OMIT) == "OMIT"
def test_omit_identity(self):
# OMIT should be a singleton-like value
value = OMIT
assert value is OMIT
class TestRequestOptions:
"""Test RequestOptions class."""
def test_defaults(self):
options = RequestOptions()
assert options.timeout is None
assert options.max_retries is None
assert options.additional_headers == {}
assert options.additional_query_params == {}
def test_with_values(self):
options = RequestOptions(
timeout=30.0,
max_retries=3,
additional_headers={"X-Custom": "value"},
additional_query_params={"param": "value"},
)
assert options.timeout == 30.0
assert options.max_retries == 3
assert options.additional_headers == {"X-Custom": "value"}
assert options.additional_query_params == {"param": "value"}
def test_get_timeout(self):
options = RequestOptions(timeout=30.0)
timeout = options.get_timeout()
assert isinstance(timeout, httpx.Timeout)
assert timeout.connect == 30.0
class TestWebSocketOptions:
"""Test WebSocketOptions class."""
def test_to_httpx_ws_kwargs_all_options(self):
"""Test to_httpx_ws_kwargs with all options set."""
options = WebSocketOptions(
keepalive_ping_timeout_seconds=60.0,
keepalive_ping_interval_seconds=30.0,
max_message_size_bytes=131072,
queue_size=1024,
)
kwargs = options.to_httpx_ws_kwargs()
assert kwargs == {
"keepalive_ping_timeout_seconds": 60.0,
"keepalive_ping_interval_seconds": 30.0,
"max_message_size_bytes": 131072,
"queue_size": 1024,
}
def test_to_httpx_ws_kwargs_partial_options(self):
"""Test to_httpx_ws_kwargs with only some options set."""
options = WebSocketOptions(keepalive_ping_timeout_seconds=60.0)
kwargs = options.to_httpx_ws_kwargs()
assert kwargs == {"keepalive_ping_timeout_seconds": 60.0}
assert "keepalive_ping_interval_seconds" not in kwargs
def test_to_httpx_ws_kwargs_no_options(self):
"""Test to_httpx_ws_kwargs with no options set."""
options = WebSocketOptions()
assert options.to_httpx_ws_kwargs() == {}
class TestClientWrapper:
"""Test sync ClientWrapper."""
def test_init_with_api_key(self, mock_api_key, mock_base_url):
wrapper = ClientWrapper(
api_key=mock_api_key, base_url=mock_base_url, timeout=60.0
)
assert wrapper.api_key == mock_api_key
assert wrapper.base_url == mock_base_url
def test_init_without_api_key_raises(self):
with patch.dict("os.environ", {}, clear=True):
with pytest.raises(ValueError, match="API key must be provided"):
ClientWrapper()
def test_init_with_env_var(self, mock_api_key):
with patch.dict("os.environ", {"FISH_API_KEY": mock_api_key}):
wrapper = ClientWrapper()
assert wrapper.api_key == mock_api_key
def test_get_headers(self, mock_api_key):
wrapper = ClientWrapper(api_key=mock_api_key)
headers = wrapper.get_headers()
assert headers["Authorization"] == f"Bearer {mock_api_key}"
assert "User-Agent" in headers
def test_get_headers_with_additional(self, mock_api_key):
wrapper = ClientWrapper(api_key=mock_api_key)
headers = wrapper.get_headers({"X-Custom": "value"})
assert headers["X-Custom"] == "value"
assert headers["Authorization"] == f"Bearer {mock_api_key}"
class TestAsyncClientWrapper:
"""Test async AsyncClientWrapper."""
def test_init_with_api_key(self, mock_api_key, mock_base_url):
wrapper = AsyncClientWrapper(
api_key=mock_api_key, base_url=mock_base_url, timeout=60.0
)
assert wrapper.api_key == mock_api_key
assert wrapper.base_url == mock_base_url
def test_init_without_api_key_raises(self):
with patch.dict("os.environ", {}, clear=True):
with pytest.raises(ValueError, match="API key must be provided"):
AsyncClientWrapper()
def test_get_headers(self, mock_api_key):
wrapper = AsyncClientWrapper(api_key=mock_api_key)
headers = wrapper.get_headers()
assert headers["Authorization"] == f"Bearer {mock_api_key}"
assert "User-Agent" in headers