Skip to content

Commit 72aab26

Browse files
committed
Add tests
1 parent 2b85831 commit 72aab26

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

src/stac_auth_proxy/utils/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def get_value_by_path(obj: dict, path: str, default: Any = None) -> Any:
8989
for key in path.split("."):
9090
if obj is None:
9191
return default
92-
obj = obj.get(key, None)
92+
obj = obj.get(key, default)
9393
return obj
9494
except (AttributeError, KeyError, TypeError):
9595
return default

tests/test_cache.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""Tests for cache utilities."""
2+
3+
from unittest.mock import patch
4+
5+
import pytest
6+
7+
from stac_auth_proxy.utils.cache import MemoryCache, get_value_by_path
8+
9+
10+
def test_memory_cache_basic_operations():
11+
"""Test basic cache operations."""
12+
cache = MemoryCache(ttl=5.0) # 5 second TTL
13+
key = "test_key"
14+
value = "test_value"
15+
16+
# Test setting and getting a value
17+
cache[key] = value
18+
assert cache[key] == value
19+
assert key in cache
20+
21+
# Test getting non-existent key
22+
with pytest.raises(KeyError):
23+
_ = cache["non_existent"]
24+
25+
# Test get() method
26+
assert cache.get(key) == value
27+
assert cache.get("non_existent") is None
28+
29+
30+
def test_memory_cache_expiration():
31+
"""Test cache expiration."""
32+
cache = MemoryCache(ttl=5.0) # 5 second TTL
33+
key = "test_key"
34+
value = "test_value"
35+
36+
# Set initial time
37+
with patch("stac_auth_proxy.utils.cache.time") as mock_time:
38+
mock_time.return_value = 1000.0
39+
cache[key] = value
40+
assert cache[key] == value
41+
42+
# Advance time past TTL
43+
mock_time.return_value = 1006.0 # 6 seconds later
44+
45+
# Test expired key
46+
with pytest.raises(MemoryCache.Expired):
47+
cache[key]
48+
49+
# Test contains after expiration
50+
assert key not in cache
51+
52+
53+
def test_memory_cache_pruning():
54+
"""Test cache pruning."""
55+
cache = MemoryCache(ttl=5.0) # 5 second TTL
56+
key1 = "key1"
57+
key2 = "key2"
58+
value = "test_value"
59+
60+
with patch("stac_auth_proxy.utils.cache.time") as mock_time:
61+
# Set initial time
62+
mock_time.return_value = 1000.0
63+
cache[key1] = value
64+
cache[key2] = value
65+
66+
# Advance time past TTL
67+
mock_time.return_value = 1006.0 # 6 seconds later
68+
69+
# Force pruning by adding a new item
70+
cache["key3"] = value
71+
72+
# Check that expired items were pruned
73+
assert key1 not in cache
74+
assert key2 not in cache
75+
assert "key3" in cache
76+
77+
78+
def test_memory_cache_key_str():
79+
"""Test key string representation."""
80+
cache = MemoryCache()
81+
82+
# Test short key
83+
short_key = "123"
84+
assert cache._key_str(short_key) == short_key
85+
86+
# Test long key
87+
long_key = "1234567890"
88+
assert cache._key_str(long_key) == "123456789..."
89+
90+
91+
@pytest.mark.parametrize(
92+
"obj, path, default, expected",
93+
[
94+
# Basic path
95+
({"a": {"b": 1}}, "a.b", None, 1),
96+
# Nested path
97+
({"a": {"b": {"c": 2}}}, "a.b.c", None, 2),
98+
# Non-existent path
99+
({"a": {"b": 1}}, "a.c", None, None),
100+
# Default value
101+
({"a": {"b": 1}}, "a.c", "default", "default"),
102+
# None in path
103+
({"a": None}, "a.b", None, None),
104+
# Empty path
105+
({"a": 1}, "", None, None),
106+
# Complex object
107+
({"a": {"b": [1, 2, 3]}}, "a.b", None, [1, 2, 3]),
108+
],
109+
)
110+
def test_get_value_by_path(obj, path, default, expected):
111+
"""Test getting values by path."""
112+
assert get_value_by_path(obj, path, default) == expected

0 commit comments

Comments
 (0)