summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2009-07-15 14:09:44 +0000
committerYorhel <git@yorhel.nl>2009-07-15 14:22:35 +0000
commit122f9de035d4ac9801483c841633ae356d4ac2ff (patch)
treee778cddb07736ac5a1dd9b05b7fca6e86fe0080c
parent0b250cb6d8823e7f63feb1d8a07019a68e287673 (diff)
Tiny improvements in the tests, and added new test for basic queries
-rw-r--r--MANIFEST1
-rw-r--r--t/01register.t4
-rw-r--r--t/02connect.t6
-rw-r--r--t/03queries.t103
4 files changed, 109 insertions, 5 deletions
diff --git a/MANIFEST b/MANIFEST
index b5ab6ce..2331eaa 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -7,3 +7,4 @@ README
t/00load.t
t/01register.t
t/02connect.t
+t/03queries.t
diff --git a/t/01register.t b/t/01register.t
index d3ba6b4..ab358dd 100644
--- a/t/01register.t
+++ b/t/01register.t
@@ -52,7 +52,7 @@ my $tester = POE::Session->create(inline_states => {
misconnect_error => sub {
if(!$state++) {
- ok 'Incorrect connection fires error';
+ pass 'Incorrect connection fires error';
$_[KERNEL]->delay('misconnect_timeout');
$_[KERNEL]->post(pg2 => unregister => 'error');
$_[KERNEL]->post(pg2 => 'connect');
@@ -64,7 +64,7 @@ my $tester = POE::Session->create(inline_states => {
misconnect_connect => sub { fail 'Incorrect connection fires error (connect)' },
misconnect_timeout => sub { fail 'Incorrect connection fires error (timeout)' },
misconnect_2nd => sub {
- ok 'unregister';
+ pass 'unregister';
$_[KERNEL]->post(pg2 => 'shutdown');
$_[KERNEL]->yield('check_lastshutdown');
},
diff --git a/t/02connect.t b/t/02connect.t
index b5caece..999e946 100644
--- a/t/02connect.t
+++ b/t/02connect.t
@@ -34,7 +34,7 @@ POE::Session->create(inline_states => {
},
pg_connect => sub {
- ok 'Connect';
+ pass 'Connect';
$_[KERNEL]->post(pg => 'shutdown');
$_[KERNEL]->delay('connect_timeout');
$_[KERNEL]->yield('disconnect');
@@ -55,7 +55,7 @@ POE::Session->create(inline_states => {
$_[KERNEL]->delay('overwrite_timeout', 10);
},
overwrite_connect => sub {
- ok 'Overwrite';
+ pass 'Overwrite';
$_[KERNEL]->delay('overwrite_timeout');
$_[KERNEL]->post(pg => 'shutdown');
$_[KERNEL]->yield('disconnect2');
@@ -76,7 +76,7 @@ POE::Session->create(inline_states => {
$_[KERNEL]->delay('spawn_timeout', 10);
},
spawn_connect => sub {
- ok 'Spawn';
+ pass 'Spawn';
$_[KERNEL]->delay('spawn_timeout');
$_[KERNEL]->post(pg => 'shutdown');
},
diff --git a/t/03queries.t b/t/03queries.t
new file mode 100644
index 0000000..03df322
--- /dev/null
+++ b/t/03queries.t
@@ -0,0 +1,103 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+use POE::Component::Pg;
+use DBI;
+use POE;
+
+plan tests => 18;
+
+my $dbh = DBI->connect(undef, undef, undef, { AutoCommit => 1, RaiseError => 1 });
+POE::Component::Pg->spawn(alias => 'pg');
+
+
+# create temporary table for testing (assuming the current user has that right)
+eval {
+ $dbh->do('CREATE TABLE poco_pg_test (col1 integer, col2 varchar)');
+};
+BAIL_OUT("Couldn't create table for testing: $@.\nMake sure the SQL user has table creation rights.") if $@;
+
+END {
+ $dbh->do('DROP TABLE poco_pg_test');
+ $dbh->disconnect();
+}
+
+
+
+POE::Session->create(inline_states => {
+ _start => sub {
+ $_[KERNEL]->post(pg => register => connect => 'pg_connect', error => 'pg_error');
+ $_[KERNEL]->post(pg => 'connect');
+ },
+ pg_error => sub { diag "ERROR $_[ARG0]: $_[ARG1]"; fail 'PostgreSQL error'; die },
+ pg_connect => sub {
+ $_[KERNEL]->post(pg => do => 'INSERT INTO poco_pg_test VALUES(?, ?)', [ $$, 'value1' ], 'insert_done', 'insert-arg');
+ $_[KERNEL]->delay('insert_timeout', 10);
+ },
+ insert_timeout => sub { fail 'Insert (timeout)'; die },
+ insert_done => sub {
+ $_[KERNEL]->delay('insert_timeout');
+ # check DB to see that it has indeed been inserted
+ my $q = $dbh->prepare('SELECT col2 FROM poco_pg_test WHERE col1 = ?');
+ is $q->execute($$), 1, 'INSERT query';
+ is $q->fetchrow_hashref()->{col2}, 'value1', 'INSERTed value';
+ is $_[ARG0], 1, 'Insert number argument';
+ is $_[ARG1], undef, 'Insert result argument';
+ is $_[ARG2], 'insert-arg', 'Insert custom argument';
+
+ # now try to select everything from our table (query without placeholders)
+ $_[KERNEL]->post(pg => query => 'SELECT col1, col2 FROM poco_pg_test', undef, 'select_done', 'select-arg');
+ $_[KERNEL]->delay('select_timeout', 10);
+ },
+
+ select_timeout => sub { fail 'Select (timeout)'; die },
+ select_done => sub {
+ $_[KERNEL]->delay('select_timeout');
+ is $_[ARG0], 1, 'Select number argument';
+ is_deeply $_[ARG1], [{ col1 => $$, col2 => 'value1' }], 'Select result argument';
+ is $_[ARG2], 'select-arg', 'Select custom argument';
+
+ # now execute a query that is guaranteed to fail
+ $_[KERNEL]->post(pg => register => error => 'error_error');
+ $_[KERNEL]->post(pg => do => 'SELECT col99 FROM poco_pg_test', undef, 'error_done', 'error-arg');
+ $_[KERNEL]->delay('error_timeout', 10);
+ },
+
+ error_timeout => sub { fail 'Error (timeout)'; die },
+ error_done => sub { fail 'Error (didn\'t fail!?)'; die },
+ error_error => sub {
+ $_[KERNEL]->delay('error_timeout');
+ pass 'Error occured';
+ is $_[ARG0], 'do', 'Error 1st argument';
+ # let's not rely on the format of argument 2
+ is $_[ARG2], 'SELECT col99 FROM poco_pg_test', 'Error 3rd argument';
+ is $_[ARG3], undef, 'Error 5th argument';
+ is $_[ARG4], $_[SESSION]->ID(), 'Error 4th argument';
+ is $_[ARG5], 'error-arg', 'Error 5th argument';
+
+ # lastly, do an UPDATE, but don't use the optional last argument
+ $_[KERNEL]->post(pg => register => error => 'pg_error');
+ $_[KERNEL]->post(pg => do => 'UPDATE poco_pg_test SET col2 = ? WHERE col1 = ?', [ 'meh', $$ ], 'update_done');
+ $_[KERNEL]->delay('update_timeout', 10);
+ },
+
+ update_timeout => sub { fail 'Update (timeout)'; die },
+ update_done => sub {
+ $_[KERNEL]->delay('update_timeout');
+
+ # check DB
+ my $q = $dbh->prepare('SELECT col2 FROM poco_pg_test WHERE col1 = ?');
+ $q->execute($$);
+ is $q->fetchrow_hashref()->{col2}, 'meh', 'UPDATEd value';
+ is $_[ARG0], 1, 'Update number argument';
+ is $_[ARG1], undef, 'Update result argument';
+ is $_[ARG2], undef, 'Update custom argument';
+
+ $_[KERNEL]->post(pg => 'shutdown');
+ },
+});
+
+$poe_kernel->run();
+