Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/utils/test_cache.py
8416 views
1
from typing import Any
2
3
import pytest
4
5
from polars._utils.cache import LRUCache
6
7
8
def test_init() -> None:
9
cache = LRUCache[str, float](maxsize=0)
10
assert cache.maxsize == 0
11
assert len(cache) == 0
12
13
with pytest.raises(ValueError, match="`maxsize` cannot be negative; found -100"):
14
LRUCache(maxsize=-100)
15
16
17
def test_init_maxsize_zero() -> None:
18
# weird, but not actually invalid
19
cache = LRUCache[str, list[int]](maxsize=0)
20
cache["key"] = [1, 2, 3]
21
assert len(cache) == 0
22
23
24
def test_setitem_getitem_delitem() -> None:
25
cache = LRUCache[int, str](maxsize=3)
26
cache[0] = "pear"
27
cache[1] = "apple"
28
cache[2] = "banana"
29
cache[3] = "cherry"
30
31
assert list(cache.keys()) == [1, 2, 3]
32
assert list(cache.values()) == ["apple", "banana", "cherry"]
33
assert list(cache.items()) == [(1, "apple"), (2, "banana"), (3, "cherry")]
34
assert len(cache) == 3
35
assert cache.maxsize == 3
36
37
del cache[2]
38
assert len(cache) == 2
39
assert cache.maxsize == 3
40
assert repr(cache) == "LRUCache({1: 'apple', 3: 'cherry'}, maxsize=3, currsize=2)"
41
42
43
def test_cache_access_updates_order() -> None:
44
cache1 = LRUCache[int, str](maxsize=3)
45
cache1[30] = "thirty"
46
cache1[20] = "twenty"
47
cache1[10] = "ten"
48
assert list(cache1.keys()) == [30, 20, 10]
49
50
cache1.get(20)
51
cache1.get(30)
52
assert list(cache1.keys()) == [10, 20, 30]
53
54
cache2 = LRUCache[str, tuple[int, int]](maxsize=2)
55
cache2["first"] = (1, 2)
56
cache2["second"] = (3, 4)
57
assert list(cache2.keys()) == ["first", "second"]
58
59
_ = cache2["first"]
60
assert list(cache2.keys()) == ["second", "first"]
61
62
63
def test_contains() -> None:
64
cache = LRUCache[str, float](maxsize=3)
65
cache["pi"] = 3.14159
66
cache["e"] = 2.71828
67
68
assert "pi" in cache
69
assert "e" in cache
70
assert "phi" not in cache
71
72
73
def test_getitem_keyerror() -> None:
74
cache = LRUCache[int, str](maxsize=3)
75
with pytest.raises(KeyError, match="999 not found in cache"):
76
cache[999]
77
78
79
def test_get_with_default() -> None:
80
cache = LRUCache[str, list[str]](maxsize=3)
81
cache["fruits"] = ["apple", "banana"]
82
83
assert cache.get("fruits") == ["apple", "banana"]
84
assert cache.get("vegetables") is None
85
assert cache.get("vegetables", ["carrot", "turnip"]) == ["carrot", "turnip"]
86
87
88
def test_iter_and_update() -> None:
89
cache = LRUCache[int, str](maxsize=4)
90
cache.update({100: "hundred", 200: "two hundred", 300: "three hundred"})
91
assert list(cache) == [100, 200, 300]
92
93
94
def test_maxsize_eviction() -> None:
95
cache = LRUCache[str, dict[str, Any]](maxsize=2)
96
cache["user1"] = {"name": "Alice", "age": 30}
97
cache["user2"] = {"name": "Bob", "age": 25}
98
cache["user3"] = {"name": "Charlie", "age": 35}
99
100
assert len(cache) == 2
101
assert "user1" not in cache
102
assert "user2" in cache
103
assert "user3" in cache
104
105
106
def test_maxsize_increase() -> None:
107
cache = LRUCache[str, int].fromkeys(4, keys=[f"k{n}" for n in range(6)], value=0)
108
assert len(cache) == 4
109
assert cache.maxsize == 4
110
assert list(cache.keys()) == ["k2", "k3", "k4", "k5"]
111
112
cache.maxsize = 6
113
cache.update(k6=1, k7=2, k8=3)
114
assert len(cache) == 6
115
assert cache.maxsize == 6
116
assert list(cache.keys()) == ["k3", "k4", "k5", "k6", "k7", "k8"]
117
118
119
def test_maxsize_decrease() -> None:
120
cache = LRUCache[int, list[int]](maxsize=4)
121
cache[1] = [1, 2]
122
cache[2] = [3, 4]
123
cache[3] = [5, 6]
124
cache[4] = [7, 8]
125
126
cache.maxsize = 2
127
assert len(cache) == 2
128
assert cache.maxsize == 2
129
assert list(cache.items()) == [(3, [5, 6]), (4, [7, 8])]
130
131
cache.maxsize = 0
132
assert len(cache) == 0
133
assert cache.maxsize == 0
134
assert list(cache.values()) == []
135
136
137
def test_pop() -> None:
138
cache = LRUCache[int, str](maxsize=4)
139
cache[42] = "answer"
140
cache[99] = "bottles"
141
142
value = cache.pop(42)
143
assert value == "answer"
144
assert 42 not in cache
145
assert len(cache) == 1
146
147
with pytest.raises(KeyError, match="404"):
148
cache.pop(404)
149
150
151
def test_popitem_setdefault() -> None:
152
cache = LRUCache[str, set[int]](maxsize=3)
153
cache["set1"] = {1, 2, 3}
154
cache["set2"] = {4, 5, 6}
155
cache["set3"] = {7, 8, 9}
156
157
key, value = cache.popitem()
158
assert key == "set1"
159
assert value == {1, 2, 3}
160
assert len(cache) == 2
161
162
res = cache.setdefault("set2", {10, 11, 12})
163
assert res == {4, 5, 6}
164
assert list(cache) == ["set3", "set2"]
165
166
res = cache.setdefault("set4", {10, 11, 12})
167
assert res == {10, 11, 12}
168
assert list(cache) == ["set3", "set2", "set4"]
169
170
171
def test_popitem_empty_cache() -> None:
172
cache = LRUCache[str, bytes](maxsize=3)
173
with pytest.raises(KeyError):
174
cache.popitem()
175
176
177
def test_update_existing_key() -> None:
178
cache = LRUCache[int, float](maxsize=3)
179
cache.update([(1, 1.5), (2, 2.5)])
180
assert list(cache.keys()) == [1, 2]
181
182
cache[1] = 10.5
183
assert list(cache.keys()) == [2, 1]
184
assert cache[1] == 10.5
185
186
187
def test_update_existing_keys() -> None:
188
cache = LRUCache[str, float](maxsize=3)
189
cache["pi"] = 3.14
190
cache["e"] = 2.71
191
192
cache.update({"phi": 1.618, "pi": 3.14159})
193
assert tuple(cache.items()) == (("e", 2.71), ("phi", 1.618), ("pi", 3.14159))
194
195
196
def test_update_check_eviction() -> None:
197
cache = LRUCache[int, str](maxsize=2)
198
cache[1] = "first"
199
cache[2] = "second"
200
201
cache.update({3: "third", 4: "fourth"})
202
203
assert 1 not in cache
204
assert 2 not in cache
205
assert cache[3] == "third"
206
assert cache[4] == "fourth"
207
assert len(cache) == 2
208
209
210
def test_update_preserves_order() -> None:
211
cache = LRUCache[str, int](maxsize=3)
212
cache["a"] = 1
213
cache["b"] = 2
214
215
cache.update({"c": 3})
216
assert list(cache.keys()) == ["a", "b", "c"]
217
218
cache.update({"a": 10, "x": 4})
219
assert list(cache.items()) == [("c", 3), ("a", 10), ("x", 4)]
220
221