Skip to content

Commit 1d5aa55

Browse files
authored
Merge pull request #137 from supabase-community/move-subclients-tests-to-subclients
tests: move subclients tests to subclients
2 parents 2cd8826 + ea00e58 commit 1d5aa55

File tree

10 files changed

+169
-119
lines changed

10 files changed

+169
-119
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ jobs:
66
test:
77
name: Test / OS ${{ matrix.os }} / Python ${{ matrix.python-version }}
88
strategy:
9-
max-parallel: 1
109
matrix:
1110
os: [ubuntu-latest]
1211
python-version: [3.7, 3.8, 3.9, '3.10']

Makefile

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ install_poetry:
55
curl -sSL https://install.python-poetry.org | python -
66
poetry install
77

8-
tests: install tests_only tests_pre_commit
9-
108
tests_pre_commit:
119
poetry run pre-commit run --all-files
1210

13-
run_tests: tests
14-
1511
tests_only:
16-
export SUPABASE_TEST_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlhdCI6MTYzNTAwODQ4NywiZXhwIjoxOTUwNTg0NDg3fQ.l8IgkO7TQokGSc9OJoobXIVXsOXkilXl4Ak6SCX5qI8" &&\
17-
export SUPABASE_TEST_URL="https://ibrydvrsxoapzgtnhpso.supabase.co" &&\
1812
poetry run pytest --cov=./ --cov-report=xml --cov-report=html -vv
13+
14+
tests: install tests_only tests_pre_commit
15+
16+
run_tests: tests

poetry.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ isort = "^5.9.3"
3030
pytest-cov = "^3.0.0"
3131
commitizen = "^2.20.3"
3232
python-semantic-release = "^7.24.0"
33+
python-dotenv = "^0.19.2"
3334

3435
[tool.semantic_release]
3536
version_variable = "supabase/__init__.py:__version__"

supabase/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
from supabase.client import Client, create_client
55
from supabase.lib.auth_client import SupabaseAuthClient
66
from supabase.lib.realtime_client import SupabaseRealtimeClient
7-
from supabase.lib.storage_client import SupabaseStorageClient
7+
from supabase.lib.storage_client import StorageFileAPI, SupabaseStorageClient

supabase/lib/storage/storage_bucket_api.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class StorageBucketAPI:
4848
"""This class abstracts access to the endpoint to the Get, List, Empty, and Delete operations on a bucket"""
4949

5050
def __init__(
51-
self, url: str, headers: dict[str, str], is_async: bool = False
51+
self, url: str, headers: Dict[str, str], is_async: bool = False
5252
) -> None:
5353
self.url = url
5454
self.headers = headers
@@ -64,7 +64,7 @@ def _request(
6464
self,
6565
method: _RequestMethod,
6666
url: str,
67-
json: Optional[dict[Any, Any]] = None,
67+
json: Optional[Dict[Any, Any]] = None,
6868
response_class: Optional[Type] = None,
6969
) -> Any:
7070
if self._is_async:
@@ -76,7 +76,7 @@ def _sync_request(
7676
self,
7777
method: _RequestMethod,
7878
url: str,
79-
json: Optional[dict[Any, Any]] = None,
79+
json: Optional[Dict[Any, Any]] = None,
8080
response_class: Optional[Type] = None,
8181
) -> ResponseType:
8282
if isinstance(self._client, AsyncClient): # only to appease the type checker
@@ -102,7 +102,7 @@ async def _async_request(
102102
self,
103103
method: _RequestMethod,
104104
url: str,
105-
json: Optional[dict[Any, Any]] = None,
105+
json: Optional[Dict[Any, Any]] = None,
106106
response_class: Optional[Type] = None,
107107
) -> ResponseType:
108108
if isinstance(self._client, Client): # only to appease the type checker
@@ -124,7 +124,7 @@ async def _async_request(
124124
else:
125125
return response_class(**response_data)
126126

127-
def list_buckets(self) -> Union[list[Bucket], Awaitable[list[Bucket]], None]:
127+
def list_buckets(self) -> Union[List[Bucket], Awaitable[List[Bucket]], None]:
128128
"""Retrieves the details of all storage buckets within an existing product."""
129129
return self._request("GET", f"{self.url}/bucket", response_class=Bucket)
130130

@@ -140,7 +140,7 @@ def get_bucket(self, id: str) -> Union[Bucket, Awaitable[Bucket], None]:
140140

141141
def create_bucket(
142142
self, id: str, name: str = None, public: bool = False
143-
) -> Union[dict[str, str], Awaitable[dict[str, str]]]:
143+
) -> Union[Dict[str, str], Awaitable[Dict[str, str]]]:
144144
"""Creates a new storage bucket.
145145
146146
Parameters
@@ -158,7 +158,7 @@ def create_bucket(
158158
json={"id": id, "name": name or id, "public": public},
159159
)
160160

161-
def empty_bucket(self, id: str) -> Union[dict[str, str], Awaitable[dict[str, str]]]:
161+
def empty_bucket(self, id: str) -> Union[Dict[str, str], Awaitable[Dict[str, str]]]:
162162
"""Removes all objects inside a single bucket.
163163
164164
Parameters
@@ -170,7 +170,7 @@ def empty_bucket(self, id: str) -> Union[dict[str, str], Awaitable[dict[str, str
170170

171171
def delete_bucket(
172172
self, id: str
173-
) -> Union[dict[str, str], Awaitable[dict[str, str]]]:
173+
) -> Union[Dict[str, str], Awaitable[Dict[str, str]]]:
174174
"""Deletes an existing bucket. Note that you cannot delete buckets with existing objects inside. You must first
175175
`empty()` the bucket.
176176

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
import os
44

55
import pytest
6+
from dotenv import load_dotenv
67

78
from supabase import Client, create_client
89

910

11+
def pytest_configure(config) -> None:
12+
load_dotenv(dotenv_path="tests/tests.env")
13+
14+
1015
@pytest.fixture(scope="session")
1116
def supabase() -> Client:
1217
url = os.environ.get("SUPABASE_TEST_URL")

tests/test_client.py

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
from __future__ import annotations
22

3-
import random
4-
import string
5-
from typing import TYPE_CHECKING, Any, Union
3+
from typing import Any
64

75
import pytest
8-
from gotrue import Session, User
9-
10-
if TYPE_CHECKING:
11-
from supabase import Client
12-
13-
14-
def _random_string(length: int = 10) -> str:
15-
"""Generate random string."""
16-
return "".join(random.choices(string.ascii_uppercase + string.digits, k=length))
17-
18-
19-
def _assert_authenticated_user(data: Union[Session, User, str, None]) -> None:
20-
"""Raise assertion error if user is not logged in correctly."""
21-
assert data is not None
22-
assert isinstance(data, Session)
23-
assert data.user is not None
24-
assert data.user.aud == "authenticated"
256

267

278
@pytest.mark.xfail(
@@ -34,85 +15,3 @@ def test_incorrect_values_dont_instanciate_client(url: Any, key: Any) -> None:
3415
from supabase import Client, create_client
3516

3617
_: Client = create_client(url, key)
37-
38-
39-
@pytest.mark.skip(reason="TO FIX: Session does not terminate with test included.")
40-
def test_client_auth(supabase: Client) -> None:
41-
"""Ensure we can create an auth user, and login with it."""
42-
# Create a random user login email and password.
43-
random_email = f"{_random_string(10)}@supamail.com"
44-
random_password = _random_string(20)
45-
# Sign up (and sign in).
46-
user = supabase.auth.sign_up(
47-
email=random_email,
48-
password=random_password,
49-
phone=None,
50-
)
51-
_assert_authenticated_user(user)
52-
# Sign out.
53-
supabase.auth.sign_out()
54-
assert supabase.auth.user() is None
55-
assert supabase.auth.session() is None
56-
# Sign in (explicitly this time).
57-
user = supabase.auth.sign_in(email=random_email, password=random_password)
58-
_assert_authenticated_user(user)
59-
60-
61-
def test_client_select(supabase: Client) -> None:
62-
"""Ensure we can select data from a table."""
63-
# TODO(fedden): Add this set back in (and expand on it) when postgrest and
64-
# realtime libs are working.
65-
data, _ = supabase.table("countries").select("*").execute()
66-
# Assert we pulled real data.
67-
assert data
68-
69-
70-
def test_client_insert(supabase: Client) -> None:
71-
"""Ensure we can select data from a table."""
72-
data, _ = supabase.table("countries").select("*").execute()
73-
# Assert we pulled real data.
74-
previous_length = len(data)
75-
new_row = {
76-
"name": "test name",
77-
"iso2": "test iso2",
78-
"iso3": "test iso3",
79-
"local_name": "test local name",
80-
"continent": None,
81-
}
82-
result, _ = supabase.table("countries").insert(new_row).execute()
83-
# Check returned result for insert was valid.
84-
assert result
85-
data, _ = supabase.table("countries").select("*").execute()
86-
current_length = len(data)
87-
# Ensure we've added a row remotely.
88-
assert current_length == previous_length + 1
89-
90-
91-
@pytest.mark.skip(reason="missing permissions on test instance")
92-
def test_client_upload_file(supabase: Client) -> None:
93-
"""Ensure we can upload files to a bucket"""
94-
95-
TEST_BUCKET_NAME = "atestbucket"
96-
97-
storage = supabase.storage()
98-
storage_file = storage.StorageFileAPI(TEST_BUCKET_NAME)
99-
100-
filename = "test.jpeg"
101-
filepath = f"tests/{filename}"
102-
mimetype = "image/jpeg"
103-
options = {"contentType": mimetype}
104-
105-
storage_file.upload(filename, filepath, options)
106-
files = storage_file.list()
107-
assert files
108-
109-
image_info = None
110-
for item in files:
111-
if item.get("name") == filename:
112-
image_info = item
113-
break
114-
115-
assert image_info is not None
116-
assert image_info.get("metadata", {}).get("mimetype") == mimetype
117-
118-
storage_file.remove([filename])

0 commit comments

Comments
 (0)