From f05a759c669d081be8dedbfe8a499e6df89373e0 Mon Sep 17 00:00:00 2001
From: Osumi Takamichi <osumi.takamichi@fujitsu.com>
Date: Fri, 5 Feb 2021 06:25:23 +0000
Subject: [PATCH] new 3 tests for AlterSubscription with refresh.

Check that to execute ALTER SUBSCRIPTION
with refresh in a transaction or in a function is not allowed.

Author : Takamichi Osumi <osumi.takamichi@fujitsu.com>

---
 src/test/subscription/t/004_sync.pl | 43 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/src/test/subscription/t/004_sync.pl b/src/test/subscription/t/004_sync.pl
index e111ab9..e24a676 100644
--- a/src/test/subscription/t/004_sync.pl
+++ b/src/test/subscription/t/004_sync.pl
@@ -3,7 +3,7 @@ use strict;
 use warnings;
 use PostgresNode;
 use TestLib;
-use Test::More tests => 7;
+use Test::More tests => 10;
 
 # Initialize publisher node
 my $node_publisher = get_new_node('publisher');
@@ -151,5 +151,46 @@ is($result, qq(20),
 
 $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub");
 
+# Executing ALTER SUBSCRIPTION with refresh in a transaction or function is not allowed.
+$node_publisher->safe_psql('postgres',
+    "CREATE PUBLICATION mypub FOR ALL TABLES;");
+
+$node_subscriber->safe_psql('postgres',
+    "CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr' PUBLICATION mypub WITH (connect = true);");
+
+my ($cmdret, $stdout, $stderr);
+($cmdret, $stdout, $stderr) =
+  $node_subscriber->psql('postgres', q[
+BEGIN;
+ALTER SUBSCRIPTION mysub SET PUBLICATION mypub WITH (refresh = true);
+END;
+]);
+
+ok($stderr =~
+   qr/ALTER SUBSCRIPTION with refresh cannot run inside a transaction block/,
+   'should fail to issue ALTER SUBSCRIPTION ... SET PUBLICATION with refresh on inside a transaction block');
+
+($cmdret, $stdout, $stderr) =
+  $node_subscriber->psql('postgres', q[
+BEGIN;
+ALTER SUBSCRIPTION mysub REFRESH PUBLICATION;
+END;
+]);
+
+ok($stderr =~
+	qr/ALTER SUBSCRIPTION ... REFRESH cannot run inside a transaction block/,
+   'should fail to issue ALTER SUBSCRIPTION ... REFRESH inside a transaction block');
+
+($cmdret, $stdout, $stderr) =
+  $node_subscriber->psql('postgres', q[
+CREATE FUNCTION func() RETURNS VOID AS
+$$ ALTER SUBSCRIPTION mysub SET PUBLICATION mypub WITH (refresh = true) $$ LANGUAGE SQL;
+SELECT func();
+]);
+
+ok($stderr =~
+   qr/ALTER SUBSCRIPTION with refresh cannot be executed from a function/,
+   'should fail to issue ALTER SUBSCRIPTION ... SET PUBLICATION with refresh on from a function');
+
 $node_subscriber->stop('fast');
 $node_publisher->stop('fast');
-- 
2.2.0

