Exemplo n.º 1
0
def test_helper_remove(m_connect, url, bucket, key):
    """Test helper _remove is correctly called."""
    with gs_client.GSClient(url) as client:
        client._remove(bucket_name=bucket, key_name=key)

    connection = m_connect.return_value
    connection.bucket.assert_called_once_with(bucket)
    connection.bucket.return_value.blob.assert_called_once_with(key)
    connection.bucket.return_value.blob.return_value.delete.assert_called_once(
    )
Exemplo n.º 2
0
def test_helper_put(m_connect, url, bucket, key):
    """Test helper _put is correctly called."""
    stream = io.StringIO()
    with gs_client.GSClient(url) as client:
        client._put(stream, bucket_name=bucket, key_name=key)

    connection = m_connect.return_value
    connection.bucket.assert_called_once_with(bucket)
    connection.bucket.return_value.blob.assert_called_once_with(key)
    connection.bucket.return_value.blob.return_value.upload_from_file.assert_called_once(
    )
Exemplo n.º 3
0
def test_invalid_path(m_connect, method, url, bucket, key):
    """Test a method with invalid paths."""
    patch_string = patch_string_from_method(method)

    with mock.patch(patch_string) as mocked_method:
        with gs_client.GSClient(url) as client, pytest.raises(
                exceptions.GSError):
            calling_method = getattr(client, method)
            if method == "remove":
                calling_method(bucket_name=bucket, key_name=key)
            else:
                calling_method(io.StringIO(), bucket_name=bucket, key_name=key)

        mocked_method.assert_not_called()
Exemplo n.º 4
0
def test_method_url_only(m_connect, method, url, bucket, key):
    """Test method with valid call with url only."""
    patch_string = patch_string_from_method(method)

    with mock.patch(patch_string) as mocked_method:
        stream = io.StringIO()
        with gs_client.GSClient(url) as client:
            calling_method = getattr(client, method)
            if method == "remove":
                calling_method()
            else:
                calling_method(stream)

        if method == "remove":
            mocked_method.assert_called_once_with(bucket, key)
        else:
            mocked_method.assert_called_once_with(stream, bucket, key)
Exemplo n.º 5
0
def test_not_found(m_connect, method, url, bucket, key):
    """That when the connection raises a NotFound it is raised."""
    patch_string = patch_string_from_method(method)

    with mock.patch(patch_string) as mocked_method:
        mocked_method.side_effect = google_exceptions.NotFound("not found")
        stream = io.StringIO()
        with gs_client.GSClient(url) as client, pytest.raises(
                google_exceptions.NotFound):
            calling_method = getattr(client, method)
            if method == "remove":
                calling_method(bucket_name=bucket, key_name=key)
            else:
                calling_method(stream, bucket_name=bucket, key_name=key)

        if method == "remove":
            mocked_method.assert_called_once_with(bucket, key)
        else:
            mocked_method.assert_called_once_with(stream, bucket, key)
Exemplo n.º 6
0
def test_parsing_gs_url(m_connect, url, hostname, path):
    """Test the parsing of the gs url."""
    client = gs_client.GSClient(url)

    assert client.key_name == path
    assert client.bucket == hostname