From 8b13584e44acd2b4ddc47c420893960fed8d54ff Mon Sep 17 00:00:00 2001
From: Osumi Takamichi <osumi.takamichi@fujitsu.com>
Date: Sun, 21 Mar 2021 07:48:13 +0000
Subject: [PATCH v01] additional tests for ALTER SUBSCRIPTION

Add new tests related to ALTER SUBSCRIPTION ... REFRESH PUBLICATION
and ALTER SUBSCRIPTION ... SET PUBLICATION with refresh option for enabled subscription.

---
 src/test/subscription/t/020_twophase.pl | 52 ++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/src/test/subscription/t/020_twophase.pl b/src/test/subscription/t/020_twophase.pl
index ac866b2..cca4f23 100644
--- a/src/test/subscription/t/020_twophase.pl
+++ b/src/test/subscription/t/020_twophase.pl
@@ -3,7 +3,7 @@ use strict;
 use warnings;
 use PostgresNode;
 use TestLib;
-use Test::More tests => 24;
+use Test::More tests => 28;
 
 ###############################
 # Setup
@@ -404,6 +404,37 @@ $result = $node_subscriber->safe_psql('postgres',
 is($result, qq(1), 'the first subscriber got the change');
 
 ###############################
+# Test ALTER SUBSCRIPTION ... REFRESH PUBLICATION
+# and ALTER SUBSCRIPTION ... SET PUBLICATION with refresh option
+# for enabled subscription.
+###############################
+
+regexp_query_stderrout('ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION;',
+	qr/ALTER SUBSCRIPTION ... REFRESH with copy_data is not allowed when two_phase is enabled/,
+	'test REFRESH PUBLICATION using default value of copy_data = true');
+
+regexp_query_stderrout('ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION WITH (copy_data = true);',
+	qr/ALTER SUBSCRIPTION ... REFRESH with copy_data is not allowed when two_phase is enabled/,
+	'test REFRESH PUBLICATION using copy_data = true');
+
+$node_subscriber->safe_psql('postgres',
+	'ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION WITH (copy_data = false);');
+
+regexp_query_stderrout('ALTER SUBSCRIPTION tap_sub SET PUBLICATION tap_pub WITH (refresh = true);',
+	qr/ALTER SUBSCRIPTION with refresh and copy_data is not allowed when two_phase is enabled/,
+	'test SET PUBLICATION using refresh = true and default copy_data = true ');
+
+regexp_query_stderrout('ALTER SUBSCRIPTION tap_sub SET PUBLICATION tap_pub WITH (refresh = true, copy_data = true);',
+	qr/ALTER SUBSCRIPTION with refresh and copy_data is not allowed when two_phase is enabled/,
+	'test SET PUBLICATION using refresh = true and copy_data = true ');
+
+$node_subscriber->safe_psql('postgres',
+	'ALTER SUBSCRIPTION tap_sub SET PUBLICATION tap_pub WITH (refresh = true, copy_data = false);');
+
+$node_subscriber->safe_psql('postgres',
+	'ALTER SUBSCRIPTION tap_sub SET PUBLICATION tap_pub WITH (refresh = false);');
+
+###############################
 # check all the cleanup
 ###############################
 
@@ -431,3 +462,22 @@ is($result, qq(0), 'check replication origin was dropped on subscriber');
 $node_subscriber->stop('fast');
 $second_subscriber->stop('fast');
 $node_publisher->stop('fast');
+
+sub regexp_query_stderrout
+{
+	my ($query, $expected_reg, $explanation) = @_;
+	my $stderr;
+
+	my $ret = $node_subscriber->psql('postgres',
+									 $query,
+									 stderr => \$stderr);
+	# get 3 when the passed SQL results in an error
+	if ($ret == 3)
+	{
+		ok ($stderr =~ $expected_reg, $explanation);
+	}
+	else
+	{
+		die 'the input query should cause an error to be checked by regular expression';
+	}
+}
-- 
2.2.0

