-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_api_token_compatibility.py
More file actions
88 lines (69 loc) · 4.37 KB
/
test_api_token_compatibility.py
File metadata and controls
88 lines (69 loc) · 4.37 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
"""Tests for api_token legacy compatibility during client instantiation."""
from __future__ import annotations
import pytest
from replicate import Replicate, AsyncReplicate, ReplicateError
from replicate._client import Client
class TestApiTokenCompatibility:
"""Test that api_token parameter works as a legacy compatibility option."""
def test_sync_client_with_api_token(self) -> None:
"""Test that Replicate accepts api_token parameter."""
client = Replicate(api_token="test_token_123")
assert client.bearer_token == "test_token_123"
def test_async_client_with_api_token(self) -> None:
"""Test that AsyncReplicate accepts api_token parameter."""
client = AsyncReplicate(api_token="test_token_123")
assert client.bearer_token == "test_token_123"
def test_sync_client_with_bearer_token(self) -> None:
"""Test that Replicate still accepts bearer_token parameter."""
client = Replicate(bearer_token="test_token_123")
assert client.bearer_token == "test_token_123"
def test_async_client_with_bearer_token(self) -> None:
"""Test that AsyncReplicate still accepts bearer_token parameter."""
client = AsyncReplicate(bearer_token="test_token_123")
assert client.bearer_token == "test_token_123"
def test_sync_client_both_tokens_error(self) -> None:
"""Test that providing both api_token and bearer_token raises an error."""
with pytest.raises(ReplicateError, match="Cannot specify both 'bearer_token' and 'api_token'"):
Replicate(api_token="test_api", bearer_token="test_bearer")
def test_async_client_both_tokens_error(self) -> None:
"""Test that providing both api_token and bearer_token raises an error."""
with pytest.raises(ReplicateError, match="Cannot specify both 'bearer_token' and 'api_token'"):
AsyncReplicate(api_token="test_api", bearer_token="test_bearer")
def test_sync_client_no_token_with_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that client reads from environment when no token is provided."""
monkeypatch.setenv("REPLICATE_API_TOKEN", "env_token_123")
client = Replicate()
assert client.bearer_token == "env_token_123"
def test_async_client_no_token_with_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that async client reads from environment when no token is provided."""
monkeypatch.setenv("REPLICATE_API_TOKEN", "env_token_123")
client = AsyncReplicate()
assert client.bearer_token == "env_token_123"
def test_sync_client_no_token_no_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that client raises error when no token is provided and env is not set."""
monkeypatch.delenv("REPLICATE_API_TOKEN", raising=False)
with pytest.raises(ReplicateError, match="The bearer_token client option must be set"):
Replicate()
def test_async_client_no_token_no_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that async client raises error when no token is provided and env is not set."""
monkeypatch.delenv("REPLICATE_API_TOKEN", raising=False)
with pytest.raises(ReplicateError, match="The bearer_token client option must be set"):
AsyncReplicate()
def test_legacy_client_alias(self) -> None:
"""Test that legacy Client import still works as an alias."""
assert Client is Replicate
def test_legacy_client_with_api_token(self) -> None:
"""Test that legacy Client alias works with api_token parameter."""
client = Client(api_token="test_token_123")
assert client.bearer_token == "test_token_123"
assert isinstance(client, Replicate)
def test_api_token_overrides_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that explicit api_token overrides environment variable."""
monkeypatch.setenv("REPLICATE_API_TOKEN", "env_token")
client = Replicate(api_token="explicit_token")
assert client.bearer_token == "explicit_token"
def test_bearer_token_overrides_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that explicit bearer_token overrides environment variable."""
monkeypatch.setenv("REPLICATE_API_TOKEN", "env_token")
client = Replicate(bearer_token="explicit_token")
assert client.bearer_token == "explicit_token"