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