Tested Joined Moose
So it is little progress day here in the Moose-Pen
Carrying on from yesterday's post I decided to get 57_dad_elements in order so I added in a few more tests;
ok( $elements->[$i]->conditions->[1]
->predicates->left->right->left->right->view() eq 'a_country',
"Link index $i condition 2 left->right->left->right->left inherits view"
);
ok($elements->[$i]->conditions->[1]->predicates->condition eq 'AND',
"Link index $i condition 2 has AND Contdition"
);
ok($elements->[$i]->conditions->[0]->predicates->right->view() eq 'a_country',
"Link index $i condition 2 right- inherits view"
);
ok($elements->[$i]->conditions->[1]->predicates->right->view() eq 'a_country',
"Link index $i condition 2 right- inherits view"
);
and I quickly got a fail on most of them
...
ok 23 - Link index 0 condition 2 left->right->left->left does not inherit view
not ok 24 - Link index 0 condition 2 left->right->left->right->left inherits view
not ok 25 - Link index 0 condition 2 has AND Condition
...
not ok 32 - Link index 1 condition 2 left->right->left->right->left inherits view
not ok 33 - Link index 1 condition 2 has AND Condition
…
so there are two things going south here; first on of my 'right' elements is not inheriting the correct view and second my 'condition' is not being set to 'AND' automatically on links.
So for the first looking at the 'right' value in question I have
right => {
function => 'abs',
left => {
expression => '*',
left => {
name => 'bonus',
view => 'Other'
},
right => { name => 'bonus', }
}
}
so that right that is under the left is not beging reached to change the view to the alias. I have to find a way to fix that up so I have to discimate on wherater an element is on the left or right side of a predicate.
What I did was change the '_check_element' sub so it takes one more param to identify it as a 'right' element, and only use the '$alias' if the $right is true;
private_method _check_element => sub {
my $self = shift;
-- my ($element,$right) = @_;
++ my ($element,$right,$alias) = @_;
if (ref($element) eq 'Database::Accessor::Element'){
unless ( $element->view() ) {
$element->view( $self->view->name() );
$element->view( $self->view()->alias() )
if ( $self->view()->alias() );
$element->view($alias )
-- if ($alias);
++ if ($alias and $right);
}
and of course I change all the places where I call _check_element only using '1' in the one that are a 'right' element and '0' elsewhere;
...
$self->_check_parentheses($element->predicates);
-- $self->_check_element($element->predicates->right,$alias);
-- $self->_check_element($element->predicates->left);
++ $self->_check_element($element->predicates->right,1,$alias);
++ $self->_check_element($element->predicates->left,0,$alias);
…
no need to put all those changes here at least now I get only those 'AND' to fail;
...
not ok 25 - Link index 0 condition 2 has AND Condition
...
not ok 33 - Link index 1 condition 2 has AND Condition
…
Tracing that one back I see that when I do the general Items check;
if (ref($item) eq 'ARRAY'){
$self->_reset_conditions();
foreach my $condition (@{$item}){
$self->_inc_conditions()
if (ref($condition) eq 'Database::Accessor::Condition');
$self->_check_element($condition,0);
}
}
I check each of the conditional array using the inc_conditions and then later on I set the 'AND' with this code
elsif (ref($element) eq 'Database::Accessor::Condition'){
$element->predicates->operator($self->default_operator())
if ( !$element->predicates->operator() );
$element->predicates->condition($self->default_condition())
if ( $self->_add_condition>=2 and !$element->predicates->condition() );
$element->predicates->condition(undef)
if ( $self->_add_condition<=1 );
Basically I am skipping this part as I do my 'check_element' for links earlier in my code. I think this little patch will solve that;
foreach my $link ((@{ $self->links },@{ $self->dynamic_links })){
my $view = $link->to;
my $alias = !$view->alias ? $view->name : $view->alias;
$self->_check_element($link->conditions,0,$alias);
++-- push(@items,$link->conditions);
}
Just add that push of conditions into the @items again and now I get a 100% pass. I do take a little iteration hit but that is much less painful than re-factoring all the 'inc_conditions' code to make it work in two places that would be a real mess to follow.
I really am enjoying test based programming I find all my bugs way before they become problems anyway I guess I am set to go back to Driver::DBI code tomorrow.
Leave a comment