Best Moose Opening Line Ever
So today in the Moose-Pen I was going to get my 33_conditions.t test case working. New we left off in my last post with this call to Accessor
my $user = Database::Accessor->new(
{
view => {name => 'People'},
elements => [{ name => 'first_name',
view=>'People' },
{ name => 'last_name',
view => 'People' },
{ name => 'user_id',
view =>'People' } ],
conditions=>[{ left=>{name=>'user_id',
view=>'People'},
operator=>'=',
right=>{param=>'test'}},
{ condition=>'AND',
left=>{name=>'first_name',
view=>'People'},
operator=>'=',
right=>{param=>'John'}}, ]
}
);
which in SQL is
SELECT People.first_name,People.last_name, People.user_id
FROM People
WHERE People.user_id = 'test'
AND People.first_name = 'John'
and Mongo something like this
db.People.find({$and: [{user_id: {$eq: test}},{user_id: {$eq: John}}]},{ street: 1, city: 1, country: 1}
Not I do not have a working Mongo or SQL DAD right not so the above are just for show so I have to use my fudged results from my last post with my Test::DAD. So I am looking to get this
RETRIEVE-->View-->'People'-->Elements-->'People.first_name',
'People.last_name','People.user_id'-->Condtion->People.user_id=test-->AND-->
People.first_name=John
The first thing I had to fix was to add in the path to my 'Test::Data' and 'Database::Accessor::DAD::Test' classes and as I did before in an earlier post, this is easily done with two use libs
use lib ('..\t\lib');
use lib ('..\lib');
and then ran my test for the first time and got;
Attribute (name) is required at C:\Dwimperl\perl\site\lib\Moose\Object.pm line 24
-->23 lines of obtuse Moose poop here<--
Moose::Object::new('Database::Accessor', 'HASH(0x54ecddc)') called at 33_conditions.t line 15
Him will have to blog about getting rid of those 23 lines of Moos poop someday. Anyway on with today’s post, what is happening here is I am trying to use a 'param' on both of my 'right' attributes of my 'conditions' predicates and I am getting a 'required' attribute fail on an Element object.
So looking back in my 'Predicate' class I have
has right => (
is => 'rw',
isa => 'Element',
required => 1,
coerce => 1,
);
Now to fix that I can just add in the 'type' I need, 'Param', to my Types role as you have seen in a number of my recent posts. Once I have done that I just add it to the 'isa'
-- isa => 'Element',
++ isa => 'Element|Param',
and try my test again but I still get the same error.
Attribute (name) is required at C:\Dwimperl\perl\site\lib\Moose\Object.pm line 24
-->23 lines of obtuse Moose poop here<--
Moose::Object::new('Database::Accessor', 'HASH(0x54ecddc)') called at 33_conditions.t
line 15
So I dig a little deeper and I notice that the my Accessor attribute 'conditions' has the 'ArrayRefofPredicates' type which I wrong it should be an 'ArrayRefofCondtions' luckily I already have that subtype so all I need to do is this
has conditions => (
is => 'ro',
-- isa => 'ArrayRefofPredicates',
++ isa => 'ArrayRefofCondtions',
coerce => 1,
default => sub { [] },
);
and I run my test and get
Attribute (Conditions) does not pass the type constraint because: Validation failed for
'ArrayRefofPredicates' with value [ Database::Accessor::Condition{ },
Database::Accessor::Condition{ } ] at
...
so still something amiss and then I remembered the upper case rule for my DAD role and made this quick change to my DAD::Role
has Conditions => (
-- isa => 'ArrayRefofPredicates',
++ isa => 'ArrayRefofConditions',
is => 'ro',
);
and got
ok 1 - use Database::Accessor;not ok 2 - Got the correct result
which is what I expected as I have not done anything in the test DAD yet. But that is another post.
Leave a comment