Moose Utils Extend

It extend test helper day here in the Moose-pen.

Yesterday I managed to get the 'Between' operator to work but there was one little thing that was bugging my and that was I had some 40 lines of new code and only three new tests for the possible exception the 'Between' operator might generate. To boot the three new tests where an anti-pattern.

Now there is no problem with having anti-patterns in test code you just end up with many more lines in your than you need and they may be problematic to fix if they ever go wrong. Normally I would not bother to re-factor just three tests but I can see myself making many more 'Exception' tests for the other operators like 'IsNull' or 'In'. Thus, in this case I will fix that.

So given the basic test;


$in_hash->{conditions} = [
{
left => {
name => 'salary',
view => 'people'
},
right => { value => 35000 },
operator => 'BETWEEN',
},
];
my $da = Database::Accessor->new($in_hash);
like(
exception { $da->retrieve( Test::Utils->connect() ) },
qr /right must be an Array Ref of two paramameters/,
"Right must be an array-ref "
);

I think I can work that into the present 'sql_param_ok' sub found in Test::Utils. First I need create a new hash to add to the array-ref that is passed into the sub;

{
caption => 'Between operator right must be an array-ref',
type => 'exception',
key => 'conditions',
conditions => [
{
left => {
name => 'salary',
view => 'people'
},
right => { value => 35000 },
operator => 'BETWEEN',
},
],
retrieve =>
{ message => 'right must be an Array Ref of two parameters' },
},

much the same as the ones I presently use except I have added in a 'type' key so I take a different path to take in the logic 'sql_param_ok' sub, and under the 'retrieve' key I have added in the message key that will contain the expected exception message.

Next it is time to play with 'Test::Utils:: sql_param_ok' and the first thing I do here is to add in my path logic like this


foreach my $action ((qw(create retrieve update delete))){
next
unless(exists($test->{$action}));
my $sub_test = $test->{$action};
++ if (!exists($test->{type}) {

++ }
++ elsif ($test->{type} eq 'exception'){
++
++ }
++ else {
++ fail(“No test for type=”.$test->{type});
++ }

Above the first thing I do is check to see if there the 'type' does not exist and if it does not I just carry on as if normal, next I check for the type 'exception' and then finally I added an 'else' with a fail so I can catch any typos of the in my 'type' key or if I add in a new 'type' and not add a test for it.

Now all I need to do is copy over my present test and paste in that 'if' slot and make a few substitutions and in the end I have this;


...
} elsif ( $test->{type} eq 'exception' )) {
like(
exception
{
$da->$action(
$self->connect(), $sub_test->{container}
)
},
$sub_test->{message},
$action . " " . $test->{caption}
);
}
else {
fail( "No test for type=" . $test->{type} );
}

and on my first run I got;

Database::Accessor::Driver::DBI::Error->Operator 'BETWEEN' right must be an Array Ref of two parameters! At

and my tests stop. After a few mins of head scratching it seem I forgot to add this;

use Test::More;
++use Test::Fatal;
use Database::Accessor;

and on my next run I got;

'right must be an Array Ref of two parameters' doesn't look much like a regex to me.
...

ok something wrong in the regex here;

exception
{
$da->$action($self->connect(), $sub_test->{container})
},
$sub_test->{message},
$action . " " . $test->{caption}

Not 100% sure why that will not work but I will just wrap that '$sub_test->{message}' with 'qr' like the other tests and see what I get

{
$da->$action($self->connect(), $sub_test->{container})
},
$sub_test->{message},
qr/$sub_test->{message}/,
$action . " " . $test->{caption}

and on this run I get

...
ok 46 - Between Operator Element and Function delete params correct
ok 47 - retrieve Between operator right must be an array-ref


I little reading later I see that 'qr' is the perl Regexp Quote-Like Operators which is intrepulating that key->value pair as a string for me. Neat how you can learn something new about perl after years of using it.

Now I need only convert those other two tests over and I am done for the day.

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