|
3 | 3 | from io import BytesIO |
4 | 4 | from pytest_mock import MockerFixture |
5 | 5 | from rohmu.common.strenum import StrEnum |
6 | | -from rohmu.errors import InvalidByteRangeError |
| 6 | +from rohmu.errors import FileNotFoundFromStorageError, InvalidByteRangeError |
7 | 7 | from rohmu.object_storage.azure import AzureTransfer |
8 | 8 | from rohmu.object_storage.config import AzureObjectStorageConfig |
9 | 9 | from tempfile import NamedTemporaryFile |
@@ -108,6 +108,52 @@ def test_get_contents_to_fileobj_raises_error_on_invalid_byte_range(mock_get_blo |
108 | 108 | ) |
109 | 109 |
|
110 | 110 |
|
| 111 | +def test_get_contents_to_fileobj_not_found(mock_get_blob_client: MagicMock) -> None: |
| 112 | + notifier = MagicMock() |
| 113 | + transfer = AzureTransfer( |
| 114 | + bucket_name="test_bucket", |
| 115 | + account_name="test_account", |
| 116 | + account_key="test_key2", |
| 117 | + notifier=notifier, |
| 118 | + ) |
| 119 | + |
| 120 | + download_blob = MagicMock(side_effect=azure.core.exceptions.ResourceNotFoundError) |
| 121 | + mock_get_blob_client.return_value = MagicMock(download_blob=download_blob) |
| 122 | + with pytest.raises(FileNotFoundFromStorageError): |
| 123 | + transfer.get_contents_to_fileobj( |
| 124 | + key="testkey", |
| 125 | + fileobj_to_store_to=BytesIO(), |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +def test_get_contents_to_fileobj_empty_object(mock_get_blob_client: MagicMock) -> None: |
| 130 | + notifier = MagicMock() |
| 131 | + transfer = AzureTransfer( |
| 132 | + bucket_name="test_bucket", |
| 133 | + account_name="test_account", |
| 134 | + account_key="test_key2", |
| 135 | + notifier=notifier, |
| 136 | + ) |
| 137 | + transfer._metadata_for_key = MagicMock(return_value={}) # type: ignore[method-assign] |
| 138 | + |
| 139 | + def download_blob(*args: Any, **kwargs: Any) -> Any: |
| 140 | + raise azure.core.exceptions.HttpResponseError( |
| 141 | + message="The range specified is invalid for the current size of the resource.", |
| 142 | + response=MagicMock(reason="Range Not Satisfiable", status_code=416), |
| 143 | + ) |
| 144 | + |
| 145 | + def get_blob_properties(*args: Any, **kwargs: Any) -> Any: |
| 146 | + return MagicMock(size=0) |
| 147 | + |
| 148 | + mock_get_blob_client.return_value = MagicMock(download_blob=download_blob, get_blob_properties=get_blob_properties) |
| 149 | + fileobj = BytesIO() |
| 150 | + transfer.get_contents_to_fileobj( |
| 151 | + key="testkey", |
| 152 | + fileobj_to_store_to=fileobj, |
| 153 | + ) |
| 154 | + assert fileobj.getvalue() == b"" |
| 155 | + |
| 156 | + |
111 | 157 | def test_minimal_config() -> None: |
112 | 158 | config = AzureObjectStorageConfig(account_name="test", bucket_name=None, account_key=None, sas_token=None) |
113 | 159 | assert config.account_name == "test" |
|
0 commit comments