diff --git a/supabase/__init__.py b/supabase/__init__.py index a3d41fe5..adf895a2 100644 --- a/supabase/__init__.py +++ b/supabase/__init__.py @@ -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 @@ -36,7 +38,7 @@ # Version from .version import __version__ -__all__ = [ +__all__ = ( "acreate_client", "create_async_client", "AClient", @@ -67,4 +69,6 @@ "FunctionsError", "AuthorizationError", "NotConnectedError", -] + "SupabaseException", + "ASupabaseException", +) diff --git a/tests/_async/test_client.py b/tests/_async/test_client.py index 6dc23b9e..ad7f913a 100644 --- a/tests/_async/test_client.py +++ b/tests/_async/test_client.py @@ -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: diff --git a/tests/test_client.py b/tests/test_client.py index 672e5e6d..d8f0be0c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -6,7 +6,7 @@ import pytest -from supabase import Client, ClientOptions, create_client +from supabase import Client, ClientOptions, SupabaseException, create_client @pytest.mark.xfail( @@ -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: diff --git a/tests/test_client_options.py b/tests/test_client_options.py index ce2279b7..67d59a29 100644 --- a/tests/test_client_options.py +++ b/tests/test_client_options.py @@ -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") @@ -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"},