Moose Tests Cheaper by the Dozen.

Its test fix up day again in the Moose-pen.

Well this post will look very similar to one I did a few days ago. Fixing tests that where invalidated by extending the API to stop users from entering dynamic attributes with elements that are not in the elements array.

The first nasty one I ran into was on '31_elements.t' where I got this fail;


Can't locate object method "_lookup_name" via package "Database::Accessor::Function" …

I was getting the error because all of the attributes in elements of the test $da are 'functions' or other classes;

...
{
function => 'left',
left => { name => 'salary' },
right => {
expression => '*',
left => { name => 'bonus' },
right => { param => .05 }
}
},
...
{
ifs => [
{
left => { name => 'Price', },
right => { value => '10' },
operator => '<',
then => { name => 'price' }
},


and somewhere in my code I do not account for such a situation. To fix this I think I have to move this code;

has '_lookup_name' => (
is => 'ro',
isa => 'Str',
lazy=>1,
builder=>"_builder_lookup_name",
init_arg => 'only_on_link',
);

sub _builder_lookup_name {
my $self = shift;
return $self->view.$self->name;
}


out of the 'Element' class and into the 'Element::Role' as more than just 'Elements' can go into the 'elements' attributes of the $da;

I also had to fix one more thing now that I had more than just 'Element' classes in my $da->elements attribute;

 
sub get_element_by_lookup {
my $self = shift;
my ($lookup) = @_;
-- my $found = $self->_get_element_by_lookup(sub { if (!defined($_->_lookup_name)) {return 0} $_->_lookup_name eq $lookup});
++ my $found = $self->_get_element_by_lookup(sub { if (ref($_) ne 'Database::Accessor::Element' or !defined($_->_lookup_name)) {return 0} $_->_lookup_name eq $lookup});
return $found;
}

As soon as I fixed that I ran into this;

Gather view_element bonus in not in the elements array! Only elements from that array...

which doesn't make any sense as I can any elements I want to on the static part so I changed the calling pattern of the '_check_element' sub a little to pass that value along and then in that sub I made this fix

private_method _check_element => sub {
my $self = shift;
-- my ($element) = @_;
++ my ($element,$action,$type) = @_;
if (ref($element) eq 'Database::Accessor::Element'){
unless ( $element->view() ) {
$element->view( $self->view->name() );
}
-- die( "Gather view_element "
++ die( "Error on Database::Accessor::->$action: Element "
.$element->name()
." in not in the elements array! Only elements from that array can be added" )
-- unless ($self->get_element_by_lookup($element->_lookup_name()));
++ if (($type eq 'dynamic' and !$self->get_element_by_lookup($element->_lookup_name())));

}
elsif (ref($element) eq 'Database::Accessor::If'){


and with that change I get a full pass on that one.

The next bug I found in 't/35_links.t' in this case I was not able to add a static link without it failing. This fix was simple enough;


-- foreach my $link ((@{ $self->links },@{ $self->dynamic_links })){
++ foreach my $link (@{ $self->dynamic_links }){
push(@items,$link->conditions);
}

Test driven coding roots out bugs once again as that little fix cleaned up a bunch more test cases and I finally got a fail on 't/40_dynamic_not_present.t' and that was just a simple change to the expected results and I was off to the next test;

The next bug was in 't/45_dynamic_links.t' and what was happing here was the '_lookup_name' was not being set correctly. In the link instance I had


left' => bless( {
'view' => 'People',
'_lookup_name' => 'countrycountry_id',
'name' => 'country_id'
'Database::Accessor::Element' ),

while the input hash for the above was;

left => {
name => 'country_id',
view => 'People'
},

A little fix was need to the '_check_elements' sub of the 'Links' class;

if ( exists( $ops->{name} ) ) {
$ops->{view} = $view_name
unless($ops->{view});
-- $ops->{only_on_link} = $lookup_view.$ops->{name};
++ $ops->{only_on_link} = $ops->{view}.$ops->{name};
}

The rest of the tests where fixed up by changing a few 'view' and 'name' attributes or addding the odd one into the 'elements' array. The only sticky error was on 't/58_parenthes.t'

not ok 11 - OR added to last gather condition predicate

most likely that is a result of the static and dynamic not being checked together and at least if give me something to look at tomorrow

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