|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import asyncio |
4 | 5 | from typing import Any |
5 | | -from unittest.mock import MagicMock |
| 6 | +from unittest.mock import MagicMock, AsyncMock |
6 | 7 |
|
7 | 8 | import pytest |
8 | 9 |
|
9 | | -from supabase import Client, ClientOptions, create_client |
| 10 | +from supabase import AsyncClient, ClientOptions, create_client |
10 | 11 |
|
11 | 12 |
|
12 | 13 | @pytest.mark.xfail( |
|
16 | 17 | @pytest.mark.parametrize("key", ["", None, "valeefgpoqwjgpj", 139, -1, {}, []]) |
17 | 18 | def test_incorrect_values_dont_instantiate_client(url: Any, key: Any) -> None: |
18 | 19 | """Ensure we can't instantiate client with invalid values.""" |
19 | | - _: Client = create_client(url, key) |
| 20 | + _: AsyncClient = create_client(url, key) |
20 | 21 |
|
21 | 22 |
|
22 | 23 | def test_uses_key_as_authorization_header_by_default() -> None: |
@@ -91,3 +92,50 @@ def test_updates_the_authorization_header_on_auth_events() -> None: |
91 | 92 |
|
92 | 93 | assert client.storage.session.headers.get("apiKey") == key |
93 | 94 | assert client.storage.session.headers.get("Authorization") == updated_authorization |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.asyncio |
| 98 | +async def test_connect_to_realtime_success(mocker): |
| 99 | + url = os.environ.get("SUPABASE_TEST_URL") |
| 100 | + key = os.environ.get("SUPABASE_TEST_KEY") |
| 101 | + |
| 102 | + client = create_client(url, key) |
| 103 | + mock_connect = mocker.patch.object(client.realtime, 'connect', return_value=None) |
| 104 | + |
| 105 | + await client.connect_to_realtime() |
| 106 | + mock_connect.assert_called_once() |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.asyncio |
| 110 | +async def test_connect_to_realtime_failure(mocker): |
| 111 | + url = os.environ.get("SUPABASE_TEST_URL") |
| 112 | + key = os.environ.get("SUPABASE_TEST_KEY") |
| 113 | + |
| 114 | + client = create_client(url, key) |
| 115 | + mock_connect = mocker.patch.object(client.realtime, 'connect', side_effect=Exception("Connection failed")) |
| 116 | + |
| 117 | + with pytest.raises(Exception, match="Connection failed"): |
| 118 | + await client.connect_to_realtime() |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.asyncio |
| 122 | +async def test_reconnect_logic(mocker): |
| 123 | + url = os.environ.get("SUPABASE_TEST_URL") |
| 124 | + key = os.environ.get("SUPABASE_TEST_KEY") |
| 125 | + |
| 126 | + client = create_client(url, key) |
| 127 | + mock_reconnect = mocker.patch.object(client.realtime, 'connect', side_effect=[Exception("Failed"), None]) |
| 128 | + |
| 129 | + await client.connect_to_realtime() # First attempt fails |
| 130 | + await client.connect_to_realtime() # Second attempt should succeed |
| 131 | + assert mock_reconnect.call_count == 2 # Check that it tried to reconnect |
| 132 | + |
| 133 | + |
| 134 | +def test_logging_on_connection_failure(caplog): |
| 135 | + url = os.environ.get("SUPABASE_TEST_URL") |
| 136 | + key = os.environ.get("SUPABASE_TEST_KEY") |
| 137 | + |
| 138 | + client = create_client(url, key) |
| 139 | + with caplog.at_level(logging.ERROR): |
| 140 | + await client.connect_to_realtime() # Assume it fails |
| 141 | + assert "Connection failed" in caplog.text |
0 commit comments