Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise FileNotFoundError if blob doesn't exist #443

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ This Changelog tracks changes to this project. The notes below include a summary
- `fixed` for any bug fixes.
- `security` in case of vulnerabilities.

## 26 Apr 2023

- `fixed` `_download_blob_bytes` should raise FileNotFoundError if the blob to download doesn't exist

## 19 Apr 2023

- `changed` API/schemas bump for microbiome req reverse index
-

## 20 Mar 2023

- `fixed` bug causing error on cloud issuing of cross-trial permissions
- this is interaction with accepted param types from API, so would need integrative testing
- this is interaction with accepted param types from API, so would need integrative testing

## 24 Feb 2023

Expand Down
2 changes: 1 addition & 1 deletion functions/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _download_blob_bytes(object_name: str) -> bytes:
bucket = storage_client.get_bucket(GOOGLE_ACL_DATA_BUCKET)
blob = bucket.get_blob(object_name)
if not blob:
FileNotFoundError(
raise FileNotFoundError(
f"Could not find file {object_name} in {GOOGLE_ACL_DATA_BUCKET}"
)
return blob.download_as_string()
Expand Down
33 changes: 33 additions & 0 deletions tests/functions/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import MagicMock

import pytest
from functions.settings import GOOGLE_ACL_DATA_BUCKET

from tests.util import make_pubsub_event
from functions import util
Expand Down Expand Up @@ -75,3 +76,35 @@ def test_get_blob_as_stream(monkeypatch):
stream = util.get_blob_as_stream("", as_string=True)
assert isinstance(stream, StringIO)
assert stream.read() == blob_str


mock_bucket = None


class MockStorageClient:
def get_bucket(self, *args):
return mock_bucket


def test_download_blob_bytes(monkeypatch):
global mock_bucket
mock_blob = MagicMock()
mock_blob.download_as_string.return_value = "expected blob contents"
mock_bucket = MagicMock()
mock_bucket.get_blob.side_effect = [
# first call checks that the proper exception is thrown for a missing object
None,
# second call checks that the contents are returned as expected
mock_blob,
]

monkeypatch.setattr(util.storage, "Client", MockStorageClient)

object_name = "x/y/z"
with pytest.raises(
FileNotFoundError,
match=f"Could not find file {object_name} in {GOOGLE_ACL_DATA_BUCKET}",
):
util._download_blob_bytes(object_name)

assert util._download_blob_bytes(object_name) == "expected blob contents"