Still Some Moose To Go
Well not much moose in the moose-pen again. Like I said in my last post I am getting nearer to that programmer nightmare (at least for me) of documenting and packaging up my project to get it ready for CPAN. But there is still a little Moose left.
One of the neat things about test driven development is all the bugs one find and squish long before you code ever gets out there, today is a good example.
I decided to add in a few more features to my Accessor, (yes I know scope creep) namely I want it to be hard to do a update or delete without having either a static or dynamic condition set.
So I added in two new attributes
has [
qw(update_requires_condtion
delete_requires_condtion
)
] => ( is => 'ro', isa => 'Bool', default => 1 );
and in my delete sub I added in this
die "Attempt to delete without condition"
if (
$self->delete_requires_condition()
and
( $self->condition_count() + $self->dynamic_condition_count() <= 0 )
);
and about the same thing in my update sub as well. Here is the moosey part, note how I used the native array delegates, condition_count and dynamic_condition_count for count, rather than the messy scalar() style I usually used.
As the above code is so simple I would just move onto something else but this time round I was very keen on doing the tests as I went along and I am glad I did.
So in when I was writing 52_delete.t I started to get this error
Can't locate object method "delete" via package "Database::Accessor" at 52_delete.t line 41.
I cheeked the spelling of 'delete' in the test and in Accessor.pm all ok and there was no syntax error anywhere then I realized in my haste I had inattentively put the delete sub outside the Database::Accessor package. Something that does happen when you have more than one package in a file, so after a quick copy and paste to the correct spot one bug squished before it got out.
Now to test this I had to try and make my test error which was easy enough
my $in_hash = {
view => { name => 'People' },
elements => [
{
name => 'first_name',
view => 'People'
},
{
name => 'last_name',
view => 'People'
},
{
name => 'user_id',
view => 'People'
}
],
};
my $da = Database::Accessor->new($in_hash);
my $return_str = {};
my $data = Data::Test->new();
$da->delete( $data, $return_str );
eval {
$da->delete( $data, $return_str );
};
ok( $@, 'Cannot delete without condition' );
ok(index($@,'Attempt to delete without condition')!=1,'Error message OK');
Nothing special really just had to remember to wrap my delete in an eval then check the $@ came back with an error and then I check to ensure the error message was correct. This was the crucial one really as not only did if fail with the delete sub out of place it failed again once I moved it about.
Again it was a simple typo in my code I had misspelled condition in Accessor I had condtion but for some reason in my test I spelled it correctly and caught the bug.
I also took the time today to clean up the names of my count delegates I had had defined them originally in plural form elements_count really that is a singular so I changed them over to element_count and alike.
As well I finished of the all the crud tests. The way I test those is I simpley pass back in the the required constant on the $return_str from my DAD test like this
ok($return_str->{type} eq Database::Accessor::Constants::DELETE,'Delete constant passed in and out');
Leave a comment