Skip to content

Commit

Permalink
test: handle exceptions and use context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
leynier committed Oct 18, 2021
1 parent 0c29382 commit b8330c0
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 134 deletions.
96 changes: 48 additions & 48 deletions tests/_async/test_api_with_auto_confirm_disabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ def create_api() -> AsyncGoTrueApi:
@pytest.mark.asyncio
async def test_sign_up_with_email_and_password():
try:
api = create_api()
response = await api.sign_up_with_email(
email,
password,
redirect_to="https://localhost:9999/welcome",
data={"status": "alpha"},
)
assert isinstance(response, User)
except Exception:
assert False
async with create_api() as api:
response = await api.sign_up_with_email(
email,
password,
redirect_to="https://localhost:9999/welcome",
data={"status": "alpha"},
)
assert isinstance(response, User)
except Exception as e:
assert False, str(e)


email2 = f"api_generate_link_signup_{fake.email().lower()}"
Expand All @@ -44,17 +44,17 @@ async def test_sign_up_with_email_and_password():
@pytest.mark.asyncio
async def test_generate_sign_up_link():
try:
api = create_api()
response = await api.generate_link(
type=LinkType.signup,
email=email2,
password=password2,
redirect_to="https://localhost:9999/welcome",
data={"status": "alpha"},
)
assert isinstance(response, User)
except Exception:
assert False
async with create_api() as api:
response = await api.generate_link(
type=LinkType.signup,
email=email2,
password=password2,
redirect_to="https://localhost:9999/welcome",
data={"status": "alpha"},
)
assert isinstance(response, User)
except Exception as e:
assert False, str(e)


email3 = f"api_generate_link_signup_{fake.email().lower()}"
Expand All @@ -63,41 +63,41 @@ async def test_generate_sign_up_link():
@pytest.mark.asyncio
async def test_generate_magic_link():
try:
api = create_api()
response = await api.generate_link(
type=LinkType.magiclink,
email=email3,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception:
assert False
async with create_api() as api:
response = await api.generate_link(
type=LinkType.magiclink,
email=email3,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception as e:
assert False, str(e)


@pytest.mark.asyncio
async def test_generate_invite_link():
try:
api = create_api()
response = await api.generate_link(
type=LinkType.invite,
email=email3,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception:
assert False
async with create_api() as api:
response = await api.generate_link(
type=LinkType.invite,
email=email3,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception as e:
assert False, str(e)


@pytest.mark.asyncio
@pytest.mark.depends(on=[test_sign_up_with_email_and_password.__name__])
async def test_generate_recovery_link():
try:
api = create_api()
response = await api.generate_link(
type=LinkType.recovery,
email=email,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception:
assert False
async with create_api() as api:
response = await api.generate_link(
type=LinkType.recovery,
email=email,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception as e:
assert False, str(e)
50 changes: 34 additions & 16 deletions tests/_async/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,53 @@
from gotrue import AsyncGoTrueClient
from gotrue.types import Provider

client = AsyncGoTrueClient(auto_refresh_token=False, persist_session=False)

def create_client() -> AsyncGoTrueClient:
return AsyncGoTrueClient(auto_refresh_token=False, persist_session=False)


@pytest.mark.asyncio
async def test_sign_in_with_provider():
response = await client.sign_in(provider=Provider.google)
assert isinstance(response, str)
try:
async with create_client() as client:
response = await client.sign_in(provider=Provider.google)
assert isinstance(response, str)
except Exception as e:
assert False, str(e)


@pytest.mark.asyncio
async def test_sign_in_with_provider_can_append_a_redirect_url():
response = await client.sign_in(
provider=Provider.google,
redirect_to="https://localhost:9000/welcome",
)
assert isinstance(response, str)
try:
async with create_client() as client:
response = await client.sign_in(
provider=Provider.google,
redirect_to="https://localhost:9000/welcome",
)
assert isinstance(response, str)
except Exception as e:
assert False, str(e)


@pytest.mark.asyncio
async def test_sign_in_with_provider_can_append_scopes():
response = await client.sign_in(provider=Provider.google, scopes="repo")
assert isinstance(response, str)
try:
async with create_client() as client:
response = await client.sign_in(provider=Provider.google, scopes="repo")
assert isinstance(response, str)
except Exception as e:
assert False, str(e)


@pytest.mark.asyncio
async def test_sign_in_with_provider_can_append_multiple_options():
response = await client.sign_in(
provider=Provider.google,
redirect_to="https://localhost:9000/welcome",
scopes="repo",
)
assert isinstance(response, str)
try:
async with create_client() as client:
response = await client.sign_in(
provider=Provider.google,
redirect_to="https://localhost:9000/welcome",
scopes="repo",
)
assert isinstance(response, str)
except Exception as e:
assert False, str(e)
12 changes: 9 additions & 3 deletions tests/_async/test_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@


def test_subscribe_a_listener():
assert len(client.state_change_emitters)
try:
assert len(client.state_change_emitters)
except Exception as e:
assert False, str(e)


@pytest.mark.depends(on=[test_subscribe_a_listener.__name__])
def test_unsubscribe_a_listener():
subscription.unsubscribe()
assert not len(client.state_change_emitters)
try:
subscription.unsubscribe()
assert not len(client.state_change_emitters)
except Exception as e:
assert False, str(e)
96 changes: 48 additions & 48 deletions tests/_sync/test_api_with_auto_confirm_disabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ def create_api() -> SyncGoTrueApi:
@pytest.mark.asyncio
def test_sign_up_with_email_and_password():
try:
api = create_api()
response = api.sign_up_with_email(
email,
password,
redirect_to="https://localhost:9999/welcome",
data={"status": "alpha"},
)
assert isinstance(response, User)
except Exception:
assert False
with create_api() as api:
response = api.sign_up_with_email(
email,
password,
redirect_to="https://localhost:9999/welcome",
data={"status": "alpha"},
)
assert isinstance(response, User)
except Exception as e:
assert False, str(e)


email2 = f"api_generate_link_signup_{fake.email().lower()}"
Expand All @@ -44,17 +44,17 @@ def test_sign_up_with_email_and_password():
@pytest.mark.asyncio
def test_generate_sign_up_link():
try:
api = create_api()
response = api.generate_link(
type=LinkType.signup,
email=email2,
password=password2,
redirect_to="https://localhost:9999/welcome",
data={"status": "alpha"},
)
assert isinstance(response, User)
except Exception:
assert False
with create_api() as api:
response = api.generate_link(
type=LinkType.signup,
email=email2,
password=password2,
redirect_to="https://localhost:9999/welcome",
data={"status": "alpha"},
)
assert isinstance(response, User)
except Exception as e:
assert False, str(e)


email3 = f"api_generate_link_signup_{fake.email().lower()}"
Expand All @@ -63,41 +63,41 @@ def test_generate_sign_up_link():
@pytest.mark.asyncio
def test_generate_magic_link():
try:
api = create_api()
response = api.generate_link(
type=LinkType.magiclink,
email=email3,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception:
assert False
with create_api() as api:
response = api.generate_link(
type=LinkType.magiclink,
email=email3,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception as e:
assert False, str(e)


@pytest.mark.asyncio
def test_generate_invite_link():
try:
api = create_api()
response = api.generate_link(
type=LinkType.invite,
email=email3,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception:
assert False
with create_api() as api:
response = api.generate_link(
type=LinkType.invite,
email=email3,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception as e:
assert False, str(e)


@pytest.mark.asyncio
@pytest.mark.depends(on=[test_sign_up_with_email_and_password.__name__])
def test_generate_recovery_link():
try:
api = create_api()
response = api.generate_link(
type=LinkType.recovery,
email=email,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception:
assert False
with create_api() as api:
response = api.generate_link(
type=LinkType.recovery,
email=email,
redirect_to="https://localhost:9999/welcome",
)
assert isinstance(response, User)
except Exception as e:
assert False, str(e)

0 comments on commit b8330c0

Please sign in to comment.