rudimentary cache decorator

This commit is contained in:
2024-02-27 16:45:16 +01:00
parent 090100290a
commit 4ef82397bc
3 changed files with 68 additions and 5 deletions

View File

@@ -1,3 +1,4 @@
import scgenerator.cache as scc
from scgenerator.cache import Cache
@@ -57,3 +58,31 @@ def test_toml():
cache1.delete()
assert not cache1.dir.exists()
assert not cache3.dir.exists()
def test_decorator():
cache = Cache("Test")
cache.delete()
call_count = 0
@cache()
def func(x: str) -> str:
nonlocal call_count
call_count += 1
return x + x
@cache(lambda li: f"{li[0]}-{li[-1]} {len(li)}")
def func2(some_list: list):
nonlocal call_count
call_count += 1
return sum(some_list)
assert func("hello") == "hellohello"
assert func2([0, 1, 2, 80]) == 83
assert func2([0, 1, 2, 80]) == 83
assert (scc.CACHE_DIR / "Test" / "test_decorator.<locals>.func hello").exists()
assert (scc.CACHE_DIR / "Test" / "test_decorator.<locals>.func2 0-80 4").exists()
assert call_count == 2
cache.delete()