PageRenderTime 59ms CodeModel.GetById 50ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/13-DBD-Pg.t

https://code.google.com/p/camelbox/
text | 83 lines | 68 code | 15 blank | 0 comment | 0 complexity | f7a1e2e62563d03ca5bfc14473d60f91 MD5 | raw file
 1#!/usr/bin/perl
 2
 3# test for DBD::SQLite
 4
 5use strict;
 6use warnings;
 7use Test::More tests => 17;
 8#use Test::More qw(no_plan);
 9
10# load up the DBI module
11BEGIN { use_ok( q(DBI) ) };
12my $db_host = q(127.0.0.1);
13my $db_port = 5432;
14my $db_name = q(camelbox);
15my $db_user = q(camelbox);
16my $db_pass;
17if ( exists $ENV{PGPASSWORD} ) {
18    $db_pass = $ENV{PGPASSWORD};
19} else {
20    die(q(Environment variable 'PGPASSWORD' is not set; exiting...));
21} # if ( exists $ENV{PGPASSWORD} )
22
23my @rows; # returned rows
24
25# CREATE DATABASE
26my $dbh = DBI->connect(
27    qq(dbi:Pg:database=$db_name;)
28    . qq(host=$db_host;port=$db_port), 
29    qq($db_user), qq($db_pass) );
30ok($dbh,  q(Database handle to PostgreSQL database successfully created));
31
32# send a ping to the database to test it
33my $db_ping_rv = $dbh->ping;
34ok($db_ping_rv > 0, qq(Database ping returned success: $db_ping_rv));
35
36# check the PostgreSQL version; this checks the connection as well
37my $db_version = $dbh->{pg_server_version};
38ok($db_version, q(PostgreSQL version is ) . $db_version);
39
40# CREATE TEST TABLE
41my $sth = $dbh->prepare(
42   	q( CREATE TABLE camelbox_test (string TEXT, number INTEGER) ) );
43ok($sth->execute, q(Executed CREATE statement));
44
45# INSERT DATA INTO TABLE
46$sth = $dbh->prepare(q(INSERT INTO camelbox_test VALUES ('hello!', 10)) );
47ok($sth->execute, q(Executed INSERT statement 'hello!'/10) );
48
49$sth = $dbh->prepare(q(INSERT INTO camelbox_test VALUES ('goodbye', 20)) );
50ok($sth->execute, q(Executed INSERT statement 'goodbye'/20) );
51
52# SELECT DATA FROM TABLE AND COMPARE
53$sth = $dbh->prepare(
54	q(SELECT string, number FROM camelbox_test WHERE number = 20) );
55ok($sth->execute, q(Executed SELECT: number = 20) );
56@rows = $sth->fetchrow_array();
57ok($rows[0] eq q(goodbye) && $rows[1] == 20, 
58	q(Selected row returned: ') . $rows[0] . q(/) . $rows[1] . q(') );
59
60$sth = $dbh->prepare(
61	q(SELECT string, number FROM camelbox_test WHERE string = 'hello!') );
62ok($sth->execute, q(Executed SELECT: string = 'hello!') );
63@rows = $sth->fetchrow_array();
64ok($rows[0] eq q(hello!) && $rows[1] == 10, 
65	q(Selected row returned: ') . $rows[0] . q(/) . $rows[1] . q(') );
66
67# DROP TABLE
68$sth = $dbh->prepare( q(DROP TABLE camelbox_test) );
69ok($sth->execute, q(Executed DROP TABLE));
70
71# SELECT ON EMPTY TABLE
72$dbh->{PrintError} = 0;
73$sth = $dbh->prepare( q(SELECT string, number )
74    . q(FROM camelbox_test WHERE number = 20) );     
75ok(! $sth->execute, q(Executed SELECT with a DROPped table));
76
77# CLEAN UP
78ok($sth->finish, q(Closed the statement handle with $sth->finish ) );
79ok(! undef $sth, q(undef'ed $sth));
80ok($dbh->disconnect, q(Closed the database handle with $dbh->disconnect ) );
81ok(! undef $dbh, q(undef'ed $dbh));
82# vi: set filetype=perl sw=4 ts=4 cin:
83# end of line