예제 #1
0
def test_cacheable_no_cache_csv(from_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
    from_csv.assert_called_once_with('/xyz/abc/example.csv')
    assert not ju_write.called, 'json write should not have been called'
    assert not ju_read.called, 'json read should not have been called'
예제 #2
0
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'
예제 #3
0
def test_cacheable_lazy_csv_file_exists(mock_json_utilities, mock_dataframe):
    @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 mock_json_utilities.read_url_get.called
    mock_dataframe.from_csv.assert_called_once_with('/xyz/abc/example.csv')
    assert not mock_json_utilities.write.called, 'json write should not have been called'
    assert not mock_json_utilities.read.called, 'json read should not have been called'
예제 #4
0
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'
예제 #5
0
def test_cacheable_lazy_csv_no_file(mkdir, dictwriter, mock_json_utilities,
                                    mock_dataframe):
    @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]

    mock_json_utilities.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()
    mock_dataframe.from_csv.assert_called_once_with('/xyz/abc/example.csv')
    assert not mock_json_utilities.write.called, 'json write should not have been called'
    assert not mock_json_utilities.read.called, 'json read should not have been called'
예제 #6
0
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'