Moose Loose Ends Part the X

Its make and mend day here in the Moose-Pen

Now that I have my constructor single error working the way I like I figure I better go back and clean up my test cases and create a new test case just to check the error on 'new' construction.

I am going to start with '30_view.t' as that is the one I have been mucking with most over the past little while.

The first thing I did was strip out the new 'validation' tests and suck them in a new tests case '42_new_validation.t' and after a few adjustments it presently looks like this;


my $da;

like(
exception { $da = Database::Accessor->new( {} ); },
qr /The following Attributes are required: \(elements,view\)/,
"View and Elements required"
);

$in_hash = {
view => {
name => undef,
alias => undef
}
};
eval { $da = Database::Accessor->new($in_hash); };
ok( $@->has_errors(), "Error on new invalid" );
ok( scalar( $@->errors ) == 1, "Single error on new invalid" );
my @errors = $@->errors;
ok( ref( $errors[0] ) eq 'MooseX::Constructor::AllErrors::Error::Misc',
"Got single Misc error" );
ok(
index( $errors[0]->message,
"'view->name' Constraint: Validation failed for 'Str' with value undef"
) > -1,
'view->name validation present'
);
ok(
index( $errors[0]->message,
"'view->alias' Constraint: Validation failed for 'Str' with value undef"
) > -1,
'view->alias validation present'
);

$ENV{DA_ALL_ERRORS} = 1;

eval { $da = Database::Accessor->new($in_hash); };
ok( scalar( $@->errors ) == 2, "Two errors on new when DA_ALL_ERRORS is one" );
my @errors = $@->errors;
ok( $errors[0]->attribute->name() == 'name', "View: param name fails" );
ok( $errors[1]->attribute->name() == 'alias', "View: param alias fails" );

Quite a few changes but I think I test all the new base functionality. First I test that the scalar die throws the correct exceptions (with all the errors) and at the same time I test that both 'view' and 'elements' are required.

Next I test the non-scalar call of die where I should get only a single MooseX::Constructor::AllErrors::Error::Misc' error back and that it contains the correct two messages.
I then set the 'DA_ALL_ERRORS' and the next tests check to ensure I get errors back and the have the correct name attributes. I could add more tests to check the messages but that would be just a little redundant.

Now the next round of test I whent with the following


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

my $tests = {
conditions => [
{
caption => 'Left is required',
exception => '\(conditions->left\)',
conditions => {
leftx => {
name => 'last_name',
view => 'People'
},
right => { value => 'test' },
operator => '=',
}
}
],

};

foreach my $in_key ( sort( keys( %{$tests} ) ) ) {
foreach my $test ( @{ $tests->{$in_key} } ) {
$in_hash->{$in_key} = $test->{$in_key};
like(
exception { $da = Database::Accessor->new($in_hash) },
qr /$in_hash->{exception}/,
"$test->{caption} $in_key"
);
delete( $in_hash->{$in_key} );
}
}


as I was hoping to automate thing a little more by looping over a set of tests. The only think out of the ordinary here is I make sure to 'delete' off the '$in_hash' at the end of the test to make sure I get a clean test every tyime.

I dutifully created a number of entries in the $tests hash but on my very first test I did not get the result I expected.


at 42_new_validation.t line 89.
# 'Attribute (left) is required at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 100'
# doesn't match '(?^:\(conditions->left\) )'

and so did most of the rest.

Then I remembered I had set '$ENV{DA_ALL_ERRORS}' on at one point so I turned that off and now the test all pass.


On last thing I snuffed out today was this little warning I had not come across before;

Use of uninitialized value $_ in concatenation (.) or string at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm

That comes from my subtype validation for 'Operator' and the fix for that was simple enough;


subtype 'Operator',
as 'Str',
where { exists( Database::Accessor::Constants::OPERATORS->{ uc($_) } ) },
message {
++ my $operator = $_;
++ $operator = 'undef'
++ unless($operator);
-- "The Operator '$operator', is not a valid Accessor Operator!"
++ "The Operator '$operator', is not a valid Accessor Operator!"
. _try_one_of( Database::Accessor::Constants::OPERATORS() );
};

Now onto more wonderful things tomorrow

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