tests: improve the test for caching by static values
- ID
1ed59b4- date
2026-11-07 06:20:22+00:00- author
Alex Chan <alexc@tailscale.com>- parent
d47dd2b- message
tests: improve the test for caching by static values- changed files
1 file, 32 additions, 21 deletions
Changed files
tests/test_manager.py (22951 → 23367)
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 7a41187..ef81a41 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -499,6 +499,38 @@ class TestFunkyCache:
with pytest.raises(TypeError, match="Unrecognised storage"):
FunkyCache(storage="does_not_exist") # type: ignore
+ def test_cache_varies_based_on_static_values(self, tmp_path: Path) -> None:
+ """
+ The static values that a function depends on are used to distinguish
+ cache instances.
+ """
+ # Count how many times the underlying function was called
+ calls = Counter[str]()
+
+ for _ in range(3):
+ c1 = FunkyCache(storage="sqlite", root=tmp_path / ".cache")
+
+ @c1.register(values=["en"])
+ def greet(name: str) -> str:
+ calls[name] += 1
+ return f"Hello {name}!"
+
+ assert greet("Alex") == "Hello Alex!"
+ assert calls == {"Alex": 1}
+
+ for kc in c1._known_caches.values():
+ cast(SQLiteCacheStorage, kc.storage).close()
+
+ c2 = FunkyCache(storage="sqlite", root=tmp_path / ".cache")
+
+ @c2.register(values=["fr"])
+ def greet(name: str) -> str:
+ calls[name] += 1
+ return f"Bonjour {name}!"
+
+ assert greet("Alex") == "Bonjour Alex!"
+ assert calls == {"Alex": 2}
+
class TestMemoryFunkyCache(FunkyCacheTestSuite):
"""
@@ -547,27 +579,6 @@ class TestSQLiteFunkyCache(FunkyCacheTestSuite):
"""
yield FunkyCache(storage="sqlite", root=tmp_path / ".cache")
- def test_cache_varies_based_on_static_values(self, tmp_path: Path) -> None:
- """
- The static values that a function depends on are used to distinguish
- cache instances.
- """
- c1 = FunkyCache(storage="sqlite", root=tmp_path / ".cache")
-
- @c1.register(values=[1])
- def myfunc() -> str:
- return "the value is one"
-
- assert myfunc() == "the value is one"
-
- c2 = FunkyCache(storage="sqlite", root=tmp_path / ".cache")
-
- @c2.register(values=[2])
- def myfunc() -> str:
- return "the value is two"
-
- assert myfunc() == "the value is two"
-
def test_stale_caches_are_removed(self, tmp_path: Path) -> None:
"""
If the cache for a function is invalidated, the old SQLite