@@ -128,17 +128,56 @@ def test_delete_pattern_calls_pipeline_delete_and_execute(
128
128
129
129
130
130
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" )
132
170
@patch ("tests.test_client.ShardClient.__init__" , return_value = None )
133
171
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 ,
135
176
):
136
177
client = ShardClient ()
137
178
client ._backend = Mock ()
138
179
client ._backend .key_prefix = ""
139
180
140
- connection = Mock ()
141
- connection .scan_iter .return_value = []
142
181
client ._server_dict = {"test" : connection }
143
182
144
183
client .delete_pattern (pattern = "foo*" , itersize = 10 )
@@ -147,14 +186,14 @@ def test_delete_pattern_calls_scan_iter_with_count_if_itersize_given(
147
186
count = 10 , match = make_pattern_mock .return_value
148
187
)
149
188
150
- @patch ("tests.test_client.DefaultClient .make_pattern" )
189
+ @patch ("tests.test_client.ShardClient .make_pattern" )
151
190
@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
+ ):
153
194
client = ShardClient ()
154
195
client ._backend = Mock ()
155
196
client ._backend .key_prefix = ""
156
- connection = Mock ()
157
- connection .scan_iter .return_value = []
158
197
client ._server_dict = {"test" : connection }
159
198
160
199
client .delete_pattern (pattern = "foo*" )
@@ -163,19 +202,37 @@ def test_delete_pattern_calls_scan_iter(self, init_mock, make_pattern_mock):
163
202
match = make_pattern_mock .return_value
164
203
)
165
204
166
- @patch ("tests.test_client.DefaultClient .make_pattern" )
205
+ @patch ("tests.test_client.ShardClient .make_pattern" )
167
206
@patch ("tests.test_client.ShardClient.__init__" , return_value = None )
168
207
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
170
209
):
171
210
client = ShardClient ()
172
211
client ._backend = Mock ()
173
212
client ._backend .key_prefix = ""
174
- connection = Mock ()
175
213
connection .scan_iter .return_value = [Mock (), Mock ()]
176
214
connection .delete .return_value = 0
177
215
client ._server_dict = {"test" : connection }
178
216
179
217
client .delete_pattern (pattern = "foo*" )
180
218
181
219
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