1
+ import uuid
2
+
1
3
import pytest
2
4
3
5
import fakeredis
4
6
from fakeredis import FakeStrictRedis
5
7
6
8
from sentry_sdk .integrations .redis import RedisIntegration
7
- from sentry_sdk .integrations .redis .utils import _get_safe_key
9
+ from sentry_sdk .integrations .redis .utils import _get_safe_key , _key_as_string
8
10
from sentry_sdk .utils import parse_version
9
11
import sentry_sdk
10
12
@@ -137,7 +139,9 @@ def test_cache_data(sentry_init, capture_events):
137
139
138
140
assert spans [0 ]["op" ] == "cache.get"
139
141
assert spans [0 ]["description" ] == "mycachekey"
140
- assert spans [0 ]["data" ]["cache.key" ] == "mycachekey"
142
+ assert spans [0 ]["data" ]["cache.key" ] == [
143
+ "mycachekey" ,
144
+ ]
141
145
assert spans [0 ]["data" ]["cache.hit" ] == False # noqa: E712
142
146
assert "cache.item_size" not in spans [0 ]["data" ]
143
147
# very old fakeredis can not handle port and/or host.
@@ -155,7 +159,9 @@ def test_cache_data(sentry_init, capture_events):
155
159
156
160
assert spans [2 ]["op" ] == "cache.put"
157
161
assert spans [2 ]["description" ] == "mycachekey"
158
- assert spans [2 ]["data" ]["cache.key" ] == "mycachekey"
162
+ assert spans [2 ]["data" ]["cache.key" ] == [
163
+ "mycachekey" ,
164
+ ]
159
165
assert "cache.hit" not in spans [1 ]["data" ]
160
166
assert spans [2 ]["data" ]["cache.item_size" ] == 18
161
167
# very old fakeredis can not handle port.
@@ -173,7 +179,9 @@ def test_cache_data(sentry_init, capture_events):
173
179
174
180
assert spans [4 ]["op" ] == "cache.get"
175
181
assert spans [4 ]["description" ] == "mycachekey"
176
- assert spans [4 ]["data" ]["cache.key" ] == "mycachekey"
182
+ assert spans [4 ]["data" ]["cache.key" ] == [
183
+ "mycachekey" ,
184
+ ]
177
185
assert spans [4 ]["data" ]["cache.hit" ] == True # noqa: E712
178
186
assert spans [4 ]["data" ]["cache.item_size" ] == 18
179
187
# very old fakeredis can not handle port.
@@ -193,17 +201,72 @@ def test_cache_data(sentry_init, capture_events):
193
201
@pytest .mark .parametrize (
194
202
"method_name,args,kwargs,expected_key" ,
195
203
[
196
- (None , None , None , "" ),
197
- ("" , None , None , "" ),
198
- ("set" , ["bla" , "valuebla" ], None , "bla" ),
199
- ("setex" , ["bla" , 10 , "valuebla" ], None , "bla" ),
200
- ("get" , ["bla" ], None , "bla" ),
201
- ("mget" , ["bla" , "blub" , "foo" ], None , "bla, blub, foo" ),
202
- ("set" , [b"bla" , "valuebla" ], None , "bla" ),
203
- ("setex" , [b"bla" , 10 , "valuebla" ], None , "bla" ),
204
- ("get" , [b"bla" ], None , "bla" ),
205
- ("mget" , [b"bla" , "blub" , "foo" ], None , "bla, blub, foo" ),
204
+ (None , None , None , None ),
205
+ ("" , None , None , None ),
206
+ ("set" , ["bla" , "valuebla" ], None , ("bla" ,)),
207
+ ("setex" , ["bla" , 10 , "valuebla" ], None , ("bla" ,)),
208
+ ("get" , ["bla" ], None , ("bla" ,)),
209
+ ("mget" , ["bla" , "blub" , "foo" ], None , ("bla" , "blub" , "foo" )),
210
+ ("set" , [b"bla" , "valuebla" ], None , (b"bla" ,)),
211
+ ("setex" , [b"bla" , 10 , "valuebla" ], None , (b"bla" ,)),
212
+ ("get" , [b"bla" ], None , (b"bla" ,)),
213
+ ("mget" , [b"bla" , "blub" , "foo" ], None , (b"bla" , "blub" , "foo" )),
214
+ ("not-important" , None , {"something" : "bla" }, None ),
215
+ ("not-important" , None , {"key" : None }, None ),
216
+ ("not-important" , None , {"key" : "bla" }, ("bla" ,)),
217
+ ("not-important" , None , {"key" : b"bla" }, (b"bla" ,)),
218
+ ("not-important" , None , {"key" : []}, None ),
219
+ (
220
+ "not-important" ,
221
+ None ,
222
+ {
223
+ "key" : [
224
+ "bla" ,
225
+ ]
226
+ },
227
+ ("bla" ,),
228
+ ),
229
+ (
230
+ "not-important" ,
231
+ None ,
232
+ {"key" : [b"bla" , "blub" , "foo" ]},
233
+ (b"bla" , "blub" , "foo" ),
234
+ ),
235
+ (
236
+ "not-important" ,
237
+ None ,
238
+ {"key" : b"\x00 c\x0f \xea C\xe1 L\x1c \xbf f\xcb \xcc \xc1 \xed \xc6 \t " },
239
+ (b"\x00 c\x0f \xea C\xe1 L\x1c \xbf f\xcb \xcc \xc1 \xed \xc6 \t " ,),
240
+ ),
241
+ (
242
+ "get" ,
243
+ [b"\x00 c\x0f \xea C\xe1 L\x1c \xbf f\xcb \xcc \xc1 \xed \xc6 \t " ],
244
+ None ,
245
+ (b"\x00 c\x0f \xea C\xe1 L\x1c \xbf f\xcb \xcc \xc1 \xed \xc6 \t " ,),
246
+ ),
206
247
],
207
248
)
208
249
def test_get_safe_key (method_name , args , kwargs , expected_key ):
209
250
assert _get_safe_key (method_name , args , kwargs ) == expected_key
251
+
252
+
253
+ @pytest .mark .parametrize (
254
+ "key,expected_key" ,
255
+ [
256
+ (None , "" ),
257
+ (("bla" ,), "bla" ),
258
+ (("bla" , "blub" , "foo" ), "bla, blub, foo" ),
259
+ ((b"bla" ,), "bla" ),
260
+ ((b"bla" , "blub" , "foo" ), "bla, blub, foo" ),
261
+ (
262
+ [
263
+ "bla" ,
264
+ ],
265
+ "bla" ,
266
+ ),
267
+ (["bla" , "blub" , "foo" ], "bla, blub, foo" ),
268
+ ([uuid .uuid4 ().bytes ], "" ),
269
+ ],
270
+ )
271
+ def test_key_as_string (key , expected_key ):
272
+ assert _key_as_string (key ) == expected_key
0 commit comments