feat: drop `cachetools` dependency in favor of simple local implement… · googleapis/google-auth-library-python@5c07e1c
1+from google.auth._cache import LRUCache
2+3+4+def test_lru_cache():
5+"""Test the LRUCache for generally expected functionality and ordering."""
6+lru_cache = LRUCache(2)
7+lru_cache["a"] = 1
8+lru_cache["b"] = 2
9+assert lru_cache["a"] == 1
10+lru_cache["c"] = 3
11+assert "b" not in lru_cache
12+assert lru_cache["a"] == 1
13+assert lru_cache["c"] == 3
14+lru_cache["d"] = 4
15+assert "a" not in lru_cache
16+assert lru_cache["c"] == 3
17+assert lru_cache["d"] == 4
18+19+20+def test_zero_size_lru_cache():
21+"""Confirm the LRUCache handles zero-size correctly."""
22+lru_cache = LRUCache(0)
23+lru_cache["a"] = 1
24+assert "a" not in lru_cache
25+26+27+def test_lru_cache_get_updates_lru():
28+"""Confirm the LRUCache handles get calls correctly."""
29+lru_cache = LRUCache(2)
30+lru_cache["a"] = 1
31+lru_cache["b"] = 2
32+33+# Access "a" via get(), making it MRU.
34+assert lru_cache.get("a") == 1
35+36+# Add "c", which should evict "b" (LRU), not "a".
37+lru_cache["c"] = 3
38+39+assert "a" in lru_cache
40+assert "b" not in lru_cache
41+assert "c" in lru_cache
42+43+44+def test_lru_cache_get_missing():
45+"""Confirm the LRUCache handles missing keys correctly."""
46+lru_cache = LRUCache(2)
47+assert lru_cache.get("missing") is None
48+assert lru_cache.get("missing", "default") == "default"
49+50+51+def test_lru_cache_clear():
52+"""Confirm the LRUCache clears the cache properly."""
53+lru_cache = LRUCache(2)
54+lru_cache["a"] = 1
55+lru_cache["b"] = 2
56+assert len(lru_cache) == 2
57+58+lru_cache.clear()
59+assert len(lru_cache) == 0
60+assert "a" not in lru_cache
61+assert "b" not in lru_cache
62+# Ensure internal order is also cleared
63+assert len(lru_cache._order) == 0
64+65+66+def test_lru_cache_delitem():
67+"""Confirm the LRUCache deletes individual items properly."""
68+lru_cache = LRUCache(2)
69+lru_cache["a"] = 1
70+lru_cache["b"] = 2
71+72+del lru_cache["a"]
73+assert "a" not in lru_cache
74+assert len(lru_cache) == 1
75+# Ensure it's removed from internal order
76+assert "a" not in lru_cache._order
77+78+# Test that we can continue using the cache
79+lru_cache["c"] = 3
80+assert "c" in lru_cache
81+assert "b" in lru_cache
82+assert len(lru_cache) == 2