Skip to content

Commit 53cc4d8

Browse files
committed
test(test_core): add tests for many* methods
1 parent 11d6024 commit 53cc4d8

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

class_cache/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def __delitem__(self, key: KeyType) -> None:
7575
if key in self._data:
7676
del self._lru_queue[key]
7777
self._data.pop(key, None)
78+
if key in self._to_write:
79+
self._to_write.remove(key)
7880
self._to_delete.add(key)
7981

8082
def write(self) -> None:

tests/test_core.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ def get_new_cache(id_: str = None, *, clear=True, **kwargs) -> Cache:
2424
return cache
2525

2626

27+
def get_cache_with_items(*args, **kwargs) -> tuple[Cache, dict[int, str]]:
28+
cache = get_new_cache(*args, **kwargs)
29+
30+
items = {i: str(i) for i in range(16)}
31+
32+
cache.set_many(items.items())
33+
return cache, items
34+
35+
2736
def test_basic_cache(test_key, test_value):
2837
first = get_new_cache()
2938
first[test_key] = test_value
@@ -142,4 +151,49 @@ def test_max_memory_usage():
142151
assert end_max_memory_usage - starting_max_memory < 3_000
143152

144153

154+
def test_equals():
155+
cache = get_new_cache()
156+
cache2 = get_new_cache(max_items=64)
157+
assert cache == cache2
158+
159+
cache3 = get_new_cache("Another")
160+
assert cache != cache3
161+
162+
163+
def test_contains_many():
164+
cache, items = get_cache_with_items()
165+
166+
keys = tuple(items.keys())
167+
in_keys, ins = zip(*cache.contains_many(keys))
168+
169+
assert in_keys == keys
170+
assert all(ins)
171+
172+
173+
def test_get_many():
174+
cache, items = get_cache_with_items()
175+
176+
keys = tuple(items.keys())
177+
in_keys, values = zip(*cache.get_many(keys))
178+
179+
assert in_keys == keys
180+
assert values == tuple(items.values())
181+
182+
183+
def test_set_many():
184+
cache, items = get_cache_with_items()
185+
186+
for key, value in items.items():
187+
assert value == cache[key]
188+
189+
190+
def test_del_many():
191+
cache, items = get_cache_with_items()
192+
193+
keys = tuple(items.keys())
194+
cache.del_many(keys)
195+
196+
assert len(cache) == 0
197+
198+
145199
# TODO: Add parallel test for cache as well (threading)

0 commit comments

Comments
 (0)