Skip to content

Commit c8a6c01

Browse files
committed
add test for the new shard logic
(cherry picked from commit 1ea3331)
1 parent 08e04c5 commit c8a6c01

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

tests/test_client.py

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,56 @@ def test_delete_pattern_calls_pipeline_delete_and_execute(
128128

129129

130130
class TestShardClient:
131-
@patch("tests.test_client.DefaultClient.make_pattern")
131+
CLIENT_METHODS_FOR_MOCK = {
132+
"add",
133+
"close",
134+
"expire",
135+
"get",
136+
"ttl",
137+
"touch",
138+
"sadd",
139+
"scan_iter",
140+
"spop",
141+
}
142+
143+
@pytest.fixture
144+
def shard_cache(self):
145+
from django.core.cache import caches, ConnectionProxy
146+
147+
cache = ConnectionProxy(caches, "default")
148+
yield cache
149+
cache.clear()
150+
151+
@pytest.fixture
152+
def connection(self, mocker):
153+
connection = mocker.Mock()
154+
for m in self.CLIENT_METHODS_FOR_MOCK:
155+
setattr(connection, m, mocker.Mock(spec_set=()))
156+
157+
connection.scan_iter.return_value = []
158+
connection.get.return_value = "this"
159+
connection.add.return_value = True
160+
connection.expire.return_value = False
161+
connection.close.return_value = None
162+
connection.ttl.return_value = 0
163+
connection.touch.return_value = True
164+
connection.sadd.return_value = 1
165+
connection.spop.return_value = {"this"}
166+
167+
yield connection
168+
169+
@patch("tests.test_client.ShardClient.make_pattern")
132170
@patch("tests.test_client.ShardClient.__init__", return_value=None)
133171
def test_delete_pattern_calls_scan_iter_with_count_if_itersize_given(
134-
self, init_mock, make_pattern_mock
172+
self,
173+
init_mock,
174+
make_pattern_mock,
175+
connection,
135176
):
136177
client = ShardClient()
137178
client._backend = Mock()
138179
client._backend.key_prefix = ""
139180

140-
connection = Mock()
141-
connection.scan_iter.return_value = []
142181
client._server_dict = {"test": connection}
143182

144183
client.delete_pattern(pattern="foo*", itersize=10)
@@ -147,14 +186,14 @@ def test_delete_pattern_calls_scan_iter_with_count_if_itersize_given(
147186
count=10, match=make_pattern_mock.return_value
148187
)
149188

150-
@patch("tests.test_client.DefaultClient.make_pattern")
189+
@patch("tests.test_client.ShardClient.make_pattern")
151190
@patch("tests.test_client.ShardClient.__init__", return_value=None)
152-
def test_delete_pattern_calls_scan_iter(self, init_mock, make_pattern_mock):
191+
def test_delete_pattern_calls_scan_iter(
192+
self, init_mock, make_pattern_mock, connection
193+
):
153194
client = ShardClient()
154195
client._backend = Mock()
155196
client._backend.key_prefix = ""
156-
connection = Mock()
157-
connection.scan_iter.return_value = []
158197
client._server_dict = {"test": connection}
159198

160199
client.delete_pattern(pattern="foo*")
@@ -163,19 +202,37 @@ def test_delete_pattern_calls_scan_iter(self, init_mock, make_pattern_mock):
163202
match=make_pattern_mock.return_value
164203
)
165204

166-
@patch("tests.test_client.DefaultClient.make_pattern")
205+
@patch("tests.test_client.ShardClient.make_pattern")
167206
@patch("tests.test_client.ShardClient.__init__", return_value=None)
168207
def test_delete_pattern_calls_delete_for_given_keys(
169-
self, init_mock, make_pattern_mock
208+
self, init_mock, make_pattern_mock, connection, cache
170209
):
171210
client = ShardClient()
172211
client._backend = Mock()
173212
client._backend.key_prefix = ""
174-
connection = Mock()
175213
connection.scan_iter.return_value = [Mock(), Mock()]
176214
connection.delete.return_value = 0
177215
client._server_dict = {"test": connection}
178216

179217
client.delete_pattern(pattern="foo*")
180218

181219
connection.delete.assert_called_once_with(*connection.scan_iter.return_value)
220+
221+
def test_shard_client_get_server_name_is_called(
222+
self, mocker, connection, shard_cache
223+
):
224+
if not isinstance(shard_cache.client, ShardClient):
225+
pytest.skip("shard only test")
226+
client = ShardClient(
227+
server=shard_cache._server, params=shard_cache._params, backend=shard_cache
228+
)
229+
spy = mocker.spy(client, "get_server_name")
230+
231+
client.add("that", "this")
232+
client.get("that")
233+
client.ttl("that")
234+
client.expire("that", 1)
235+
client.touch("that")
236+
client.sadd("key", "value1", "value2")
237+
client.spop("key")
238+
assert spy.call_count == 7

0 commit comments

Comments
 (0)