Moose Finds More Bugs

Back to the API day here in the Moose-Pen

Well I was just going to do a simple test post-ette today but on my first run I ran into this in the '30_view.t' test case for Database::Accessor;


Can't call method "view_count" on an undefined value at D:\GitHub\database-
accessor\lib/Database/Accessor.pm line 762.
# Looks like your test exited with 255 before it could output anything.

Hmm so I have a few loose ends from yesterday's post. The code in question is this line

@elements = @{$self->gather->view_elements()}
if ($self->gather->view_count());

In this case the test starts with this $in_hash;

my $in_hash = {
view => {
name => 'name',
alias => 'alias'
},
elements => [ { name => 'first_name', }, { name => 'last_name', }, ],
};

so not Gather and thus the attribute gather is 'undef' and hence the error. So a little fix is needed.

my @elements = @{$self->elements()};
@elements = @{$self->gather->view_elements()}
-- if ($self->gather->view_count());
++ if ($self->gather() and $self->gather->view_count());

for (my $index=0; $index < scalar(@elements); $index++) {
my $element = $self->elements()->[$index];
$element = $self->gather->view_elements()->[$index]
-- if ($self->gather->view_count());
++ if ($self->gather() and $self->gather->view_count());


and now I get a full pass on '30_view.t'.

My next problem with my new Gater API was in '47_dynamic_gathers.t';


Can't use an undefined value as an ARRAY reference
at D:\GitHub\database-accessor\lib/Database/Accessor.pm line 917.

again it was the same error which is fixed in the same way as the last

if ($self->gather()) {
push(@elements,@{$self->gather()->elements()});
push(@conditions,@{$self->gather()->conditions});
push(@view_elements,@{$self->gather()->view_elements})
++ if ($self->gather->view_elements());
}
if ($self->dynamic_gather()){
push(@elements, @{$self->dynamic_gather()->elements()});
push(@conditions,@{$self->dynamic_gather()->conditions});
push(@view_elements,@{$self->dynamic_gather()->view_elements})
++ if ($self->dynamic_gather->view_elements());
}

and that fixes that problem but the next problem was;

Attribute (view_elements) does not pass the type constraint because: ArrayRefofGroupElements can
not be an empty array ref at D:\GitHub\database-accessor\lib/Database/Accessor.pm line 921

and this fixes that problem;

$gather = Database::Accessor::Gather->new({elements=>\@elements,
++ view_elements=>\@view_elements,
conditions=>\@conditions});
++ $gather->view_elements(\@view_elements)
++ if (@view_elements);

now that test passes.

Now that is all nice and good but I think I need another test to check that dynamic_gather to see if it is working with the 'view_elements' so I added this in.


$da->reset_gather();
$gather2 = {
elements => [
{
name => 'first_name',
view => 'People4'
},
{ name => 'user_id',
view => 'People6'
}
],
view_elements => [
{
name => 'first_name',
view => 'People4'
},
{
function => 'count',
left => { name => 'user_id',
view => 'People6'}
}
],
conditions => [
{
left => {
name => 'last_name',
view => 'People7'
},
right => { value => 'test' },
operator => '=',

},
]};
$da->add_gather($gather2);
$da->retrieve( Data::Test->new(), $return );
$dad = $da->result->error();
ok( $dad->elements->[1] ) eq "Database::Accessor::Function",
'elements 1 is a function' );

which adds in some view_elements and checks the returned $dad second element is a Function class. I ran it and it failed;

ok 20 - dynamic_gather()->elements->[0] is a If
not ok 21 - elements 1 is a function

Still some bugs in there.

The fix was a little involved, first I had to do this;


my $dad = $driver->new(
{
view => $self->view,
++ elements => $self->get_dad_elements($action,$opt),
++ elements => $self->get_dad_elements($action,$gather),
conditions => [@{$self->conditions},@{$self->dynamic_conditions}],


pass the $gather down to the 'get_dad_elements' function which I then had to change to this;

private_method get_dad_elements => sub {
my $self = shift;
-- my ($action,$opt) = @_;
++ my ($action,$gather) = @_;
$self->_identity_index(-1);
my @allowed;
my @elements = @{$self->elements()};
@elements = @{$self->gather->view_elements()}
-- if ($self->gather->view_count());
++ @elements = @{$gather->view_elements()}
++ if (ref($gather) and $gather->view_elements() and $gather->view_count());

for (my $index=0; $index < scalar(@elements); $index++) {
my $element = $self->elements()->[$index];
-- $element = $self->gather->view_elements()->[$index]
-- if ($self->gather->view_count());
++ $element = $gather->view_elements()->[$index]
++ if (ref($gather) and $gather->view_elements() and $gather->view_count());


and now my test case passes.

Pushing along the next problem I ran into was with '57_dad_elements.t' and I got;


Can't use an undefined value as an ARRAY reference at D:\GitHub\database-accessor\lib/Database/Accessor.pm line 846.
# Looks like your test exited with 255 before it could output anything.

Before any tests run. Still more problems sigh. Here is the patch for that one

push(@items,(@{ $self->gather->conditions }, @{ $self->gather->elements }))
if ( $self->gather());
push(@items,@{ $self->gather->view_elements })
if ($self->gather() and $self->gather->view_elements);
push(@items,(@{ $self->dynamic_gather->conditions }, @{ $self->dynamic_gather->elements }))
if ( $self->dynamic_gather());
push(@items,@{ $self->dynamic_gather->view_elements })
if ($self->dynamic_gather() and $self->dynamic_gather->view_elements);

and then I get a full pass on the test case.

Finally I ran the rest of the test cases for Database::Accessor and everything passes finally.

So much for a post-ette.

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