Coverage for nilearn/_utils/tests/test_cache_mixin.py: 0%
82 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-20 10:58 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-20 10:58 +0200
1"""Test the _utils.cache_mixin module."""
3import shutil
4from pathlib import Path
6import pytest
7from joblib import Memory
9import nilearn
10from nilearn._utils import CacheMixin, cache_mixin
13def _get_subdirs(top_dir):
14 top_dir = Path(top_dir)
15 children = list(top_dir.glob("*"))
16 return [child for child in children if child.is_dir()]
19def f(x):
20 # A simple test function
21 return x
24def test_check_memory(tmp_path):
25 # Test if check_memory returns a memory object with the location equal to
26 # input path
28 mem_none = Memory(location=None)
29 mem_temp = Memory(location=str(tmp_path))
31 for mem in [None, mem_none]:
32 memory = cache_mixin.check_memory(mem, verbose=0)
33 assert memory, Memory
34 assert memory.location == mem_none.location
36 for mem in [str(tmp_path), mem_temp]:
37 memory = cache_mixin.check_memory(mem, verbose=0)
38 assert memory.location == mem_temp.location
39 assert memory, Memory
42class CacheMixinTest(CacheMixin):
43 """Dummy mock object that wraps a CacheMixin."""
45 def __init__(self, memory=None, memory_level=1):
46 self.memory = memory
47 self.memory_level = memory_level
49 def run(self):
50 self._cache(f)
53def test_cache_mixin_with_expand_user():
54 # Test the memory cache is correctly created when using ~.
55 cache_dir = "~/nilearn_data/test_cache"
56 expand_cache_dir = Path(cache_dir).expanduser()
57 mixin_mock = CacheMixinTest(cache_dir)
59 try:
60 assert not expand_cache_dir.exists()
61 mixin_mock.run()
62 assert expand_cache_dir.exists()
63 finally:
64 if expand_cache_dir.exists():
65 shutil.rmtree(expand_cache_dir)
68def test_cache_mixin_without_expand_user():
69 # Test the memory cache is correctly created when using ~.
70 cache_dir = "~/nilearn_data/test_cache"
71 expand_cache_dir = Path(cache_dir).expanduser()
72 mixin_mock = CacheMixinTest(cache_dir)
74 try:
75 assert not expand_cache_dir.exists()
76 nilearn.EXPAND_PATH_WILDCARDS = False
77 with pytest.raises(
78 ValueError, match="Given cache path parent directory doesn't"
79 ):
80 mixin_mock.run()
81 assert not expand_cache_dir.exists()
82 nilearn.EXPAND_PATH_WILDCARDS = True
83 finally:
84 if expand_cache_dir.exists():
85 shutil.rmtree(expand_cache_dir)
88def test_cache_mixin_wrong_dirs():
89 # Test the memory cache raises a ValueError when input base path doesn't
90 # exist.
92 for cache_dir in ("/bad_dir/cache", "~/nilearn_data/tmp/test_cache"):
93 expand_cache_dir = Path(cache_dir).expanduser()
94 mixin_mock = CacheMixinTest(cache_dir)
96 try:
97 with pytest.raises(
98 ValueError, match="Given cache path parent directory doesn't"
99 ):
100 mixin_mock.run()
101 assert not expand_cache_dir.exists()
102 finally:
103 if expand_cache_dir.exists():
104 shutil.rmtree(expand_cache_dir)
107def test_cache_memory_level(tmp_path):
108 joblib_dir = (
109 tmp_path
110 / "joblib"
111 / "nilearn"
112 / "_utils"
113 / "tests"
114 / "test_cache_mixin"
115 / "f"
116 )
118 cache_mixin.cache(f, Memory(location=None))(2)
119 assert len(_get_subdirs(joblib_dir)) == 0
121 mem = Memory(location=str(tmp_path), verbose=0)
123 cache_mixin.cache(f, mem, func_memory_level=2, memory_level=1)(2)
124 assert len(_get_subdirs(joblib_dir)) == 0
126 cache_mixin.cache(f, mem, func_memory_level=2, memory_level=3)(2)
127 assert len(_get_subdirs(joblib_dir)) == 1
129 cache_mixin.cache(f, mem)(3)
130 assert len(_get_subdirs(joblib_dir)) == 2
133def test_cache_shelving(tmp_path):
134 joblib_dir = (
135 tmp_path
136 / "joblib"
137 / "nilearn"
138 / "_utils"
139 / "tests"
140 / "test_cache_mixin"
141 / "f"
142 )
143 mem = Memory(location=str(tmp_path), verbose=0)
144 res = cache_mixin.cache(f, mem, shelve=True)(2)
145 assert res.get() == 2
146 assert len(_get_subdirs(joblib_dir)) == 1
147 res = cache_mixin.cache(f, mem, shelve=True)(2)
148 assert res.get() == 2
149 assert len(_get_subdirs(joblib_dir)) == 1