diff --git a/CHANGELOG.md b/CHANGELOG.md index ec58f05..d2dc915 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/functions/util.py b/functions/util.py index 4dc924e..8ed12ec 100644 --- a/functions/util.py +++ b/functions/util.py @@ -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() diff --git a/tests/functions/test_util.py b/tests/functions/test_util.py index 37c562e..55f6848 100644 --- a/tests/functions/test_util.py +++ b/tests/functions/test_util.py @@ -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 @@ -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"