-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_cache.py
More file actions
88 lines (58 loc) · 1.95 KB
/
test_cache.py
File metadata and controls
88 lines (58 loc) · 1.95 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Tests for caching functionality."""
import time
import pytest
from tradingview_mcp.api.cache import ResponseCache
def test_cache_set_and_get():
"""Test basic cache set and get operations."""
cache = ResponseCache()
cache.set("test_key", "test_value", ttl=60)
assert cache.get("test_key") == "test_value"
def test_cache_miss():
"""Test cache miss returns None."""
cache = ResponseCache()
assert cache.get("nonexistent_key") is None
def test_cache_expiration():
"""Test that cache entries expire after TTL."""
cache = ResponseCache()
cache.set("expire_key", "expire_value", ttl=1)
assert cache.get("expire_key") == "expire_value"
# Wait for expiration
time.sleep(1.1)
assert cache.get("expire_key") is None
def test_cache_invalidate():
"""Test cache invalidation."""
cache = ResponseCache()
cache.set("key", "value", ttl=60)
assert cache.get("key") == "value"
cache.invalidate("key")
assert cache.get("key") is None
def test_cache_clear():
"""Test clearing entire cache."""
cache = ResponseCache()
cache.set("key1", "value1", ttl=60)
cache.set("key2", "value2", ttl=60)
cache.clear()
assert cache.get("key1") is None
assert cache.get("key2") is None
def test_cache_stats():
"""Test cache statistics."""
cache = ResponseCache()
# Generate some hits and misses
cache.set("key", "value", ttl=60)
cache.get("key") # Hit
cache.get("key") # Hit
cache.get("nonexistent") # Miss
stats = cache.get_stats()
assert stats["hits"] == 2
assert stats["misses"] == 1
assert stats["size"] == 1
def test_cache_cleanup():
"""Test cleanup of expired entries."""
cache = ResponseCache()
cache.set("key1", "value1", ttl=1)
cache.set("key2", "value2", ttl=60)
time.sleep(1.1)
removed = cache.cleanup_expired()
assert removed == 1
assert cache.get("key1") is None
assert cache.get("key2") == "value2"