Silence pesky Devel::Cycle warning in leak test on 5.12+
[dbsrgits/DBIx-Class.git] / t / 88result_set_column.t
CommitLineData
70350518 1use strict;
4e55c3ae 2use warnings;
70350518 3
4use Test::More;
4e55c3ae 5use Test::Warn;
5d1fc7dc 6use Test::Exception;
70350518 7use lib qw(t/lib);
8use DBICTest;
66e33361 9use DBIC::SqlMakerTest;
70350518 10
a47e1233 11my $schema = DBICTest->init_schema();
2bb7b40b 12
ee12e25a 13my $rs = $schema->resultset("CD");
14
15cmp_ok (
16 $rs->count,
fb88ca2c 17 '>',
ee12e25a 18 $rs->search ({}, {columns => ['year'], distinct => 1})->count,
19 'At least one year is the same in rs'
20);
2bb7b40b 21
22my $rs_title = $rs->get_column('title');
23my $rs_year = $rs->get_column('year');
20ea4813 24my $max_year = $rs->get_column(\'MAX (year)');
2bb7b40b 25
fb88ca2c 26my @all_titles = $rs_title->all;
27cmp_ok(scalar @all_titles, '==', 5, "five titles returned");
28
29my @nexted_titles;
30while (my $r = $rs_title->next) {
31 push @nexted_titles, $r;
32}
33
34is_deeply (\@all_titles, \@nexted_titles, 'next works');
35
5d62876f 36is_deeply( [ sort $rs_year->func('DISTINCT') ], [ 1997, 1998, 1999, 2001 ], "wantarray context okay");
20ea4813 37ok ($max_year->next == $rs_year->max, q/get_column (\'FUNC') ok/);
5d62876f 38
2bb7b40b 39cmp_ok($rs_year->max, '==', 2001, "max okay for year");
40is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title");
41
42cmp_ok($rs_year->sum, '==', 9996, "three artists returned");
43
fb88ca2c 44my $rso_year = $rs->search({}, { order_by => 'cdid' })->get_column('year');
45is($rso_year->next, 1999, "reset okay");
66521001 46
fb88ca2c 47is($rso_year->first, 1999, "first okay");
66521001 48
4e55c3ae 49warnings_exist (sub {
fb88ca2c 50 is($rso_year->single, 1999, "single okay");
4e55c3ae 51}, qr/Query returned more than one row/, 'single warned');
52
ee12e25a 53
54# test distinct propagation
55is_deeply (
6f723769 56 [sort $rs->search ({}, { distinct => 1 })->get_column ('year')->all],
57 [sort $rs_year->func('distinct')],
ee12e25a 58 'distinct => 1 is passed through properly',
59);
60
5d1fc7dc 61# test +select/+as for single column
ae515736 62my $psrs = $schema->resultset('CD')->search({},
63 {
472d7df3 64 '+select' => \'MAX(year)',
65 '+as' => 'last_year'
ae515736 66 }
67);
472d7df3 68lives_ok(sub { $psrs->get_column('last_year')->next }, '+select/+as additional column "last_year" present (scalar)');
5d1fc7dc 69dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception');
ae515736 70
66e33361 71# test +select/+as for overriding a column
72$psrs = $schema->resultset('CD')->search({},
73 {
74 'select' => \"'The Final Countdown'",
75 'as' => 'title'
76 }
77);
78is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"');
79
80
5d1fc7dc 81# test +select/+as for multiple columns
ae515736 82$psrs = $schema->resultset('CD')->search({},
83 {
472d7df3 84 '+select' => [ \'LENGTH(title) AS title_length', 'title' ],
85 '+as' => [ 'tlength', 'addedtitle' ]
ae515736 86 }
87);
472d7df3 88lives_ok(sub { $psrs->get_column('tlength')->next }, '+select/+as multiple additional columns, "tlength" column present');
5d1fc7dc 89lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present');
90
66e33361 91# test that +select/+as specs do not leak
92is_same_sql_bind (
93 $psrs->get_column('year')->as_query,
94 '(SELECT me.year FROM cd me)',
95 [],
96 'Correct SQL for get_column/as'
5d1fc7dc 97);
66e33361 98
99is_same_sql_bind (
100 $psrs->get_column('addedtitle')->as_query,
101 '(SELECT me.title FROM cd me)',
102 [],
103 'Correct SQL for get_column/+as col'
104);
105
106is_same_sql_bind (
472d7df3 107 $psrs->get_column('tlength')->as_query,
108 '(SELECT LENGTH(title) AS title_length FROM cd me)',
66e33361 109 [],
110 'Correct SQL for get_column/+as func'
111);
112
472d7df3 113# test that order_by over a function forces a subquery
114lives_ok ( sub {
115 is_deeply (
116 [ $psrs->search ({}, { order_by => { -desc => 'title_length' } })->get_column ('title')->all ],
117 [
118 "Generic Manufactured Singles",
119 "Come Be Depressed With Us",
120 "Caterwaulin' Blues",
121 "Spoonful of bees",
122 "Forkful of bees",
123 ],
124 'Subquery count induced by aliased ordering function',
125 );
126});
127
128# test for prefetch not leaking
3f6cc7e4 129{
130 my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' });
131 my $rsc = $rs->get_column('year');
132 is( $rsc->{_parent_resultset}->{attrs}->{prefetch}, undef, 'prefetch wiped' );
133}
cda5e082 134
135# test sum()
136is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly');
137
138# test sum over search_related
139my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' });
140ok ($owner->books->count > 1, 'Owner Newton has multiple books');
141is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books');
d8dbe471 142
143
144# make sure joined/prefetched get_column of a PK dtrt
d8dbe471 145$rs->reset;
146my $j_rs = $rs->search ({}, { join => 'tracks' })->get_column ('cdid');
147is_deeply (
148 [ $j_rs->all ],
149 [ map { my $c = $rs->next; ( ($c->id) x $c->tracks->count ) } (1 .. $rs->count) ],
150 'join properly explodes amount of rows from get_column',
151);
152
153$rs->reset;
154my $p_rs = $rs->search ({}, { prefetch => 'tracks' })->get_column ('cdid');
155is_deeply (
fb88ca2c 156 [ sort $p_rs->all ],
157 [ sort $rs->get_column ('cdid')->all ],
d8dbe471 158 'prefetch properly collapses amount of rows from get_column',
159);
b7c79955 160
908aa1bb 161$rs->reset;
162my $pob_rs = $rs->search({}, {
163 select => ['me.title', 'tracks.title'],
164 prefetch => 'tracks',
165 order_by => [{-asc => ['position']}],
166 group_by => ['me.title', 'tracks.title'],
167});
168is_same_sql_bind (
169 $pob_rs->get_column("me.title")->as_query,
170 '(SELECT me.title FROM (SELECT me.title, tracks.title FROM cd me LEFT JOIN track tracks ON tracks.cd = me.cdid GROUP BY me.title, tracks.title ORDER BY position ASC) me)',
171 [],
172 'Correct SQL for prefetch/order_by/group_by'
173);
174
3214abc7 175# test aggregate on a function
176{
177 my $tr_rs = $schema->resultset("Track");
178 $tr_rs->create({ cd => 2, title => 'dealbreaker' });
179
180 is(
181 $tr_rs->get_column('cd')->max,
182 5,
183 "Correct: Max cd in Track is 5"
184 );
185
186 my $track_counts_per_cd_via_group_by = $tr_rs->search({}, {
187 columns => [ 'cd', { cnt => { count => 'trackid', -as => 'cnt' } } ],
188 group_by => 'cd',
189 })->get_column('cnt');
190
191 is ($track_counts_per_cd_via_group_by->max, 4, 'Correct max tracks per cd');
192 is ($track_counts_per_cd_via_group_by->min, 3, 'Correct min tracks per cd');
193 is (
194 sprintf('%0.1f', $track_counts_per_cd_via_group_by->func('avg') ),
195 '3.2',
196 'Correct avg tracks per cd'
197 );
198}
199
b7c79955 200done_testing;