From 8113ff8705da4debab7638e305a8a7cedd9b1e3a Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 28 Dec 2022 15:44:56 +0000 Subject: [PATCH] moar tests --- src/textual/_cache.py | 4 ++-- tests/test_cache.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/textual/_cache.py b/src/textual/_cache.py index 3127bd22d..2698c4228 100644 --- a/src/textual/_cache.py +++ b/src/textual/_cache.py @@ -14,7 +14,7 @@ where the overhead of the cache is a small fraction of the total processing time from __future__ import annotations -from typing import cast, Dict, Generic, KeysView, TypeVar, overload +from typing import Dict, Generic, KeysView, TypeVar, overload CacheKey = TypeVar("CacheKey") CacheValue = TypeVar("CacheValue") @@ -71,7 +71,7 @@ class LRUCache(Generic[CacheKey, CacheValue]): def __repr__(self) -> str: return ( - f"" ) def grow(self, maxsize: int) -> None: diff --git a/tests/test_cache.py b/tests/test_cache.py index 9a4bb2ef3..86d7139f0 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -9,6 +9,8 @@ from textual._cache import FIFOCache, LRUCache def test_lru_cache(): cache = LRUCache(3) + assert str(cache) == "" + # insert some values cache["foo"] = 1 cache["bar"] = 2 @@ -35,6 +37,38 @@ def test_lru_cache(): assert "eggegg" in cache +def test_lru_cache_hits(): + cache = LRUCache(4) + assert cache.hits == 0 + assert cache.misses == 0 + + try: + cache["foo"] + except KeyError: + assert cache.hits == 0 + assert cache.misses == 1 + + cache["foo"] = 1 + assert cache.hits == 0 + assert cache.misses == 1 + + cache["foo"] + cache["foo"] + + assert cache.hits == 2 + assert cache.misses == 1 + + cache.get("bar") + assert cache.hits == 2 + assert cache.misses == 2 + + cache.get("foo") + assert cache.hits == 3 + assert cache.misses == 2 + + assert str(cache) == "" + + def test_lru_cache_get(): cache = LRUCache(3)