Fix minor RSC bug
[dbsrgits/DBIx-Class.git] / t / 88result_set_column.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Warn;
6 use Test::Exception;
7 use lib qw(t/lib);
8 use DBICTest;
9 use DBIC::SqlMakerTest;
10
11 my $schema = DBICTest->init_schema();
12
13 my $rs = $schema->resultset("CD");
14
15 cmp_ok (
16   $rs->count,
17     '!=',
18   $rs->search ({}, {columns => ['year'], distinct => 1})->count,
19   'At least one year is the same in rs'
20 );
21
22 my $rs_title = $rs->get_column('title');
23 my $rs_year = $rs->get_column('year');
24 my $max_year = $rs->get_column(\'MAX (year)');
25
26 is($rs_title->next, 'Spoonful of bees', "next okay");
27 is_deeply( [ sort $rs_year->func('DISTINCT') ], [ 1997, 1998, 1999, 2001 ],  "wantarray context okay");
28 ok ($max_year->next == $rs_year->max, q/get_column (\'FUNC') ok/);
29
30 my @all = $rs_title->all;
31 cmp_ok(scalar @all, '==', 5, "five titles returned");
32
33 cmp_ok($rs_year->max, '==', 2001, "max okay for year");
34 is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title");
35
36 cmp_ok($rs_year->sum, '==', 9996, "three artists returned");
37
38 $rs_year->reset;
39 is($rs_year->next, 1999, "reset okay");
40
41 is($rs_year->first, 1999, "first okay");
42
43 warnings_exist (sub {
44   is($rs_year->single, 1999, "single okay");
45 }, qr/Query returned more than one row/, 'single warned');
46
47
48 # test distinct propagation
49 is_deeply (
50   [$rs->search ({}, { distinct => 1 })->get_column ('year')->all],
51   [$rs_year->func('distinct')],
52   'distinct => 1 is passed through properly',
53 );
54
55 # test +select/+as for single column
56 my $psrs = $schema->resultset('CD')->search({},
57     {
58         '+select'   => \'COUNT(*)',
59         '+as'       => 'count'
60     }
61 );
62 lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as additional column "count" present (scalar)');
63 dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception');
64
65 # test +select/+as for overriding a column
66 $psrs = $schema->resultset('CD')->search({},
67     {
68         'select'   => \"'The Final Countdown'",
69         'as'       => 'title'
70     }
71 );
72 is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"');
73
74
75 # test +select/+as for multiple columns
76 $psrs = $schema->resultset('CD')->search({},
77     {
78         '+select'   => [ \'COUNT(*)', 'title' ],
79         '+as'       => [ 'count', 'addedtitle' ]
80     }
81 );
82 lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as multiple additional columns, "count" column present');
83 lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present');
84
85 # test that +select/+as specs do not leak
86 is_same_sql_bind (
87   $psrs->get_column('year')->as_query,
88   '(SELECT me.year FROM cd me)',
89   [],
90   'Correct SQL for get_column/as'
91 );
92
93 is_same_sql_bind (
94   $psrs->get_column('addedtitle')->as_query,
95   '(SELECT me.title FROM cd me)',
96   [],
97   'Correct SQL for get_column/+as col'
98 );
99
100 is_same_sql_bind (
101   $psrs->get_column('count')->as_query,
102   '(SELECT COUNT(*) FROM cd me)',
103   [],
104   'Correct SQL for get_column/+as func'
105 );
106
107
108 {
109   my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' });
110   my $rsc = $rs->get_column('year');
111   is( $rsc->{_parent_resultset}->{attrs}->{prefetch}, undef, 'prefetch wiped' );
112 }
113
114 # test sum()
115 is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly');
116
117 # test sum over search_related
118 my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' });
119 ok ($owner->books->count > 1, 'Owner Newton has multiple books');
120 is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books');
121
122
123 # make sure joined/prefetched get_column of a PK dtrt
124
125 $rs->reset;
126 my $j_rs = $rs->search ({}, { join => 'tracks' })->get_column ('cdid');
127 is_deeply (
128   [ $j_rs->all ],
129   [ map { my $c = $rs->next; ( ($c->id) x $c->tracks->count ) } (1 .. $rs->count) ],
130   'join properly explodes amount of rows from get_column',
131 );
132
133 $rs->reset;
134 my $p_rs = $rs->search ({}, { prefetch => 'tracks' })->get_column ('cdid');
135 is_deeply (
136   [ $p_rs->all ],
137   [ $rs->get_column ('cdid')->all ],
138   'prefetch properly collapses amount of rows from get_column',
139 );
140
141 done_testing;