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