Author: turnstep
Date: Thu Jan 10 14:09:23 2008
New Revision: 10510

Modified:
   DBD-Pg/trunk/Pg.pm
   DBD-Pg/trunk/t/01connect.t
   DBD-Pg/trunk/t/01setup.t
   DBD-Pg/trunk/t/03smethod.t
   DBD-Pg/trunk/t/04misc.t

Log:
Add connection tests, data_sources tests, and others.


Modified: DBD-Pg/trunk/Pg.pm
==============================================================================
--- DBD-Pg/trunk/Pg.pm  (original)
+++ DBD-Pg/trunk/Pg.pm  Thu Jan 10 14:09:23 2008
@@ -1741,7 +1741,7 @@
 
 This driver supports this method. Note that the necessary database connection
 to the database "template1" will be made on the localhost without any user
-authentication. Other preferences can only be set with the environment
+authentication. Other preferences can be set with the environment
 variables C<PGHOST>, C<PGPORT>, C<DBI_USER>, C<DBI_PASS>, and C<PGSERVICE>.
 
 You can also pass in options to add to the connection string as the second 
argument 

Modified: DBD-Pg/trunk/t/01connect.t
==============================================================================
--- DBD-Pg/trunk/t/01connect.t  (original)
+++ DBD-Pg/trunk/t/01connect.t  Thu Jan 10 14:09:23 2008
@@ -10,7 +10,7 @@
 $|=1;
 
 if (defined $ENV{DBI_DSN}) {
-       plan tests => 8;
+       plan tests => 15;
 } else {
        plan skip_all => 'Cannot run test unless DBI_DSN is defined. See the 
README file.';
 }
@@ -23,7 +23,7 @@
 # the first is when we truly do not connect, usually a bad DBI_DSN;
 # the second is an invalid login, usually a bad DBI_USER or DBI_PASS
 
-my $dbh;
+my ($dbh,$t);
 eval {
        $dbh = DBI->connect($ENV{DBI_DSN}, $ENV{DBI_USER}, $ENV{DBI_PASS},
                                                                                
        {RaiseError => 1, PrintError => 0, AutoCommit => 0});
@@ -67,6 +67,52 @@
 };
 ok( $@, 'Execute fails on a disconnected statement');
 
+# Try out various connection options
+SKIP: {
+       my $alias = qr{(database|db|dbname)};
+       if ($ENV{DBI_DSN} !~ /$alias\s*=\s*\S+/) {
+               skip "DBI_DSN contains no database option, so skipping 
connection tests", 4;
+       }
+
+       $t=q{Connect with invalid option fails};
+       my $oldname = $1;
+       (my $dbi = $ENV{DBI_DSN}) =~ s/$alias\s*=/dbbarf=/;
+       eval {
+               $dbh = DBI->connect($dbi, $ENV{DBI_USER}, $ENV{DBI_PASS}, 
{RaiseError=>1});
+       };
+       like ($@, qr{invalid connection option}, $t);
+       for my $opt (qw/db dbname database/) {
+               $t=qq{Connect using string '$opt' works};
+               ($dbi = $ENV{DBI_DSN}) =~ s/$alias\s*=/$opt=/;
+               eval {
+                       $dbh = DBI->connect($dbi, $ENV{DBI_USER}, 
$ENV{DBI_PASS}, {RaiseError=>1});
+               };
+               is($@, q{}, $t);
+       }
+
+       if ($ENV{DBI_DSN} =~ /$alias\s*=\s*"/) {
+               skip "DBI_DSN already contains quoted database, no need for 
explicit test", 1;
+       }
+       $t=q{Connect using a quoted database argument};
+       ($dbi = $ENV{DBI_DSN}) =~ s/$alias\s*=(\w+)/'db="'.lc $2.'"'/e;
+       eval {
+               $dbh = DBI->connect($dbi, $ENV{DBI_USER}, $ENV{DBI_PASS}, 
{RaiseError=>1});
+       };
+       is($@, q{}, $t);
+}
+
+$t=q{Connect with an undefined user picks up $ENV{DBI_USER}};
+eval {
+       $dbh = DBI->connect($ENV{DBI_DSN}, undef, $ENV{DBI_PASS}, 
{RaiseError=>1});
+};
+is($@, q{}, $t);
+
+$t=q{Connect with an undefined password picks up $ENV{DBI_PASS}};
+eval {
+       $dbh = DBI->connect($ENV{DBI_DSN}, $ENV{DBI_USER}, undef, 
{RaiseError=>1});
+};
+is($@, q{}, $t);
+
 END {
        my $pv = sprintf("%vd", $^V);
        my $schema = exists $ENV{DBD_SCHEMA} ? 

Modified: DBD-Pg/trunk/t/01setup.t
==============================================================================
--- DBD-Pg/trunk/t/01setup.t    (original)
+++ DBD-Pg/trunk/t/01setup.t    Thu Jan 10 14:09:23 2008
@@ -15,9 +15,12 @@
        plan skip_all => 'Cannot run test unless DBI_DSN is defined. See the 
README file.';
 }
 
-my $dbh = DBI->connect($ENV{DBI_DSN}, $ENV{DBI_USER}, $ENV{DBI_PASS},
-                                                                               
         {RaiseError => 0, PrintError => 0, AutoCommit => 1});
-ok( defined $dbh, "Connect to database for test table creation");
+my $dbh;
+eval {
+       $dbh = DBI->connect($ENV{DBI_DSN}, $ENV{DBI_USER}, $ENV{DBI_PASS},
+                                               {RaiseError => 1, PrintError => 
0, AutoCommit => 1});
+};
+is($@, q{}, "Connect to database for test table creation");
 
 # Remove the test relations if they exist
 my $schema = DBD::Pg::_pg_use_catalog($dbh);

Modified: DBD-Pg/trunk/t/03smethod.t
==============================================================================
--- DBD-Pg/trunk/t/03smethod.t  (original)
+++ DBD-Pg/trunk/t/03smethod.t  Thu Jan 10 14:09:23 2008
@@ -12,7 +12,7 @@
 $|=1;
 
 if (defined $ENV{DBI_DSN}) {
-       plan tests => 69;
+       plan tests => 71;
 }
 else {
        plan skip_all => 'Cannot run test unless DBI_DSN is defined. See the 
README file';
@@ -27,12 +27,20 @@
                 (exists $ENV{DBD_SCHEMA} ? $ENV{DBD_SCHEMA} : 'public'));
 
 $dbh->do("DELETE FROM dbd_pg_test");
-my ($SQL, $sth, $sth2, $result, @result, $expected, $warning, $rows);
+my ($SQL, $sth, $sth2, $result, @result, $expected, $warning, $rows, $t);
 
 #
 # Test of the prepare flags
 #
 
+$t=q{Calling prepare() with no arguments gives an error};
+eval{ $sth = $dbh->prepare(); };
+like($@, qr{\+ 0}, $t);
+
+$t=q{Calling prepare() with an undefined value returns undef};
+$sth = $dbh->prepare(undef);
+is($sth, undef, $t);
+
 $SQL = "SELECT id FROM dbd_pg_test WHERE id = ?";
 $sth = $dbh->prepare($SQL);
 $sth->execute(1);

Modified: DBD-Pg/trunk/t/04misc.t
==============================================================================
--- DBD-Pg/trunk/t/04misc.t     (original)
+++ DBD-Pg/trunk/t/04misc.t     Thu Jan 10 14:09:23 2008
@@ -10,7 +10,7 @@
 $|=1;
 
 if (defined $ENV{DBI_DSN}) {
-       plan tests => 3;
+       plan tests => 6;
 } else {
        plan skip_all => 'Cannot run test unless DBI_DSN is defined. See the 
README file';
 }
@@ -29,14 +29,24 @@
 # Test of the "data_sources" method
 #
 
-my @result = DBI->data_sources('Pg');
-# This may fail due to the wrong port, etc.
-if (defined $result[0]) {
-       is (grep (/^dbi:Pg:dbname=template1$/, @result), '1', 'The 
data_sources() method returns a template1 listing');
-}
-else {
-       pass("The data_sources() method returned undef");
-}
+my @result;
+eval {
+       @result = DBI->data_sources('Pg');
+};
+is($@, q{}, 'The data_sources() method did not throw an exception');
+
+is (grep (/^dbi:Pg:dbname=template1$/, @result), '1', 'The data_sources() 
method returns a template1 listing');
+
+my $t=q{The data_sources() returns undef when fed a bogus second argument};
[EMAIL PROTECTED] = DBI->data_sources('Pg','foobar');
+is_deeply(@result, undef, $t);
+
+my $port = $dbh->{pg_port};
+
+$t=q{The data_sources() returns information when fed a valid port as the 
second arg};
[EMAIL PROTECTED] = DBI->data_sources('Pg',"port=$port");
+ok(defined $result[0], $t);
+
 
 #
 # Test the use of $DBDPG_DEFAULT

Reply via email to