Skip to content

Commit bc49e19

Browse files
committed
Add tests for metrics: validate entry_count and total_size_bytes for memory and pickle backends
1 parent 3158564 commit bc49e19

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests/test_metrics.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,57 @@ def test_metrics_context_manager_none():
517517
"""Test MetricsContext with metrics=None does not raise."""
518518
with MetricsContext(None):
519519
pass # should not raise
520+
521+
522+
@pytest.mark.memory
523+
def test_metrics_entry_count_and_size_memory():
524+
"""Test that entry_count and total_size_bytes reflect cache state for memory backend.
525+
526+
_MemoryCore overrides _get_entry_count and _get_total_size; both should
527+
return real values after entries are written.
528+
"""
529+
530+
@cachier(backend="memory", enable_metrics=True)
531+
def test_func(x):
532+
return x * 2
533+
534+
test_func.clear_cache()
535+
536+
# No entries yet
537+
stats = test_func.metrics.get_stats()
538+
assert stats.entry_count == 0
539+
assert stats.total_size_bytes == 0
540+
541+
# Cache two distinct entries
542+
test_func(1)
543+
test_func(2)
544+
545+
stats = test_func.metrics.get_stats()
546+
assert stats.entry_count == 2
547+
assert stats.total_size_bytes > 0
548+
549+
test_func.clear_cache()
550+
551+
552+
@pytest.mark.pickle
553+
def test_metrics_entry_count_and_size_base_default():
554+
"""Test that entry_count and total_size_bytes are 0 for backends without override.
555+
556+
The base-class _get_entry_count and _get_total_size return 0. Pickle does
557+
not override them, so the snapshot values must stay at the default.
558+
"""
559+
560+
@cachier(backend="pickle", enable_metrics=True)
561+
def test_func(x):
562+
return x * 2
563+
564+
test_func.clear_cache()
565+
566+
test_func(1)
567+
test_func(2)
568+
569+
stats = test_func.metrics.get_stats()
570+
assert stats.entry_count == 0
571+
assert stats.total_size_bytes == 0
572+
573+
test_func.clear_cache()

0 commit comments

Comments
 (0)