Skip to content

Commit

Permalink
add test enqueue and dequeue on flexible table name
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Nov 26, 2010
1 parent 894e15a commit 24db5a4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
20 changes: 13 additions & 7 deletions t/Utils.pm
Expand Up @@ -5,18 +5,22 @@ use DBI;
use Pod::Simple::SimpleTree;

sub _get_schema {
my $dbh = shift;
my $dbh = shift;
my $table = shift || "job";

my $driver = $dbh->{Driver}{Name};
my $sql;
if ( $driver eq 'mysql' ) {
return _read_pod('MySQL');
$sql = _read_pod('MySQL');
} elsif ( $driver eq 'Pg' ) {
return _read_pod('PostgreSQL');
$sql = _read_pod('PostgreSQL');
} elsif ( $driver eq 'SQLite' ) {
return _read_pod('SQLite');
$sql = _read_pod('SQLite');
} else {
Carp::croak "this driver unsupport: $driver";
}
$sql =~ s/(CREATE\s+TABLE\s+)job/CREATE TABLE $table/;
$sql;
}

sub _read_pod {
Expand All @@ -40,14 +44,16 @@ sub _read_pod {
}

sub setup {
my ($class, $table) = @_;
my $dbh = DBI->connect('dbi:SQLite:');
$dbh->do(_get_schema($dbh));
$dbh->do(_get_schema($dbh, $table));
$dbh;
}

sub cleanup {
my ($class, $dbh) = @_;
$dbh->do('DROP TABLE job');
my ($class, $dbh, $table) = @_;
$table ||= "job";
$dbh->do("DROP TABLE $table");
}

1;
Expand Down
20 changes: 20 additions & 0 deletions t/client.t
Expand Up @@ -67,5 +67,25 @@ subtest 'error handling' => sub {

t::Utils->cleanup($dbh);


subtest 'enqueue / flexible job table name' => sub {
my $dbh = t::Utils->setup("my_job");
my $jonk = Jonk::Client->new($dbh, +{table_name => "my_job"});

my $job_id = $jonk->enqueue('MyWorker', 'arg');
ok $job_id;

my $sth = $dbh->prepare('SELECT * FROM my_job WHERE id = ?');
$sth->execute($job_id);
my $row = $sth->fetchrow_hashref;

is $row->{arg}, 'arg';
is $row->{func}, 'MyWorker';
ok not $jonk->errstr;

t::Utils->cleanup($dbh, "my_job");
done_testing;
};

done_testing;

18 changes: 18 additions & 0 deletions t/worker.t
Expand Up @@ -72,5 +72,23 @@ subtest 'error handling' => sub {

t::Utils->cleanup($dbh);


subtest 'dequeue / flexible job table name' => sub {
my $dbh = t::Utils->setup("my_job");
my $client = Jonk::Client->new($dbh, { table_name => "my_job" });

my $job_id = $client->enqueue('MyWorker', 'arg');
ok $job_id;

my $jonk = Jonk::Worker->new($dbh, { table_name => "my_job", functions => [qw/MyWorker/]});
my $job = $jonk->dequeue();
is $job->{arg}, 'arg';
is $job->{func}, 'MyWorker';
ok not $jonk->errstr;

t::Utils->cleanup($dbh, "my_job");
done_testing;
};

done_testing;

3 changes: 2 additions & 1 deletion xt/Utils/mysql.pm
Expand Up @@ -12,8 +12,9 @@ my $mysql = Test::mysqld->new
{
no warnings "redefine";
sub t::Utils::setup {
my ($class, $table) = @_;
my $dbh = DBI->connect($mysql->dsn( dbname => "test" ));
$dbh->do(t::Utils::_get_schema($dbh));
$dbh->do(t::Utils::_get_schema($dbh, $table));
$dbh;
}
}
Expand Down
3 changes: 2 additions & 1 deletion xt/Utils/postgresql.pm
Expand Up @@ -12,9 +12,10 @@ my $pgsql = Test::postgresql->new
{
no warnings "redefine";
sub t::Utils::setup {
my ($class, $table) = @_;
my $dbh = DBI->connect($pgsql->dsn);
local $dbh->{"Warn"} = 0;
$dbh->do(t::Utils::_get_schema($dbh));
$dbh->do(t::Utils::_get_schema($dbh, $table));
$dbh;
}
}
Expand Down

0 comments on commit 24db5a4

Please sign in to comment.