Test prompted by discussion about tasty stuff
[dbsrgits/DBIx-Class.git] / t / 88result_set_column.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
a47e1233 8my $schema = DBICTest->init_schema();
2bb7b40b 9
cda5e082 10plan tests => 14;
2bb7b40b 11
58d387fe 12my $cd;
2bb7b40b 13my $rs = $cd = $schema->resultset("CD")->search({});
14
15my $rs_title = $rs->get_column('title');
16my $rs_year = $rs->get_column('year');
20ea4813 17my $max_year = $rs->get_column(\'MAX (year)');
2bb7b40b 18
19is($rs_title->next, 'Spoonful of bees', "next okay");
5d62876f 20is_deeply( [ sort $rs_year->func('DISTINCT') ], [ 1997, 1998, 1999, 2001 ], "wantarray context okay");
20ea4813 21ok ($max_year->next == $rs_year->max, q/get_column (\'FUNC') ok/);
5d62876f 22
2bb7b40b 23my @all = $rs_title->all;
24cmp_ok(scalar @all, '==', 5, "five titles returned");
25
26cmp_ok($rs_year->max, '==', 2001, "max okay for year");
27is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title");
28
29cmp_ok($rs_year->sum, '==', 9996, "three artists returned");
30
ae515736 31my $psrs = $schema->resultset('CD')->search({},
32 {
33 '+select' => \'COUNT(*)',
34 '+as' => 'count'
35 }
36);
37ok(defined($psrs->get_column('count')), '+select/+as count');
38
39$psrs = $schema->resultset('CD')->search({},
40 {
41 '+select' => [ \'COUNT(*)', 'title' ],
42 '+as' => [ 'count', 'addedtitle' ]
43 }
44);
45ok(defined($psrs->get_column('count')), '+select/+as arrayref count');
46ok(defined($psrs->get_column('addedtitle')), '+select/+as title');
47
3f6cc7e4 48{
49 my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' });
50 my $rsc = $rs->get_column('year');
51 is( $rsc->{_parent_resultset}->{attrs}->{prefetch}, undef, 'prefetch wiped' );
52}
cda5e082 53
54# test sum()
55is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly');
56
57# test sum over search_related
58my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' });
59ok ($owner->books->count > 1, 'Owner Newton has multiple books');
60is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books');