Final count tests
[dbsrgits/DBIx-Class-Historic.git] / t / 76select.t
CommitLineData
218aa968 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
faf79003 8use DBIC::SqlMakerTest;
218aa968 9
10my $schema = DBICTest->init_schema();
11
b3b13ec9 12plan tests => 24;
218aa968 13
14my $rs = $schema->resultset('CD')->search({},
15 {
16 '+select' => \ 'COUNT(*)',
17 '+as' => 'count'
18 }
19);
20lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present');
21dies_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);
29lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present');
30lives_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);
39my @counts = $rs->get_column('cdid')->all;
40ok(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);
53lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present');
54lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present');
55lives_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 63my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent
64cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
65
66my $table = $cds->result_source->name;
67my $subsel = $cds->search ({}, {
68 columns => [qw/cdid title/],
69 from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
70});
71
72is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
73is ($subsel->next->title, $cds->next->title, 'First CD title match');
74is ($subsel->next->title, $cds->next->title, 'Second CD title match');
bbdff861 75
76is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');
61d26fae 77
faf79003 78
79
80$rs = $schema->resultset('CD')->search({},
61d26fae 81 {
82 'join' => 'artist',
faf79003 83 'columns' => ['cdid', 'title', 'artist.name'],
61d26fae 84 }
85);
faf79003 86
2b4fdf57 87my ($sql, @bind) = @${$rs->as_query};
faf79003 88is_same_sql_bind (
2b4fdf57 89 $sql,
90 \@bind,
faf79003 91 '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
92 [],
93 'Use of columns attribute results in proper sql'
94);
95
96lives_ok(sub {
97 $rs->first->get_column('cdid')
98}, 'columns 1st rscolumn present');
99
100lives_ok(sub {
101 $rs->first->get_column('title')
102}, 'columns 2nd rscolumn present');
103
0ca2f0d1 104lives_ok(sub {
105 $rs->first->artist->get_column('name')
106}, 'columns 3rd rscolumn present');
4c253752 107
108
faf79003 109
110$rs = $schema->resultset('CD')->search({},
111 {
112 'join' => 'artist',
113 '+columns' => ['cdid', 'title', 'artist.name'],
114 }
115);
116
2b4fdf57 117($sql, @bind) = @${$rs->as_query};
faf79003 118is_same_sql_bind (
2b4fdf57 119 $sql,
120 \@bind,
faf79003 121 '(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)',
122 [],
123 'Use of columns attribute results in proper sql'
124);
125
126lives_ok(sub {
127 $rs->first->get_column('cdid')
128}, 'columns 1st rscolumn present');
129
130lives_ok(sub {
131 $rs->first->get_column('title')
132}, 'columns 2nd rscolumn present');
4c253752 133
0ca2f0d1 134lives_ok(sub {
135 $rs->first->artist->get_column('name')
136}, 'columns 3rd rscolumn present');
b3b13ec9 137
138
139$rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } },
140 {
141 join => 'tracks',
142 columns => [qw/me.cdid me.title/],
143 '+select' => ['tracks.position'],
144 '+as' => ['track_position'],
145
146 # get a hashref of CD1 only (the first with a second track)
147 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
148 order_by => 'cdid',
149 rows => 1,
150 }
151);
152
153is_deeply (
154 $rs->single,
155 {
156 cdid => 1,
157 track_position => 2,
158 title => 'Spoonful of bees',
159 },
160 'limited prefetch via column works on a multi-relationship',
161);
162
163my $sub_rs = $rs->search ({},
164 {
165 columns => [qw/artist tracks.trackid/], # columns should not be merged but override $rs columns
166 '+select' => ['tracks.title'],
167 '+as' => ['tracks.title'],
168 }
169);
170
171is_deeply (
172 $sub_rs->single,
173 {
174 artist => 1,
175 track_position => 2,
176 tracks =>
177 {
178 trackid => 17,
179 title => 'Apiary',
180 },
181 },
182 'columns/select/as fold properly on sub-searches',
183);
184
185TODO: {
186 local $TODO = "Multi-collapsing still doesn't work right - HRI should be getting an arrayref, not an individual hash";
187 is_deeply (
188 $sub_rs->single,
189 {
190 artist => 1,
191 track_position => 2,
192 tracks => [
193 {
194 trackid => 17,
195 title => 'Apiary',
196 },
197 ],
198 },
199 'columns/select/as fold properly on sub-searches',
200 );
201}