Skip to content

Commit 118862e

Browse files
author
Dani Reinón
committed
tests: make uuid fixture a factory
1 parent ac9e9c4 commit 118862e

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

tests/test_storage.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,62 @@
11
from __future__ import annotations
22

3-
from pathlib import Path
43
from time import sleep
54
from typing import TYPE_CHECKING
65
from uuid import uuid4
76

87
import pytest
98

109
if TYPE_CHECKING:
11-
from typing import Any, Dict, List
10+
from pathlib import Path
11+
from typing import Any, Dict, List, Callable
1212

1313
from supabase import Client, StorageFileAPI, SupabaseStorageClient
1414

1515

16+
UUID_PREFIX = "pytest-"
17+
18+
19+
@pytest.fixture(scope="module")
20+
def uuid_factory() -> Callable[[], str]:
21+
def method() -> str:
22+
"""Generate a UUID"""
23+
uuid = uuid4().hex[:8] # Get the first 8 digits part to make it shorter
24+
return f"{UUID_PREFIX}{uuid}"
25+
26+
return method
27+
28+
1629
@pytest.fixture(scope="module")
1730
def storage_client(supabase: Client) -> SupabaseStorageClient:
1831
"""Creates the storage client for the whole storage tests run"""
1932
return supabase.storage()
2033

2134

2235
@pytest.fixture(scope="module", autouse=True)
23-
def delete_left_buckets(request, storage_client: SupabaseStorageClient):
36+
def delete_left_buckets(
37+
request: pytest.FixtureRequest, storage_client: SupabaseStorageClient
38+
):
2439
"""Ensures no test buckets are left"""
2540

2641
def finalizer():
2742
# Sleep 15 seconds in order to let buckets be deleted before the double-check
2843
sleep(15)
29-
for bucket in storage_client.list_buckets():
30-
if bucket.id.startswith("pytest-"):
44+
buckets_list = storage_client.list_buckets()
45+
if not buckets_list:
46+
return
47+
48+
for bucket in buckets_list:
49+
if bucket.id.startswith(UUID_PREFIX):
3150
storage_client.empty_bucket(bucket.id)
3251
storage_client.delete_bucket(bucket.id)
3352

3453
request.addfinalizer(finalizer)
3554

3655

37-
@pytest.fixture
38-
def uuid() -> str:
39-
# Get the first 8 digits part to make it shorter
40-
uuid = uuid4().hex[:8]
41-
return f"pytest-{uuid}"
42-
43-
4456
@pytest.fixture(scope="module")
45-
def bucket(storage_client: SupabaseStorageClient, uuid: str) -> str:
57+
def bucket(storage_client: SupabaseStorageClient, uuid_factory: Callable[[], str]) -> str:
4658
"""Creates a test bucket which will be used in the whole storage tests run and deleted at the end"""
47-
bucket_id = uuid
59+
bucket_id = uuid_factory()
4860
storage_client.create_bucket(id=bucket_id)
4961

5062
yield bucket_id
@@ -62,7 +74,7 @@ def storage_file_client(
6274

6375

6476
@pytest.fixture
65-
def file(tmp_path: Path, uuid: str) -> Dict[str, str]:
77+
def file(tmp_path: Path, uuid_factory: Callable[[], str]) -> Dict[str, str]:
6678
"""Creates a different test file (same content but different path) for each test"""
6779
file_name = "test_image.svg"
6880
file_content = (
@@ -78,7 +90,7 @@ def file(tmp_path: Path, uuid: str) -> Dict[str, str]:
7890
b'</linearGradient> <linearGradient id="paint1_linear" x1="36.1558" y1="30.578" x2="54.4844" y2="65.0806" '
7991
b'gradientUnits="userSpaceOnUse"> <stop/> <stop offset="1" stop-opacity="0"/> </linearGradient> </defs> </svg>'
8092
)
81-
bucket_folder = uuid
93+
bucket_folder = uuid_factory()
8294
bucket_path = f"{bucket_folder}/{file_name}"
8395
file_path = tmp_path / file_name
8496
with open(file_path, "wb") as f:

0 commit comments

Comments
 (0)