Just a Baby Moose

It quick postette day again here in the Moose-Pen

Just a quick one off re-factor for today. In the past few days I have been using a code snipitte like this;


my $tests = [{
key =>'sorts',
sorts => [
{name => 'last_name',
view => 'people'
},
{
name => 'first_name',
view => 'people'
},
],
caption => "Order by ",
sql => "SELECT people.first_name, people.last_name, people.user_id FROM people ORDER BY people.last_name, people.first_name",
}];
use Test::More tests => 1;
my $utils = Test::Utils->new();
my $da = Database::Accessor->new($in_hash);
my $dbh = $utils->connect();
foreach my $test (@{$tests}){
$utils->sql_param_ok($dbh,$in_hash,$test);
}

which of I re-factored down to just this;

...
use Test::More tests =>1;
my $utils = Test::Utils->new();
$utils->sql_param_ok($in_hash,$tests);

I moved the iteration and the Database::Accessor and DB handle code down in to Utils::sql_param_ok which now looks like this;

sub sql_param_ok {
my $self = shift;
my ($in_hash,$tests ) = @_;
my $da = Database::Accessor->new($in_hash);
foreach my $test (@{$tests}){
if (exists($test->{index})) {
$in_hash->{ $test->{key} }->[ $test->{index} ] = $test->{ $test->{key} };
}
else {
$in_hash->{ $test->{key} } = $test->{ $test->{key} };
}
my $da = Database::Accessor->new($in_hash);
$da->retrieve($self->connect());
ok(
$da->result()->query() eq $test->{sql},
$test->{caption} . " SQL correct"
);
cmp_deeply( $da->result()->params, $test->{params},
$test->{caption} . " params correct" )
if (exists($test->{params}));
}
}

So that will make testing all the possible SQL combination a little easier, but now I have to go and fix up a whole bunch of test. Oh well a post for another day

sr84-94.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