Call mro reinit in tests only on 5.8
[dbsrgits/DBIx-Class-Historic.git] / t / 76select.t
CommitLineData
218aa968 1use strict;
c4af4c4e 2use warnings;
218aa968 3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
a5a7bb73 7use DBICTest ':DiffSQL';
218aa968 8
9my $schema = DBICTest->init_schema();
10
218aa968 11my $rs = $schema->resultset('CD')->search({},
12 {
13 '+select' => \ 'COUNT(*)',
14 '+as' => 'count'
15 }
16);
17lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present');
18dies_ok(sub { $rs->first->get_column('nonexistent_column') }, 'nonexistant column requests still throw exceptions');
19
20$rs = $schema->resultset('CD')->search({},
21 {
22 '+select' => [ \ 'COUNT(*)', 'title' ],
23 '+as' => [ 'count', 'addedtitle' ]
24 }
25);
26lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present');
27lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present');
28
29$rs = $schema->resultset('CD')->search({},
30 {
31 '+select' => [ \ 'COUNT(*)', 'title' ],
32 '+as' => [ 'count', 'addedtitle' ]
33 }
34)->search({},
35 {
36 '+select' => 'title',
37 '+as' => 'addedtitle2'
38 }
39);
40lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present');
41lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present');
42lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present');
0eb27426 43
44
45# test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects)
46# also shows that outer select attributes are ok (i.e. order_by)
47#
48# from doesn't seem to be useful without using a scalarref - there were no initial tests >:(
49#
0eb27426 50my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent
51cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
52
53my $table = $cds->result_source->name;
80625830 54$table = $$table if ref $table eq 'SCALAR';
0eb27426 55my $subsel = $cds->search ({}, {
56 columns => [qw/cdid title/],
57 from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
58});
59
60is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
61is ($subsel->next->title, $cds->next->title, 'First CD title match');
62is ($subsel->next->title, $cds->next->title, 'Second CD title match');
bbdff861 63
64is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');
61d26fae 65
faf79003 66
67
68$rs = $schema->resultset('CD')->search({},
61d26fae 69 {
70 'join' => 'artist',
faf79003 71 'columns' => ['cdid', 'title', 'artist.name'],
61d26fae 72 }
73);
faf79003 74
faf79003 75is_same_sql_bind (
af6aac2d 76 $rs->as_query,
faf79003 77 '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
78 [],
79 'Use of columns attribute results in proper sql'
80);
81
82lives_ok(sub {
83 $rs->first->get_column('cdid')
84}, 'columns 1st rscolumn present');
85
86lives_ok(sub {
87 $rs->first->get_column('title')
88}, 'columns 2nd rscolumn present');
89
0ca2f0d1 90lives_ok(sub {
bbd828a6 91 $rs->first->artist->get_column('name')
92}, 'columns 3rd rscolumn present');
4c253752 93
94
faf79003 95
96$rs = $schema->resultset('CD')->search({},
bbd828a6 97 {
faf79003 98 'join' => 'artist',
99 '+columns' => ['cdid', 'title', 'artist.name'],
100 }
101);
102
faf79003 103is_same_sql_bind (
af6aac2d 104 $rs->as_query,
37aafa2e 105 '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
faf79003 106 [],
107 'Use of columns attribute results in proper sql'
108);
109
110lives_ok(sub {
bbd828a6 111 $rs->first->get_column('cdid')
faf79003 112}, 'columns 1st rscolumn present');
113
114lives_ok(sub {
115 $rs->first->get_column('title')
116}, 'columns 2nd rscolumn present');
4c253752 117
0ca2f0d1 118lives_ok(sub {
119 $rs->first->artist->get_column('name')
120}, 'columns 3rd rscolumn present');
b3b13ec9 121
122
123$rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } },
124 {
125 join => 'tracks',
126 columns => [qw/me.cdid me.title/],
127 '+select' => ['tracks.position'],
128 '+as' => ['track_position'],
129
130 # get a hashref of CD1 only (the first with a second track)
131 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
132 order_by => 'cdid',
133 rows => 1,
134 }
135);
136
137is_deeply (
138 $rs->single,
139 {
140 cdid => 1,
141 track_position => 2,
142 title => 'Spoonful of bees',
143 },
144 'limited prefetch via column works on a multi-relationship',
145);
146
18850595 147my $sub_rs = $rs->search ({},
b3b13ec9 148 {
149 columns => [qw/artist tracks.trackid/], # columns should not be merged but override $rs columns
150 '+select' => ['tracks.title'],
151 '+as' => ['tracks.title'],
152 }
153);
154
c9d29bb2 155is_deeply(
b3b13ec9 156 $sub_rs->single,
157 {
c9d29bb2 158 artist => 1,
0d5e6c6c 159 tracks => {
160 title => 'Apiary',
1dfb11d4 161 trackid => 17,
c9d29bb2 162 },
b3b13ec9 163 },
164 'columns/select/as fold properly on sub-searches',
165);
166
fd4b0742 167# *very* esoteric use-case, yet valid (the "empty" object should not be undef):
168$rs = $schema->resultset('Artist');
169$rs->create({ artistid => 69, name => 'Ranetki' });
170
171my $relations_or_1_count =
172 $rs->search_related('cds')->count
173 +
174 $rs->search({ 'cds.cdid' => undef }, { join => 'cds' })->count
175;
176
177my $weird_rs = $rs->search({}, {
178 order_by => { -desc => [ 'me.artistid', 'cds.cdid' ] },
179 columns => [{ cd_title => 'cds.title', cd_year => 'cds.year' }],
180 join => 'cds',
181});
182
183my $weird_rs_hri = $weird_rs->search({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
184
185for my $rs ($weird_rs, $weird_rs_hri) {
186 is ($rs->count, $relations_or_1_count, 'count on rhs data injection matches');
187
188 my @all;
189 while (my $r = $rs->next) {
190 push @all, $r;
191 }
192
193 is (scalar @all, $relations_or_1_count, 'object count on rhs data injection matches');
194 is_deeply (
195 ( $rs->result_class eq 'DBIx::Class::ResultClass::HashRefInflator'
196 ? \@all
197 : [ map { +{$_->get_columns} } @all ]
198 ),
199 [
200 {
201 cd_title => undef,
202 cd_year => undef,
203 },
204 {
205 cd_title => "Come Be Depressed With Us",
206 cd_year => 1998,
207 },
208 {
209 cd_title => "Generic Manufactured Singles",
210 cd_year => 2001,
211 },
212 {
213 cd_title => "Caterwaulin' Blues",
214 cd_year => 1997,
215 },
216 {
217 cd_title => "Forkful of bees",
218 cd_year => 2001,
219 },
220 {
221 cd_title => "Spoonful of bees",
222 cd_year => 1999,
223 },
224 ],
225 'Correct data retrieved'
226 );
227
228 is_deeply( [ $rs->all ], \@all, '->all matches' );
229}
230
c4af4c4e 231done_testing;