Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/realtime/src/realtime/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ class PostgresChangesCallback:
select: Optional[list[str]] = None
id: Optional[int] = None

def __post_init__(self) -> None:
if not self.schema:
self.schema = "public"

def __call__(self, payload: PostgresChangesPayload) -> None:
event_matches = (
self.event == payload["data"]["type"]
Expand All @@ -147,9 +151,7 @@ def __call__(self, payload: PostgresChangesPayload) -> None:

@property
def binding_filter(self) -> dict[str, Any]:
binding: dict[str, Any] = {"event": self.event}
if self.schema:
binding["schema"] = self.schema
binding: dict[str, Any] = {"event": self.event, "schema": self.schema}
if self.table:
binding["table"] = self.table
if self.filter:
Expand Down
82 changes: 80 additions & 2 deletions src/realtime/tests/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import datetime
import os
from typing import Optional
from unittest.mock import AsyncMock, patch
from urllib.parse import parse_qs, urlparse

Expand All @@ -15,8 +16,13 @@
RealtimePostgresChangesListenEvent,
RealtimeSubscribeStates,
)
from realtime.message import Message
from realtime.types import DEFAULT_HEARTBEAT_INTERVAL, DEFAULT_TIMEOUT, ChannelEvents
from realtime.message import Message, ReplyPostgresChanges
from realtime.types import (
DEFAULT_HEARTBEAT_INTERVAL,
DEFAULT_TIMEOUT,
ChannelEvents,
RealtimeAcknowledgementStatus,
)

load_dotenv()

Expand Down Expand Up @@ -725,6 +731,7 @@ def test_postgres_changes_callback_binding_filter_includes_select() -> None:
)
assert with_select.binding_filter == {
"event": RealtimePostgresChangesListenEvent.Insert,
"schema": "public",
"table": "todos",
"filter": "id=eq.1",
"select": ["id", "name"],
Expand Down Expand Up @@ -787,6 +794,77 @@ async def test_subscribe_forwards_select_in_postgres_changes(
await socket.close()


@pytest.mark.asyncio
async def test_subscribe_defaults_postgres_changes_schema_to_public(
socket: AsyncRealtimeClient,
) -> None:
import json

mock_ws = AsyncMock()
socket._ws_connection = mock_ws
await socket.connect()

channel: AsyncRealtimeChannel = socket.channel("test-default-schema")
channel.on_postgres_changes(RealtimePostgresChangesListenEvent.All, lambda _: None)
channel.on_postgres_changes(
RealtimePostgresChangesListenEvent.Insert,
lambda _: None,
table="todos",
filter="id=eq.1",
)

await channel.subscribe(lambda state, error: None)

message_data = json.loads(mock_ws.send.call_args[0][0])
assert message_data["payload"]["config"]["postgres_changes"] == [
{"event": RealtimePostgresChangesListenEvent.All, "schema": "public"},
{
"event": RealtimePostgresChangesListenEvent.Insert,
"schema": "public",
"table": "todos",
"filter": "id=eq.1",
},
]

await socket.close()


@pytest.mark.asyncio
async def test_subscribe_accepts_server_echo_of_default_schema(
socket: AsyncRealtimeClient,
) -> None:
socket._ws_connection = AsyncMock()
await socket.connect()

statuses: list[tuple[RealtimeSubscribeStates, Optional[Exception]]] = []
channel: AsyncRealtimeChannel = socket.channel("test-default-schema-reply")
channel.on_postgres_changes(
RealtimePostgresChangesListenEvent.Insert, lambda _: None, table="todos"
)
await channel.subscribe(lambda state, error: statuses.append((state, error)))

channel.join_push.trigger(
RealtimeAcknowledgementStatus.Ok,
ReplyPostgresChanges.model_validate(
{
"postgres_changes": [
{
"id": 42,
"event": RealtimePostgresChangesListenEvent.Insert,
"schema": "public",
"table": "todos",
}
]
}
),
)

assert statuses == [(RealtimeSubscribeStates.SUBSCRIBED, None)]
assert channel.postgres_changes_callbacks[0].id == 42

await socket.close()


@pytest.mark.asyncio
async def test_subscribe_forwards_broadcast_replication_ready(
socket: AsyncRealtimeClient,
Expand Down
Loading