Moose Stuck on Case

Its change my case yet again day here in the Moose-Pen

Yesterday as I had finishing off my tests for the Case class I was just about to move over and start writing up the code for Driver::DBI when I remember that I should have a test or two to ensure that Database::Accessor can use the new 'Case' class and that is passed it correctly down to the DAD.

I am using it right now as a field and I can test by just adding to the '31_elements.t' test case.

Frist I add in just a simple 'case' as I have already tested all the funny permutations in the '15_case.t' test case;


$in_hash->{elements} = [
...
{
whens => [
{
left => { name => 'Price', },
right => { value => '10' },
operator => '<',
statement => { name => 'price' }
},
{ statement => { name => 'price' } }
]
}
];

and now some tests;

ok( ref( $dad->elements->[3] ) eq 'Database::Accessor::Case',
'Element 3 is an case' );
ok( ref( $dad->elements->[3]->whens->[0]->left ) eq 'Database::Accessor::Element',
'Element 3 whens->[0]->left is an elememt' );
ok( ref( $dad->elements->[3]->whens->[0]->right ) eq 'Database::Accessor::Param',
'Element 3 whens->[0]->right is an param' );
ok( $dad->elements->[3]->whens->[0]->operator eq '<',
'Element 3 whens->[0]->operator is an <' );
ok( ref( $dad->elements->[3]->whens->[0]->statment ) eq 'Database::Accessor::Element',
'Element 3 whens->[0]->statment is an elememt' );
ok( $dad->elements->[3]->whens->[0]->statment->name eq 'price',
'Element 3 whens->[0]->statment->name is price' );
ok( ref( $dad->elements->[3]->whens->[1]->statment ) eq 'Database::Accessor::Element',
'Element 3 whens->[1]->statment is an elememt' );
ok( $dad->elements->[3]->whens->[1]->statment->name eq 'prices',
'Element 3 whens->[1]->statment->name is prices' );

and on my first go I get the now very familiar;

Attribute (elements) does not pass the type constraint because: ArrayRefofElements can not be an empty array ref

Again something in Coercion or my types and I quickly found what I most likely needed to do was extend the 'ArrayRefofElements' subtype with the new 'Case' class;

subtype 'ArrayRefofElements' => as
-- 'ArrayRef[Element|Param|Function|Expression]',
++ 'ArrayRef[Element|Param|Function|Expression|Case]',
where { scalar(@{$_})<=0 ? 0 : 1; },
message {
"ArrayRefofElements can not be an empty array ref";
};

and now I get a little further along with my test case but still get a fatal error

ok 34 - Element 3 whens->[0]->right is an param
ok 35 - Element 3 whens->[0]->operator is an <
Can't locate object method "statment" via package "Database::Accessor::Case::When" at 31_elements.t line 206.
# Looks like your test exited with 255 just after 35.

and that is just a typo in my tests and with a quick search and replace I am good to go again and I get


ok 31 - Element 2 right->right->left->right is an Element
ok 32 - Element 3 is an case
ok 33 - Element 3 whens->[0]->left is an elememt
ok 34 - Element 3 whens->[0]->right is an param
ok 35 - Element 3 whens->[0]->operator is an <
ok 36 - Element 3 whens->[0]->statement is an elememt
ok 37 - Element 3 whens->[0]->statement->name is price
ok 38 - Element 3 whens->[1]->statement is an elememt
ok 39 - Element 3 whens->[1]->statement->name is prices

so maybe in my next post I will get to Driver::DBI??

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