App-Basis-Queue
view release on metacpan or search on metacpan
t/03_simple.t view on Meta::CPAN
my $queue_three = "$qname/three" ;
my ( $queue, $dbh ) ;
my $add_items = 1000 ;
# get optional DSN info from the user environment
my $dsn
= $ENV{SQ_DSN}
? $ENV{SQ_DSN}
: "dbi:SQLite:/tmp/queue_simple-tasks.$test_q.sqlite3" ;
my $user = $ENV{SQ_USER} ;
my $passwd = $ENV{SQ_PASSWD} ;
# ----------------------------------------------------------------------------
=item query_db
general purpose db query tool, returns all results as a arrayref of hashes
this function was created by kevin outside of home
copyright is retained with him
returns ref to data and a status msg
=cut
sub query_db
{
my ( $dbh, $query, $p ) = @_ ;
our @params = $p ? @$p : () ;
my ( $result, $err, $sth ) ;
try {
$sth = $dbh->prepare($query) ;
my $rv = $sth->execute(@params) ;
# so as to get an array of hashes
$result = $sth->fetchall_arrayref( {} ) ;
}
catch {} ;
return $result, $err ;
}
# ----------------------------------------------------------------------------
# Testing starts here
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# author testing can use sql, automated CPAN testing may not be able to
if ( $ENV{AUTHOR_TESTING} ) {
# $add_items = ( $add_items / 10 ) if ( $dsn =~ /SQLite/i );
# set PrintError off otherwise it will tell us that tables do not exist, we know that!
$dbh
= DBI->connect( $dsn, $user, $passwd,
{ RaiseError => 1, PrintError => 0, AutoCommit => 1 } )
or die "Could not connect to DB $dsn" ;
note "Testing against $dsn" ;
if ( $dsn =~ /SQLite/i ) {
$dbh->do("PRAGMA journal_mode = WAL") ;
$dbh->do("PRAGMA synchronous = NORMAL") ;
}
# -----------------------------------------------------------------------------
subtest "check clean start\n" => sub {
# remove all entries from the tables to make sure we are starting clean
my $table_name = $test_q . "_queue" ;
my ( $ret, $err ) = query_db( $dbh, "DROP TABLE $table_name;" ) ;
# check the table does not exist before we start
( $ret, $err )
= query_db( $dbh, "SELECT * from $table_name LIMIT 1;" ) ;
ok( !$ret && !$err, "Table $table_name does not exist" ) ;
} ;
# -----------------------------------------------------------------------------
subtest "create queue object\n" => sub {
$queue = App::Basis::Queue->new(
dbh => $dbh,
prefix => $test_q,
debug => $ENV{DEBUG}
) ;
isa_ok( $queue, 'App::Basis::Queue' ) ;
# ->new should have created the various database tables,
# lets check if this is the case
# we know that the tables will start with $test_q
# and they are 'queue_names' and 'queue_info'
my $table_name = $test_q . "_queue" ;
my ( $ret, $err )
= query_db( $dbh, "SELECT * from $table_name LIMIT 1;" ) ;
ok( $ret, "Table $table_name exists" ) ;
my $queue_list = $queue->list_queues() ;
ok( !scalar(@$queue_list), 'No queues listed' ) ;
} ;
# -----------------------------------------------------------------------------
subtest "adding to queue\n" => sub {
my ( $stats, $old_stats ) ;
# add 1 thing to the queue
my $resp = $queue->push(
queue => $qname,
data => { item => 1, desc => "test data" }
) ;
ok( $resp, "pudhed 1 item to $qname" ) ;
my $count = 0 ;
foreach my $i ( 2 .. 11 ) {
my $resp = $queue->push(
queue => $qname,
data => { item => $i, desc => "test data" }
) ;
$count++ if ($resp) ;
}
( run in 0.560 second using v1.01-cache-2.11-cpan-39bf76dae61 )