def test_caches(): explorer = Explorer() mock = MagicMock() my_function = mock.my_function wrapped = explorer.cache_function(my_function) wrapped(a=1, b=2) wrapped(a=1, b=2) my_function.assert_called_once() my_function.assert_called_once_with(a=1, b=2)
def test_map_no_call(): explorer = Explorer(parallel='thread') cached = explorer.cache_function(my_function) data = explorer.map(cached, processes=2, x=range(5)) data_no_call = explorer.map_no_call(cached, x=range(2, 9)) print(data, data_no_call) assert data_no_call[-1] is None assert data_no_call[-2] is None assert all(data_no_call[0:3] == data[2:])
def test_caches(tmpdir): """ Tests if cache indeed caches.""" cache = JobLibCache(str(tmpdir), verbose=1) explorer = Explorer(cache=cache) calls = 0 @cache def my_function(a, b): nonlocal calls calls += 1 return a + b + 1 wrapped = explorer.cache_function(my_function) print('wrapped', wrapped) wrapped(a=1, b=2) wrapped(a=1, b=2) assert calls == 1
def test_mproc_cache(tmpdir): cache = JobLibCache(str(tmpdir), verbose=1) # smoke explorer = Explorer(parallel='joblib', cache=cache) parallel = JobLib(n_jobs=3, verbose=10) explorer = Explorer(parallel=parallel, cache=cache) func = explorer.cache_function(my_function) start = time.time() data = explorer.map(func, processes=2, x=range(6)) dur = time.time() - start assert dur < 6 * DELAY data_no_call = explorer.map_no_call(func, x=range(3, 10)) print(data, data_no_call) assert data_no_call[-1] is None assert data_no_call[-2] is None assert all(data_no_call[0:3] == data[3:])