Moose Baby Runs

Well it is still fix that test day here in the Moose-pen

Not sure why I am calling today's post a Moose-Pen as there is very little Moose if any in it. Yesterday I left of with a little script that was suppose to init a table using 'DBD::DBM' and add a few records, next get a DBI handle to that database then pass it to the 'Test::DB::User' accessor class while doing a 'create' call.

Well on my first try on running this I got

DBD::DBM::db do failed: Couldn't parse at /usr/local/lib/perl/5.18.2/DBI/DBD/SqlEngine.pm line 339.
 [for Statement "DROP"] at test_dbi.pl line 14.
and after a few mins of head scratching I saw that in my little script I forgot to properly format my array of SQL commands. I had

my @sql = qw {DROP TABLE IF EXISTS users;
              CREATE TABLE user ( username TEXT, address TEXT);
              INSERT INTO user VALUES ( 'user1',  1);
              INSERT INTO user VALUES ( 'user2',  2);
};
Should of had something more like this

my @sql = (“DROP TABLE IF EXISTS users”,
           “CREATE TABLE users ( username TEXT, address TEXT)”,
           “INSERT INTO users VALUES ( 'user1',  1)”,
           “INSERT INTO users VALUES ( 'user2',  2)”,
);
Anyway I did manage to get the script to run stand alone but when ever I ran it as part of the test I would get nothing. So I thoght to my selft even use that rater dated 'do' call and just add what I need to a class that I can reuse elsewhere in my testing. So that is what I did. Not going to bore you with that code listing I really is just a very simple Moose class, that I will add onto as time goes by. Now my test looks like this

#!perl
use Test::More;
use Test::Fatal;
use DBI;
use Database::Accessor;
use Test::Utils;
use Test::DB::User;

my $utils = Test::Utils->new();
$utils->create_users_table();
my $user = Test::DB::User->new();
my $container =  {username=>'user_new',
                  address =>'address_new'};
eval{
   $user->create($utils->connect(),
                 $container);
};
if ($@) {
    fail("Create function error=$@");
}
else {
    pass("Create function");
}
much neater and now it actually passes;

ok 1 - Create function
Unfortunately that does not mean much as all the Driver::DBI is doing is

sub execute {
    my $self = shift;
    my ( $type, $conn, $container, $opt ) = @_;
    return 1;
}
or in other words nothing. What I have to do if I want a meaningful tests is to check and see if my new record was created on the DB. I could add in a function in my Test::Utils to look up the new value and the new test would look like this

if ($@) {
    fail("Create function error=$@");
}
else {
    pass("Create function");
}
ok($utils->get_user('user_new') == 1,'got new user from DB' );

That will would work as a basic test so lets run with that for now.

3519CF3200000578-0-image-a-163_1465504563512.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations