88
99import pytest
1010from azure .core .exceptions import ResourceExistsError
11- from azure .storage .blob import BlobBlock , BlobType
11+ from azure .storage .blob import BlobBlock , BlobType , ContainerClient as SyncContainerClient
1212from azure .storage .blob .aio import (
1313 BlobClient ,
1414 BlobServiceClient ,
@@ -46,11 +46,16 @@ async def _setup(self, account_name):
4646 except ResourceExistsError :
4747 pass
4848
49- # TODO: Figure out how to get this to run automatically
50- async def _teardown ( self ):
49+ def teardown_method ( self , _ ):
50+ # Use sync client as teardown_method must be sync
5151 if self .container :
52+ sync_credential = self .get_credential (SyncContainerClient , is_async = False )
53+ sync_container = SyncContainerClient .from_container_url (
54+ self .container .url ,
55+ credential = sync_credential )
56+
5257 try :
53- await self . container .delete_container ()
58+ sync_container .delete_container ()
5459 except :
5560 pass
5661
@@ -74,6 +79,9 @@ async def test_encryption_blocked_crc64(self, **kwargs):
7479 with pytest .raises (ValueError ):
7580 await blob .upload_blob (b'123' , validate_content = 'crc64' )
7681
82+ # Needed for teardown
83+ self .container = None
84+
7785 @BlobPreparer ()
7886 @pytest .mark .parametrize ('a' , [BlobType .BLOCKBLOB , BlobType .PAGEBLOB , BlobType .APPENDBLOB ]) # a: blob_type
7987 @pytest .mark .parametrize ('b' , [True , "auto" , 'md5' , 'crc64' ]) # b: validate_content
@@ -106,8 +114,6 @@ async def test_upload_blob(self, a, b, **kwargs):
106114 await blob .upload_blob (str_iter , blob_type = a , length = len (str_data_encoded ), encoding = 'utf-8' , validate_content = b , overwrite = True , raw_request_hook = assert_method )
107115 assert await (await blob .download_blob ()).read () == str_data_encoded
108116
109- await self ._teardown ()
110-
111117 @BlobPreparer ()
112118 @pytest .mark .parametrize ('a' , [BlobType .BLOCKBLOB , BlobType .PAGEBLOB , BlobType .APPENDBLOB ]) # a: blob_type
113119 @pytest .mark .parametrize ('b' , [True , 'md5' , 'crc64' ]) # b: validate_content
@@ -143,8 +149,6 @@ async def test_upload_blob_chunks(self, a, b, **kwargs):
143149 await blob .upload_blob (str_iter , blob_type = a , length = len (str_data_encoded ), encoding = 'utf-8' , validate_content = b , overwrite = True , raw_request_hook = assert_method )
144150 assert await (await blob .download_blob ()).read () == str_data_encoded
145151
146- await self ._teardown ()
147-
148152 @BlobPreparer ()
149153 @pytest .mark .parametrize ('a' , [True , 'md5' , 'crc64' ]) # a: validate_content
150154 @GenericTestProxyParametrize1 ()
@@ -169,7 +173,6 @@ async def test_upload_blob_substream(self, a, **kwargs):
169173 # Assert
170174 content = await blob .download_blob ()
171175 assert await content .read () == data
172- await self ._teardown ()
173176
174177 @BlobPreparer ()
175178 @pytest .mark .parametrize ('a' , [True , 'auto' , 'md5' , 'crc64' ]) # a: validate_content
@@ -200,7 +203,6 @@ def generator():
200203 # Assert
201204 content = await blob .download_blob ()
202205 assert await content .read () == data1 + data2 .encode ('utf-8-sig' ) + data1
203- await self ._teardown ()
204206
205207 @BlobPreparer ()
206208 @pytest .mark .parametrize ('a' , [True , 'md5' , 'crc64' ]) # a: validate_content
@@ -221,7 +223,6 @@ async def test_stage_block_streaming(self, a, **kwargs):
221223 # Assert
222224 result = await blob .download_blob ()
223225 assert await result .read () == content
224- await self ._teardown ()
225226
226227 @BlobPreparer ()
227228 @pytest .mark .parametrize ('a' , [True , 'md5' , 'crc64' ]) # a: validate_content
@@ -274,7 +275,6 @@ def generator():
274275 # Assert
275276 content = await blob .download_blob ()
276277 assert await content .readall () == data1 + data2 .encode ('utf-16' ) + data1
277- await self ._teardown ()
278278
279279 @BlobPreparer ()
280280 @pytest .mark .parametrize ('a' , [True , 'md5' , 'crc64' ]) # a: validate_content
@@ -294,7 +294,6 @@ async def test_append_block_streaming(self, a, **kwargs):
294294
295295 result = await blob .download_blob ()
296296 assert await result .read () == content
297- await self ._teardown ()
298297
299298 @BlobPreparer ()
300299 @pytest .mark .parametrize ('a' , [True , 'md5' , 'crc64' ]) # a: validate_content
@@ -317,7 +316,6 @@ async def test_append_block_streaming_large(self, a, **kwargs):
317316
318317 result = await blob .download_blob ()
319318 assert await result .read () == data1 + data2 + data3
320- await self ._teardown ()
321319
322320 @BlobPreparer ()
323321 @pytest .mark .parametrize ('a' , [True , 'auto' , 'md5' , 'crc64' ]) # a: validate_content
@@ -341,7 +339,6 @@ async def test_upload_page(self, a, **kwargs):
341339 # Assert
342340 content = await blob .download_blob (offset = 0 , length = len (data1 ) + len (data2_encoded ))
343341 assert await content .read () == data1 + data2_encoded
344- await self ._teardown ()
345342
346343 @BlobPreparer ()
347344 @pytest .mark .parametrize ('a' , [True , 'auto' , 'md5' , 'crc64' ]) # a: validate_content
@@ -368,7 +365,6 @@ async def test_download_blob(self, a, **kwargs):
368365 # Assert
369366 assert content == data
370367 assert stream .read () == data
371- await self ._teardown ()
372368
373369 @BlobPreparer ()
374370 @pytest .mark .parametrize ('a' , [True , 'md5' , 'crc64' ]) # a: validate_content
@@ -403,7 +399,6 @@ async def test_download_blob_chunks(self, a, **kwargs):
403399 assert content == data
404400 assert stream .read () == data
405401 assert read_content == data
406- await self ._teardown ()
407402
408403 @BlobPreparer ()
409404 @pytest .mark .parametrize ('a' , [True , 'md5' , 'crc64' ]) # a: validate_content
@@ -432,7 +427,6 @@ async def test_download_blob_chunks_partial(self, a, **kwargs):
432427 # Assert
433428 assert content == data [10 :1010 ]
434429 assert stream .read () == data [10 :1010 ]
435- await self ._teardown ()
436430
437431 @BlobPreparer ()
438432 @pytest .mark .live_test_only
@@ -447,16 +441,15 @@ async def test_download_blob_large_chunks(self, **kwargs):
447441 await blob .upload_blob (data , overwrite = True , max_concurrency = 5 )
448442
449443 # Act
450- downloader = await blob .download_blob (validate_content = 'crc64' , max_concurrency = 3 )
444+ downloader = await blob .download_blob (validate_content = 'crc64' , max_concurrency = 5 )
451445 content = await downloader .read ()
452446
453- downloader = await blob .download_blob (offset = 5 * 1024 * 1024 , length = 25 * 1024 * 1024 )
447+ downloader = await blob .download_blob (offset = 5 * 1024 * 1024 , length = 25 * 1024 * 1024 , validate_content = 'crc64' )
454448 partial = await downloader .read ()
455449
456450 # Assert
457451 assert content == data
458452 assert partial == data [5 * 1024 * 1024 : 30 * 1024 * 1024 ]
459- await self ._teardown ()
460453
461454 @BlobPreparer ()
462455 @pytest .mark .parametrize ('a' , [True , 'md5' , 'crc64' ]) # a: validate_content
@@ -488,4 +481,3 @@ async def test_download_blob_chars(self, a, **kwargs):
488481
489482 result += await stream .readall ()
490483 assert result == data
491- await self ._teardown ()
0 commit comments