Skip to content

Commit 2cae0df

Browse files
committed
tests: track created buckets in a global variable to only delete these
1 parent b6d2135 commit 2cae0df

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

tests/test_storage.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
from supabase import Client, StorageFileAPI, SupabaseStorageClient
1515

1616

17-
UUID_PREFIX = "pytest-"
17+
# Global variable to track the ids from the buckets created in the tests run
18+
temp_test_buckets_ids = []
1819

1920

2021
@pytest.fixture(scope="module")
2122
def uuid_factory() -> Callable[[], str]:
2223
def method() -> str:
23-
"""Generate a UUID"""
24-
uuid = uuid4().hex[:8] # Get the first 8 digits part to make it shorter
25-
return f"{UUID_PREFIX}{uuid}"
24+
"""Generate a 8 digits long UUID"""
25+
return uuid4().hex[:8]
2626

2727
return method
2828

@@ -37,24 +37,19 @@ def storage_client(supabase: Client) -> SupabaseStorageClient:
3737
def delete_left_buckets(
3838
request: pytest.FixtureRequest, storage_client: SupabaseStorageClient
3939
):
40-
"""Ensures no test buckets are left"""
40+
"""Ensures no test buckets are left when a test that created a bucket fails"""
4141

4242
def finalizer():
43-
buckets_list = storage_client.list_buckets()
44-
if not buckets_list:
45-
return
46-
47-
for bucket in buckets_list:
48-
if bucket.id.startswith(UUID_PREFIX):
49-
try:
50-
storage_client.empty_bucket(bucket.id)
51-
storage_client.delete_bucket(bucket.id)
52-
except StorageException as e:
53-
# Ignore 404 responses since they mean the bucket was already deleted
54-
response = e.args[0]
55-
if response["statusCode"] != 404:
56-
raise e
57-
continue
43+
for bucket in temp_test_buckets_ids:
44+
try:
45+
storage_client.empty_bucket(bucket.id)
46+
storage_client.delete_bucket(bucket.id)
47+
except StorageException as e:
48+
# Ignore 404 responses since they mean the bucket was already deleted
49+
response = e.args[0]
50+
if response["statusCode"] != 404:
51+
raise e
52+
continue
5853

5954
request.addfinalizer(finalizer)
6055

@@ -65,13 +60,20 @@ def bucket(
6560
) -> str:
6661
"""Creates a test bucket which will be used in the whole storage tests run and deleted at the end"""
6762
bucket_id = uuid_factory()
63+
64+
# Store bucket_id in global list
65+
global temp_test_buckets_ids
66+
temp_test_buckets_ids.append(bucket_id)
67+
6868
storage_client.create_bucket(id=bucket_id)
6969

7070
yield bucket_id
7171

7272
storage_client.empty_bucket(bucket_id)
7373
storage_client.delete_bucket(bucket_id)
7474

75+
temp_test_buckets_ids.remove(bucket_id)
76+
7577

7678
@pytest.fixture(scope="module")
7779
def storage_file_client(

0 commit comments

Comments
 (0)