/tests/13-DBD-Pg.t

https://code.google.com/p/camelbox/ · Raku · 83 lines · 53 code · 15 blank · 15 comment · 9 complexity · f7a1e2e62563d03ca5bfc14473d60f91 MD5 · raw file

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