-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathtest_numpy_hash.py
More file actions
49 lines (36 loc) · 1.17 KB
/
test_numpy_hash.py
File metadata and controls
49 lines (36 loc) · 1.17 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
"""Tests for NumPy-aware default hash behavior."""
from datetime import timedelta
import pytest
from cachier import cachier
np = pytest.importorskip("numpy")
@pytest.mark.parametrize(
"backend",
[
pytest.param("memory", marks=pytest.mark.memory),
pytest.param("pickle", marks=pytest.mark.pickle),
],
)
def test_default_hash_func_uses_array_content_for_cache_keys(backend, tmp_path):
"""Verify equal arrays map to a cache hit and different arrays miss."""
call_count = 0
decorator_kwargs = {"backend": backend, "stale_after": timedelta(seconds=120)}
if backend == "pickle":
decorator_kwargs["cache_dir"] = tmp_path
@cachier(**decorator_kwargs)
def array_sum(values):
nonlocal call_count
call_count += 1
return int(values.sum())
arr = np.arange(100_000, dtype=np.int64)
arr_copy = arr.copy()
changed = arr.copy()
changed[-1] = -1
first = array_sum(arr)
assert call_count == 1
second = array_sum(arr_copy)
assert second == first
assert call_count == 1
third = array_sum(changed)
assert third != first
assert call_count == 2
array_sum.clear_cache()