Skip to content

Commit

Permalink
chore(tests): increase coverage (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlospaco authored Feb 4, 2025
1 parent 29fed38 commit 614cacc
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
8 changes: 6 additions & 2 deletions supabase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
from ._async.client import AsyncClient
from ._async.client import AsyncClient as AClient
from ._async.client import AsyncStorageClient as ASupabaseStorageClient
from ._async.client import SupabaseException as ASupabaseException
from ._async.client import create_client as acreate_client
from ._async.client import create_client as create_async_client

# Sync Client
from ._sync.auth_client import SyncSupabaseAuthClient as SupabaseAuthClient
from ._sync.client import SupabaseException
from ._sync.client import SyncClient as Client
from ._sync.client import SyncStorageClient as SupabaseStorageClient
from ._sync.client import create_client
Expand All @@ -36,7 +38,7 @@
# Version
from .version import __version__

__all__ = [
__all__ = (
"acreate_client",
"create_async_client",
"AClient",
Expand Down Expand Up @@ -67,4 +69,6 @@
"FunctionsError",
"AuthorizationError",
"NotConnectedError",
]
"SupabaseException",
"ASupabaseException",
)
50 changes: 49 additions & 1 deletion tests/_async/test_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
import os
from unittest.mock import AsyncMock, MagicMock

from supabase import create_async_client
from supabase import AClient, ASupabaseException, create_async_client


async def test_incorrect_values_dont_instantiate_client() -> None:
"""Ensure we can't instantiate client with invalid values."""
try:
client: AClient = create_async_client(None, None)
except ASupabaseException:
pass


async def test_supabase_exception() -> None:
try:
raise ASupabaseException("err")
except ASupabaseException:
pass


async def test_postgrest_client() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")

client = await create_async_client(url, key)
assert client.table("sample")


async def test_rpc_client() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")

client = await create_async_client(url, key)
assert client.rpc("test_fn")


async def test_function_initialization() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")

client = await create_async_client(url, key)
assert client.functions


async def test_schema_update() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")

client = await create_async_client(url, key)
assert client.postgrest
assert client.schema("new_schema")


async def test_updates_the_authorization_header_on_auth_events() -> None:
Expand Down
32 changes: 30 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from supabase import Client, ClientOptions, create_client
from supabase import Client, ClientOptions, SupabaseException, create_client


@pytest.mark.xfail(
Expand All @@ -16,7 +16,35 @@
@pytest.mark.parametrize("key", ["", None, "valeefgpoqwjgpj", 139, -1, {}, []])
def test_incorrect_values_dont_instantiate_client(url: Any, key: Any) -> None:
"""Ensure we can't instantiate client with invalid values."""
_: Client = create_client(url, key)
try:
_: Client = create_client(url, key)
except SupabaseException as e:
pass


def test_function_initialization() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")

client = create_client(url, key)
assert client.functions


def test_postgrest_schema() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")

client = create_client(url, key)
assert client.postgrest
assert client.postgrest.schema("new_schema")


def test_rpc_client() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")

client = create_client(url, key)
assert client.rpc("test_fn")


def test_uses_key_as_authorization_header_by_default() -> None:
Expand Down
28 changes: 27 additions & 1 deletion tests/test_client_options.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
from gotrue import SyncMemoryStorage

from supabase import ClientOptions
from supabase import AClientOptions, ClientOptions


class TestClientOptions:

def test_replace_returns_updated_aclient_options(self):
storage = SyncMemoryStorage()
storage.set_item("key", "value")
options = AClientOptions(
schema="schema",
headers={"key": "value"},
auto_refresh_token=False,
persist_session=False,
storage=storage,
realtime={"key": "value"},
)

actual = options.replace(schema="new schema")
expected = AClientOptions(
schema="new schema",
headers={"key": "value"},
auto_refresh_token=False,
persist_session=False,
storage=storage,
realtime={"key": "value"},
)

assert actual == expected

def test_replace_returns_updated_options(self):
storage = SyncMemoryStorage()
storage.set_item("key", "value")
Expand All @@ -17,6 +42,7 @@ def test_replace_returns_updated_options(self):
)

actual = options.replace(schema="new schema")
assert actual
expected = ClientOptions(
schema="new schema",
headers={"key": "value"},
Expand Down

0 comments on commit 614cacc

Please sign in to comment.