From 73893218a52d341b25fda98eea7d97638b70e0f9 Mon Sep 17 00:00:00 2001 From: Artem Vasenin Date: Mon, 27 May 2024 15:08:24 +0100 Subject: [PATCH] fix(core.py): fix clear() and add more tests --- class_cache/backends.py | 3 --- class_cache/core.py | 8 +++++- tests/test_core.py | 60 +++++++++++++++++++++++++++++++++++------ 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/class_cache/backends.py b/class_cache/backends.py index 2b0c5ab..da9a143 100644 --- a/class_cache/backends.py +++ b/class_cache/backends.py @@ -49,9 +49,6 @@ def __init__(self, id_: str | int, target_block_size=1024**2) -> None: self._dir.mkdir(exist_ok=True, parents=True) self._target_block_size = target_block_size - def __hash__(self): - return hash(self.id) - def _get_key_hash(self, key: KeyType) -> str: return f"{consistent_hash(key):x}" diff --git a/class_cache/core.py b/class_cache/core.py index b6a17bf..651b699 100644 --- a/class_cache/core.py +++ b/class_cache/core.py @@ -46,8 +46,14 @@ def __delitem__(self, key: KeyType) -> None: def write(self) -> None: """Write values to backend""" self._backend.set_many((key, self._data[key]) for key in self._to_write) - self._to_write = set() self._backend.del_many(self._to_delete) + self._to_write = set() + self._to_delete = set() + + def clear(self) -> None: + self._backend.clear() + self._data = {} + self._to_write = set() self._to_delete = set() diff --git a/tests/test_core.py b/tests/test_core.py index f222da3..61d3e8d 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,7 +1,9 @@ + from class_cache import Cache, CacheWithDefault TEST_KEY = "class_cache.tests.core.key" TEST_VALUE = "class_cache.tests.core.value" +TEST_DICT = {1: "foo", "foo": "bar", (2, 3): [4, 5]} class CacheWithAttr(CacheWithDefault[str, str]): @@ -15,28 +17,36 @@ def _get_data(self, key: str) -> str: return self._name + key +# TODO: Change this into a fixture and set id based on request to allow running tests in parallel +def get_new_cache(id_: str = None, *, clear=True) -> Cache: + cache = Cache(id_) + if clear: + cache.clear() + return cache + + # TODO: Remake this into a test-suite to test different backends, not just default one def test_basic_cache(): - first = Cache() + first = get_new_cache() first[TEST_KEY] = TEST_VALUE first.write() del first - second = Cache() + second = get_new_cache(clear=False) assert TEST_KEY in second def test_cache_separate(): - base = Cache() + base = get_new_cache() base[TEST_KEY] = "base" base.write() - first = Cache("first") + first = get_new_cache("first") assert first.get(TEST_KEY) != "base" first[TEST_KEY] = "first" first.write() - second = Cache("second") + second = get_new_cache("second") assert second.get(TEST_KEY) != "first" second[TEST_KEY] = "second" second.write() @@ -47,17 +57,17 @@ def test_cache_separate(): def test_del_item(): - first = Cache() + first = get_new_cache() first[TEST_KEY] = TEST_VALUE first.write() del first - second = Cache() + second = get_new_cache(clear=False) del second[TEST_KEY] assert TEST_KEY not in second second.write() - third = Cache() + third = get_new_cache(clear=False) assert TEST_KEY not in third @@ -68,3 +78,37 @@ def test_attribute_cache(): second.clear() assert first["foo"] == "firstfoo" assert second["foo"] == "secondfoo" + + +def test_keys(): + cache = get_new_cache() + cache.update(TEST_DICT) + cache.write() + del cache + + read_cache = get_new_cache(clear=False) + assert set(read_cache.keys()) == set(TEST_DICT.keys()) + + +def test_values(): + cache = get_new_cache() + cache.update(TEST_DICT) + cache.write() + del cache + + read_cache = get_new_cache(clear=False) + read_values = list(read_cache.values()) + for value in TEST_DICT.values(): + read_values.remove(value) + assert not read_values + + +def test_items(): + cache = get_new_cache() + cache.update(TEST_DICT) + cache.write() + + del cache + + read_cache = get_new_cache(clear=False) + assert dict(read_cache.items()) == TEST_DICT