From e1c3b900e5ad476fe858bec72e08aab08e6b2648 Mon Sep 17 00:00:00 2001 From: Oliver Rice Date: Wed, 19 May 2021 07:38:18 -0500 Subject: [PATCH 1/3] reduce test duplication via supabase client in pytest fixture --- tests/conftest.py | 15 +++++++++++++++ tests/test_client.py | 34 ++++++++++++---------------------- tests/test_dummy.py | 6 ++---- 3 files changed, 29 insertions(+), 26 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..05dc4b19 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +import os + +import pytest + +from supabase_py import Client, create_client + + +@pytest.fixture(scope="function") +def supabase() -> Client: + url: str = os.environ.get("SUPABASE_TEST_URL") + key: str = os.environ.get("SUPABASE_TEST_KEY") + supabase: Client = create_client(url, key) + return supabase diff --git a/tests/test_client.py b/tests/test_client.py index 1cde4110..61ae5bcb 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,17 +1,22 @@ +from __future__ import annotations + import os import random import string -from typing import Any, Dict +from typing import TYPE_CHECKING, Any, Dict import pytest +if TYPE_CHECKING: + from supabase_py import Client, create_client + def _random_string(length: int = 10) -> str: """Generate random string.""" return "".join(random.choices(string.ascii_uppercase + string.digits, k=length)) -def _assert_authenticated_user(data: Dict[str, Any]): +def _assert_authenticated_user(data: Dict[str, Any]) -> None: """Raise assertion error if user is not logged in correctly.""" assert "access_token" in data assert "refresh_token" in data @@ -27,20 +32,15 @@ def _assert_authenticated_user(data: Dict[str, Any]): ) @pytest.mark.parametrize("url", ["", None, "valeefgpoqwjgpj", 139, -1, {}, []]) @pytest.mark.parametrize("key", ["", None, "valeefgpoqwjgpj", 139, -1, {}, []]) -def test_incorrect_values_dont_instanciate_client(url: Any, key: Any): +def test_incorrect_values_dont_instanciate_client(url: Any, key: Any) -> None: """Ensure we can't instanciate client with nonesense values.""" - from supabase_py import create_client, Client + from supabase_py import Client, create_client _: Client = create_client(url, key) -def test_client_auth(): +def test_client_auth(supabase: Client) -> None: """Ensure we can create an auth user, and login with it.""" - from supabase_py import create_client, Client - - url: str = os.environ.get("SUPABASE_TEST_URL") - key: str = os.environ.get("SUPABASE_TEST_KEY") - supabase: Client = create_client(url, key) # Create a random user login email and password. random_email: str = f"{_random_string(10)}@supamail.com" random_password: str = _random_string(20) @@ -56,13 +56,8 @@ def test_client_auth(): _assert_authenticated_user(user) -def test_client_select(): +def test_client_select(supabase: Client) -> None: """Ensure we can select data from a table.""" - from supabase_py import create_client, Client - - url: str = os.environ.get("SUPABASE_TEST_URL") - key: str = os.environ.get("SUPABASE_TEST_KEY") - supabase: Client = create_client(url, key) # TODO(fedden): Add this set back in (and expand on it) when postgrest and # realtime libs are working. data = supabase.table("countries").select("*").execute() @@ -70,13 +65,8 @@ def test_client_select(): assert len(data.get("data", [])) > 0 -def test_client_insert(): +def test_client_insert(supabase: Client) -> None: """Ensure we can select data from a table.""" - from supabase_py import create_client, Client - - url: str = os.environ.get("SUPABASE_TEST_URL") - key: str = os.environ.get("SUPABASE_TEST_KEY") - supabase: Client = create_client(url, key) data = supabase.table("countries").select("*").execute() # Assert we pulled real data. previous_length: int = len(data.get("data", [])) diff --git a/tests/test_dummy.py b/tests/test_dummy.py index c9fa8ef5..597b8184 100644 --- a/tests/test_dummy.py +++ b/tests/test_dummy.py @@ -1,8 +1,5 @@ import pytest -import sys -print(sys.path) - import supabase_py """ @@ -11,10 +8,11 @@ client.auth.sign_up({"email": "anemail@gmail.com", "password": "apassword"}) """ + def test_dummy(): # Test auth component assert True == True + def test_client_initialziation(): client = supabase_py.Client("http://testwebsite.com", "atestapi") - From 0cf02da5cdc4aa25827343f1a0431e1cc0dfb779 Mon Sep 17 00:00:00 2001 From: Oliver Rice Date: Wed, 19 May 2021 07:45:58 -0500 Subject: [PATCH 2/3] session scope for pytest client fixture --- tests/conftest.py | 2 +- tests/test_dummy.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 05dc4b19..a6d3c3be 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,7 +7,7 @@ from supabase_py import Client, create_client -@pytest.fixture(scope="function") +@pytest.fixture(scope="session") def supabase() -> Client: url: str = os.environ.get("SUPABASE_TEST_URL") key: str = os.environ.get("SUPABASE_TEST_KEY") diff --git a/tests/test_dummy.py b/tests/test_dummy.py index 597b8184..677cc97f 100644 --- a/tests/test_dummy.py +++ b/tests/test_dummy.py @@ -9,10 +9,10 @@ """ -def test_dummy(): +def test_dummy() -> None: # Test auth component assert True == True -def test_client_initialziation(): +def test_client_initialziation() -> None: client = supabase_py.Client("http://testwebsite.com", "atestapi") From 6bc59458f52fa1af68fc100109fcd7cffb427177 Mon Sep 17 00:00:00 2001 From: Oliver Rice Date: Wed, 19 May 2021 07:52:36 -0500 Subject: [PATCH 3/3] remove unused import --- tests/test_client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_client.py b/tests/test_client.py index 61ae5bcb..54fee7ac 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,6 +1,5 @@ from __future__ import annotations -import os import random import string from typing import TYPE_CHECKING, Any, Dict