@@ -811,25 +811,27 @@ def test_get_size(self):
811
811
@pytest .mark .parametrize (
812
812
"cache_key" , [{"command" : "GET" , "redis_keys" : ("bar" ,)}], indirect = True
813
813
)
814
- def test_set_non_existing_cache_key (self , cache_key ):
814
+ def test_set_non_existing_cache_key (self , cache_key , mock_connection ):
815
815
cache = DefaultCache (CacheConfig (max_size = 5 ))
816
816
817
817
assert cache .set (
818
818
CacheEntry (
819
- cache_key = cache_key , cache_value = b"val" , status = CacheEntryStatus .VALID
819
+ cache_key = cache_key , cache_value = b"val" , status = CacheEntryStatus .VALID ,
820
+ connection_ref = mock_connection
820
821
)
821
822
)
822
823
assert cache .get (cache_key ).cache_value == b"val"
823
824
824
825
@pytest .mark .parametrize (
825
826
"cache_key" , [{"command" : "GET" , "redis_keys" : ("bar" ,)}], indirect = True
826
827
)
827
- def test_set_updates_existing_cache_key (self , cache_key ):
828
+ def test_set_updates_existing_cache_key (self , cache_key , mock_connection ):
828
829
cache = DefaultCache (CacheConfig (max_size = 5 ))
829
830
830
831
assert cache .set (
831
832
CacheEntry (
832
- cache_key = cache_key , cache_value = b"val" , status = CacheEntryStatus .VALID
833
+ cache_key = cache_key , cache_value = b"val" , status = CacheEntryStatus .VALID ,
834
+ connection_ref = mock_connection
833
835
)
834
836
)
835
837
assert cache .get (cache_key ).cache_value == b"val"
@@ -839,23 +841,25 @@ def test_set_updates_existing_cache_key(self, cache_key):
839
841
cache_key = cache_key ,
840
842
cache_value = b"new_val" ,
841
843
status = CacheEntryStatus .VALID ,
844
+ connection_ref = mock_connection ,
842
845
)
843
846
)
844
847
assert cache .get (cache_key ).cache_value == b"new_val"
845
848
846
849
@pytest .mark .parametrize (
847
850
"cache_key" , [{"command" : "HRANDFIELD" , "redis_keys" : ("bar" ,)}], indirect = True
848
851
)
849
- def test_set_does_not_store_not_allowed_key (self , cache_key ):
852
+ def test_set_does_not_store_not_allowed_key (self , cache_key , mock_connection ):
850
853
cache = DefaultCache (CacheConfig (max_size = 5 ))
851
854
852
855
assert not cache .set (
853
856
CacheEntry (
854
- cache_key = cache_key , cache_value = b"val" , status = CacheEntryStatus .VALID
857
+ cache_key = cache_key , cache_value = b"val" , status = CacheEntryStatus .VALID ,
858
+ connection_ref = mock_connection
855
859
)
856
860
)
857
861
858
- def test_set_evict_lru_cache_key_on_reaching_max_size (self ):
862
+ def test_set_evict_lru_cache_key_on_reaching_max_size (self , mock_connection ):
859
863
cache = DefaultCache (CacheConfig (max_size = 3 ))
860
864
cache_key1 = CacheKey (command = "GET" , redis_keys = ("foo" ,))
861
865
cache_key2 = CacheKey (command = "GET" , redis_keys = ("foo1" ,))
@@ -864,17 +868,20 @@ def test_set_evict_lru_cache_key_on_reaching_max_size(self):
864
868
# Set 3 different keys
865
869
assert cache .set (
866
870
CacheEntry (
867
- cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID
871
+ cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID ,
872
+ connection_ref = mock_connection
868
873
)
869
874
)
870
875
assert cache .set (
871
876
CacheEntry (
872
- cache_key = cache_key2 , cache_value = b"bar1" , status = CacheEntryStatus .VALID
877
+ cache_key = cache_key2 , cache_value = b"bar1" , status = CacheEntryStatus .VALID ,
878
+ connection_ref = mock_connection
873
879
)
874
880
)
875
881
assert cache .set (
876
882
CacheEntry (
877
- cache_key = cache_key3 , cache_value = b"bar2" , status = CacheEntryStatus .VALID
883
+ cache_key = cache_key3 , cache_value = b"bar2" , status = CacheEntryStatus .VALID ,
884
+ connection_ref = mock_connection
878
885
)
879
886
)
880
887
@@ -887,7 +894,8 @@ def test_set_evict_lru_cache_key_on_reaching_max_size(self):
887
894
cache_key4 = CacheKey (command = "GET" , redis_keys = ("foo3" ,))
888
895
assert cache .set (
889
896
CacheEntry (
890
- cache_key = cache_key4 , cache_value = b"bar3" , status = CacheEntryStatus .VALID
897
+ cache_key = cache_key4 , cache_value = b"bar3" , status = CacheEntryStatus .VALID ,
898
+ connection_ref = mock_connection
891
899
)
892
900
)
893
901
@@ -898,12 +906,13 @@ def test_set_evict_lru_cache_key_on_reaching_max_size(self):
898
906
@pytest .mark .parametrize (
899
907
"cache_key" , [{"command" : "GET" , "redis_keys" : ("bar" ,)}], indirect = True
900
908
)
901
- def test_get_return_correct_value (self , cache_key ):
909
+ def test_get_return_correct_value (self , cache_key , mock_connection ):
902
910
cache = DefaultCache (CacheConfig (max_size = 5 ))
903
911
904
912
assert cache .set (
905
913
CacheEntry (
906
- cache_key = cache_key , cache_value = b"val" , status = CacheEntryStatus .VALID
914
+ cache_key = cache_key , cache_value = b"val" , status = CacheEntryStatus .VALID ,
915
+ connection_ref = mock_connection
907
916
)
908
917
)
909
918
assert cache .get (cache_key ).cache_value == b"val"
@@ -917,13 +926,14 @@ def test_get_return_correct_value(self, cache_key):
917
926
cache_key = cache_key ,
918
927
cache_value = b"new_val" ,
919
928
status = CacheEntryStatus .VALID ,
929
+ connection_ref = mock_connection
920
930
)
921
931
)
922
932
923
933
# Make sure that result is immutable.
924
934
assert result .cache_value != cache .get (cache_key ).cache_value
925
935
926
- def test_delete_by_cache_keys_removes_associated_entries (self ):
936
+ def test_delete_by_cache_keys_removes_associated_entries (self , mock_connection ):
927
937
cache = DefaultCache (CacheConfig (max_size = 5 ))
928
938
929
939
cache_key1 = CacheKey (command = "GET" , redis_keys = ("foo" ,))
@@ -934,17 +944,20 @@ def test_delete_by_cache_keys_removes_associated_entries(self):
934
944
# Set 3 different keys
935
945
assert cache .set (
936
946
CacheEntry (
937
- cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID
947
+ cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID ,
948
+ connection_ref = mock_connection
938
949
)
939
950
)
940
951
assert cache .set (
941
952
CacheEntry (
942
- cache_key = cache_key2 , cache_value = b"bar1" , status = CacheEntryStatus .VALID
953
+ cache_key = cache_key2 , cache_value = b"bar1" , status = CacheEntryStatus .VALID ,
954
+ connection_ref = mock_connection
943
955
)
944
956
)
945
957
assert cache .set (
946
958
CacheEntry (
947
- cache_key = cache_key3 , cache_value = b"bar2" , status = CacheEntryStatus .VALID
959
+ cache_key = cache_key3 , cache_value = b"bar2" , status = CacheEntryStatus .VALID ,
960
+ connection_ref = mock_connection
948
961
)
949
962
)
950
963
@@ -956,7 +969,7 @@ def test_delete_by_cache_keys_removes_associated_entries(self):
956
969
assert len (cache .get_collection ()) == 1
957
970
assert cache .get (cache_key3 ).cache_value == b"bar2"
958
971
959
- def test_delete_by_redis_keys_removes_associated_entries (self ):
972
+ def test_delete_by_redis_keys_removes_associated_entries (self , mock_connection ):
960
973
cache = DefaultCache (CacheConfig (max_size = 5 ))
961
974
962
975
cache_key1 = CacheKey (command = "GET" , redis_keys = ("foo" ,))
@@ -967,30 +980,34 @@ def test_delete_by_redis_keys_removes_associated_entries(self):
967
980
# Set 3 different keys
968
981
assert cache .set (
969
982
CacheEntry (
970
- cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID
983
+ cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID ,
984
+ connection_ref = mock_connection
971
985
)
972
986
)
973
987
assert cache .set (
974
988
CacheEntry (
975
- cache_key = cache_key2 , cache_value = b"bar1" , status = CacheEntryStatus .VALID
989
+ cache_key = cache_key2 , cache_value = b"bar1" , status = CacheEntryStatus .VALID ,
990
+ connection_ref = mock_connection
976
991
)
977
992
)
978
993
assert cache .set (
979
994
CacheEntry (
980
- cache_key = cache_key3 , cache_value = b"bar2" , status = CacheEntryStatus .VALID
995
+ cache_key = cache_key3 , cache_value = b"bar2" , status = CacheEntryStatus .VALID ,
996
+ connection_ref = mock_connection
981
997
)
982
998
)
983
999
assert cache .set (
984
1000
CacheEntry (
985
- cache_key = cache_key4 , cache_value = b"bar3" , status = CacheEntryStatus .VALID
1001
+ cache_key = cache_key4 , cache_value = b"bar3" , status = CacheEntryStatus .VALID ,
1002
+ connection_ref = mock_connection
986
1003
)
987
1004
)
988
1005
989
1006
assert cache .delete_by_redis_keys ([b"foo" , b"foo1" ]) == [True , True , True ]
990
1007
assert len (cache .get_collection ()) == 1
991
1008
assert cache .get (cache_key4 ).cache_value == b"bar3"
992
1009
993
- def test_flush (self ):
1010
+ def test_flush (self , mock_connection ):
994
1011
cache = DefaultCache (CacheConfig (max_size = 5 ))
995
1012
996
1013
cache_key1 = CacheKey (command = "GET" , redis_keys = ("foo" ,))
@@ -1000,17 +1017,20 @@ def test_flush(self):
1000
1017
# Set 3 different keys
1001
1018
assert cache .set (
1002
1019
CacheEntry (
1003
- cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID
1020
+ cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID ,
1021
+ connection_ref = mock_connection
1004
1022
)
1005
1023
)
1006
1024
assert cache .set (
1007
1025
CacheEntry (
1008
- cache_key = cache_key2 , cache_value = b"bar1" , status = CacheEntryStatus .VALID
1026
+ cache_key = cache_key2 , cache_value = b"bar1" , status = CacheEntryStatus .VALID ,
1027
+ connection_ref = mock_connection
1009
1028
)
1010
1029
)
1011
1030
assert cache .set (
1012
1031
CacheEntry (
1013
- cache_key = cache_key3 , cache_value = b"bar2" , status = CacheEntryStatus .VALID
1032
+ cache_key = cache_key3 , cache_value = b"bar2" , status = CacheEntryStatus .VALID ,
1033
+ connection_ref = mock_connection
1014
1034
)
1015
1035
)
1016
1036
@@ -1023,7 +1043,7 @@ def test_type(self):
1023
1043
policy = LRUPolicy ()
1024
1044
assert policy .type == EvictionPolicyType .time_based
1025
1045
1026
- def test_evict_next (self ):
1046
+ def test_evict_next (self , mock_connection ):
1027
1047
cache = DefaultCache (
1028
1048
CacheConfig (max_size = 5 , eviction_policy = EvictionPolicy .LRU )
1029
1049
)
@@ -1034,19 +1054,21 @@ def test_evict_next(self):
1034
1054
1035
1055
assert cache .set (
1036
1056
CacheEntry (
1037
- cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID
1057
+ cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID ,
1058
+ connection_ref = mock_connection
1038
1059
)
1039
1060
)
1040
1061
assert cache .set (
1041
1062
CacheEntry (
1042
- cache_key = cache_key2 , cache_value = b"foo" , status = CacheEntryStatus .VALID
1063
+ cache_key = cache_key2 , cache_value = b"foo" , status = CacheEntryStatus .VALID ,
1064
+ connection_ref = mock_connection
1043
1065
)
1044
1066
)
1045
1067
1046
1068
assert policy .evict_next () == cache_key1
1047
1069
assert cache .get (cache_key1 ) is None
1048
1070
1049
- def test_evict_many (self ):
1071
+ def test_evict_many (self , mock_connection ):
1050
1072
cache = DefaultCache (
1051
1073
CacheConfig (max_size = 5 , eviction_policy = EvictionPolicy .LRU )
1052
1074
)
@@ -1057,17 +1079,20 @@ def test_evict_many(self):
1057
1079
1058
1080
assert cache .set (
1059
1081
CacheEntry (
1060
- cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID
1082
+ cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID ,
1083
+ connection_ref = mock_connection
1061
1084
)
1062
1085
)
1063
1086
assert cache .set (
1064
1087
CacheEntry (
1065
- cache_key = cache_key2 , cache_value = b"foo" , status = CacheEntryStatus .VALID
1088
+ cache_key = cache_key2 , cache_value = b"foo" , status = CacheEntryStatus .VALID ,
1089
+ connection_ref = mock_connection
1066
1090
)
1067
1091
)
1068
1092
assert cache .set (
1069
1093
CacheEntry (
1070
- cache_key = cache_key3 , cache_value = b"baz" , status = CacheEntryStatus .VALID
1094
+ cache_key = cache_key3 , cache_value = b"baz" , status = CacheEntryStatus .VALID ,
1095
+ connection_ref = mock_connection
1071
1096
)
1072
1097
)
1073
1098
@@ -1078,7 +1103,7 @@ def test_evict_many(self):
1078
1103
with pytest .raises (ValueError , match = "Evictions count is above cache size" ):
1079
1104
policy .evict_many (99 )
1080
1105
1081
- def test_touch (self ):
1106
+ def test_touch (self , mock_connection ):
1082
1107
cache = DefaultCache (
1083
1108
CacheConfig (max_size = 5 , eviction_policy = EvictionPolicy .LRU )
1084
1109
)
@@ -1089,19 +1114,22 @@ def test_touch(self):
1089
1114
1090
1115
cache .set (
1091
1116
CacheEntry (
1092
- cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID
1117
+ cache_key = cache_key1 , cache_value = b"bar" , status = CacheEntryStatus .VALID ,
1118
+ connection_ref = mock_connection
1093
1119
)
1094
1120
)
1095
1121
cache .set (
1096
1122
CacheEntry (
1097
- cache_key = cache_key2 , cache_value = b"foo" , status = CacheEntryStatus .VALID
1123
+ cache_key = cache_key2 , cache_value = b"foo" , status = CacheEntryStatus .VALID ,
1124
+ connection_ref = mock_connection
1098
1125
)
1099
1126
)
1100
1127
1101
1128
assert cache .get_collection ().popitem (last = True )[0 ] == cache_key2
1102
1129
cache .set (
1103
1130
CacheEntry (
1104
- cache_key = cache_key2 , cache_value = b"foo" , status = CacheEntryStatus .VALID
1131
+ cache_key = cache_key2 , cache_value = b"foo" , status = CacheEntryStatus .VALID ,
1132
+ connection_ref = mock_connection
1105
1133
)
1106
1134
)
1107
1135
0 commit comments