| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 
 | #!/usr/bin/perl -w
# $Id: test.pl,v 1.18 2004/04/21 17:37:13 bmomjian Exp $
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'
######################### We start with some black magic to print on failure.
BEGIN { $| = 1; }
END {print "test failed\n" unless $loaded;}
use Pg;
$loaded = 1;
use strict;
######################### End of black magic.
my $dbmain = 'template1';
my $dbname = 'pgperltest';
my $trace  = '/tmp/pgtrace.out';
my ($conn, $result, $i);
my $DEBUG  = 0; # set this to 1 for traces
######################### the following methods will be tested
#	connectdb
#	conndefaults
#	db
#	user
#	port
#	status
#	errorMessage
#	trace
#	untrace
#	exec
#	getline
#	putline
#	endcopy
#	resultStatus
#	fname
#	fnumber
#	ftype
#	fsize
#	cmdStatus
#	oidStatus
#	cmdTuples
#	fetchrow
######################### the following methods will not be tested
#	setdb
#	setdbLogin
#	reset
#	requestCancel
#	pass
#	host
#	tty
#	options
#	socket
#	backendPID
#	notifies
#	sendQuery
#	getResult
#	isBusy
#	consumeInput
#	getlineAsync
#	putnbytes
#	makeEmptyPGresult
#	ntuples
#	nfields
#	binaryTuples
#	fmod
#	getvalue
#	getlength
#	getisnull
#	print
#	displayTuples
#	printTuples
#	lo_import
#	lo_export
#	lo_unlink
#	lo_open
#	lo_close
#	lo_read
#	lo_write
#	lo_creat
#	lo_seek
#	lo_tell
######################### handles error condition
$SIG{PIPE} = sub { print "broken pipe\n" };
######################### create and connect to test database
my $Option_ref = Pg::conndefaults();
my ($key, $val);
( $$Option_ref{port} ne "" && $$Option_ref{dbname} ne "" && $$Option_ref{user} ne "" )
    and print "Pg::conndefaults ........ ok\n"
    or  die   "Pg::conndefaults ........ not ok: ";
$conn = Pg::connectdb("dbname=$dbmain");
( PGRES_CONNECTION_OK eq $conn->status )
    and print "Pg::connectdb ........... ok\n"
    or  die   "Pg::connectdb ........... not ok: ", $conn->errorMessage;
# do not complain when dropping $dbname
$conn->exec("DROP DATABASE $dbname");
$result = $conn->exec("CREATE DATABASE $dbname");
( PGRES_COMMAND_OK eq $result->resultStatus )
    and print "\$conn->exec ............. ok\n"
    or  die   "\$conn->exec ............. not ok: ", $conn->errorMessage;
$conn = Pg::connectdb("dbname=rumpumpel");
( $conn->errorMessage =~ /[Dd]atabase .?rumpumpel.? does not exist/ )
    and print "\$conn->errorMessage ..... ok\n"
    or  die   "\$conn->errorMessage ..... not ok: ", $conn->errorMessage;
$conn = Pg::connectdb("dbname=$dbname");
die $conn->errorMessage unless PGRES_CONNECTION_OK eq $conn->status;
######################### debug, PQtrace
if ($DEBUG) {
    open(FD, ">$trace") || die "can not open $trace: $!";
    $conn->trace("FD");
}
######################### check PGconn
my $db = $conn->db;
( $dbname eq $db )
    and print "\$conn->db ............... ok\n"
    or  print "\$conn->db ............... not ok: $db\n";
my $user = $conn->user;
( "" ne $user )
    and print "\$conn->user ............. ok\n"
    or  print "\$conn->user ............. not ok: $user\n";
my $port = $conn->port;
( "" ne $port )
    and print "\$conn->port ............. ok\n"
    or  print "\$conn->port ............. not ok: $port\n";
######################### create and insert into table
# we test comments inside string and with no trailing newline here
$result = $conn->exec("CREATE TABLE person (id int4, -- test\n name char(16)) -- test");
die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus;
my $cmd = $result->cmdStatus;
( $cmd =~ /^CREATE/ )
    and print "\$conn->cmdStatus ........ ok\n"
    or  print "\$conn->cmdStatus ........ not ok: $cmd\n";
for ($i = 1; $i <= 5; $i++) {
    $result = $conn->exec("INSERT INTO person VALUES ($i, 'Edmund Mergl')");
    die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus;
}
######################### copy to stdout, PQgetline
$result = $conn->exec("COPY person TO STDOUT");
die $conn->errorMessage unless PGRES_COPY_OUT eq $result->resultStatus;
my $ret = 0;
my $buf;
my $string;
$i = 1;
while (-1 != $ret) {
    $ret = $conn->getline($buf, 256);
    last if $buf eq "\\.";
    $string = $buf if 1 == $i;
    $i++;
}
( "1	Edmund Mergl    " eq $string )
    and print "\$conn->getline .......... ok\n"
    or  print "\$conn->getline .......... not ok: $string\n";
# should be "no COPY in progress" now
$ret = $conn->endcopy;
( 1 == $ret )
    and print "\$conn->endcopy .......... ok\n"
    or  print "\$conn->endcopy .......... not ok: $ret ", $conn->errorMessage, "\n";
######################### delete and copy from stdin, PQputline
$result = $conn->exec("BEGIN");
die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus;
$result = $conn->exec("DELETE FROM person");
die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus;
$ret = $result->cmdTuples;
( 5 == $ret )
    and print "\$result->cmdTuples ...... ok\n"
    or  print "\$result->cmdTuples ...... not ok: $ret\n";
$result = $conn->exec("COPY person FROM STDIN");
die $conn->errorMessage unless PGRES_COPY_IN eq $result->resultStatus;
for ($i = 1; $i <= 5; $i++) {
    # watch the tabs and do not forget the newlines
    $conn->putline("$i	Edmund Mergl\n");
}
$conn->putline("\\.\n");
die $conn->errorMessage if $conn->endcopy;
$result = $conn->exec("END");
die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus;
######################### select from person, PQgetvalue
$result = $conn->exec("SELECT * FROM person");
die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus;
my $fname = $result->fname(0);
( "id" eq $fname )
    and print "\$result->fname .......... ok\n"
    or  print "\$result->fname .......... not ok: $fname\n";
my $ftype = $result->ftype(0);
( 23 == $ftype )
    and print "\$result->ftype .......... ok\n"
    or  print "\$result->ftype .......... not ok: $ftype\n";
my $fsize = $result->fsize(0);
( 4 == $fsize )
    and print "\$result->fsize .......... ok\n"
    or  print "\$result->fsize .......... not ok: $fsize\n";
my $fnumber = $result->fnumber($fname);
( 0 == $fnumber )
    and print "\$result->fnumber ........ ok\n"
    or  print "\$result->fnumber ........ not ok: $fnumber\n";
$string = "";
my @row;
while (@row = $result->fetchrow) {
    $string = join(" ", @row);
}
( "5 Edmund Mergl    " eq $string )
    and print "\$result->fetchrow ....... ok\n"
    or  print "\$result->fetchrow ....... not ok: $string\n";
######################### debug, PQuntrace
if ($DEBUG) {
    close(FD) || die "bad TRACE: $!";
    $conn->untrace;
}
######################### disconnect and drop test database
undef $conn;
$conn = Pg::connectdb("dbname=$dbmain");
die $conn->errorMessage unless PGRES_CONNECTION_OK eq $conn->status;
# Race condition: it's quite possible that the DROP command will arrive
# at the new backend before the old backend has finished shutting down,
# resulting in an error message.
# There doesn't seem to be any more graceful way around this than to
# insert a small delay ...
sleep(1);
$result = $conn->exec("DROP DATABASE $dbname");
die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus;
print "test sequence finished.\n";
######################### EOF
 |