rs->get_column now properly recognizes prefetch and collapses if at all possible
[dbsrgits/DBIx-Class.git] / t / 88result_set_column.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5d1fc7dc 5use Test::Exception;
70350518 6use lib qw(t/lib);
7use DBICTest;
8
a47e1233 9my $schema = DBICTest->init_schema();
2bb7b40b 10
d8dbe471 11plan tests => 20;
2bb7b40b 12
d8dbe471 13my $rs = $schema->resultset("CD")->search({}, { order_by => 'cdid' });
2bb7b40b 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
66521001 31$rs_year->reset;
32is($rs_year->next, 1999, "reset okay");
33
34is($rs_year->first, 1999, "first okay");
35
5d1fc7dc 36# test +select/+as for single column
ae515736 37my $psrs = $schema->resultset('CD')->search({},
38 {
39 '+select' => \'COUNT(*)',
40 '+as' => 'count'
41 }
42);
5d1fc7dc 43lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as additional column "count" present (scalar)');
44dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception');
ae515736 45
5d1fc7dc 46# test +select/+as for multiple columns
ae515736 47$psrs = $schema->resultset('CD')->search({},
48 {
49 '+select' => [ \'COUNT(*)', 'title' ],
50 '+as' => [ 'count', 'addedtitle' ]
51 }
52);
5d1fc7dc 53lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as multiple additional columns, "count" column present');
54lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present');
55
56# test +select/+as for overriding a column
57$psrs = $schema->resultset('CD')->search({},
58 {
59 'select' => \"'The Final Countdown'",
60 'as' => 'title'
61 }
62);
63is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"');
ae515736 64
3f6cc7e4 65{
66 my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' });
67 my $rsc = $rs->get_column('year');
68 is( $rsc->{_parent_resultset}->{attrs}->{prefetch}, undef, 'prefetch wiped' );
69}
cda5e082 70
71# test sum()
72is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly');
73
74# test sum over search_related
75my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' });
76ok ($owner->books->count > 1, 'Owner Newton has multiple books');
77is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books');
d8dbe471 78
79
80# make sure joined/prefetched get_column of a PK dtrt
81
82$rs->reset;
83my $j_rs = $rs->search ({}, { join => 'tracks' })->get_column ('cdid');
84is_deeply (
85 [ $j_rs->all ],
86 [ map { my $c = $rs->next; ( ($c->id) x $c->tracks->count ) } (1 .. $rs->count) ],
87 'join properly explodes amount of rows from get_column',
88);
89
90$rs->reset;
91my $p_rs = $rs->search ({}, { prefetch => 'tracks' })->get_column ('cdid');
92is_deeply (
93 [ $p_rs->all ],
94 [ $rs->get_column ('cdid')->all ],
95 'prefetch properly collapses amount of rows from get_column',
96);