Commit | Line | Data |
218aa968 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
faf79003 |
8 | use DBIC::SqlMakerTest; |
218aa968 |
9 | |
10 | my $schema = DBICTest->init_schema(); |
11 | |
b3b13ec9 |
12 | plan tests => 24; |
218aa968 |
13 | |
14 | my $rs = $schema->resultset('CD')->search({}, |
15 | { |
16 | '+select' => \ 'COUNT(*)', |
17 | '+as' => 'count' |
18 | } |
19 | ); |
20 | lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present'); |
21 | dies_ok(sub { $rs->first->get_column('nonexistent_column') }, 'nonexistant column requests still throw exceptions'); |
22 | |
23 | $rs = $schema->resultset('CD')->search({}, |
24 | { |
25 | '+select' => [ \ 'COUNT(*)', 'title' ], |
26 | '+as' => [ 'count', 'addedtitle' ] |
27 | } |
28 | ); |
29 | lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present'); |
30 | lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present'); |
31 | |
121068ec |
32 | # Tests a regression in ResultSetColumn wrt +select |
9dbe8766 |
33 | $rs = $schema->resultset('CD')->search(undef, |
121068ec |
34 | { |
35 | '+select' => [ \'COUNT(*) AS year_count' ], |
36 | order_by => 'year_count' |
37 | } |
38 | ); |
39 | my @counts = $rs->get_column('cdid')->all; |
40 | ok(scalar(@counts), 'got rows from ->all using +select'); |
41 | |
218aa968 |
42 | $rs = $schema->resultset('CD')->search({}, |
43 | { |
44 | '+select' => [ \ 'COUNT(*)', 'title' ], |
45 | '+as' => [ 'count', 'addedtitle' ] |
46 | } |
47 | )->search({}, |
48 | { |
49 | '+select' => 'title', |
50 | '+as' => 'addedtitle2' |
51 | } |
52 | ); |
53 | lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present'); |
54 | lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present'); |
55 | lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present'); |
0eb27426 |
56 | |
57 | |
58 | # test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects) |
59 | # also shows that outer select attributes are ok (i.e. order_by) |
60 | # |
61 | # from doesn't seem to be useful without using a scalarref - there were no initial tests >:( |
62 | # |
0eb27426 |
63 | my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent |
64 | cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs'); |
65 | |
66 | my $table = $cds->result_source->name; |
80625830 |
67 | $table = $$table if ref $table eq 'SCALAR'; |
0eb27426 |
68 | my $subsel = $cds->search ({}, { |
69 | columns => [qw/cdid title/], |
70 | from => \ "(SELECT cdid, title FROM $table LIMIT 2) me", |
71 | }); |
72 | |
73 | is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds'); |
74 | is ($subsel->next->title, $cds->next->title, 'First CD title match'); |
75 | is ($subsel->next->title, $cds->next->title, 'Second CD title match'); |
bbdff861 |
76 | |
77 | is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"'); |
61d26fae |
78 | |
faf79003 |
79 | |
80 | |
81 | $rs = $schema->resultset('CD')->search({}, |
61d26fae |
82 | { |
83 | 'join' => 'artist', |
faf79003 |
84 | 'columns' => ['cdid', 'title', 'artist.name'], |
61d26fae |
85 | } |
86 | ); |
faf79003 |
87 | |
faf79003 |
88 | is_same_sql_bind ( |
af6aac2d |
89 | $rs->as_query, |
faf79003 |
90 | '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)', |
91 | [], |
92 | 'Use of columns attribute results in proper sql' |
93 | ); |
94 | |
95 | lives_ok(sub { |
96 | $rs->first->get_column('cdid') |
97 | }, 'columns 1st rscolumn present'); |
98 | |
99 | lives_ok(sub { |
100 | $rs->first->get_column('title') |
101 | }, 'columns 2nd rscolumn present'); |
102 | |
0ca2f0d1 |
103 | lives_ok(sub { |
104 | $rs->first->artist->get_column('name') |
105 | }, 'columns 3rd rscolumn present'); |
4c253752 |
106 | |
107 | |
faf79003 |
108 | |
109 | $rs = $schema->resultset('CD')->search({}, |
110 | { |
111 | 'join' => 'artist', |
112 | '+columns' => ['cdid', 'title', 'artist.name'], |
113 | } |
114 | ); |
115 | |
faf79003 |
116 | is_same_sql_bind ( |
af6aac2d |
117 | $rs->as_query, |
faf79003 |
118 | '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)', |
119 | [], |
120 | 'Use of columns attribute results in proper sql' |
121 | ); |
122 | |
123 | lives_ok(sub { |
124 | $rs->first->get_column('cdid') |
125 | }, 'columns 1st rscolumn present'); |
126 | |
127 | lives_ok(sub { |
128 | $rs->first->get_column('title') |
129 | }, 'columns 2nd rscolumn present'); |
4c253752 |
130 | |
0ca2f0d1 |
131 | lives_ok(sub { |
132 | $rs->first->artist->get_column('name') |
133 | }, 'columns 3rd rscolumn present'); |
b3b13ec9 |
134 | |
135 | |
136 | $rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } }, |
137 | { |
138 | join => 'tracks', |
139 | columns => [qw/me.cdid me.title/], |
140 | '+select' => ['tracks.position'], |
141 | '+as' => ['track_position'], |
142 | |
143 | # get a hashref of CD1 only (the first with a second track) |
144 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
145 | order_by => 'cdid', |
146 | rows => 1, |
147 | } |
148 | ); |
149 | |
150 | is_deeply ( |
151 | $rs->single, |
152 | { |
153 | cdid => 1, |
154 | track_position => 2, |
155 | title => 'Spoonful of bees', |
156 | }, |
157 | 'limited prefetch via column works on a multi-relationship', |
158 | ); |
159 | |
160 | my $sub_rs = $rs->search ({}, |
161 | { |
162 | columns => [qw/artist tracks.trackid/], # columns should not be merged but override $rs columns |
163 | '+select' => ['tracks.title'], |
164 | '+as' => ['tracks.title'], |
165 | } |
166 | ); |
167 | |
168 | is_deeply ( |
169 | $sub_rs->single, |
170 | { |
171 | artist => 1, |
172 | track_position => 2, |
173 | tracks => |
174 | { |
175 | trackid => 17, |
176 | title => 'Apiary', |
177 | }, |
178 | }, |
179 | 'columns/select/as fold properly on sub-searches', |
180 | ); |
181 | |
182 | TODO: { |
183 | local $TODO = "Multi-collapsing still doesn't work right - HRI should be getting an arrayref, not an individual hash"; |
184 | is_deeply ( |
185 | $sub_rs->single, |
186 | { |
187 | artist => 1, |
188 | track_position => 2, |
189 | tracks => [ |
190 | { |
191 | trackid => 17, |
192 | title => 'Apiary', |
193 | }, |
194 | ], |
195 | }, |
196 | 'columns/select/as fold properly on sub-searches', |
197 | ); |
198 | } |