Skip to content

Commit 30717aa

Browse files
committed
Fixed broken tests
1 parent 1f6876a commit 30717aa

File tree

1 file changed

+65
-37
lines changed

1 file changed

+65
-37
lines changed

tests/test_cache.py

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -811,25 +811,27 @@ def test_get_size(self):
811811
@pytest.mark.parametrize(
812812
"cache_key", [{"command": "GET", "redis_keys": ("bar",)}], indirect=True
813813
)
814-
def test_set_non_existing_cache_key(self, cache_key):
814+
def test_set_non_existing_cache_key(self, cache_key, mock_connection):
815815
cache = DefaultCache(CacheConfig(max_size=5))
816816

817817
assert cache.set(
818818
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
820821
)
821822
)
822823
assert cache.get(cache_key).cache_value == b"val"
823824

824825
@pytest.mark.parametrize(
825826
"cache_key", [{"command": "GET", "redis_keys": ("bar",)}], indirect=True
826827
)
827-
def test_set_updates_existing_cache_key(self, cache_key):
828+
def test_set_updates_existing_cache_key(self, cache_key, mock_connection):
828829
cache = DefaultCache(CacheConfig(max_size=5))
829830

830831
assert cache.set(
831832
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
833835
)
834836
)
835837
assert cache.get(cache_key).cache_value == b"val"
@@ -839,23 +841,25 @@ def test_set_updates_existing_cache_key(self, cache_key):
839841
cache_key=cache_key,
840842
cache_value=b"new_val",
841843
status=CacheEntryStatus.VALID,
844+
connection_ref=mock_connection,
842845
)
843846
)
844847
assert cache.get(cache_key).cache_value == b"new_val"
845848

846849
@pytest.mark.parametrize(
847850
"cache_key", [{"command": "HRANDFIELD", "redis_keys": ("bar",)}], indirect=True
848851
)
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):
850853
cache = DefaultCache(CacheConfig(max_size=5))
851854

852855
assert not cache.set(
853856
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
855859
)
856860
)
857861

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):
859863
cache = DefaultCache(CacheConfig(max_size=3))
860864
cache_key1 = CacheKey(command="GET", redis_keys=("foo",))
861865
cache_key2 = CacheKey(command="GET", redis_keys=("foo1",))
@@ -864,17 +868,20 @@ def test_set_evict_lru_cache_key_on_reaching_max_size(self):
864868
# Set 3 different keys
865869
assert cache.set(
866870
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
868873
)
869874
)
870875
assert cache.set(
871876
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
873879
)
874880
)
875881
assert cache.set(
876882
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
878885
)
879886
)
880887

@@ -887,7 +894,8 @@ def test_set_evict_lru_cache_key_on_reaching_max_size(self):
887894
cache_key4 = CacheKey(command="GET", redis_keys=("foo3",))
888895
assert cache.set(
889896
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
891899
)
892900
)
893901

@@ -898,12 +906,13 @@ def test_set_evict_lru_cache_key_on_reaching_max_size(self):
898906
@pytest.mark.parametrize(
899907
"cache_key", [{"command": "GET", "redis_keys": ("bar",)}], indirect=True
900908
)
901-
def test_get_return_correct_value(self, cache_key):
909+
def test_get_return_correct_value(self, cache_key, mock_connection):
902910
cache = DefaultCache(CacheConfig(max_size=5))
903911

904912
assert cache.set(
905913
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
907916
)
908917
)
909918
assert cache.get(cache_key).cache_value == b"val"
@@ -917,13 +926,14 @@ def test_get_return_correct_value(self, cache_key):
917926
cache_key=cache_key,
918927
cache_value=b"new_val",
919928
status=CacheEntryStatus.VALID,
929+
connection_ref=mock_connection
920930
)
921931
)
922932

923933
# Make sure that result is immutable.
924934
assert result.cache_value != cache.get(cache_key).cache_value
925935

926-
def test_delete_by_cache_keys_removes_associated_entries(self):
936+
def test_delete_by_cache_keys_removes_associated_entries(self, mock_connection):
927937
cache = DefaultCache(CacheConfig(max_size=5))
928938

929939
cache_key1 = CacheKey(command="GET", redis_keys=("foo",))
@@ -934,17 +944,20 @@ def test_delete_by_cache_keys_removes_associated_entries(self):
934944
# Set 3 different keys
935945
assert cache.set(
936946
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
938949
)
939950
)
940951
assert cache.set(
941952
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
943955
)
944956
)
945957
assert cache.set(
946958
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
948961
)
949962
)
950963

@@ -956,7 +969,7 @@ def test_delete_by_cache_keys_removes_associated_entries(self):
956969
assert len(cache.get_collection()) == 1
957970
assert cache.get(cache_key3).cache_value == b"bar2"
958971

959-
def test_delete_by_redis_keys_removes_associated_entries(self):
972+
def test_delete_by_redis_keys_removes_associated_entries(self, mock_connection):
960973
cache = DefaultCache(CacheConfig(max_size=5))
961974

962975
cache_key1 = CacheKey(command="GET", redis_keys=("foo",))
@@ -967,30 +980,34 @@ def test_delete_by_redis_keys_removes_associated_entries(self):
967980
# Set 3 different keys
968981
assert cache.set(
969982
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
971985
)
972986
)
973987
assert cache.set(
974988
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
976991
)
977992
)
978993
assert cache.set(
979994
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
981997
)
982998
)
983999
assert cache.set(
9841000
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
9861003
)
9871004
)
9881005

9891006
assert cache.delete_by_redis_keys([b"foo", b"foo1"]) == [True, True, True]
9901007
assert len(cache.get_collection()) == 1
9911008
assert cache.get(cache_key4).cache_value == b"bar3"
9921009

993-
def test_flush(self):
1010+
def test_flush(self, mock_connection):
9941011
cache = DefaultCache(CacheConfig(max_size=5))
9951012

9961013
cache_key1 = CacheKey(command="GET", redis_keys=("foo",))
@@ -1000,17 +1017,20 @@ def test_flush(self):
10001017
# Set 3 different keys
10011018
assert cache.set(
10021019
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
10041022
)
10051023
)
10061024
assert cache.set(
10071025
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
10091028
)
10101029
)
10111030
assert cache.set(
10121031
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
10141034
)
10151035
)
10161036

@@ -1023,7 +1043,7 @@ def test_type(self):
10231043
policy = LRUPolicy()
10241044
assert policy.type == EvictionPolicyType.time_based
10251045

1026-
def test_evict_next(self):
1046+
def test_evict_next(self, mock_connection):
10271047
cache = DefaultCache(
10281048
CacheConfig(max_size=5, eviction_policy=EvictionPolicy.LRU)
10291049
)
@@ -1034,19 +1054,21 @@ def test_evict_next(self):
10341054

10351055
assert cache.set(
10361056
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
10381059
)
10391060
)
10401061
assert cache.set(
10411062
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
10431065
)
10441066
)
10451067

10461068
assert policy.evict_next() == cache_key1
10471069
assert cache.get(cache_key1) is None
10481070

1049-
def test_evict_many(self):
1071+
def test_evict_many(self, mock_connection):
10501072
cache = DefaultCache(
10511073
CacheConfig(max_size=5, eviction_policy=EvictionPolicy.LRU)
10521074
)
@@ -1057,17 +1079,20 @@ def test_evict_many(self):
10571079

10581080
assert cache.set(
10591081
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
10611084
)
10621085
)
10631086
assert cache.set(
10641087
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
10661090
)
10671091
)
10681092
assert cache.set(
10691093
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
10711096
)
10721097
)
10731098

@@ -1078,7 +1103,7 @@ def test_evict_many(self):
10781103
with pytest.raises(ValueError, match="Evictions count is above cache size"):
10791104
policy.evict_many(99)
10801105

1081-
def test_touch(self):
1106+
def test_touch(self, mock_connection):
10821107
cache = DefaultCache(
10831108
CacheConfig(max_size=5, eviction_policy=EvictionPolicy.LRU)
10841109
)
@@ -1089,19 +1114,22 @@ def test_touch(self):
10891114

10901115
cache.set(
10911116
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
10931119
)
10941120
)
10951121
cache.set(
10961122
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
10981125
)
10991126
)
11001127

11011128
assert cache.get_collection().popitem(last=True)[0] == cache_key2
11021129
cache.set(
11031130
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
11051133
)
11061134
)
11071135

0 commit comments

Comments
 (0)