@@ -25,17 +25,12 @@ def blob_manager(monkeypatch):
25
25
26
26
@pytest .mark .asyncio
27
27
@pytest .mark .skipif (sys .version_info .minor < 10 , reason = "requires Python 3.10 or higher" )
28
- async def test_upload_and_remove (monkeypatch , mock_env , blob_manager ):
28
+ async def test_upload_and_remove (monkeypatch , mock_env , mock_blob_container_client_exists , blob_manager ):
29
29
with NamedTemporaryFile (suffix = ".pdf" ) as temp_file :
30
30
f = File (temp_file .file )
31
31
filename = os .path .basename (f .content .name )
32
32
33
- # Set up mocks used by upload_blob
34
- async def mock_exists (* args , ** kwargs ):
35
- return True
36
-
37
- monkeypatch .setattr ("azure.storage.blob.aio.ContainerClient.exists" , mock_exists )
38
-
33
+ # Set up mock of upload_blob
39
34
async def mock_upload_blob (self , name , * args , ** kwargs ):
40
35
assert name == filename
41
36
return azure .storage .blob .aio .BlobClient .from_blob_url (
@@ -78,17 +73,12 @@ async def mock_delete_blob(self, name, *args, **kwargs):
78
73
79
74
@pytest .mark .asyncio
80
75
@pytest .mark .skipif (sys .version_info .minor < 10 , reason = "requires Python 3.10 or higher" )
81
- async def test_upload_and_remove_all (monkeypatch , mock_env , blob_manager ):
76
+ async def test_upload_and_remove_all (monkeypatch , mock_env , mock_blob_container_client_exists , blob_manager ):
82
77
with NamedTemporaryFile (suffix = ".pdf" ) as temp_file :
83
78
f = File (temp_file .file )
84
79
filename = os .path .basename (f .content .name )
85
80
86
- # Set up mocks used by upload_blob
87
- async def mock_exists (* args , ** kwargs ):
88
- return True
89
-
90
- monkeypatch .setattr ("azure.storage.blob.aio.ContainerClient.exists" , mock_exists )
91
-
81
+ # Set up mock of upload_blob
92
82
async def mock_upload_blob (self , name , * args , ** kwargs ):
93
83
assert name == filename
94
84
return azure .storage .blob .aio .BlobClient .from_blob_url (
@@ -161,12 +151,9 @@ async def mock_upload_blob(self, name, *args, **kwargs):
161
151
162
152
@pytest .mark .asyncio
163
153
@pytest .mark .skipif (sys .version_info .minor < 10 , reason = "requires Python 3.10 or higher" )
164
- async def test_dont_remove_if_no_container (monkeypatch , mock_env , blob_manager ):
165
- async def mock_exists (* args , ** kwargs ):
166
- return False
167
-
168
- monkeypatch .setattr ("azure.storage.blob.aio.ContainerClient.exists" , mock_exists )
169
-
154
+ async def test_dont_remove_if_no_container (
155
+ monkeypatch , mock_env , mock_blob_container_client_does_not_exist , blob_manager
156
+ ):
170
157
async def mock_delete_blob (* args , ** kwargs ):
171
158
assert False , "delete_blob() shouldn't have been called"
172
159
@@ -177,7 +164,8 @@ async def mock_delete_blob(*args, **kwargs):
177
164
178
165
@pytest .mark .asyncio
179
166
@pytest .mark .skipif (sys .version_info .minor < 10 , reason = "requires Python 3.10 or higher" )
180
- async def test_upload_document_image (monkeypatch , mock_env ):
167
+ @pytest .mark .parametrize ("directory_exists" , [True , False ])
168
+ async def test_upload_document_image (monkeypatch , mock_env , directory_exists ):
181
169
# Create a blob manager with an image container
182
170
blob_manager = BlobManager (
183
171
endpoint = f"https://{ os .environ ['AZURE_STORAGE_ACCOUNT' ]} .blob.core.windows.net" ,
@@ -202,10 +190,15 @@ async def test_upload_document_image(monkeypatch, mock_env):
202
190
203
191
# Mock container client operations
204
192
async def mock_exists (* args , ** kwargs ):
205
- return True
193
+ return directory_exists
206
194
207
195
monkeypatch .setattr ("azure.storage.blob.aio.ContainerClient.exists" , mock_exists )
208
196
197
+ async def mock_create_container (* args , ** kwargs ):
198
+ return
199
+
200
+ monkeypatch .setattr ("azure.storage.blob.aio.ContainerClient.create_container" , mock_create_container )
201
+
209
202
expected_blob_name = f"{ os .path .basename (temp_file .name )} /page{ image_page_num } /{ image_filename } "
210
203
211
204
async def mock_upload_blob (self , name , * args , ** kwargs ):
@@ -241,3 +234,80 @@ def test_sourcepage_from_file_page():
241
234
def test_blob_name_from_file_name ():
242
235
assert BlobManager .blob_name_from_file_name ("tmp/test.pdf" ) == "test.pdf"
243
236
assert BlobManager .blob_name_from_file_name ("tmp/test.html" ) == "test.html"
237
+
238
+
239
+ @pytest .mark .asyncio
240
+ @pytest .mark .skipif (sys .version_info .minor < 10 , reason = "requires Python 3.10 or higher" )
241
+ async def test_download_blob (monkeypatch , mock_env , mock_blob_container_client_exists , blob_manager ):
242
+ # Mock the download_blob method
243
+ test_content = b"test content bytes"
244
+
245
+ class MockDownloadResponse :
246
+ def __init__ (self ):
247
+ # Create properties with content_settings
248
+ class ContentSettings :
249
+ content_type = "application/pdf"
250
+
251
+ class Properties :
252
+ def __init__ (self ):
253
+ self .content_settings = ContentSettings ()
254
+
255
+ self .properties = Properties ()
256
+
257
+ async def readall (self ):
258
+ return test_content
259
+
260
+ async def mock_download_blob (* args , ** kwargs ):
261
+ return MockDownloadResponse ()
262
+
263
+ monkeypatch .setattr ("azure.storage.blob.aio.BlobClient.download_blob" , mock_download_blob )
264
+
265
+ result = await blob_manager .download_blob ("test_document.pdf" )
266
+
267
+ assert result is not None
268
+ content , properties = result
269
+ assert content == test_content
270
+ assert properties ["content_settings" ]["content_type" ] == "application/pdf"
271
+
272
+
273
+ @pytest .mark .asyncio
274
+ @pytest .mark .skipif (sys .version_info .minor < 10 , reason = "requires Python 3.10 or higher" )
275
+ async def test_download_blob_not_found (monkeypatch , mock_env , mock_blob_container_client_exists , blob_manager ):
276
+ # Mock the download_blob method to raise ResourceNotFoundError
277
+ async def mock_download_blob (* args , ** kwargs ):
278
+ from azure .core .exceptions import ResourceNotFoundError
279
+
280
+ raise ResourceNotFoundError ("Blob not found" )
281
+
282
+ monkeypatch .setattr ("azure.storage.blob.aio.BlobClient.download_blob" , mock_download_blob )
283
+
284
+ result = await blob_manager .download_blob ("nonexistent.pdf" )
285
+
286
+ assert result is None
287
+
288
+
289
+ @pytest .mark .asyncio
290
+ @pytest .mark .skipif (sys .version_info .minor < 10 , reason = "requires Python 3.10 or higher" )
291
+ async def test_download_blob_container_not_exist (
292
+ monkeypatch , mock_env , mock_blob_container_client_does_not_exist , blob_manager
293
+ ):
294
+ result = await blob_manager .download_blob ("test_document.pdf" )
295
+
296
+ assert result is None
297
+
298
+
299
+ @pytest .mark .asyncio
300
+ @pytest .mark .skipif (sys .version_info .minor < 10 , reason = "requires Python 3.10 or higher" )
301
+ async def test_download_blob_empty_path (monkeypatch , mock_env , mock_blob_container_client_exists , blob_manager ):
302
+ result = await blob_manager .download_blob ("" )
303
+
304
+ assert result is None
305
+
306
+
307
+ @pytest .mark .asyncio
308
+ @pytest .mark .skipif (sys .version_info .minor < 10 , reason = "requires Python 3.10 or higher" )
309
+ async def test_download_blob_with_user_oid (monkeypatch , mock_env , blob_manager ):
310
+ with pytest .raises (ValueError ) as excinfo :
311
+ await blob_manager .download_blob ("test_document.pdf" , user_oid = "user123" )
312
+
313
+ assert "user_oid is not supported for BlobManager" in str (excinfo .value )
0 commit comments