3434from cachier import cachier
3535from cachier .config import CacheEntry , _global_params
3636from cachier .cores .pickle import _PickleCore
37+ from cachier .cores .redis import _RedisCore
3738
3839
3940def _get_decorated_func (func , ** kwargs ):
@@ -42,9 +43,6 @@ def _get_decorated_func(func, **kwargs):
4243 return decorated_func
4344
4445
45- # Pickle core tests
46-
47-
4846def _takes_2_seconds (arg_1 , arg_2 ):
4947 """Some function."""
5048 sleep (2 )
@@ -528,14 +526,14 @@ def _error_throwing_func(arg1):
528526@pytest .mark .parametrize ("separate_files" , [True , False ])
529527def test_error_throwing_func (separate_files ):
530528 # with
531- _error_throwing_func .count = 0
532529 _error_throwing_func_decorated = _get_decorated_func (
533530 _error_throwing_func ,
534531 stale_after = timedelta (seconds = 1 ),
535532 next_time = True ,
536533 separate_files = separate_files ,
537534 )
538535 _error_throwing_func_decorated .clear_cache ()
536+ _error_throwing_func .count = 0
539537 res1 = _error_throwing_func_decorated (4 )
540538 sleep (1.5 )
541539 res2 = _error_throwing_func_decorated (4 )
@@ -1074,3 +1072,70 @@ def mock_func():
10741072 with patch ("os.remove" , side_effect = FileNotFoundError ):
10751073 # Should not raise exception
10761074 core .delete_stale_entries (timedelta (hours = 1 ))
1075+
1076+
1077+ # Redis core static method tests
1078+ @pytest .mark .parametrize (
1079+ ("test_input" , "expected" ),
1080+ [
1081+ (pickle .dumps ({"test" : 123 }), {"test" : 123 }), # valid string
1082+ # (pickle.dumps({"test": 123}).decode("utf-8"), {"test": 123}),
1083+ # (b"\x80\x04\x95", None), # corrupted bytes
1084+ (123 , None ), # unexpected type
1085+ # (b"corrupted", None), # triggers warning
1086+ ],
1087+ )
1088+ def test_redis_loading_pickle (test_input , expected ):
1089+ """Test _RedisCore._loading_pickle with various inputs and exceptions."""
1090+ assert _RedisCore ._loading_pickle (test_input ) == expected
1091+
1092+
1093+ def test_redis_loading_pickle_failed ():
1094+ """Test _RedisCore._loading_pickle with various inputs and exceptions."""
1095+ with patch ("pickle.loads" , side_effect = Exception ("Failed" )):
1096+ assert _RedisCore ._loading_pickle (123 ) is None
1097+
1098+
1099+ def test_redis_loading_pickle_latin1_fallback ():
1100+ """Test _RedisCore._loading_pickle with latin-1 fallback."""
1101+ valid_obj = {"test" : 123 }
1102+ with patch ("pickle.loads" ) as mock_loads :
1103+ mock_loads .side_effect = [Exception ("UTF-8 failed" ), valid_obj ]
1104+ result = _RedisCore ._loading_pickle ("invalid_utf8_string" )
1105+ assert result == valid_obj
1106+ assert mock_loads .call_count == 2
1107+
1108+
1109+ @pytest .mark .parametrize (
1110+ ("cached_data" , "key" , "expected" ),
1111+ [
1112+ ({b"field" : b"value" , "other" : "data" }, "field" , b"value" ),
1113+ ({"field" : "value" , b"other" : b"data" }, "field" , "value" ),
1114+ ({"other" : "value" }, "field" , None ),
1115+ ],
1116+ )
1117+ def test_redis_get_raw_field (cached_data , key , expected ):
1118+ """Test _RedisCore._get_raw_field with bytes and string keys."""
1119+ assert _RedisCore ._get_raw_field (cached_data , key ) == expected
1120+
1121+
1122+ @pytest .mark .parametrize (
1123+ ("cached_data" , "key" , "expected" ),
1124+ [
1125+ ({b"flag" : b"true" }, "flag" , True ),
1126+ ({b"flag" : b"false" }, "flag" , False ),
1127+ ({"flag" : "TRUE" }, "flag" , True ),
1128+ ({}, "flag" , False ),
1129+ ({b"flag" : 123 }, "flag" , False ),
1130+ ],
1131+ )
1132+ def test_redis_get_bool_field (cached_data , key , expected ):
1133+ """Test _RedisCore._get_bool_field with various inputs."""
1134+ assert _RedisCore ._get_bool_field (cached_data , key ) == expected
1135+
1136+
1137+ def test_redis_get_bool_field_decode_fallback ():
1138+ """Test _RedisCore._get_bool_field with decoding fallback."""
1139+ with patch .object (_RedisCore , "_get_raw_field" , return_value = b"\xff \xfe " ):
1140+ result = _RedisCore ._get_bool_field ({}, "flag" )
1141+ assert result is False
0 commit comments