-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathtest_include.py
More file actions
358 lines (257 loc) · 9.78 KB
/
Copy pathtest_include.py
File metadata and controls
358 lines (257 loc) · 9.78 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import os
import threading
import unittest.mock as mock
import pytest
from replicate.exceptions import ModelError
from replicate.include import (
Function,
Run,
get_run_state,
get_run_token,
include,
run_state,
run_token,
)
@pytest.fixture(autouse=True)
def no_api_token_in_env():
"""
Remove REPLICATE_API_TOKEN from environment during tests and restore it after.
"""
original_token = os.environ.get("REPLICATE_API_TOKEN")
if "REPLICATE_API_TOKEN" in os.environ:
del os.environ["REPLICATE_API_TOKEN"]
yield
if original_token is not None:
os.environ["REPLICATE_API_TOKEN"] = original_token
@pytest.fixture
def client():
with mock.patch("replicate.Client") as client_class:
client_instance = mock.MagicMock()
client_class.return_value = client_instance
yield client_class, client_instance
@pytest.fixture
def model():
model_obj = mock.MagicMock()
yield model_obj
@pytest.fixture
def version():
version_obj = mock.MagicMock()
version_obj.openapi_schema = {
"components": {"schemas": {"Output": {"type": "string"}}}
}
version_obj.cog_version = "0.4.0"
yield version_obj
@pytest.fixture
def prediction():
pred = mock.MagicMock()
pred.status = "succeeded"
pred.output = "test output"
pred.id = "pred123"
yield pred
@pytest.fixture
def iterator_version():
iter_version = mock.MagicMock()
iter_version.openapi_schema = {
"components": {
"schemas": {"Output": {"type": "array", "x-cog-array-type": "iterator"}}
}
}
iter_version.cog_version = "0.4.0"
yield iter_version
def test_run_state_context_manager():
with pytest.raises(RuntimeError):
include("owner/model:version")
with run_state("load"):
include("owner/model:version")
with run_state("load"):
include("owner/model:version")
with run_state("setup"):
with pytest.raises(RuntimeError):
include("owner/model:version")
def test_run_token_context_manager(client):
client_class, _ = client
fn = Function("owner/model:version")
with mock.patch.dict(os.environ, {}, clear=True):
with pytest.raises(ValueError, match="No run token found"):
fn._client()
with run_token("test-token"):
fn._client()
client_class.assert_called_with(api_token="test-token")
with run_token("another-token"):
fn._client()
client_class.assert_called_with(api_token="another-token")
def test_find_api_token_from_env(monkeypatch, client):
client_class, _ = client
monkeypatch.setenv("REPLICATE_API_TOKEN", "env-token")
with mock.patch("sys.stderr"):
fn = Function("owner/model:version")
fn._client()
client_class.assert_called_with(api_token="env-token")
def test_find_api_token_from_context(client):
client_class, _ = client
with run_token("context-token"):
fn = Function("owner/model:version")
fn._client()
client_class.assert_called_with(api_token="context-token")
def test_find_api_token_raises_error():
assert "REPLICATE_API_TOKEN" not in os.environ
fn = Function("owner/model:version")
with pytest.raises(ValueError, match="No run token found"):
fn._client()
def test_include_outside_load_state():
with pytest.raises(RuntimeError, match="You may only call .* at the top level"):
include("owner/model:version")
def test_include_in_load_state():
with run_state("load"):
fn = include("owner/model:version")
assert isinstance(fn, Function)
assert fn.function_ref == "owner/model:version"
def test_function_split_function_ref():
fn = Function("owner/model:version")
assert fn._split_function_ref() == ("owner", "model", "version")
fn = Function("owner/model")
assert fn._split_function_ref() == ("owner", "model", None)
def test_function_client(client):
client_class, client_instance = client
with run_token("test-token"):
fn = Function("owner/model:version")
client = fn._client()
client_class.assert_called_once_with(api_token="test-token")
assert client == client_instance
def test_function_model(client, model):
_, client_instance = client
client_instance.models.get.return_value = model
with run_token("test-token"):
fn = Function("owner/model:version")
result = fn._model()
client_instance.models.get.assert_called_once_with("owner/model")
assert result == model
def test_function_version_with_version_id(client, model, version):
_, client_instance = client
client_instance.models.get.return_value = model
model.versions.get.return_value = version
with run_token("test-token"):
fn = Function("owner/model:version")
result = fn._version()
client_instance.models.get.assert_called_once_with("owner/model")
model.versions.get.assert_called_once_with("version")
assert result == version
def test_function_version_with_latest(client, model, version):
_, client_instance = client
client_instance.models.get.return_value = model
model.latest_version = version
with run_token("test-token"):
fn = Function("owner/model")
result = fn._version()
client_instance.models.get.assert_called_once_with("owner/model")
assert result == version
@mock.patch.object(Function, "start")
@mock.patch.object(Function, "_version")
def test_function_call(version_patch, start_patch):
run_obj = mock.MagicMock()
start_patch.return_value = run_obj
with run_token("test-token"):
fn = Function("owner/model:version")
fn(prompt="Hello", temperature=0.7)
start_patch.assert_called_once_with(prompt="Hello", temperature=0.7)
run_obj.wait.assert_called_once()
def test_function_start(client, model, version, prediction, capsys):
_, client_instance = client
client_instance.models.get.return_value = model
model.versions.get.return_value = version
client_instance.predictions.create.return_value = prediction
with run_token("test-token"):
fn = Function("owner/model:version")
run = fn.start(prompt="Hello", temperature=0.7)
client_instance.predictions.create.assert_called_once_with(
version=version, input={"prompt": "Hello", "temperature": 0.7}
)
assert isinstance(run, Run)
assert run.prediction == prediction
assert run.version == version
captured = capsys.readouterr()
assert "https://replicate.com/p/pred123" in captured.out
def test_function_default_example(client, model):
_, client_instance = client
example_obj = mock.MagicMock()
client_instance.models.get.return_value = model
model.default_example = example_obj
with run_token("test-token"):
fn = Function("owner/model:version")
example = fn.default_example
assert example == example_obj
def test_function_openapi_schema(client, model, version):
_, client_instance = client
client_instance.models.get.return_value = model
model.versions.get.return_value = version
with run_token("test-token"):
fn = Function("owner/model:version")
schema = fn.openapi_schema
assert schema == version.openapi_schema
def test_run_wait_success(prediction, version):
with mock.patch(
"replicate.include._has_output_iterator_array_type", return_value=False
):
run = Run(prediction=prediction, version=version)
result = run.wait()
prediction.wait.assert_called_once()
assert result == "test output"
def test_run_wait_failure(version):
failed_prediction = mock.MagicMock()
failed_prediction.status = "failed"
run = Run(prediction=failed_prediction, version=version)
with pytest.raises(ModelError):
run.wait()
failed_prediction.wait.assert_called_once()
def test_run_wait_iterator_output(iterator_version):
iter_prediction = mock.MagicMock()
iter_prediction.status = "succeeded"
iter_prediction.output = ["Hello", " ", "world"]
with mock.patch(
"replicate.include._has_output_iterator_array_type", return_value=True
):
run = Run(prediction=iter_prediction, version=iterator_version)
result = run.wait()
iter_prediction.wait.assert_called_once()
assert result == "Hello world"
def test_run_logs(prediction, version):
prediction.logs = "log content"
run = Run(prediction=prediction, version=version)
logs = run.logs()
prediction.reload.assert_called_once()
assert logs == "log content"
def test_thread_safety_concepts():
with run_state("load"), run_token("test-token"):
assert get_run_state() == "load"
assert get_run_token() == "test-token"
results = []
def worker_thread_fn():
thread_sees_state = get_run_state() == "load"
thread_sees_token = get_run_token() == "test-token"
can_modify = True
try:
with run_state("setup"):
pass
can_modify = True
except RuntimeError:
can_modify = False
results.append(
{
"reads_state": thread_sees_state,
"reads_token": thread_sees_token,
"can_modify": can_modify,
}
)
threads = []
for _ in range(3):
t = threading.Thread(target=worker_thread_fn)
threads.append(t)
t.start()
for t in threads:
t.join()
for result in results:
assert result["reads_state"]
assert result["reads_token"]
assert not result["can_modify"]
assert get_run_state() is None
assert get_run_token() is None