Beispiel #1
0
def test_invalidate_cache(sdk, mocker):
    mock_write = mocker.patch('toucan_data_sdk.sdk.ToucanDataSdk.write')
    mock_write.return_value = {'df': DF, 'df2': DF2}

    # Cache is empty -> fill it
    dfs = sdk.dfs
    assert sdk.client.sdk.post.call_count == 1
    assert isinstance(dfs, dict)
    assert 'df' in dfs
    assert 'df2' in dfs

    assert DF.equals(dfs['df'])
    assert DF2.equals(dfs['df2'])

    # Invalidate cache
    sdk.invalidate_cache()
    sdk.client.sdk.post.reset_mock()

    dfs = sdk.dfs
    assert sdk.client.sdk.post.call_count == 1
    assert isinstance(dfs, dict)
    assert 'df' in dfs
    assert 'df2' in dfs

    assert DF.equals(dfs['df'])
    assert DF2.equals(dfs['df2'])
Beispiel #2
0
def test_read_from_cache(sdk):
    with tempfile.TemporaryDirectory() as tmp_dir:
        extraction_dir = os.path.join(tmp_dir, sdk.EXTRACTION_CACHE_PATH)
        sdk.EXTRACTION_CACHE_PATH = extraction_dir
        os.makedirs(extraction_dir)

        joblib.dump(DF, os.path.join(extraction_dir, 'a'))
        joblib.dump(DF2, os.path.join(extraction_dir, 'b'))

        dfs = sdk.read_from_cache()
        assert 'a' in dfs
        assert 'b' in dfs
        assert DF.equals(dfs['a'])
        assert DF2.equals(dfs['b'])

        dfs = sdk.read_from_cache(['a'])
        assert 'a' in dfs
        assert 'b' not in dfs
        assert DF.equals(dfs['a'])
Beispiel #3
0
def test_extract_zip(zip_content):
    with tempfile.NamedTemporaryFile() as tmp_file:
        tmp_file.write(zip_content)
        tmp_file.flush()
        tmp_file.seek(0)

        dfs = extract_zip(tmp_file.name)
        assert 'df' in dfs
        assert 'df2' in dfs
        assert DF.equals(dfs['df'])
        assert DF2.equals(dfs['df2'])
Beispiel #4
0
def test_cache(sdk, mocker):
    mock_read = mocker.patch('toucan_data_sdk.sdk.ToucanDataSdk.read')
    mock_write = mocker.patch('toucan_data_sdk.sdk.ToucanDataSdk.write')
    mock_path_exists = mocker.patch('os.path.exists')
    mock_is_dir = mocker.patch('os.path.isdir')

    # Cache is empty -> fill it
    mock_path_exists.return_value = False
    mock_write.return_value = {'df': DF, 'df2': DF2}

    dfs = sdk.dfs
    assert sdk.client.sdk.post.call_count == 1
    assert isinstance(dfs, dict)
    assert 'df' in dfs
    assert 'df2' in dfs

    assert DF.equals(dfs['df'])
    assert DF2.equals(dfs['df2'])

    # Cache is filled, no request to the server
    mock_path_exists.return_value = True
    mock_is_dir.return_value = True
    mock_read.return_value = {'df': DF, 'df2': DF2}

    sdk.client.sdk.post.reset_mock()
    _ = sdk.dfs
    sdk.client.sdk.post.assert_not_called()

    assert isinstance(dfs, dict)
    assert 'df' in dfs
    assert 'df2' in dfs

    assert DF.equals(dfs['df'])
    assert DF2.equals(dfs['df2'])

    mocker.stopall()
Beispiel #5
0
def gen_df():
    return DF.copy(deep=True)