delta_0004-some-negative-TAP-test-case-for-pg_restore-when-dump.noci

application/octet-stream

Filename: delta_0004-some-negative-TAP-test-case-for-pg_restore-when-dump.noci
Type: application/octet-stream
Part: 0
Message: Re: Non-text mode for pg_dumpall
From 6ca50003f084b6d01f1d9ee8bc9dd56369ca4054 Mon Sep 17 00:00:00 2001
From: Mahendra Singh Thalor <mahi6run@gmail.com>
Date: Fri, 4 Apr 2025 13:48:30 +0530
Subject: [PATCH] some negative TAP-test case for pg_restore when dump of
 pg_dumpall is used.

---
 src/bin/pg_dump/t/006_pg_dumpall.pl | 76 ++++++++++++++++++++++++++---
 1 file changed, 68 insertions(+), 8 deletions(-)
 mode change 100644 => 100755 src/bin/pg_dump/t/006_pg_dumpall.pl

diff --git a/src/bin/pg_dump/t/006_pg_dumpall.pl b/src/bin/pg_dump/t/006_pg_dumpall.pl
old mode 100644
new mode 100755
index fdfd1ae990b..44afdf525ff
--- a/src/bin/pg_dump/t/006_pg_dumpall.pl
+++ b/src/bin/pg_dump/t/006_pg_dumpall.pl
@@ -115,6 +115,7 @@ my %pgdumpall_runs = (
 		CREATE ROLE grant8;
 
 		CREATE TABLE t (id int);
+		INSERT INTO t VALUES (1), (2), (3), (4);
 
 		GRANT SELECT ON TABLE t TO grant1;
 		GRANT INSERT ON TABLE t TO grant2;
@@ -157,27 +158,37 @@ my %pgdumpall_runs = (
 		setup_sql => 'CREATE DATABASE db1;
 		\c db1
 		CREATE TABLE t1 (id int);
+		INSERT INTO t1 VALUES (1), (2), (3), (4);
 		CREATE TABLE t2 (id int);
+		INSERT INTO t2 VALUES (1), (2), (3), (4);
 
 		CREATE DATABASE db2;
 		\c db2
 		CREATE TABLE t3 (id int);
+		INSERT INTO t3 VALUES (1), (2), (3), (4);
 		CREATE TABLE t4 (id int);
+		INSERT INTO t4 VALUES (1), (2), (3), (4);
 
 		CREATE DATABASE dbex3;
 		\c dbex3
 		CREATE TABLE t5 (id int);
+		INSERT INTO t5 VALUES (1), (2), (3), (4);
 		CREATE TABLE t6 (id int);
+		INSERT INTO t6 VALUES (1), (2), (3), (4);
 
 		CREATE DATABASE dbex4;
 		\c dbex4
 		CREATE TABLE t7 (id int);
+		INSERT INTO t7 VALUES (1), (2), (3), (4);
 		CREATE TABLE t8 (id int);
+		INSERT INTO t8 VALUES (1), (2), (3), (4);
 
 		CREATE DATABASE db5;
 		\c db5
 		CREATE TABLE t9 (id int);
+		INSERT INTO t9 VALUES (1), (2), (3), (4);
 		CREATE TABLE t10 (id int);
+		INSERT INTO t10 VALUES (1), (2), (3), (4);
 		',
 		dump_cmd => [
 			'pg_dumpall',
@@ -225,8 +236,8 @@ my %pgdumpall_runs = (
 	},
 
 	format_directory => {
-		setup_sql =>
-		  'CREATE TABLE format_directory(a int, b boolean, c text);',
+		setup_sql => "CREATE TABLE format_directory(a int, b boolean, c text);
+		INSERT INTO format_directory VALUES (1, true, 'name1'), (2, false, 'name2');",
 		dump_cmd => [
 			'pg_dumpall',
 			'--format' => 'directory',
@@ -238,11 +249,12 @@ my %pgdumpall_runs = (
 			'--file' => "$tempdir/format_directory.sql",
 			"$tempdir/format_directory",
 		],
-		like => qr/^\n\QCREATE TABLE public.format_directory (/xm
+		like => qr/^\n\QCOPY public.format_directory (a, b, c) FROM stdin;/xm
 	},
 
 	format_tar => {
-		setup_sql => 'CREATE TABLE format_tar(id int);',
+		setup_sql => "CREATE TABLE format_tar(a int, b boolean, c text);
+		INSERT INTO format_tar VALUES (1, false, 'name3'), (2, true, 'name4');",
 		dump_cmd => [
 			'pg_dumpall',
 			'--format' => 'tar',
@@ -254,11 +266,12 @@ my %pgdumpall_runs = (
 			'--file' => "$tempdir/format_tar.sql",
 			"$tempdir/format_tar",
 		],
-		like => qr/^\n\QCREATE TABLE public.format_tar (/xm
+		like => qr/^\n\QCOPY public.format_tar (a, b, c) FROM stdin;/xm
 	},
 
 	format_custom => {
-		setup_sql => 'CREATE TABLE format_custom(a int, b boolean, c text);',
+		setup_sql => "CREATE TABLE format_custom(a int, b boolean, c text);
+		INSERT INTO format_custom VALUES (1, false, 'name5'), (2, true, 'name6');",
 		dump_cmd => [
 			'pg_dumpall',
 			'--format' => 'custom',
@@ -270,9 +283,28 @@ my %pgdumpall_runs = (
 			'--file' => "$tempdir/format_custom.sql",
 			"$tempdir/format_custom",
 		],
-		like => qr/^ \n\QCREATE TABLE public.format_custom (/xm
-	},);
+		like => qr/^ \n\QCOPY public.format_custom (a, b, c) FROM stdin;/xm
+	},
 
+	dump_globals_only => {
+		setup_sql => "CREATE TABLE format_dir(a int, b boolean, c text);
+		INSERT INTO format_dir VALUES (1, false, 'name5'), (2, true, 'name6');",
+		dump_cmd => [
+			'pg_dumpall',
+			'--format' => 'directory',
+			'--globals-only',
+			'--file' => "$tempdir/dump_globals_only",
+			],
+			restore_cmd => [
+				'pg_restore', '-C', '--globals-only',
+				'--format' => 'directory',
+				'--file' => "$tempdir/dump_globals_only.sql",
+				"$tempdir/dump_globals_only",
+				],
+				like => qr/
+            ^\s*\QCREATE ROLE dumpall;\E\s*\n
+			/xm
+			}, );
 
 # First execute the setup_sql
 foreach my $run (sort keys %pgdumpall_runs)
@@ -326,6 +358,34 @@ foreach my $run (sort keys %pgdumpall_runs)
 	}
 }
 
+# Some negative test case with dump of pg_dumpall and restore using pg_restore
+# test case 1: when -C is not used in pg_restore with dump of pg_dumpall
+$node->command_fails_like(
+    [ 'pg_restore',
+    "$tempdir/format_custom",
+    '--format' => 'custom',
+    '--file' => "$tempdir/error_test.sql", ],
+    qr/\Qpg_restore: error: -C\/--create option should be specified when restoring multiple databases by archive of pg_dumpall\E/,
+    'When -C is not used in pg_restore with dump of pg_dumpall');
+
+# test case 2: When --list option is used with dump of pg_dumpall
+$node->command_fails_like(
+	[ 'pg_restore',
+		"$tempdir/format_custom", '-C',
+		'--format' => 'custom', '--list',
+		'--file' => "$tempdir/error_test.sql", ],
+	qr/\Qpg_restore: error: option -l\/--list cannot be used when restoring multiple databases by archive of pg_dumpall\E/,
+	'When --list is used in pg_restore with dump of pg_dumpall');
+
+# test case 3: When non-exist database is given with -d option
+$node->command_fails_like(
+	[ 'pg_restore',
+		"$tempdir/format_custom", '-C',
+		'--format' => 'custom',
+		'-d' => 'dbpq', ],
+	qr/\Qpg_restore: error: could not connect to database "dbpq"\E/,
+	'When non-exist datbase is given with -d option in pg_restore with dump of pg_dumpall');
+
 $node->stop('fast');
 
 done_testing();
-- 
2.39.3