Skip to content

Commit

Permalink
tests: deque
Browse files Browse the repository at this point in the history
  • Loading branch information
phi-friday committed Nov 9, 2024
1 parent 55a22fc commit c8d0f56
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/tests/test_deque.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from __future__ import annotations

import pytest

from typed_diskcache import exception as te
from typed_diskcache.core.types import EvictionPolicy
from typed_diskcache.utils.deque import Deque

pytestmark = pytest.mark.anyio


@pytest.fixture
def deque(cache_directory):
deque = Deque(directory=cache_directory)
try:
yield deque
finally:
deque.cache.close()


@pytest.mark.parametrize(
("maxlen", "expected"), [(None, float("inf")), (1, 1), (10, 10)]
)
def test_attributes(cache_directory, maxlen, expected):
deque = Deque(directory=cache_directory, maxlen=maxlen)
assert deque.cache.directory == cache_directory
assert deque.maxlen == expected


@pytest.mark.parametrize("eviction_policy", [x for x in EvictionPolicy if x != "none"])
def test_eviction_warning(cache_directory, eviction_policy):
with pytest.warns(te.TypedDiskcacheWarning):
Deque(directory=cache_directory, eviction_policy=eviction_policy)


def test_set_maxlen(deque):
assert deque.maxlen == float("inf")
deque.extendleft("abcde")
deque.maxlen = 3
assert deque.maxlen == 3
assert list(deque) == ["c", "b", "a"]


def test_append(deque):
deque.append("a")
deque.append("b")
deque.append("c")
assert list(deque) == ["a", "b", "c"]
9 changes: 7 additions & 2 deletions src/typed_diskcache/utils/deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import operator as op
import shutil
import warnings
from collections.abc import Callable, Iterable, Iterator, MutableSequence, Sequence
from contextlib import suppress
from functools import partial
Expand Down Expand Up @@ -79,7 +80,11 @@ def __init__(
) -> None:
eviction_policy = kwargs.pop("eviction_policy", EvictionPolicy.NONE)
if eviction_policy != EvictionPolicy.NONE:
logger.warning("Deque eviction policy must be none")
warnings.warn(
"Deque eviction policy must be none",
te.TypedDiskcacheWarning,
stacklevel=2,
)

kwargs["eviction_policy"] = EvictionPolicy.NONE
self._cache = Cache(directory=directory, **kwargs)
Expand Down Expand Up @@ -116,7 +121,7 @@ def main() -> None:
deque.extendleft("abcde")
deque.maxlen = 3
print(list(deque))
# ['c', 'd', 'e']
# ['c', 'b', 'a']
```
"""
self._maxlen = value
Expand Down

0 comments on commit c8d0f56

Please sign in to comment.