Little Moose Tries to Run

Well more testing and re-factoring with just a little Moose in the Moose-Pen today. So I spend a good deal of time getting my tests cases all in a row. Now just a few interesting details on that, I created a number of generic utility tests that can be reused in my test cases. What I did was take the 'cmp_deeply' test like this on in 33_constants.t


my $da_conditions  = $da->conditions();
   my $dad_conditions = $dad->Conditions();
   my $in_conditions  = $in_hash2->{conditions};
   foreach my $index (0..scalar(@{$in_conditions}-1)) {
     my $in   = $in_conditions->[$index];
     bless($in,"Database::Accessor::Predicate"); 
     bless($in->{left},"'Database::Accessor::Element"); 
     bless($in->{right},"Database::Accessor::Param"); 
     cmp_deeply($da_conditions->[$index]->predicates->[0], methods(%{$in}),
"DA predicates correct" );
     cmp_deeply($dad_conditions->[$index]->predicates->[0], methods(%{$in}),
"DAD predicates no $index correct" );
      # cmp_deeply( $dad_conditions->predicates->[$index], methods(%{$in}),DAD predicates correct" );
   }

and converted them into a sub, in this case deep_predicates, then created a packages to hold them 'Test::Database::Accessor::Utils' and then rather than the above all I call is

 my $da = Database::Accessor->new($in_hash);
  my $dad = $da->retrieve(Data::Test->new());
  Test::Database::Accessor::Utils::deep_predicate(
$in_hash->conditions},
$da->conditions(),
$dad->Conditions(),'conditions');

and I can re-use that ' deep_predicates' in other test cases as I do here in 37_gathers.twhere I reuse the 'deep_element' and the 'deep_predicate' test;

 my $da = Database::Accessor->new($in_hash);
  my $dad = $da->retrieve(Data::Test->new());
  Test::Database::Accessor::Utils::deep_element(
$in_hash->{gathers},
$da->gathers,
$dad->Gathers,'Gather');
  Test::Database::Accessor::Utils::deep_predicate(
$in_hash->{filters},
$da->filters(),
$dad->Filters(),'Filters');

Well I did say there would be a little Moose in this post and here it is. After I had converted 33_condtions.t over and working properly I tried the same for 35_links.t and I get this error;

# Failed test 'DA Link 0->predicates 0 correct'
# at ..\t\lib/Test/Database/Accessor/Utils.pm line 47.
# Comparing hash keys of $data->right
# Missing: 'view'

Hmm looking at the difference between the two input hash-refs the only difference 'right' param, in 35_links.t I have

predicates =>[{left =>{name =>'country_id',
view =>'People'},
right =>{name=>'id',
view=>'a_country'},

while in 33_conditions.t I had

onditions=>[{left =>{name =>'last_name',
view =>'People'},
right =>{value=>'test'},

so my 'right' in the 35_links.t was an 'Element' class not a 'Param' class so there is a problem with my coercion.

I flipped the value of the coerce on 'right' like this


isa => 'Element|Param',

and as I suspected I my 35_links.t passed but 33_conditions.t failed as it now wanted a 'name' on that Param.

So back to my types where I did some more Moose Type majik;


– coerce 'Element', from 'HashRef', via { Database::Accessor::Element->new( %{$_} ) };
++ coerce 'Element', from 'HashRef', via { 
++ if (exists($_->{value}) || exists($_->{param})){
++ Database::Accessor::Param->new( %{$_} );
++ }
++ else{
++ Database::Accessor::Element->new( %{$_} ) ;
++ }
++};

and now I get all pass for both test cases. It also teaches one the lesson to use different data between test cases it I did not do that it might of been a while before I found this but.


content03042015.gif

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