From 2cae0df10f6ef43d4bd4e008b7129308c53c13f1 Mon Sep 17 00:00:00 2001 From: dreinon Date: Fri, 4 Feb 2022 02:18:14 +0100 Subject: [PATCH] tests: track created buckets in a global variable to only delete these --- tests/test_storage.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/tests/test_storage.py b/tests/test_storage.py index e1e8fa42..b22dfbb1 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -14,15 +14,15 @@ from supabase import Client, StorageFileAPI, SupabaseStorageClient -UUID_PREFIX = "pytest-" +# Global variable to track the ids from the buckets created in the tests run +temp_test_buckets_ids = [] @pytest.fixture(scope="module") def uuid_factory() -> Callable[[], str]: def method() -> str: - """Generate a UUID""" - uuid = uuid4().hex[:8] # Get the first 8 digits part to make it shorter - return f"{UUID_PREFIX}{uuid}" + """Generate a 8 digits long UUID""" + return uuid4().hex[:8] return method @@ -37,24 +37,19 @@ def storage_client(supabase: Client) -> SupabaseStorageClient: def delete_left_buckets( request: pytest.FixtureRequest, storage_client: SupabaseStorageClient ): - """Ensures no test buckets are left""" + """Ensures no test buckets are left when a test that created a bucket fails""" def finalizer(): - buckets_list = storage_client.list_buckets() - if not buckets_list: - return - - for bucket in buckets_list: - if bucket.id.startswith(UUID_PREFIX): - try: - storage_client.empty_bucket(bucket.id) - storage_client.delete_bucket(bucket.id) - except StorageException as e: - # Ignore 404 responses since they mean the bucket was already deleted - response = e.args[0] - if response["statusCode"] != 404: - raise e - continue + for bucket in temp_test_buckets_ids: + try: + storage_client.empty_bucket(bucket.id) + storage_client.delete_bucket(bucket.id) + except StorageException as e: + # Ignore 404 responses since they mean the bucket was already deleted + response = e.args[0] + if response["statusCode"] != 404: + raise e + continue request.addfinalizer(finalizer) @@ -65,6 +60,11 @@ def bucket( ) -> str: """Creates a test bucket which will be used in the whole storage tests run and deleted at the end""" bucket_id = uuid_factory() + + # Store bucket_id in global list + global temp_test_buckets_ids + temp_test_buckets_ids.append(bucket_id) + storage_client.create_bucket(id=bucket_id) yield bucket_id @@ -72,6 +72,8 @@ def bucket( storage_client.empty_bucket(bucket_id) storage_client.delete_bucket(bucket_id) + temp_test_buckets_ids.remove(bucket_id) + @pytest.fixture(scope="module") def storage_file_client(