Files
fhi-statistikk-mcp/tests/test_cache.py
Torbjørn Lindahl 3b3721091b
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
final implementation
2026-03-27 23:38:52 +01:00

49 lines
997 B
Python

"""Tests for cache module."""
import time
from fhi_statistikk_mcp.cache import TTLCache
def test_set_and_get():
cache = TTLCache()
cache.set("key", "value", 60)
assert cache.get("key") == "value"
def test_get_missing_key():
cache = TTLCache()
assert cache.get("nonexistent") is None
def test_expiry():
cache = TTLCache()
cache.set("key", "value", 0.1)
time.sleep(0.15)
assert cache.get("key") is None
def test_clear():
cache = TTLCache()
cache.set("a", 1, 60)
cache.set("b", 2, 60)
cache.clear()
assert cache.get("a") is None
assert cache.get("b") is None
def test_overwrite():
cache = TTLCache()
cache.set("key", "old", 60)
cache.set("key", "new", 60)
assert cache.get("key") == "new"
def test_different_ttls():
cache = TTLCache()
cache.set("short", "value", 0.1)
cache.set("long", "value", 60)
time.sleep(0.15)
assert cache.get("short") is None
assert cache.get("long") == "value"