Moose Fixes Test
It fix a test postette day here at the Moose-Pen
Felling a little better today as the mind is a little less foggy, that might change back on October 17th though?
Today I am going to fix the first of the bugs I ran into yesterday;
Not a HASH reference at ,,,\t\lib/Database/Accessor/Driver/Test.pm line 13
# Looks like your test exited with 255 just after 6.
The first thing I did was look back at my API and see how I define a $container and it can be Hash-Ref or a Class or an Array-Ref of Hash-refs and Classes and I looked at my failing test. In this test I am using an array_ref;
my $container_array = [];
…
ok($da->create( $data, $container_array ),
"Container can be an Array-ref of Hash-ref and Classed");
...
Perfectly legal. I could just change this to an hash-ref but I would be cutting my test coverage down. I have to got into the Driver::Tests and fix the code there;
What I did was re-factor the code; first I took out all the appropriate code and moved in into a private sub '_get_processed_container'
my ($result, $type, $conn, $container, $opt ) = @_;
-- my $processed_container = {dad_fiddle=>1};
-- foreach my $key (keys(%{$container})){
-- $processed_container->{$key} = $container->{$key};
-- }
-- $result->processed_container($processed_container);
++ $result->processed_container($self->_get_processed_container($container));
and that sub look like this;
sub _get_processed_container {
my $self = shift;
my ($in_container) = @_;
my $in_hash_class = 0;
my @process_array;
my @return_container;
if (ref($in_container) eq 'HASH' or blessed($in_container)){
push(@process_array,$in_container);
$in_hash_class = 1;
}
else {
push(@process_array,@{$in_container});
}
foreach my $record (@process_array){
my $processed_record = {dad_fiddle=>1};
foreach my $key (keys(%{$record})){
$processed_record->{$key} = $record->{$key};
}
push(@return_container,$processed_record);
}
if ($in_hash_class){
return shift(@process_array);
}
else{
return \@process_array;
}
}
I borrowed my idea from my earlier work with '_clean_up_container' sub and how I call it from the '_create_or_update' sub. I check to see if I have a hash-ref or Class and if I do I add it to the '@process_array' and set the '$in_hash_class' flag. Otherwise I add the '$in_container' to the ' @process_array'
Now I simply iterate over the '@process_array' using the same code from before at each iteration and once I am finished, if my '$in_hash_class' flag is set I return the first item in the array otherwise I return a reference to the array.
Now when I run my test case I get;
ok 1 - No Create with out connection class
ok 2 - No Create with empty hash-ref container
ok 3 - Container can be a non empty Hash-ref
ok 4 - Container can be a Class
ok 5 - No Create with empty array-ref container
ok 6 - No Create with array-ref container that has a scalar
ok 7 - Container can be an Array-ref of Hash-ref and Classed
ok 8 - Container drops street on create
ok 9 - Container drops street and phone on create
Leave a comment