@@ -889,6 +889,12 @@ def test_cache_values_no_max_size(self):
889
889
assert 2 == cache .hits
890
890
assert 1 == cache .misses
891
891
892
+ # manually clear all cached values
893
+ cache .clear_values ()
894
+ assert b'zzz' == cache ['foo' ]
895
+ assert 2 == store .counter ['__getitem__' , 'foo' ]
896
+ assert 2 == store .counter ['__setitem__' , 'foo' ]
897
+
892
898
# test __delitem__
893
899
del cache ['foo' ]
894
900
with pytest .raises (KeyError ):
@@ -902,7 +908,6 @@ def test_cache_values_no_max_size(self):
902
908
assert 0 == store .counter ['__getitem__' , 'bar' ]
903
909
assert 1 == store .counter ['__setitem__' , 'bar' ]
904
910
905
- # TODO test max size
906
911
def test_cache_values_with_max_size (self ):
907
912
908
913
# setup store
@@ -999,7 +1004,63 @@ def test_cache_values_with_max_size(self):
999
1004
assert 4 == cache .hits
1000
1005
assert 2 == cache .misses
1001
1006
1002
- # TODO test key caching
1007
+ def test_cache_keys (self ):
1008
+
1009
+ # setup
1010
+ store = CountingDict ()
1011
+ store ['foo' ] = b'xxx'
1012
+ store ['bar' ] = b'yyy'
1013
+ assert 0 == store .counter ['__contains__' , 'foo' ]
1014
+ assert 0 == store .counter ['__iter__' ]
1015
+ assert 0 == store .counter ['keys' ]
1016
+ cache = LRUStoreCache (store , max_size = None )
1017
+
1018
+ # keys should be cached on first call
1019
+ keys = sorted (cache .keys ())
1020
+ assert keys == ['bar' , 'foo' ]
1021
+ assert 1 == store .counter ['keys' ]
1022
+ # keys should now be cached
1023
+ assert keys == sorted (cache .keys ())
1024
+ assert 1 == store .counter ['keys' ]
1025
+ assert 'foo' in cache
1026
+ assert 0 == store .counter ['__contains__' , 'foo' ]
1027
+ assert keys == sorted (cache )
1028
+ assert 0 == store .counter ['__iter__' ]
1029
+ assert 1 == store .counter ['keys' ]
1030
+
1031
+ # cache should be cleared if store is modified - crude but simple for now
1032
+ cache ['baz' ] = b'zzz'
1033
+ keys = sorted (cache .keys ())
1034
+ assert keys == ['bar' , 'baz' , 'foo' ]
1035
+ assert 2 == store .counter ['keys' ]
1036
+ # keys should now be cached
1037
+ assert keys == sorted (cache .keys ())
1038
+ assert 2 == store .counter ['keys' ]
1039
+
1040
+ # manually clear keys
1041
+ cache .clear_keys ()
1042
+ keys = sorted (cache .keys ())
1043
+ assert keys == ['bar' , 'baz' , 'foo' ]
1044
+ assert 3 == store .counter ['keys' ]
1045
+ assert 0 == store .counter ['__contains__' , 'foo' ]
1046
+ assert 0 == store .counter ['__iter__' ]
1047
+ cache .clear_keys ()
1048
+ keys = sorted (cache )
1049
+ assert keys == ['bar' , 'baz' , 'foo' ]
1050
+ assert 4 == store .counter ['keys' ]
1051
+ assert 0 == store .counter ['__contains__' , 'foo' ]
1052
+ assert 0 == store .counter ['__iter__' ]
1053
+ cache .clear_keys ()
1054
+ assert 'foo' in cache
1055
+ assert 5 == store .counter ['keys' ]
1056
+ assert 0 == store .counter ['__contains__' , 'foo' ]
1057
+ assert 0 == store .counter ['__iter__' ]
1058
+
1059
+ # check these would get counted if called directly
1060
+ assert 'foo' in store
1061
+ assert 1 == store .counter ['__contains__' , 'foo' ]
1062
+ assert keys == sorted (store )
1063
+ assert 1 == store .counter ['__iter__' ]
1003
1064
1004
1065
1005
1066
def test_getsize ():
0 commit comments