Download repos/ttlcache/tests/test_cache.py from noodcon/SWE-DockerLess: direct link, hf CLI and curl.
- Browser
- Download file 2.35 kB
-
https://huggingface.co/datasets/noodcon/SWE-DockerLess/resolve/main/repos/ttlcache/tests/test_cache.py
- Command line
-
hf download hf://datasets/noodcon/SWE-DockerLess/repos/ttlcache/tests/test_cache.py
-
curl -L -o test_cache.py https://huggingface.co/datasets/noodcon/SWE-DockerLess/resolve/main/repos/ttlcache/tests/test_cache.py
2.35 kB
| import pytest | |
| from ttlcache import FakeClock, TTLCache | |
| def make(clock, maxsize=3, ttl=10.0): | |
| return TTLCache(maxsize=maxsize, ttl=ttl, clock=clock) | |
| def test_set_and_get(): | |
| clock = FakeClock() | |
| cache = make(clock) | |
| cache["a"] = 1 | |
| assert cache["a"] == 1 | |
| assert cache.get("a") == 1 | |
| def test_missing_key_returns_default(): | |
| cache = make(FakeClock()) | |
| assert cache.get("nope") is None | |
| assert cache.get("nope", 7) == 7 | |
| def test_getitem_raises_for_missing_key(): | |
| cache = make(FakeClock()) | |
| with pytest.raises(KeyError): | |
| cache["nope"] | |
| def test_entry_expires(): | |
| clock = FakeClock() | |
| cache = make(clock) | |
| cache["a"] = 1 | |
| clock.advance(10) | |
| assert "a" not in cache | |
| assert cache.get("a") is None | |
| with pytest.raises(KeyError): | |
| cache["a"] | |
| def test_entry_survives_before_deadline(): | |
| clock = FakeClock() | |
| cache = make(clock) | |
| cache["a"] = 1 | |
| clock.advance(9.5) | |
| assert cache["a"] == 1 | |
| def test_eviction_order_on_insert(): | |
| clock = FakeClock() | |
| cache = make(clock, maxsize=2) | |
| cache["a"] = 1 | |
| cache["b"] = 2 | |
| cache["c"] = 3 | |
| assert "a" not in cache | |
| assert "b" in cache and "c" in cache | |
| def test_overwrite_updates_value(): | |
| clock = FakeClock() | |
| cache = make(clock) | |
| cache["a"] = 1 | |
| cache["a"] = 2 | |
| assert cache["a"] == 2 | |
| assert len(cache) == 1 | |
| def test_setdefault_uses_factory_once(): | |
| clock = FakeClock() | |
| cache = make(clock) | |
| calls = [] | |
| def factory(): | |
| calls.append(1) | |
| return "v" | |
| assert cache.setdefault("k", factory) == "v" | |
| assert cache.setdefault("k", factory) == "v" | |
| assert len(calls) == 1 | |
| def test_pop_removes_entry(): | |
| clock = FakeClock() | |
| cache = make(clock) | |
| cache["a"] = 1 | |
| assert cache.pop("a") == 1 | |
| assert cache.pop("a", "gone") == "gone" | |
| def test_items_skips_expired(): | |
| clock = FakeClock() | |
| cache = make(clock) | |
| cache["a"] = 1 | |
| clock.advance(4) | |
| cache["b"] = 2 | |
| clock.advance(7) | |
| assert list(cache.items()) == [("b", 2)] | |
| def test_invalid_arguments(): | |
| with pytest.raises(ValueError): | |
| TTLCache(maxsize=0) | |
| with pytest.raises(ValueError): | |
| TTLCache(ttl=0) | |
| def test_repr_reports_live_count(): | |
| clock = FakeClock() | |
| cache = make(clock) | |
| cache["a"] = 1 | |
| assert "live=1" in repr(cache) | |