pyinfra/tests/lru_test.py
Matthias Bisping 892a803726 Refactoring
Replace custom storage caching logic with LRU decorator
2023-03-28 14:43:04 +02:00

49 lines
937 B
Python

from functools import lru_cache
import pytest
def func(callback):
return callback()
@pytest.fixture()
def fn(maxsize):
return lru_cache(maxsize)(func)
@pytest.fixture(params=[1, 2, 5])
def maxsize(request):
return request.param
class Callback:
def __init__(self, x):
self.initial_x = x
self.x = x
def __call__(self, *args, **kwargs):
self.x += 1
return self.x
def __hash__(self):
return hash(self.initial_x)
def test_adding_to_cache_within_maxsize_does_not_overwrite(fn, maxsize):
c = Callback(0)
for i in range(maxsize):
assert fn(c) == 1
assert fn(c) == 1
def test_adding_to_cache_more_than_maxsize_does_overwrite(fn, maxsize):
callbacks = [Callback(i) for i in range(maxsize)]
for i in range(maxsize):
assert fn(callbacks[i]) == i + 1
assert fn(Callback(maxsize)) == maxsize + 1
assert fn(callbacks[0]) == 2