|
| 1 | +import pytest |
| 2 | +from io import BytesIO |
| 3 | +from libs.azure_helper.storage_blob import StorageBlobHelper |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def mock_blob_service_client(mocker): |
| 8 | + return mocker.patch("libs.azure_helper.storage_blob.BlobServiceClient") |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def mock_default_azure_credential(mocker): |
| 13 | + return mocker.patch("libs.azure_helper.storage_blob.DefaultAzureCredential") |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def storage_blob_helper(mock_blob_service_client, mock_default_azure_credential): |
| 18 | + return StorageBlobHelper( |
| 19 | + account_url="https://testaccount.blob.core.windows.net", |
| 20 | + container_name="testcontainer", |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +def test_get_container_client_with_parent_container( |
| 25 | + storage_blob_helper, mock_blob_service_client, mocker |
| 26 | +): |
| 27 | + mock_container_client = mocker.MagicMock() |
| 28 | + mock_blob_service_client.return_value.get_container_client.return_value = ( |
| 29 | + mock_container_client |
| 30 | + ) |
| 31 | + |
| 32 | + # Reset call count before the specific action |
| 33 | + mock_blob_service_client.return_value.get_container_client.reset_mock() |
| 34 | + |
| 35 | + # Call _get_container_client without passing container_name |
| 36 | + container_client = storage_blob_helper._get_container_client() |
| 37 | + |
| 38 | + assert container_client == mock_container_client |
| 39 | + assert mock_blob_service_client.return_value.get_container_client.call_count == 1 |
| 40 | + mock_blob_service_client.return_value.get_container_client.assert_called_once_with( |
| 41 | + "testcontainer" |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def test_get_container_client_without_container_name(storage_blob_helper): |
| 46 | + storage_blob_helper.parent_container_name = None |
| 47 | + |
| 48 | + with pytest.raises( |
| 49 | + ValueError, |
| 50 | + match="Container name must be provided either during initialization or as a function argument.", |
| 51 | + ): |
| 52 | + storage_blob_helper._get_container_client() |
| 53 | + |
| 54 | + |
| 55 | +def test_upload_file(storage_blob_helper, mock_blob_service_client, mocker): |
| 56 | + mock_blob_client = mocker.MagicMock() |
| 57 | + mock_blob_service_client.return_value.get_container_client.return_value.get_blob_client.return_value = ( |
| 58 | + mock_blob_client |
| 59 | + ) |
| 60 | + |
| 61 | + # Mock the open function to simulate reading a file |
| 62 | + mocker.patch("builtins.open", mocker.mock_open(read_data="test content")) |
| 63 | + |
| 64 | + storage_blob_helper.upload_file("testcontainer", "testblob", "testfile.txt") |
| 65 | + |
| 66 | + mock_blob_client.upload_blob.assert_called_once() |
| 67 | + |
| 68 | + |
| 69 | +def test_upload_stream(storage_blob_helper, mock_blob_service_client, mocker): |
| 70 | + mock_blob_client = mocker.MagicMock() |
| 71 | + mock_blob_service_client.return_value.get_container_client.return_value.get_blob_client.return_value = ( |
| 72 | + mock_blob_client |
| 73 | + ) |
| 74 | + stream = BytesIO(b"test data") |
| 75 | + |
| 76 | + storage_blob_helper.upload_stream("testcontainer", "testblob", stream) |
| 77 | + |
| 78 | + mock_blob_client.upload_blob.assert_called_once_with(stream, overwrite=True) |
| 79 | + |
| 80 | + |
| 81 | +def test_upload_text(storage_blob_helper, mock_blob_service_client, mocker): |
| 82 | + mock_blob_client = mocker.MagicMock() |
| 83 | + mock_blob_service_client.return_value.get_container_client.return_value.get_blob_client.return_value = ( |
| 84 | + mock_blob_client |
| 85 | + ) |
| 86 | + |
| 87 | + storage_blob_helper.upload_text("testcontainer", "testblob", "test text") |
| 88 | + |
| 89 | + mock_blob_client.upload_blob.assert_called_once_with("test text", overwrite=True) |
| 90 | + |
| 91 | + |
| 92 | +def test_download_file(storage_blob_helper, mock_blob_service_client, mocker): |
| 93 | + mock_blob_client = mocker.MagicMock() |
| 94 | + mock_blob_service_client.return_value.get_container_client.return_value.get_blob_client.return_value = ( |
| 95 | + mock_blob_client |
| 96 | + ) |
| 97 | + mock_blob_client.download_blob.return_value.readall.return_value = b"test data" |
| 98 | + |
| 99 | + mock_open = mocker.patch("builtins.open", mocker.mock_open()) |
| 100 | + storage_blob_helper.download_file("testcontainer", "testblob", "downloaded.txt") |
| 101 | + mock_open.return_value.write.assert_called_once_with(b"test data") |
| 102 | + |
| 103 | + |
| 104 | +def test_download_stream(storage_blob_helper, mock_blob_service_client, mocker): |
| 105 | + mock_blob_client = mocker.MagicMock() |
| 106 | + mock_blob_service_client.return_value.get_container_client.return_value.get_blob_client.return_value = ( |
| 107 | + mock_blob_client |
| 108 | + ) |
| 109 | + mock_blob_client.download_blob.return_value.readall.return_value = b"test data" |
| 110 | + |
| 111 | + stream = storage_blob_helper.download_stream("testcontainer", "testblob") |
| 112 | + |
| 113 | + assert stream == b"test data" |
| 114 | + |
| 115 | + |
| 116 | +def test_download_text(storage_blob_helper, mock_blob_service_client, mocker): |
| 117 | + mock_blob_client = mocker.MagicMock() |
| 118 | + mock_blob_service_client.return_value.get_container_client.return_value.get_blob_client.return_value = ( |
| 119 | + mock_blob_client |
| 120 | + ) |
| 121 | + mock_blob_client.download_blob.return_value.content_as_text.return_value = ( |
| 122 | + "test text" |
| 123 | + ) |
| 124 | + |
| 125 | + text = storage_blob_helper.download_text("testcontainer", "testblob") |
| 126 | + |
| 127 | + assert text == "test text" |
| 128 | + |
| 129 | + |
| 130 | +def test_delete_blob(storage_blob_helper, mock_blob_service_client, mocker): |
| 131 | + mock_blob_client = mocker.MagicMock() |
| 132 | + mock_blob_service_client.return_value.get_container_client.return_value.get_blob_client.return_value = ( |
| 133 | + mock_blob_client |
| 134 | + ) |
| 135 | + |
| 136 | + storage_blob_helper.delete_blob("testcontainer", "testblob") |
| 137 | + |
| 138 | + mock_blob_client.delete_blob.assert_called_once() |
| 139 | + |
| 140 | + |
| 141 | +def test_upload_blob_with_str(storage_blob_helper, mock_blob_service_client, mocker): |
| 142 | + mock_blob_client = mocker.MagicMock() |
| 143 | + mock_blob_service_client.return_value.get_container_client.return_value.get_blob_client.return_value = ( |
| 144 | + mock_blob_client |
| 145 | + ) |
| 146 | + |
| 147 | + storage_blob_helper.upload_blob("testcontainer", "testblob", "test string data") |
| 148 | + |
| 149 | + mock_blob_client.upload_blob.assert_called_once_with( |
| 150 | + "test string data", overwrite=True |
| 151 | + ) |
| 152 | + |
| 153 | + |
| 154 | +def test_upload_blob_with_bytes(storage_blob_helper, mock_blob_service_client, mocker): |
| 155 | + mock_blob_client = mocker.MagicMock() |
| 156 | + mock_blob_service_client.return_value.get_container_client.return_value.get_blob_client.return_value = ( |
| 157 | + mock_blob_client |
| 158 | + ) |
| 159 | + |
| 160 | + storage_blob_helper.upload_blob("testcontainer", "testblob", b"test bytes data") |
| 161 | + |
| 162 | + mock_blob_client.upload_blob.assert_called_once_with( |
| 163 | + b"test bytes data", overwrite=True |
| 164 | + ) |
| 165 | + |
| 166 | + |
| 167 | +def test_upload_blob_with_io(storage_blob_helper, mock_blob_service_client, mocker): |
| 168 | + mock_blob_client = mocker.MagicMock() |
| 169 | + mock_blob_service_client.return_value.get_container_client.return_value.get_blob_client.return_value = ( |
| 170 | + mock_blob_client |
| 171 | + ) |
| 172 | + stream = BytesIO(b"test stream data") |
| 173 | + |
| 174 | + storage_blob_helper.upload_blob("testcontainer", "testblob", stream) |
| 175 | + |
| 176 | + mock_blob_client.upload_blob.assert_called_once_with(stream, overwrite=True) |
| 177 | + |
| 178 | + |
| 179 | +def test_upload_blob_with_unsupported_type(storage_blob_helper): |
| 180 | + with pytest.raises(ValueError, match="Unsupported data type for upload"): |
| 181 | + storage_blob_helper.upload_blob("testcontainer", "testblob", 12345) |
0 commit comments