Skip to content

Commit c3583e6

Browse files
committed
fix: tests
1 parent feb579b commit c3583e6

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

lib/extensions/postgres_cdc_rls/subscriptions.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Extensions.PostgresCdcRls.Subscriptions do
88

99
@type conn() :: Postgrex.conn()
1010
@type filter :: {binary, binary, binary}
11-
@type subscription_params :: {binary, binary, binary, [filter]}
11+
@type subscription_params :: {action_filter :: binary, schema :: binary, table :: binary, [filter]}
1212
@type subscription_list :: [%{id: binary, claims: map, subscription_params: subscription_params}]
1313

1414
@filter_types ["eq", "neq", "lt", "lte", "gt", "gte", "in"]

test/integration/rt_channel_test.exs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ defmodule Realtime.Integration.RtChannelTest do
3434
{:ok, conn} = Database.connect(tenant, "realtime_test")
3535

3636
# Let's drop the publication to cause an error
37-
Database.transaction(conn, fn db_conn ->
38-
Postgrex.query!(db_conn, "drop publication if exists supabase_realtime_test")
39-
end)
37+
{:ok, _} =
38+
Database.transaction(conn, fn db_conn ->
39+
Postgrex.query!(db_conn, "drop publication if exists supabase_realtime_test")
40+
end)
4041

4142
{socket, _} = get_connection(tenant, serializer)
4243
topic = "realtime:any"
@@ -52,7 +53,7 @@ defmodule Realtime.Integration.RtChannelTest do
5253
"channel" => "any",
5354
"extension" => "postgres_changes",
5455
"message" =>
55-
"Unable to subscribe to changes with given parameters. Please check Realtime is enabled for the given connect parameters: [schema: public, table: *, filters: []]",
56+
"Unable to subscribe to changes with given parameters. Please check Realtime is enabled for the given connect parameters: [event: INSERT, schema: public, table: *, filters: []]",
5657
"status" => "error"
5758
},
5859
ref: nil,
@@ -2300,7 +2301,7 @@ defmodule Realtime.Integration.RtChannelTest do
23002301
# Does it recover?
23012302
assert Connect.ready?(tenant.external_id)
23022303
{:ok, db_conn} = Connect.lookup_or_start_connection(tenant.external_id)
2303-
Process.sleep(1000)
2304+
Process.sleep(5000)
23042305
%{rows: [[new_db_pid]]} = Postgrex.query!(db_conn, active_slot_query, [])
23052306

23062307
assert new_db_pid != original_db_pid

test/realtime/extensions/cdc_rls/subscription_manager_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ defmodule Realtime.Extensions.CdcRls.SubscriptionManagerTest do
147147

148148
pg_change_params = %{
149149
id: uuid,
150-
subscription_params: {"public", "*", []},
150+
subscription_params: {"*", "public", "*", []},
151151
claims: %{
152152
"exp" => System.system_time(:second) + 100_000,
153153
"iat" => 0,

test/realtime_web/channels/realtime_channel_test.exs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ defmodule RealtimeWeb.RealtimeChannelTest do
107107

108108
assert %{
109109
postgres_changes: [
110-
%{:id => sub_id, "event" => "INSERT", "schema" => "public", "table" => "test"},
110+
%{:id => insert_sub_id, "event" => "INSERT", "schema" => "public", "table" => "test"},
111111
%{
112-
:id => 4_845_530,
112+
:id => delete_sub_id,
113113
"event" => "DELETE",
114114
"schema" => "public",
115115
"table" => "test"
@@ -123,9 +123,12 @@ defmodule RealtimeWeb.RealtimeChannelTest do
123123
5000
124124

125125
{:ok, conn} = Connect.lookup_or_start_connection(tenant.external_id)
126+
# Insert, update and delete but update should not be received
126127
%{rows: [[id]]} = Postgrex.query!(conn, "insert into test (details) values ('test') returning id", [])
128+
Postgrex.query!(conn, "update test set details = 'test' where id = $1", [id])
129+
Postgrex.query!(conn, "delete from test where id = $1", [id])
127130

128-
assert_push "postgres_changes", %{data: data, ids: [4_845_530, ^sub_id]}, 500
131+
assert_push "postgres_changes", %{data: data, ids: [^insert_sub_id]}, 500
129132

130133
# we encode and decode because the data is a Jason.Fragment
131134
assert %{
@@ -138,6 +141,19 @@ defmodule RealtimeWeb.RealtimeChannelTest do
138141
"commit_timestamp" => _
139142
} = Jason.encode!(data) |> Jason.decode!()
140143

144+
assert_push "postgres_changes", %{data: data, ids: [^delete_sub_id]}, 500
145+
146+
# we encode and decode because the data is a Jason.Fragment
147+
assert %{
148+
"table" => "test",
149+
"type" => "DELETE",
150+
"old_record" => %{"id" => ^id},
151+
"columns" => [%{"name" => "id", "type" => "int4"}, %{"name" => "details", "type" => "text"}],
152+
"errors" => nil,
153+
"schema" => "public",
154+
"commit_timestamp" => _
155+
} = Jason.encode!(data) |> Jason.decode!()
156+
141157
refute_receive _any
142158
end
143159

@@ -185,7 +201,7 @@ defmodule RealtimeWeb.RealtimeChannelTest do
185201
assert_push "system",
186202
%{
187203
message:
188-
"Unable to subscribe to changes with given parameters. Please check Realtime is enabled for the given connect parameters: [schema: public, table: doesnotexist, filters: []]",
204+
"Unable to subscribe to changes with given parameters. Please check Realtime is enabled for the given connect parameters: [event: *, schema: public, table: doesnotexist, filters: []]",
189205
status: "error",
190206
extension: "postgres_changes",
191207
channel: "test"
@@ -216,7 +232,7 @@ defmodule RealtimeWeb.RealtimeChannelTest do
216232
assert_push "system",
217233
%{
218234
message:
219-
"Unable to subscribe to changes with given parameters. An exception happened so please check your connect parameters: [schema: public, table: test, filters: [{\"notacolumn\", \"eq\", \"123\"}]]. Exception: ERROR P0001 (raise_exception) invalid column for filter notacolumn",
235+
"Unable to subscribe to changes with given parameters. An exception happened so please check your connect parameters: [event: *, schema: public, table: test, filters: [{\"notacolumn\", \"eq\", \"123\"}]]. Exception: ERROR P0001 (raise_exception) invalid column for filter notacolumn",
220236
status: "error",
221237
extension: "postgres_changes",
222238
channel: "test"

0 commit comments

Comments
 (0)