Skip to content

Commit 873b85b

Browse files
authored
Merge pull request #25 from olirice/client_in_fixture
Reduce test code duplication via supabase Client in pytest fixture
2 parents 268bfe5 + 6bc5945 commit 873b85b

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

tests/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__ import annotations
2+
3+
import os
4+
5+
import pytest
6+
7+
from supabase_py import Client, create_client
8+
9+
10+
@pytest.fixture(scope="session")
11+
def supabase() -> Client:
12+
url: str = os.environ.get("SUPABASE_TEST_URL")
13+
key: str = os.environ.get("SUPABASE_TEST_KEY")
14+
supabase: Client = create_client(url, key)
15+
return supabase

tests/test_client.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
import os
1+
from __future__ import annotations
2+
23
import random
34
import string
4-
from typing import Any, Dict
5+
from typing import TYPE_CHECKING, Any, Dict
56

67
import pytest
78

9+
if TYPE_CHECKING:
10+
from supabase_py import Client, create_client
11+
812

913
def _random_string(length: int = 10) -> str:
1014
"""Generate random string."""
1115
return "".join(random.choices(string.ascii_uppercase + string.digits, k=length))
1216

1317

14-
def _assert_authenticated_user(data: Dict[str, Any]):
18+
def _assert_authenticated_user(data: Dict[str, Any]) -> None:
1519
"""Raise assertion error if user is not logged in correctly."""
1620
assert "access_token" in data
1721
assert "refresh_token" in data
@@ -27,20 +31,15 @@ def _assert_authenticated_user(data: Dict[str, Any]):
2731
)
2832
@pytest.mark.parametrize("url", ["", None, "valeefgpoqwjgpj", 139, -1, {}, []])
2933
@pytest.mark.parametrize("key", ["", None, "valeefgpoqwjgpj", 139, -1, {}, []])
30-
def test_incorrect_values_dont_instanciate_client(url: Any, key: Any):
34+
def test_incorrect_values_dont_instanciate_client(url: Any, key: Any) -> None:
3135
"""Ensure we can't instanciate client with nonesense values."""
32-
from supabase_py import create_client, Client
36+
from supabase_py import Client, create_client
3337

3438
_: Client = create_client(url, key)
3539

3640

37-
def test_client_auth():
41+
def test_client_auth(supabase: Client) -> None:
3842
"""Ensure we can create an auth user, and login with it."""
39-
from supabase_py import create_client, Client
40-
41-
url: str = os.environ.get("SUPABASE_TEST_URL")
42-
key: str = os.environ.get("SUPABASE_TEST_KEY")
43-
supabase: Client = create_client(url, key)
4443
# Create a random user login email and password.
4544
random_email: str = f"{_random_string(10)}@supamail.com"
4645
random_password: str = _random_string(20)
@@ -56,27 +55,17 @@ def test_client_auth():
5655
_assert_authenticated_user(user)
5756

5857

59-
def test_client_select():
58+
def test_client_select(supabase: Client) -> None:
6059
"""Ensure we can select data from a table."""
61-
from supabase_py import create_client, Client
62-
63-
url: str = os.environ.get("SUPABASE_TEST_URL")
64-
key: str = os.environ.get("SUPABASE_TEST_KEY")
65-
supabase: Client = create_client(url, key)
6660
# TODO(fedden): Add this set back in (and expand on it) when postgrest and
6761
# realtime libs are working.
6862
data = supabase.table("countries").select("*").execute()
6963
# Assert we pulled real data.
7064
assert len(data.get("data", [])) > 0
7165

7266

73-
def test_client_insert():
67+
def test_client_insert(supabase: Client) -> None:
7468
"""Ensure we can select data from a table."""
75-
from supabase_py import create_client, Client
76-
77-
url: str = os.environ.get("SUPABASE_TEST_URL")
78-
key: str = os.environ.get("SUPABASE_TEST_KEY")
79-
supabase: Client = create_client(url, key)
8069
data = supabase.table("countries").select("*").execute()
8170
# Assert we pulled real data.
8271
previous_length: int = len(data.get("data", []))

tests/test_dummy.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import pytest
22

3-
import sys
4-
print(sys.path)
5-
63
import supabase_py
74

85
"""
@@ -11,10 +8,11 @@
118
client.auth.sign_up({"email": "[email protected]", "password": "apassword"})
129
"""
1310

14-
def test_dummy():
11+
12+
def test_dummy() -> None:
1513
# Test auth component
1614
assert True == True
1715

18-
def test_client_initialziation():
19-
client = supabase_py.Client("http://testwebsite.com", "atestapi")
2016

17+
def test_client_initialziation() -> None:
18+
client = supabase_py.Client("http://testwebsite.com", "atestapi")

0 commit comments

Comments
 (0)