1
mirror of https://github.com/streamlink/streamlink synced 2024-09-27 11:10:27 +02:00
streamlink/tests/utils/test_cache.py
bastimeyer 6325c74e68 chore: remove unnecessary collection.OrderedDict
- Replace collection.OrderedDict with builtins.dict where possible:
  Python 3.7+ ensures the correct order in builtins.dict objects and is
  no longer an implementation detail of cpython.
- Fix OrderedDict type annotation in streamlink.utils.cache.LRUCache
- Add unit test for streamlink.utils.cache.LRUCache
2022-04-17 11:06:29 -07:00

29 lines
1.2 KiB
Python

from streamlink.utils.cache import LRUCache
def test_lru_cache():
cache = LRUCache(num=3)
assert cache.get("foo") is None, "Getter returns None for unknown items"
cache.set("foo", "FOO")
assert list(cache.cache.items()) == [("foo", "FOO")], "Setter adds new items"
assert cache.get("foo") == "FOO", "Getter returns correct value of known items"
cache.set("bar", "BAR")
cache.set("baz", "BAZ")
cache.set("qux", "QUX")
assert list(cache.cache.items()) == [("bar", "BAR"), ("baz", "BAZ"), ("qux", "QUX")], "Setter respects max queue size"
cache.get("bar")
assert list(cache.cache.items()) == [("baz", "BAZ"), ("qux", "QUX"), ("bar", "BAR")], "Getter moves known items to the end"
cache.get("unknown")
assert list(cache.cache.items()) == [("baz", "BAZ"), ("qux", "QUX"), ("bar", "BAR")], "Getter keeps order on unknown items"
cache.set("foo", "FOO")
assert list(cache.cache.items()) == [("qux", "QUX"), ("bar", "BAR"), ("foo", "FOO")], "Setter moves new items to the end"
cache.set("qux", "QUUX")
assert list(cache.cache.items()) == [("bar", "BAR"), ("foo", "FOO"), ("qux", "QUUX")], "Setter moves known items to the end"