def test_cacheable_no_cache_csv(read_csv, ju_read_url_get, ju_read, ju_write): @cacheable() def get_hemispheres(): return RmaApi().model_query(model='Hemisphere') df = get_hemispheres(path='/xyz/abc/example.csv', strategy='file', **Cache.cache_csv()) assert df.loc[:, 'whatever'][0] assert not ju_read_url_get.called read_csv.assert_called_once_with('/xyz/abc/example.csv', parse_dates=True) assert not ju_write.called, 'json write should not have been called' assert not ju_read.called, 'json read should not have been called'
def test_cacheable_lazy_csv_file_exists(read_csv, ju_read_url_get, ju_read, ju_write): @cacheable() def get_hemispheres(): return RmaApi().model_query(model='Hemisphere') with patch('os.path.exists', MagicMock(return_value=True)) as ope: df = get_hemispheres(path='/xyz/abc/example.csv', strategy='lazy', **Cache.cache_csv()) assert df.loc[:, 'whatever'][0] assert not ju_read_url_get.called read_csv.assert_called_once_with('/xyz/abc/example.csv', parse_dates=True) assert not ju_write.called, 'json write should not have been called' assert not ju_read.called, 'json read should not have been called'
def test_cacheable_lazy_csv_no_file(mkdir, dictwriter, ju_read_url_get, ju_read, ju_write, read_csv): @cacheable() def get_hemispheres(): return RmaApi().model_query(model='Hemisphere') with patch('os.path.exists', MagicMock(return_value=False)) as ope: with patch(builtins.__name__ + '.open', mock_open(), create=True) as open_mock: open_mock.return_value.write = MagicMock() df = get_hemispheres(path='/xyz/abc/example.csv', strategy='lazy', **Cache.cache_csv()) assert df.loc[:, 'whatever'][0] ju_read_url_get.assert_called_once_with( 'http://api.brain-map.org/api/v2/data/query.json?q=model::Hemisphere') open_mock.assert_called_once_with('/xyz/abc/example.csv', 'w') dictwriter.return_value.writerow.assert_called() read_csv.assert_called_once_with('/xyz/abc/example.csv', parse_dates=True) assert not ju_write.called, 'json write should not have been called' assert not ju_read.called, 'json read should not have been called'