forked from opendecree/decree-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_compat.py
More file actions
213 lines (141 loc) · 5.85 KB
/
test_compat.py
File metadata and controls
213 lines (141 loc) · 5.85 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
"""Tests for version compatibility checking."""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
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") == (0, 3, 1)
def test_parse_with_v_prefix():
assert _parse_version("v1.2.3") == (1, 2, 3)
def test_parse_major_only():
assert _parse_version("2") == (2,)
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((0, 3, 1), ">=0.3.0") is True
assert _satisfies((0, 3, 0), ">=0.3.0") is True
assert _satisfies((0, 2, 9), ">=0.3.0") is False
def test_satisfies_lt():
assert _satisfies((0, 9, 0), "<1.0.0") is True
assert _satisfies((1, 0, 0), "<1.0.0") is False
def test_satisfies_eq():
assert _satisfies((1, 2, 3), "==1.2.3") is True
assert _satisfies((1, 2, 4), "==1.2.3") is False
def test_satisfies_neq():
assert _satisfies((1, 2, 4), "!=1.2.3") is True
assert _satisfies((1, 2, 3), "!=1.2.3") is False
def test_satisfies_padding():
"""Shorter version tuples are padded with zeros."""
assert _satisfies((1,), ">=1.0.0") is True
assert _satisfies((1,), "<2.0.0") is True
def test_satisfies_invalid_constraint():
assert _satisfies((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():
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
with pytest.warns(DeprecationWarning, match="get_server_version"):
v1 = client.server_version
assert v1.version == "0.3.1"
assert mock_stub.GetServerInfo.call_count == 1
with pytest.warns(DeprecationWarning, match="get_server_version"):
v2 = client.server_version
assert v2 is v1
assert mock_stub.GetServerInfo.call_count == 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()