2015-08-14 10:59:26 +00:00
|
|
|
from tempfile import TemporaryFile
|
2013-06-24 20:41:05 +00:00
|
|
|
|
2016-05-30 23:18:03 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-08-13 20:02:04 +00:00
|
|
|
from ..helpers.lrucache import LRUCache
|
2016-05-30 23:18:03 +00:00
|
|
|
|
2013-06-24 20:41:05 +00:00
|
|
|
|
2015-08-14 13:44:32 +00:00
|
|
|
class TestLRUCache:
|
|
|
|
def test_lrucache(self):
|
2023-06-10 16:16:40 +00:00
|
|
|
c = LRUCache(2)
|
2015-08-14 13:44:32 +00:00
|
|
|
assert len(c) == 0
|
|
|
|
assert c.items() == set()
|
2013-06-24 20:41:05 +00:00
|
|
|
for i, x in enumerate("abc"):
|
|
|
|
c[x] = i
|
2015-08-14 13:44:32 +00:00
|
|
|
assert len(c) == 2
|
2022-02-27 18:31:33 +00:00
|
|
|
assert c.items() == {("b", 1), ("c", 2)}
|
2015-08-14 13:44:32 +00:00
|
|
|
assert "a" not in c
|
|
|
|
assert "b" in c
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
c["a"]
|
2017-06-14 10:15:46 +00:00
|
|
|
assert c.get("a") is None
|
|
|
|
assert c.get("a", "foo") == "foo"
|
2015-08-14 13:44:32 +00:00
|
|
|
assert c["b"] == 1
|
2017-06-14 10:15:46 +00:00
|
|
|
assert c.get("b") == 1
|
2015-08-14 13:44:32 +00:00
|
|
|
assert c["c"] == 2
|
2013-06-24 20:41:05 +00:00
|
|
|
c["d"] = 3
|
2015-08-14 13:44:32 +00:00
|
|
|
assert len(c) == 2
|
|
|
|
assert c["c"] == 2
|
|
|
|
assert c["d"] == 3
|
2013-06-24 20:41:05 +00:00
|
|
|
del c["c"]
|
2015-08-14 13:44:32 +00:00
|
|
|
assert len(c) == 1
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
c["c"]
|
|
|
|
assert c["d"] == 3
|
2015-08-14 10:59:26 +00:00
|
|
|
c.clear()
|
2015-08-14 13:44:32 +00:00
|
|
|
assert c.items() == set()
|
2015-08-14 10:59:26 +00:00
|
|
|
|
|
|
|
def test_dispose(self):
|
|
|
|
c = LRUCache(2, dispose=lambda f: f.close())
|
|
|
|
f1 = TemporaryFile()
|
|
|
|
f2 = TemporaryFile()
|
|
|
|
f3 = TemporaryFile()
|
|
|
|
c[1] = f1
|
|
|
|
c[2] = f2
|
2015-08-14 13:44:32 +00:00
|
|
|
assert not f2.closed
|
2015-08-14 10:59:26 +00:00
|
|
|
c[3] = f3
|
2015-08-14 13:44:32 +00:00
|
|
|
assert 1 not in c
|
|
|
|
assert f1.closed
|
|
|
|
assert 2 in c
|
|
|
|
assert not f2.closed
|
2015-08-14 10:59:26 +00:00
|
|
|
del c[2]
|
2015-08-14 13:44:32 +00:00
|
|
|
assert 2 not in c
|
|
|
|
assert f2.closed
|
2015-08-14 10:59:26 +00:00
|
|
|
c.clear()
|
2015-08-15 14:15:10 +00:00
|
|
|
assert c.items() == set()
|
2015-08-14 13:44:32 +00:00
|
|
|
assert f3.closed
|