Exemplo n.º 1
0
def test_download_folder(tmp_directory, monkeypatch, mock_client):
    Path('backup', 'dir', 'subdir', 'nested').mkdir(parents=True)
    Path('backup', 'dir', 'a').touch()
    Path('backup', 'dir', 'b').touch()
    Path('backup', 'dir', 'subdir', 'c').touch()
    Path('backup', 'dir', 'subdir', 'nested', 'd').touch()

    mock_blobs = []

    for name in [
            'backup/dir/a', 'backup/dir/b', 'backup/dir/subdir/c',
            'backup/dir/subdir/nested/d'
    ]:
        m = Mock()
        m.name = name
        mock_blobs.append(m)

    mock = Mock()
    client = GCloudStorageClient('my-bucket-name',
                                 parent='backup',
                                 path_to_project_root='.')
    monkeypatch.setattr(client, '_download', mock)
    # simulate this is not a file
    mock_client().bucket().blob('backup/a').exists.return_value = False
    mock_client().bucket().client.list_blobs.return_value = mock_blobs

    client.download('dir')

    mock_blobs[0].download_to_filename.assert_called_once_with(Path('dir/a'))
    mock_blobs[1].download_to_filename.assert_called_once_with(Path('dir/b'))
    mock_blobs[2].download_to_filename.assert_called_once_with(
        Path('dir/subdir/c'))
    mock_blobs[3].download_to_filename.assert_called_once_with(
        Path('dir/subdir/nested/d'))
Exemplo n.º 2
0
def test_download(monkeypatch, parent, mock_client):
    mock = Mock()
    client = GCloudStorageClient('my-bucket-name', parent=parent)
    monkeypatch.setattr(client, '_download', mock)

    client.download('file.txt')

    mock.assert_called_once_with('file.txt',
                                 str(PurePosixPath(parent, 'file.txt')))
Exemplo n.º 3
0
def test_download_with_custom_destination(monkeypatch, mock_client):
    mock = Mock()
    client = GCloudStorageClient('my-bucket-name',
                                 parent='parent',
                                 path_to_project_root='.')
    monkeypatch.setattr(client, '_download', mock)

    client.download('file.txt', destination='another.txt')

    mock.assert_called_once_with('another.txt',
                                 str(PurePosixPath('parent', 'file.txt')))
Exemplo n.º 4
0
def test_error_when_downloading_non_existing(monkeypatch, mock_client):
    client = GCloudStorageClient('my-bucket-name',
                                 parent='parent',
                                 path_to_project_root='.')
    monkeypatch.setattr(client, '_is_file', lambda _: False)
    list_blobs_mock = Mock(return_value=[])
    monkeypatch.setattr(client._bucket.client, 'list_blobs', list_blobs_mock)

    with pytest.raises(RemoteFileNotFound) as excinfo:
        client.download('non-existing-file')

    assert ("Could not download 'non-existing-file' using client"
            in str(excinfo.value))