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
|
from google.auth._cache import LRUCache
def test_lru_cache():
"""Test the LRUCache for generally expected functionality and ordering."""
lru_cache = LRUCache(2)
lru_cache["a"] = 1
lru_cache["b"] = 2
assert lru_cache["a"] == 1
lru_cache["c"] = 3
assert "b" not in lru_cache
assert lru_cache["a"] == 1
assert lru_cache["c"] == 3
lru_cache["d"] = 4
assert "a" not in lru_cache
assert lru_cache["c"] == 3
assert lru_cache["d"] == 4
def test_zero_size_lru_cache():
"""Confirm the LRUCache handles zero-size correctly."""
lru_cache = LRUCache(0)
lru_cache["a"] = 1
assert "a" not in lru_cache
def test_lru_cache_get_updates_lru():
"""Confirm the LRUCache handles get calls correctly."""
lru_cache = LRUCache(2)
lru_cache["a"] = 1
lru_cache["b"] = 2
# Access "a" via get(), making it MRU.
assert lru_cache.get("a") == 1
# Add "c", which should evict "b" (LRU), not "a".
lru_cache["c"] = 3
assert "a" in lru_cache
assert "b" not in lru_cache
assert "c" in lru_cache
def test_lru_cache_get_missing():
"""Confirm the LRUCache handles missing keys correctly."""
lru_cache = LRUCache(2)
assert lru_cache.get("missing") is None
assert lru_cache.get("missing", "default") == "default"
def test_lru_cache_clear():
"""Confirm the LRUCache clears the cache properly."""
lru_cache = LRUCache(2)
lru_cache["a"] = 1
lru_cache["b"] = 2
assert len(lru_cache) == 2
lru_cache.clear()
assert len(lru_cache) == 0
assert "a" not in lru_cache
assert "b" not in lru_cache
# Ensure internal order is also cleared
assert len(lru_cache._order) == 0
def test_lru_cache_delitem():
"""Confirm the LRUCache deletes individual items properly."""
lru_cache = LRUCache(2)
lru_cache["a"] = 1
lru_cache["b"] = 2
del lru_cache["a"]
assert "a" not in lru_cache
assert len(lru_cache) == 1
# Ensure it's removed from internal order
assert "a" not in lru_cache._order
# Test that we can continue using the cache
lru_cache["c"] = 3
assert "c" in lru_cache
assert "b" in lru_cache
assert len(lru_cache) == 2
|