Skip to content

Commit

Permalink
adjust testing of PgPir driver (not working yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Berends committed Jul 1, 2010
1 parent 84a45aa commit db65fed
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 31 deletions.
8 changes: 7 additions & 1 deletion lib/MiniDBD/PgPir.pm6
Expand Up @@ -35,6 +35,8 @@ class MiniDBD::PgPir::Connection does MiniDBD::Connection {
class MiniDBD::PgPir:auth<moritz> {

has $.Version = 0.01;

sub pg_escape($x) {
q[']
~ $x.subst(rx/\\|\'/, -> $m { '\\' ~ $m }, :g)
Expand All @@ -44,20 +46,24 @@ class MiniDBD::PgPir:auth<moritz> {
method connect(Str $user, Str $password, Str $params, $RaiseError) {
my $pg = pir::new__pS('Pg');

my %params = $params.split(';').map({ .split(rx{\s*\=\s*}, 2)}).flat;
my %params = $params.split(';').map({ .split(regex {\s*\=\s*}, 2)}).flat;


my %opt =
user => pg_escape($user),
password => pg_escape($password),
# user => $user,
# password => $password,
%params;
%opt<application_name> //= 'Perl6MiniDBD';

say "Options: %opt.perl()";

# nearly scary how concise this is in Perl 6 :-)
my $connection_string = %opt.fmt('%s=%s', ';');
say "Connection string: $connection_string";
my $con = $pg.connectdb($connection_string);
say "con: $con";
}

method finish() {
Expand Down
1 change: 1 addition & 0 deletions lib/MiniDBI.pm6
Expand Up @@ -27,6 +27,7 @@ class MiniDBI:auth<mberends>:ver<0.1.0> {
given $drivername {
when 'CSV' { eval 'use MiniDBD::CSV; $driver = MiniDBD::CSV.new()' }
when 'mysql' { eval 'use MiniDBD::mysql; $driver = MiniDBD::mysql.new()' }
when 'PgPir' { eval 'use MiniDBD::PgPir; $driver = MiniDBD::PgPir.new()' }
default { die "driver name '$drivername' is not known"; }
}
return $driver;
Expand Down
43 changes: 43 additions & 0 deletions t/30-PgPir-common.t
@@ -0,0 +1,43 @@
# MiniDBI/t/30-PgPir-common.t
use v6;
use Test;
use MiniDBI;

# Define the only database specific values used by the common tests.
my ( $mdriver, $test_dsn, $test_user, $test_password );
{
# Define values that are relevant only to MySQL
my $hostname = 'localhost';
my $database = 'zavolaj';
# Set up the common variables with the MySQL specific values
$mdriver = 'PgPir';
$test_dsn = "MiniDBI:$mdriver" ~ ":database=$database;"
~ "host=$hostname";
$test_user = 'testuser';
$test_password = 'testpass';
}

# Detect and report possible errors from eval of the common test script
warn $! if "ok 99-common.pl6" ne eval slurp 't/99-common.pl6';

=begin pod
=head1 PREREQUISITES
Your system should already have libpq-dev installed. Change to the
postgres user and connect to the postgres server as follows:
sudo -u postgres psql
Then set up a test environment with the following:
CREATE DATABASE zavolaj;
CREATE ROLE testuser LOGIN PASSWORD 'testpass';
GRANT ALL PRIVILEGES ON DATABASE zavolaj TO testuser;
The '\l' psql command output should include zavolaj as a database name.
Exit the psql client with a ^D, then try to use the new account:
psql --host=localhost --dbname=zavolaj --username=testuser --password
SELECT * FROM pg_database;
=end pod
53 changes: 53 additions & 0 deletions t/30-PgPir-specific.t
@@ -0,0 +1,53 @@
use v6;
use Test;
plan *;

use MiniDBI;

my $mdriver = 'PgPir';
my $host = 'localhost';
my $port = 5432;
my $database = 'testdb';
my $user = 'testuser';
my $password = 'testpass';
# my $password = 'Cho5thae';

my $test_dsn = "FakeDBI:{$mdriver}:dbname=$database;host=$host;port=$port";

my $drh = MiniDBI.install_driver($mdriver);
ok $drh, 'Install driver';

my $dbh;
lives_ok { $dbh = MiniDBI.connect($test_dsn, $user, $password,
RaiseError => 1, PrintError => 1, AutoCommit => 1) },
'Connecting lives';

ok defined($dbh), 'DBH is defined';
ok $dbh, 'DBH is true';
# lives_ok { $dbh.finish }, 'Can finish DBH';
#nok $dbh, 'finished DBH is false';


done_testing;

=begin pod
=head1 PREREQUISITES
Your system should already have libpq-dev installed. Change to the
postgres user and connect to the postgres server as follows:
sudo -u postgres psql
Then set up a test environment with the following:
CREATE DATABASE testdb;
CREATE ROLE testuser LOGIN PASSWORD 'testpass';
GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser;
The '\l' psql command output should include testdb as a database name.
Exit the psql client with a ^D, then try to use the new account:
psql --host=localhost --dbname=testdb --username=testuser --password
SELECT * FROM pg_database;
=end pod
30 changes: 0 additions & 30 deletions t/30-pgpir.t

This file was deleted.

0 comments on commit db65fed

Please sign in to comment.