1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
# 3rd party
import pytest
# this package
from apeye.cache import Cache
@pytest.fixture(scope="session")
def testing_cache():
cache = Cache("testing_apeye")
assert cache.clear()
yield cache
assert cache.clear()
@pytest.mark.parametrize("run_number", [1, 2])
def test_cache(testing_cache, capsys, run_number):
@testing_cache
def cached_function(arg1: int, arg2: float, arg3: str):
print("Running")
return (arg1**int(arg2)) * arg3
assert not (testing_cache.cache_dir / "cached_function.json").is_file()
assert cached_function(2, 5.5, '☃') == '☃' * 32
assert (testing_cache.cache_dir / "cached_function.json").is_file()
for i in range(10):
assert cached_function(2, 5.5, '☃') == '☃' * 32
assert cached_function(2, 5.8, '☃') == '☃' * 32
captured = capsys.readouterr()
assert captured.out == "Running\nRunning\n"
assert testing_cache.clear(cached_function)
assert not (testing_cache.cache_dir / "cached_function.json").is_file()
assert testing_cache.cache_dir.is_dir()
assert cached_function(2, 5.5, '☃') == '☃' * 32
capsys.readouterr() # prevents the above call polluting stdout
old_id = id(cached_function)
@testing_cache # type: ignore[no-redef]
def cached_function(arg1: int, arg2: float, arg3: str):
print("Running 2nd function")
return (arg1**int(arg2)) * arg3
assert id(cached_function) != old_id
assert cached_function(2, 5.5, '☃') == '☃' * 32
assert (testing_cache.cache_dir / "cached_function.json").is_file()
assert testing_cache.clear(cached_function)
captured = capsys.readouterr()
# if the cache wasn't working this would be "Running 2nd function\n"
assert captured.out == ''
|