-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_client.py
More file actions
185 lines (146 loc) · 6.48 KB
/
test_client.py
File metadata and controls
185 lines (146 loc) · 6.48 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
"""Tests for the Redis Cloud Python client."""
import os
import pytest
from redis_cloud import CloudClient, RedisCloudError
class TestClientCreation:
"""Tests for client creation."""
def test_client_creation_with_credentials(self):
"""Test creating a client with explicit credentials."""
client = CloudClient(api_key="test-key", api_secret="test-secret")
assert client is not None
def test_client_creation_with_base_url(self):
"""Test creating a client with a custom base URL."""
client = CloudClient(
api_key="test-key",
api_secret="test-secret",
base_url="https://custom.api.example.com",
)
assert client is not None
def test_client_creation_with_timeout(self):
"""Test creating a client with a custom timeout."""
client = CloudClient(
api_key="test-key", api_secret="test-secret", timeout_secs=60
)
assert client is not None
assert client.timeout == 60.0
def test_client_timeout_default(self):
"""Test that timeout has a default value when not specified."""
client = CloudClient(api_key="test-key", api_secret="test-secret")
# Default timeout is set by the underlying Rust client
assert client.timeout > 0
def test_from_env_missing_api_key(self):
"""Test that from_env raises error when API key is missing."""
# Clear any existing env vars
for var in [
"REDIS_CLOUD_API_KEY",
"REDIS_CLOUD_ACCOUNT_KEY",
"REDIS_CLOUD_API_SECRET",
"REDIS_CLOUD_SECRET_KEY",
"REDIS_CLOUD_USER_KEY",
]:
os.environ.pop(var, None)
with pytest.raises(ValueError, match="API key not found"):
CloudClient.from_env()
def test_from_env_missing_api_secret(self):
"""Test that from_env raises error when API secret is missing."""
os.environ["REDIS_CLOUD_API_KEY"] = "test-key"
# Clear secret vars
for var in [
"REDIS_CLOUD_API_SECRET",
"REDIS_CLOUD_SECRET_KEY",
"REDIS_CLOUD_USER_KEY",
]:
os.environ.pop(var, None)
try:
with pytest.raises(ValueError, match="API secret not found") as exc_info:
CloudClient.from_env()
message = str(exc_info.value)
assert "REDIS_CLOUD_API_SECRET" in message
assert "REDIS_CLOUD_SECRET_KEY" in message
assert "REDIS_CLOUD_USER_KEY" in message
finally:
os.environ.pop("REDIS_CLOUD_API_KEY", None)
def test_from_env_with_valid_credentials(self):
"""Test that from_env works with valid environment variables."""
os.environ["REDIS_CLOUD_API_KEY"] = "test-key"
os.environ["REDIS_CLOUD_API_SECRET"] = "test-secret"
try:
client = CloudClient.from_env()
assert client is not None
finally:
os.environ.pop("REDIS_CLOUD_API_KEY", None)
os.environ.pop("REDIS_CLOUD_API_SECRET", None)
def test_from_env_with_alternate_key_names(self):
"""Test that from_env works with alternate environment variable names."""
os.environ["REDIS_CLOUD_ACCOUNT_KEY"] = "test-key"
os.environ["REDIS_CLOUD_SECRET_KEY"] = "test-secret"
try:
client = CloudClient.from_env()
assert client is not None
finally:
os.environ.pop("REDIS_CLOUD_ACCOUNT_KEY", None)
os.environ.pop("REDIS_CLOUD_SECRET_KEY", None)
class TestClientMethods:
"""Tests for client methods (without actual API calls)."""
@pytest.fixture
def client(self):
"""Create a client for testing."""
return CloudClient(api_key="test-key", api_secret="test-secret")
def test_client_has_subscriptions_method(self, client):
"""Test that client has subscriptions method."""
assert hasattr(client, "subscriptions")
assert hasattr(client, "subscriptions_sync")
def test_client_has_subscription_method(self, client):
"""Test that client has subscription method."""
assert hasattr(client, "subscription")
assert hasattr(client, "subscription_sync")
def test_client_has_databases_method(self, client):
"""Test that client has databases method."""
assert hasattr(client, "databases")
assert hasattr(client, "databases_sync")
def test_client_has_database_method(self, client):
"""Test that client has database method."""
assert hasattr(client, "database")
assert hasattr(client, "database_sync")
def test_client_has_all_databases_method(self, client):
"""Test that client has all_databases pagination helper."""
assert hasattr(client, "all_databases")
assert hasattr(client, "all_databases_sync")
def test_client_has_account_method(self, client):
"""Test that client has account method."""
assert hasattr(client, "account")
assert hasattr(client, "account_sync")
def test_client_has_raw_methods(self, client):
"""Test that client has raw HTTP methods."""
assert hasattr(client, "get")
assert hasattr(client, "get_sync")
assert hasattr(client, "post")
assert hasattr(client, "post_sync")
assert hasattr(client, "delete")
assert hasattr(client, "delete_sync")
def test_client_has_timeout_property(self, client):
"""Test that client has timeout property."""
assert hasattr(client, "timeout")
class TestErrorHandling:
"""Tests for error handling."""
def test_redis_cloud_error_exists(self):
"""Test that RedisCloudError is exported."""
assert RedisCloudError is not None
def test_redis_cloud_error_is_exception(self):
"""Test that RedisCloudError is an Exception subclass."""
assert issubclass(RedisCloudError, Exception)
class TestModuleExports:
"""Tests for module exports."""
def test_cloud_client_exported(self):
"""Test that CloudClient is exported."""
from redis_cloud import CloudClient
assert CloudClient is not None
def test_redis_cloud_error_exported(self):
"""Test that RedisCloudError is exported."""
from redis_cloud import RedisCloudError
assert RedisCloudError is not None
def test_version_exported(self):
"""Test that __version__ is exported."""
import redis_cloud
assert hasattr(redis_cloud, "__version__")
assert isinstance(redis_cloud.__version__, str)