-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_memory_cache.py
More file actions
65 lines (44 loc) · 1.69 KB
/
Copy pathtest_memory_cache.py
File metadata and controls
65 lines (44 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Tests for MemoryCache TTL behavior."""
from __future__ import annotations
import time
from unittest.mock import patch
from allow2_service.cache import MemoryCache
def test_set_and_get() -> None:
cache = MemoryCache()
cache.set("key1", "value1", ttl=60)
assert cache.get("key1") == "value1"
def test_get_returns_none_for_missing() -> None:
cache = MemoryCache()
assert cache.get("nonexistent") is None
def test_delete() -> None:
cache = MemoryCache()
cache.set("key1", "value1")
cache.delete("key1")
assert cache.get("key1") is None
def test_delete_nonexistent_does_not_raise() -> None:
cache = MemoryCache()
cache.delete("nonexistent") # should not raise
def test_expired_entry_returns_none() -> None:
cache = MemoryCache()
# Store with TTL 1 second
cache.set("key1", "value1", ttl=1)
# Verify it's there initially
assert cache.get("key1") == "value1"
# Mock time to be in the future
with patch("allow2_service.cache.memory_cache.time") as mock_time:
# First call is for the get check
mock_time.time.return_value = time.time() + 10
assert cache.get("key1") is None
def test_overwrite_existing_key() -> None:
cache = MemoryCache()
cache.set("key1", "value1")
cache.set("key1", "value2")
assert cache.get("key1") == "value2"
def test_different_ttls() -> None:
cache = MemoryCache()
cache.set("short", "short-value", ttl=1)
cache.set("long", "long-value", ttl=3600)
with patch("allow2_service.cache.memory_cache.time") as mock_time:
mock_time.time.return_value = time.time() + 10
assert cache.get("short") is None
assert cache.get("long") == "long-value"