More Moose Case Tests

It't clean-up test day again here in the Moose-Pen

Well over the past few days I seem to be just about ready to go back to Driver::DBI but I find yet another little thing wrong with my Database::Accessor when playing with my new test Class.

The first thing is to clean up the test case '15_case.t' as it hard 255 fails with;


Can't call method "statement" on unblessed reference at 15_case.t line 281.

That one is really just an update to take into account yesterday's change from a flat array-ref to an array-ref that can contain other array-refs and the final tests now like this;

...
ok( ref( $case->whens->[2]) eq 'ARRAY',
"big when [2] is an array ref" );
ok( ref( $case->whens->[2]->[1]) eq 'Database::Accessor::Case::When',
"big when [2]->[1] is a when" );
ok( ref( $case->whens->[2]->[1]->statement) eq 'Database::Accessor::Element',
"big when [2]->[1]->statement is an Element" );
ok( ref( $case->whens->[3]) eq 'ARRAY',
"big when [3] is an array ref" );
ok( ref( $case->whens->[3]->[1]) eq 'Database::Accessor::Case::When',
"big when [3]->[1] is a when" );
ok( ref( $case->whens->[3]->[1]->statement) eq 'Database::Accessor::Function',
"big when [3]->[1]->statement is an Fucntion" );
ok( ref( $case->whens->[4]->statement ) eq 'Database::Accessor::Expression',
"big when [4] statment is a Expression" );

So that one is cleaned up nicely, onto the next little situation.

I really only have been testing to see if a 'case' class works on it own and if it can be used in an 'elements' attribute. I know that the 'case' can be used in an SQL 'where' a 'sort' and even a 'group by' clauses so I better test those as well;

I started in 33_conditions.t with this test;


$in_hash3->{conditions} =
{
left => {
name => 'second_1',
view => 'People'
},
right =>{
whens => [
{
left => { name => 'Price', },
right => { value => '10' },
operator => '<',
statement => { name => 'price' }
},
{ statement => { name => 'prices' } }
]},
operator => '=',
};
$da2 = Database::Accessor->new($in_hash3);
ok( ref( $da2->conditions()->[0]->predicates->right ) eq "Database::Accessor::Case",
'condition->[0]->right is a Case' );

and got this on the first run;

Attribute (right) does not pass the type constraint because: Validation failed for 'ArrayRefofElements|ArrayRefofExpressions|ArrayRefofParams|Element|Expression
|Function|Param' with

so will have to check the 'coercion' and 'types' again;

Looking there for a few mins I did not find the big long list of failed classes. I found it, in a little different forms, in Database::Accessor::Roles::Comparators;


has right => (
is => 'rw',
isa =>
'Element|Param|Function|Expression|ArrayRefofParams
|ArrayRefofElements|ArrayRefofExpressions',
coerce => 1,
);

and all I did was add in my 'Case' to that 'isa' list;

-- isa =>'Element|Param|Function|Expression|ArrayRefofParams
|ArrayRefofElements|ArrayRefofExpressions',
++ isa =>'Case|Element|Param|Function|Expression|ArrayRefofParams
|ArrayRefofElements|ArrayRefofExpressions',

and I get a pass;

ok 13 - condition->[0]->right is a When

While I was here I also updated the 'left' attribute of that class as well;

has left => (
is => 'rw',
-- isa => 'Expression|Param|Element|Function|ArrayRefofParams
|ArrayRefofElements|ArrayRefofExpressions',
++ isa => 'Case|Expression|Param|Element|Function|ArrayRefofParams
|ArrayRefofElements|ArrayRefofExpressions',
required => 1,
coerce => 1,
);

Now for sorts and the test for that is much the same;

$in_hash->{sorts} = [{
whens => [
{
left => { name => 'Price', },
right => { value => '10' },
operator => '<',
statement => { name => 'price' }
},
{ statement => { name => 'prices' } }
]
}];
$da = Database::Accessor->new($in_hash);
ok( ref( $da->sorts()->[0] ) eq "Database::Accessor::Case",
'condition->[0]->right is a When' );

and again I get

Attribute (sorts) does not pass the type constraint because: Validation failed for
'ArrayRefofParams' with value [
...

This time the fix was in the 'Database::Accessor::Types' and all I needed to so was expand the 'ArrayRrefofParams' with that 'Case' class

--subtype 'ArrayRefofParams' => as 'ArrayRef[Element|Param|Function|Expression]';
++subtype 'ArrayRefofParams' => as 'ArrayRef[Case|Element|Param|Function|Expression]';

and I get a pass;


ok 11 - correct Sort count on delete
ok 12 - sort->[0]->right is a Case

Now for the next one the 'Gather' or 'Group By' in SQL and the same sort of test;

$da->reset_gather();
$gather2 = {
elements => [
{
whens => [
{
left => { name => 'Price', },
right => { value => '10' },
operator => '<',
statement => { name => 'price' }
},
{ statement => { name => 'prices' } }
]
}
],
};
$da->add_gather($gather2);
$da->retrieve( Data::Test->new(), $return );
ok( ref( $da->dynamic_gather()->elements->[0] ) eq "Database::Accessor::Case",
'dynamic_gather()->elements->[0] is a Case' );

and this time I am lucky I get a pass first time;

...
ok 19 - DAD Dynamic Gather 2 elements correct 0 correct
ok 20 - dynamic_gather()->elements->[0] is a Case

So a good days work and maybe tomorrow I can get onto Driver::DBI?

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