Not a Mousse Moose!

So in my last Moose-pen I ran into problem with my 33_conditions.t test case where it was giving me errors like this

# Compared $data->left
# got : Does not exist


Well after some playing about I saw the errors of my ways. Looking at the Database::Accessor::Condition class I remembered to load that properly I will have to pass in any conditions with the 'predicates' param. So I modified my$$in_hash to this;

conditions=>[{predicates=>[{left=>{name=>'last_name',
view=>'People'},
right=>{ param=>'test'},
operator=>'='
},
{condition=>'AND',
left=>{name=>'first_name',
view=>'People'},
right=>{ param=>'test'},
operator=>'='}]}],

I ran my test again but then I was back at square one again?
Attribute (name) is required at C:\Dwimperl\perl\site\lib\Moose\Object.pm line 24
Moose::Object::new('Database::Accessor::Element', 'param', 'test') called at D:\GitHub\database-accessor\lib\Database\Accessor\Types.pm line 29


Hmm...

So it looks like it does not like my 'param' coercion again. I double checked my 'Database::Accessor::Predicate' class and saw that I had


has right => (
is => 'rw',
isa => 'Element|Param',
required => 1,
coerce => 1,
);

Now from what I read about on Moose coercion, it will try each in type succession until it finds one that works or one that errors out. So the simple remedy in this case it to just flip the pair about;

-- isa => 'Element|Param',
++ isa => 'Param|Element',

so now it will check for 'Param' first. This works because this is a very simple case of two objects with very different parameters. I re-ran my tests and got;
# Comparing hash keys of $data->predicates->[0]
# Extra: 'close_parenthes', 'open_parenthes'

which is a different error. The above tells me that I am missing a value on my $in_hash, not really a bug per-say but a good to have it for a test. So I add in

operator=>'='
++ open=>1,
++ close=>0

in both and re-run my tests and get

Comparing hash keys of $data->predicates->[0]
# Missing: 'close', 'open'
# Extra: 'close_parenthes', 'open_parenthes'
# Failed test ' DAD Condition attributes correct '

opp I guess it is not seeing those MooseX::Alias of mine as it is telling my that my preidcate class does not have 'open' and 'close' and $in_hash is still missing the 'close_parenthes' and 'open_parenthes'.
So another quick change


operator=>'='
++ open_parenthes=>1,
++ close_parenthes=>0

and a re-run;

# Compared blessed($data->predicates->[0])
# got : 'Database::Accessor::Predicate'
# expect : undef

so now I am getting a fail on the class, well at least I know I am getting a Predicate class. I did try this

cmp_deeply( noclass($dad_conditions->[$index]), methods(%{$in})," DAD Condition attributes correct " )

the noclass from Test::Deep however that did not work as I got

Found a special comparison in $data
You can only use specials in the expects structure at C:/Dwimperl/perl/site/lib/Test/Deep.pm line 348

Well after a some time playing about with Test::Deep I stumbled upon the answer; unfortunately I did not find a very elegant solution using Test::Deep I found the only way to make this work as to 'bless' my $in_hash in the appropriate places like this


foreach my $index (0..scalar(@{$in_conditions}-1)) {
my $in = $in_conditions->[$index];
++ for $index_d (0..1){
++ bless($in->{predicates}->[$index_d],"Database::Accessor::Predicate");
++ bless($in->{predicates}->[$index_d]->{left},"'Database::Accessor::Element");
++ bless($in->{predicates}->[$index_d]->{right},"Database::Accessor::Param");
++ }
cmp_deeply( $da_conditions->[$index], methods(%{$in})," DA condition attributes correct ");
cmp_deeply( $dad_conditions->[$index], methods(%{$in})," DAD Condition attributes correct " );
}

and my test came up all correct

ok 1 - use Database::Accessor;
ok 2 - Got the Test DAD
ok 3 - DA condition attributes correct
ok 4 - DAD Condition attributes correct

So all nice and good. Now I did have to make a few other minor changes to get the above result. I had to drop using 'param' in my $in_hash and use 'value' and I had my 'Condition' attribute in the wrong place. It should be in my 'Predicate' class and not in the 'Condtions' class. I will need this attribute when I have to join an array of predicates together with a logic operators like 'AND'.

A little clunky on that blessed part but still much easier then trying to iterate over classes directly.

5166ec699dd26e9fcf7c846a2f00dd63--chocolate-moose-chocolate-art.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