Skip to content

Commit

Permalink
fix(core.py): fix clear() and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rizhiy committed May 27, 2024
1 parent 88ba39b commit 7389321
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
3 changes: 0 additions & 3 deletions class_cache/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
8 changes: 7 additions & 1 deletion class_cache/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
60 changes: 52 additions & 8 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -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]):
Expand All @@ -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()
Expand All @@ -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


Expand All @@ -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

0 comments on commit 7389321

Please sign in to comment.