1+ import uuid
2+
13import pytest
24
35import fakeredis
46from fakeredis import FakeStrictRedis
57
68from 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
810from sentry_sdk .utils import parse_version
911import sentry_sdk
1012
@@ -137,7 +139,9 @@ def test_cache_data(sentry_init, capture_events):
137139
138140 assert spans [0 ]["op" ] == "cache.get"
139141 assert spans [0 ]["description" ] == "mycachekey"
140- assert spans [0 ]["data" ]["cache.key" ] == "mycachekey"
142+ assert spans [0 ]["data" ]["cache.key" ] == [
143+ "mycachekey" ,
144+ ]
141145 assert spans [0 ]["data" ]["cache.hit" ] == False # noqa: E712
142146 assert "cache.item_size" not in spans [0 ]["data" ]
143147 # very old fakeredis can not handle port and/or host.
@@ -155,7 +159,9 @@ def test_cache_data(sentry_init, capture_events):
155159
156160 assert spans [2 ]["op" ] == "cache.put"
157161 assert spans [2 ]["description" ] == "mycachekey"
158- assert spans [2 ]["data" ]["cache.key" ] == "mycachekey"
162+ assert spans [2 ]["data" ]["cache.key" ] == [
163+ "mycachekey" ,
164+ ]
159165 assert "cache.hit" not in spans [1 ]["data" ]
160166 assert spans [2 ]["data" ]["cache.item_size" ] == 18
161167 # very old fakeredis can not handle port.
@@ -173,7 +179,9 @@ def test_cache_data(sentry_init, capture_events):
173179
174180 assert spans [4 ]["op" ] == "cache.get"
175181 assert spans [4 ]["description" ] == "mycachekey"
176- assert spans [4 ]["data" ]["cache.key" ] == "mycachekey"
182+ assert spans [4 ]["data" ]["cache.key" ] == [
183+ "mycachekey" ,
184+ ]
177185 assert spans [4 ]["data" ]["cache.hit" ] == True # noqa: E712
178186 assert spans [4 ]["data" ]["cache.item_size" ] == 18
179187 # very old fakeredis can not handle port.
@@ -193,17 +201,72 @@ def test_cache_data(sentry_init, capture_events):
193201@pytest .mark .parametrize (
194202 "method_name,args,kwargs,expected_key" ,
195203 [
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+ ),
206247 ],
207248)
208249def test_get_safe_key (method_name , args , kwargs , expected_key ):
209250 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