-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathtest_base_core.py
More file actions
69 lines (50 loc) · 1.96 KB
/
test_base_core.py
File metadata and controls
69 lines (50 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Additional tests for base core to improve coverage."""
from unittest.mock import Mock, patch
import pytest
from cachier.cores.base import _BaseCore
class ConcreteCachingCore(_BaseCore):
"""Concrete implementation of _BaseCore for testing."""
def get_entry_by_key(self, key, reload=False):
return key, None
def set_entry(self, key, func_res):
return True
def mark_entry_being_calculated(self, key):
pass
def mark_entry_not_calculated(self, key):
pass
def wait_on_entry_calc(self, key):
return None
def clear_cache(self):
pass
def clear_being_calculated(self):
pass
def delete_stale_entries(self, stale_after):
pass
def test_estimate_size_fallback():
"""Test _estimate_size falls back to sys.getsizeof when asizeof fails."""
# Test lines 101-102: exception handling in _estimate_size
core = ConcreteCachingCore(
hash_func=None, wait_for_calc_timeout=10, entry_size_limit=1000
)
# Mock asizeof to raise exception
with patch(
"cachier.cores.base.asizeof.asizeof",
side_effect=Exception("asizeof failed"),
):
# Should fall back to sys.getsizeof
size = core._estimate_size("test_value")
assert size > 0 # sys.getsizeof should return a positive value
def test_should_store_exception():
"""Test _should_store returns True when size estimation fails."""
# Test lines 109-110: exception handling in _should_store
core = ConcreteCachingCore(
hash_func=None, wait_for_calc_timeout=10, entry_size_limit=1000
)
# Mock both size estimation methods to fail
with patch(
"cachier.cores.base.asizeof.asizeof",
side_effect=Exception("asizeof failed"),
):
with patch("sys.getsizeof", side_effect=Exception("getsizeof failed")):
# Should return True (allow storage) when size can't be determined
assert core._should_store("test_value") is True