Debian Codegraph @

tests/test_base_cache.py

raw
1 from datetime import timedelta
2
3 import pytest
4
5 from cachelib import BaseCache
6
7
8 @pytest.fixture(autouse=True)
9 def cache_factory(request):
10 def _factory(self, *args, **kwargs):
11 return BaseCache(*args, **kwargs)
12
13 request.cls.cache_factory = _factory
14
15
16 class TestBaseCache:
17 def test_get(self):
18 cache = self.cache_factory()
19 assert cache.get("bacon") is None
20
21 def test_delete(self):
22 cache = self.cache_factory()
23 assert cache.delete("eggs")
24
25 def test_get_many(self):
26 cache = self.cache_factory()
27 keys = ["bacon", "spam", "eggs"]
28 expected = [None] * 3
29 assert cache.get_many(*keys) == expected
30
31 def test_get_dict(self):
32 cache = self.cache_factory()
33 keys = ["bacon", "spam", "eggs"]
34 expected = dict.fromkeys(keys, None)
35 assert cache.get_dict(*keys) == expected
36
37 def test_set(self):
38 cache = self.cache_factory()
39 assert cache.set("sausage", "tomato")
40
41 def test_add(self):
42 cache = self.cache_factory()
43 assert cache.add("baked", "beans")
44
45 def test_set_many(self):
46 cache = self.cache_factory()
47 keys = ["bacon", "spam", "eggs"]
48 mapping = dict.fromkeys(keys, None)
49 assert cache.set_many(mapping) == keys
50
51 def test_delete_many(self):
52 cache = self.cache_factory()
53 keys = ["bacon", "spam", "eggs"]
54 assert cache.delete_many(*keys) == keys
55
56 def test_delete_many_raise_errors(self):
57 class FailingDeleteCache(BaseCache):
58 """Simulates a backend where deletes fail and keys remain"""
59
60 def delete(self, key):
61 return False
62
63 def has(self, key):
64 return True
65
66 cache = FailingDeleteCache(ignore_delete_many_errors=False)
67 with pytest.raises(RuntimeError, match="Failed to delete keys"):
68 cache.delete_many("bacon", "spam")
69
70 cache = FailingDeleteCache(ignore_delete_many_errors=True)
71 assert cache.delete_many("bacon", "spam") == []
72
73 def test_has(self):
74 cache = self.cache_factory()
75 with pytest.raises(NotImplementedError):
76 cache.has("lobster")
77
78 def test_clear(self):
79 cache = self.cache_factory()
80 assert cache.clear()
81
82 def test_inc(self):
83 cache = self.cache_factory()
84 assert cache.inc("crevettes", delta=10) == 10
85
86 def test_dec(self):
87 cache = self.cache_factory()
88 assert cache.dec("truffle", delta=10) == -10
89
90 @pytest.mark.parametrize(
91 "default_timeout,input_timeout,expected",
92 [
93 (42, None, 42), # None falls back to default
94 (300, 0, 0), # explicit zero stays zero (permanent)
95 (300, 60, 60), # explicit value is returned as-is
96 (0, None, 0), # default_timeout=0 means permanently cached by default
97 (300, timedelta(seconds=60), 60), # timedelta is converted to seconds
98 (300, timedelta(minutes=2), 120),
99 (300, timedelta(days=1), 86400),
100 (300, timedelta(), 0), # a zero timedelta is permanent, like 0
101 (300, timedelta(milliseconds=500), 1), # sub-second timeouts round up
102 (300, timedelta(seconds=1, milliseconds=200), 2),
103 (timedelta(minutes=1), None, 60), # timedelta default
104 (timedelta(minutes=1), 30, 30), # explicit value beats timedelta default
105 ],
106 )
107 def test_normalize_timeout(self, default_timeout, input_timeout, expected):
108 cache = BaseCache(default_timeout=default_timeout)
109 assert cache._normalize_timeout(input_timeout) == expected
110
111 def test_default_timeout_timedelta_is_stored_as_seconds(self):
112 cache = BaseCache(default_timeout=timedelta(minutes=5))
113 assert cache.default_timeout == 300
114
115 @pytest.mark.parametrize(
116 "input_timeout,expected",
117 [
118 (0.1, 1), # subsecond floats round up, not down to "never expires"
119 (5.0, 5),
120 (5.5, 6),
121 ],
122 )
123 def test_float_timeout_is_deprecated_and_rounded_up(self, input_timeout, expected):
124 cache = self.cache_factory()
125 with pytest.warns(DeprecationWarning, match="Float timeouts are deprecated"):
126 assert cache._normalize_timeout(input_timeout) == expected
127
128 def test_float_default_timeout_is_deprecated(self):
129 with pytest.warns(DeprecationWarning, match="Float timeouts are deprecated"):
130 cache = BaseCache(default_timeout=1.5)
131 assert cache.default_timeout == 2
132
133 def test_invalid_timeout_type_raises(self):
134 cache = self.cache_factory()
135 with pytest.raises(TypeError, match="timeout must be an int"):
136 cache._normalize_timeout("60")