Skip to content

Commit 6318247

Browse files
khushthecoderclaude
andcommitted
test: add coverage for lru_cache and single-value cache branches
Cover the previously untested lru_cache and "other" (single-value) code paths in cache_info() and clear_cache() to fix the codecov/project coverage drop. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 430e972 commit 6318247

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

tests/anoph/test_cache_api.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,79 @@ def __init__(self):
222222
for entry in info.values():
223223
if entry["kind"] == "dict":
224224
assert entry["entries"] == 0
225+
226+
227+
def test_cache_info_and_clear_lru_cache():
228+
"""Test cache_info and clear_cache with lru_cache-wrapped functions."""
229+
from functools import lru_cache
230+
231+
class FakeBase:
232+
_CACHE_CATEGORIES = {
233+
"base": ("_cache_releases",),
234+
}
235+
236+
def __init__(self):
237+
@lru_cache(maxsize=32)
238+
def _releases_fn(key):
239+
return key
240+
241+
self._cache_releases = _releases_fn
242+
243+
_iter_cache_attrs = AnophelesBase._iter_cache_attrs
244+
_estimate_cache_entry_nbytes = staticmethod(
245+
AnophelesBase._estimate_cache_entry_nbytes
246+
)
247+
cache_info = AnophelesBase.cache_info
248+
clear_cache = AnophelesBase.clear_cache
249+
250+
fake = FakeBase()
251+
252+
# Populate the lru_cache.
253+
fake._cache_releases("a")
254+
fake._cache_releases("b")
255+
256+
info = fake.cache_info()
257+
assert "_cache_releases" in info
258+
assert info["_cache_releases"]["kind"] == "lru_cache"
259+
assert info["_cache_releases"]["entries"] == 2
260+
261+
# Clear and verify.
262+
fake.clear_cache("base")
263+
info = fake.cache_info()
264+
assert info["_cache_releases"]["entries"] == 0
265+
266+
267+
def test_cache_info_and_clear_single_value():
268+
"""Test cache_info and clear_cache with single-value (non-dict, non-lru) caches."""
269+
270+
class FakeBase:
271+
_CACHE_CATEGORIES = {
272+
"genome_sequence": ("_cache_genome",),
273+
}
274+
275+
def __init__(self):
276+
# Simulate a single-value cache (e.g. a zarr group stored directly).
277+
self._cache_genome = np.zeros((10, 10), dtype=np.float64)
278+
279+
_iter_cache_attrs = AnophelesBase._iter_cache_attrs
280+
_estimate_cache_entry_nbytes = staticmethod(
281+
AnophelesBase._estimate_cache_entry_nbytes
282+
)
283+
cache_info = AnophelesBase.cache_info
284+
clear_cache = AnophelesBase.clear_cache
285+
286+
fake = FakeBase()
287+
288+
info = fake.cache_info()
289+
assert "_cache_genome" in info
290+
assert info["_cache_genome"]["kind"] == "other"
291+
assert info["_cache_genome"]["entries"] == 1
292+
assert info["_cache_genome"]["nbytes"] == 800 # 10*10*8 bytes
293+
294+
# Clear and verify it gets set to None.
295+
fake.clear_cache("genome_sequence")
296+
assert fake._cache_genome is None
297+
298+
# After clearing, cache_info should not list it (it's None now).
299+
info = fake.cache_info()
300+
assert "_cache_genome" not in info

0 commit comments

Comments
 (0)