-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_current_scope.py
More file actions
220 lines (188 loc) · 11.1 KB
/
Copy pathtest_current_scope.py
File metadata and controls
220 lines (188 loc) · 11.1 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Tests for current_scope token functionality."""
import os
import sys
from unittest import mock
import pytest
from replicate import Replicate, AsyncReplicate
from replicate.lib.cog import _get_api_token_from_environment
from replicate._exceptions import ReplicateError
class TestGetApiTokenFromEnvironment:
"""Test the _get_api_token_from_environment function."""
def test_cog_no_current_scope_method_falls_back_to_env(self):
"""Test fallback when cog exists but has no current_scope method."""
mock_cog = mock.MagicMock()
del mock_cog.current_scope # Remove the method
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
token = _get_api_token_from_environment()
assert token == "env-token"
def test_cog_current_scope_returns_none_falls_back_to_env(self):
"""Test fallback when current_scope() returns None."""
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = None
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
token = _get_api_token_from_environment()
assert token == "env-token"
def test_cog_scope_no_context_attr_falls_back_to_env(self):
"""Test fallback when scope has no context attribute."""
mock_scope = mock.MagicMock()
del mock_scope.context # Remove the context attribute
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
token = _get_api_token_from_environment()
assert token == "env-token"
def test_cog_scope_context_not_dict_falls_back_to_env(self):
"""Test fallback when scope.context is not a dictionary."""
mock_scope = mock.MagicMock()
mock_scope.context = "not a dict"
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
token = _get_api_token_from_environment()
assert token == "env-token"
def test_cog_scope_no_replicate_api_token_key_falls_back_to_env(self):
"""Test fallback when replicate_api_token key is missing from context."""
mock_scope = mock.MagicMock()
mock_scope.context = {"other_key": "other_value"} # Missing replicate_api_token
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
token = _get_api_token_from_environment()
assert token == "env-token"
def test_cog_scope_replicate_api_token_valid_string(self):
"""Test successful retrieval of non-empty token from cog."""
mock_scope = mock.MagicMock()
mock_scope.context = {"REPLICATE_API_TOKEN": "cog-token"}
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
token = _get_api_token_from_environment()
assert token == "cog-token"
def test_cog_scope_replicate_api_token_case_insensitive(self):
"""Test successful retrieval of non-empty token from cog ignoring case."""
mock_scope = mock.MagicMock()
mock_scope.context = {"replicate_api_token": "cog-token"}
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
token = _get_api_token_from_environment()
assert token == "cog-token"
def test_cog_scope_replicate_api_token_empty_string(self):
"""Test that empty string from cog is returned (not falling back to env)."""
mock_scope = mock.MagicMock()
mock_scope.context = {"replicate_api_token": ""} # Empty string
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
token = _get_api_token_from_environment()
assert token == "" # Should return empty string, not env token
def test_cog_scope_replicate_api_token_none(self):
"""Test that None from cog is returned (not falling back to env)."""
mock_scope = mock.MagicMock()
mock_scope.context = {"replicate_api_token": None}
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
token = _get_api_token_from_environment()
assert token is None # Should return None, not env token
def test_cog_current_scope_raises_exception_falls_back_to_env(self):
"""Test fallback when current_scope() raises an exception."""
mock_cog = mock.MagicMock()
mock_cog.current_scope.side_effect = RuntimeError("Scope error")
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
token = _get_api_token_from_environment()
assert token == "env-token"
def test_no_env_token_returns_none(self):
"""Test that None is returned when no environment token is set and cog unavailable."""
with mock.patch.dict(os.environ, {}, clear=True): # Clear all env vars
with mock.patch.dict(sys.modules, {"cog": None}):
token = _get_api_token_from_environment()
assert token is None
def test_env_token_empty_string(self):
"""Test that empty string from environment is returned."""
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": ""}):
with mock.patch.dict(sys.modules, {"cog": None}):
token = _get_api_token_from_environment()
assert token == ""
def test_env_token_valid_string(self):
"""Test that valid token from environment is returned."""
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": None}):
token = _get_api_token_from_environment()
assert token == "env-token"
class TestClientCurrentScopeIntegration:
"""Test that the client uses current_scope functionality."""
def test_sync_client_uses_current_scope_token(self):
"""Test that sync client retrieves token from current_scope."""
mock_scope = mock.MagicMock()
mock_scope.context = {"REPLICATE_API_TOKEN": "cog-token"}
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
# Clear environment variable to ensure we're using cog
with mock.patch.dict(os.environ, {}, clear=True):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
client = Replicate(base_url="http://test.example.com")
assert client.bearer_token == "cog-token"
def test_async_client_uses_current_scope_token(self):
"""Test that async client retrieves token from current_scope."""
mock_scope = mock.MagicMock()
mock_scope.context = {"REPLICATE_API_TOKEN": "cog-token"}
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
# Clear environment variable to ensure we're using cog
with mock.patch.dict(os.environ, {}, clear=True):
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
client = AsyncReplicate(base_url="http://test.example.com")
assert client.bearer_token == "cog-token"
def test_sync_client_falls_back_to_env_when_cog_unavailable(self):
"""Test that sync client falls back to env when cog is unavailable."""
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": None}):
client = Replicate(base_url="http://test.example.com")
assert client.bearer_token == "env-token"
def test_async_client_falls_back_to_env_when_cog_unavailable(self):
"""Test that async client falls back to env when cog is unavailable."""
with mock.patch.dict(os.environ, {"REPLICATE_API_TOKEN": "env-token"}):
with mock.patch.dict(sys.modules, {"cog": None}):
client = AsyncReplicate(base_url="http://test.example.com")
assert client.bearer_token == "env-token"
def test_sync_client_raises_error_when_no_token_available(self):
"""Test that sync client raises error when no token is available."""
with mock.patch.dict(os.environ, {}, clear=True):
with mock.patch.dict(sys.modules, {"cog": None}):
with pytest.raises(ReplicateError, match="bearer_token client option must be set"):
Replicate(base_url="http://test.example.com")
def test_async_client_raises_error_when_no_token_available(self):
"""Test that async client raises error when no token is available."""
with mock.patch.dict(os.environ, {}, clear=True):
with mock.patch.dict(sys.modules, {"cog": None}):
with pytest.raises(ReplicateError, match="bearer_token client option must be set"):
AsyncReplicate(base_url="http://test.example.com")
def test_explicit_token_overrides_current_scope(self):
"""Test that explicitly provided token overrides current_scope."""
mock_scope = mock.MagicMock()
mock_scope.context = {"REPLICATE_API_TOKEN": "cog-token"}
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
client = Replicate(bearer_token="explicit-token", base_url="http://test.example.com")
assert client.bearer_token == "explicit-token"
def test_explicit_async_token_overrides_current_scope(self):
"""Test that explicitly provided token overrides current_scope for async client."""
mock_scope = mock.MagicMock()
mock_scope.context = {"REPLICATE_API_TOKEN": "cog-token"}
mock_cog = mock.MagicMock()
mock_cog.current_scope.return_value = mock_scope
with mock.patch.dict(sys.modules, {"cog": mock_cog}):
client = AsyncReplicate(bearer_token="explicit-token", base_url="http://test.example.com")
assert client.bearer_token == "explicit-token"