More Bugs on Moose
It try it out day here in the Moose-Pen
With that new API in hand lets give a practical test in my 'xt' dir another go. Starting with this test;
my $regions_count = $user_db->regions_count_data();
$da->only_elements();
$da->add_gather({elements => [{name => 'description',
view => 'region'},
{ name => 'user_id',
view => 'people' }],
view_elements=>[{name => 'description',
view => 'region'},
{function => 'count',
left => { name => 'user_id',
view => 'people' }
},]});
$da->retrieve($dbh);
cmp_deeply( $da->result()->set, $regions_count,
"3 regions selected with proper counts");
Just doing a count on the user_id by region and $regions_count_data contains my expected results;
[
['NA',2],
['West',2],
[ undef,0],
];
The only thing that is worrying me is the;
{name => 'description',
view => 'region'},
I am not 100% sure that it is going to get the correct field/element as the original in the elements list has an alias;
{name=>'description',
alias=>'region',
view=>'region',},
Well let's see what comes out of the test;
not ok 2 - 3 regions selected with proper counts
# Failed test '3 regions selected with proper counts'
# at 50_group_by.t line 53.
# Compared array length of $data
# got : array with 8 element(s)
# expect : array with 3 element(s)
Opps silly me the SQL is not what I want. I am doing something like
SELECT region.description, COUNT(people.user_id) FROM people ...
which is not want I want I want
SELECT region.description, COUNT(region.description) FROM people …
so I quick fix to;
$da->add_gather({elements => [{name => 'description',
view => 'region'},
-- { name => 'user_id',
-- view => 'people' }],
view_elements=>[{name => 'description',
view => 'region'},
{function => 'count',
left => { name => 'description',
view => 'region' }
},]});
The good thing is that my SQL did at least work with that element/field with an alias. On the next run I now get;
ok 1 - 3 regions selected in order
ok 2 - 3 regions selected with proper counts
Now I was looking at this one and I though I might have a little whole in my API while using 'view_elements' I don't seem to remember having any constraint or check on an add to that attribute to ensure the value entered is in the 'elements' array of the Database::Accessor.
So I added in another field called salary to my persons table and gave it some data than I added this test;
$da->add_gather({elements => [{name => 'user_id',
view => 'people'},
{name => 'salary',
view => 'people'}],
view_elements=>[{name => 'user_id'',
view => 'people'},
{name => 'salary',
view => 'people'},]
});
$da->retrieve($dbh);
ok( $da->result()->is_error,"Cannot do back door select");
which should fail because;
{name => 'salary',
view => 'people'}
is not in the 'elements/fields' and when I run it I get;
ok 1 - 3 regions selected in order
ok 2 - 3 regions selected with proper counts
not ok 3 - Cannot do back door select
Opps will have to fix that as I dumped trhe results and I did see the correct values for 'salary' comming up. Something for tomorrow I guess.
Leave a comment