-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_compat.py
More file actions
296 lines (199 loc) · 8.41 KB
/
test_compat.py
File metadata and controls
296 lines (199 loc) · 8.41 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
"""Tests for version compatibility checking."""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from packaging.version import Version
from opendecree._compat import (
_parse_version,
_satisfies,
async_fetch_server_version,
check_version_compatible,
fetch_server_version,
)
from opendecree.errors import IncompatibleServerError
from opendecree.types import ServerVersion
# --- _parse_version ---
def test_parse_semver():
assert _parse_version("0.3.1") == Version("0.3.1")
def test_parse_with_v_prefix():
assert _parse_version("v1.2.3") == Version("1.2.3")
def test_parse_major_only():
assert _parse_version("2") == Version("2")
def test_parse_prerelease():
assert _parse_version("0.3.0-rc1") == Version("0.3.0rc1")
def test_parse_local_version():
v = _parse_version("v0.3.0+sha123")
assert v is not None
assert v == Version("0.3.0+sha123")
def test_parse_dev():
assert _parse_version("dev") is None
def test_parse_empty():
assert _parse_version("") is None
# --- _satisfies ---
def test_satisfies_gte():
assert _satisfies(Version("0.3.1"), ">=0.3.0") is True
assert _satisfies(Version("0.3.0"), ">=0.3.0") is True
assert _satisfies(Version("0.2.9"), ">=0.3.0") is False
def test_satisfies_lt():
assert _satisfies(Version("0.9.0"), "<1.0.0") is True
assert _satisfies(Version("1.0.0"), "<1.0.0") is False
def test_satisfies_eq():
assert _satisfies(Version("1.2.3"), "==1.2.3") is True
assert _satisfies(Version("1.2.4"), "==1.2.3") is False
def test_satisfies_neq():
assert _satisfies(Version("1.2.4"), "!=1.2.3") is True
assert _satisfies(Version("1.2.3"), "!=1.2.3") is False
def test_satisfies_short_version():
assert _satisfies(Version("1"), ">=1.0.0") is True
assert _satisfies(Version("1"), "<2.0.0") is True
def test_satisfies_prerelease_lt_release():
assert _satisfies(Version("0.3.0rc1"), ">=0.3.0") is False
def test_satisfies_invalid_constraint():
assert _satisfies(Version("1.0.0"), "garbage") is True
# --- check_version_compatible ---
def test_compatible_version():
check_version_compatible("0.3.1", ">=0.3.0,<1.0.0") # should not raise
def test_incompatible_too_old():
with pytest.raises(IncompatibleServerError, match="not compatible"):
check_version_compatible("0.2.0", ">=0.3.0,<1.0.0")
def test_incompatible_too_new():
with pytest.raises(IncompatibleServerError, match="not compatible"):
check_version_compatible("1.0.0", ">=0.3.0,<1.0.0")
def test_dev_version_skips_check():
check_version_compatible("dev", ">=0.3.0,<1.0.0") # should not raise
def test_uses_default_range():
# Default is SUPPORTED_SERVER_VERSION = ">=0.3.0,<1.0.0"
check_version_compatible("0.5.0") # should not raise
# --- fetch_server_version ---
def test_fetch_server_version():
stub = MagicMock()
pb2 = MagicMock()
resp = MagicMock()
resp.version = "0.3.1"
resp.commit = "abc123"
stub.GetServerInfo.return_value = resp
sv = fetch_server_version(stub, pb2, timeout=5.0)
assert sv == ServerVersion(version="0.3.1", commit="abc123")
stub.GetServerInfo.assert_called_once()
@pytest.mark.asyncio
async def test_async_fetch_server_version():
stub = MagicMock()
pb2 = MagicMock()
resp = MagicMock()
resp.version = "0.3.1"
resp.commit = "abc123"
stub.GetServerInfo = AsyncMock(return_value=resp)
sv = await async_fetch_server_version(stub, pb2, timeout=5.0)
assert sv == ServerVersion(version="0.3.1", commit="abc123")
# --- ConfigClient.get_server_version + check_compatibility ---
def test_client_get_server_version_cached():
"""get_server_version() fetches once, returns cached."""
with patch("opendecree.client.create_channel"):
from opendecree import ConfigClient
client = ConfigClient.__new__(ConfigClient)
client._timeout = 5.0
mock_stub = MagicMock()
resp = MagicMock()
resp.version = "0.3.1"
resp.commit = "abc123"
mock_stub.GetServerInfo.return_value = resp
client._version_stub = mock_stub
client._version_pb2 = MagicMock()
client._server_version = None
# First call fetches
v1 = client.get_server_version()
assert v1.version == "0.3.1"
assert mock_stub.GetServerInfo.call_count == 1
# Second call returns cached
v2 = client.get_server_version()
assert v2 is v1
assert mock_stub.GetServerInfo.call_count == 1
def test_client_server_version_property_deprecated():
"""server_version property emits DeprecationWarning."""
with patch("opendecree.client.create_channel"):
from opendecree import ConfigClient
client = ConfigClient.__new__(ConfigClient)
client._timeout = 5.0
client._server_version = ServerVersion(version="0.3.1", commit="abc")
client._version_stub = MagicMock()
client._version_pb2 = MagicMock()
with pytest.warns(DeprecationWarning, match="get_server_version"):
sv = client.server_version
assert sv.version == "0.3.1"
def test_client_check_compatibility_passes():
with patch("opendecree.client.create_channel"):
from opendecree import ConfigClient
client = ConfigClient.__new__(ConfigClient)
client._timeout = 5.0
client._server_version = ServerVersion(version="0.3.1", commit="abc")
client._version_stub = MagicMock()
client._version_pb2 = MagicMock()
client.check_compatibility() # should not raise
def test_client_check_compatibility_fails():
with patch("opendecree.client.create_channel"):
from opendecree import ConfigClient
client = ConfigClient.__new__(ConfigClient)
client._timeout = 5.0
client._server_version = ServerVersion(version="0.1.0", commit="abc")
client._version_stub = MagicMock()
client._version_pb2 = MagicMock()
with pytest.raises(IncompatibleServerError):
client.check_compatibility()
# --- check_version ctor flag ---
def _make_client_with_version(server_version: str, check_version: bool = True):
"""Return a ConfigClient.__new__ instance wired with a mock server version."""
from opendecree import ConfigClient
client = ConfigClient.__new__(ConfigClient)
client._timeout = 5.0
client._check_version = check_version
client._version_checked = False
client._server_version = ServerVersion(version=server_version, commit="abc")
client._version_stub = MagicMock()
client._version_pb2 = MagicMock()
return client
def test_ensure_version_checked_runs_once():
client = _make_client_with_version("0.3.1")
with patch.object(client, "check_compatibility") as mock_check:
client._ensure_version_checked()
client._ensure_version_checked()
mock_check.assert_called_once()
def test_ensure_version_checked_noop_when_disabled():
client = _make_client_with_version("0.3.1", check_version=False)
with patch.object(client, "check_compatibility") as mock_check:
client._ensure_version_checked()
mock_check.assert_not_called()
def test_ensure_version_checked_raises_on_incompatible():
client = _make_client_with_version("0.1.0")
with pytest.raises(IncompatibleServerError):
client._ensure_version_checked()
@pytest.mark.asyncio
async def test_async_ensure_version_checked_runs_once():
from opendecree import AsyncConfigClient
client = AsyncConfigClient.__new__(AsyncConfigClient)
client._timeout = 5.0
client._check_version = True
client._version_checked = False
client._server_version = ServerVersion(version="0.3.1", commit="abc")
client._version_stub = MagicMock()
client._version_pb2 = MagicMock()
call_count = 0
async def fake_check():
nonlocal call_count
call_count += 1
client.check_compatibility = fake_check
await client._ensure_version_checked()
await client._ensure_version_checked()
assert call_count == 1
@pytest.mark.asyncio
async def test_async_ensure_version_checked_noop_when_disabled():
from opendecree import AsyncConfigClient
client = AsyncConfigClient.__new__(AsyncConfigClient)
client._timeout = 5.0
client._check_version = False
client._version_checked = False
called = False
async def fake_check():
nonlocal called
called = True
client.check_compatibility = fake_check
await client._ensure_version_checked()
assert not called