Matureing Baby Moose

Its re-factor test postette day in the Moose-Pen

Over the past few days I have made up quite a few tests and they where all the same pattern

  • Base hash
  • add function/expression element
  • create DA
  • run select/retrieve
  • check SQL
  • check parms
so a little re-factoring is in order me thinks., Though there is a school of thought that it is a waste of resources to re-factor working test. Fortunately I do not ascribe to that thought so here we go.

The re-factoring was quite simple, I took my test;


$in_hash->{elements}->[1] = { function => 'substr',
left => { name => 'username' },
right => [{ param =>3},{ param =>5}] };
my $da = Database::Accessor->new($in_hash);
$da->retrieve( $utils->connect() );
ok(
$da->result()->query() eq
"SELECT user.username, substr(user.username,?,?), user.address FROM user WHERE user.username = ?",
"Function with 2 param binds SQL correct"
);
cmp_deeply(
$da->result()->params,
[3,5,'Bill'],
"Function params correct"
);

and took out all the dynamic bits and added them as key value pairs in a hash-ref in an array-ref;

my $tests = [
{ element => { function => 'substr',
left => { name => 'username' },
right => [{ param =>3},{ param =>5}] },
caption => "Function with 1 param",
sql => "SELECT user.username, left(user.username,?), user.address FROM user WHERE user.username = ?",
params => [3,5,'Bill']
}];

Now a sub to handle all the static bits of the test;

sub element_sql_ok {
my ($dbh,$in_hash,$opts) = @_;
$in_hash->{elements}->[$opts->{index}] = $opts->{element};
my $da = Database::Accessor->new($in_hash);
$da->retrieve( $dbh );
ok(
$da->result()->query() eq
$opts->{sql},
$opts->{caption}." SQL correct"
);
cmp_deeply(
$da->result()->params,
$opts->{params},
$opts->{caption}." params correct"
);
}

which I dump into Test::Utils;

Now all I need is iterate over that array-ref and add in test as I need them.

Quick and simple re-factor for today.

SI852703a.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