Fix RSC->reset() to no longer return $self, which fixes Cursor::Cached + RSC.
[dbsrgits/DBIx-Class.git] / t / 88result_set_column.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
9 my $schema = DBICTest->init_schema();
10
11 plan tests => 21;
12
13 my $rs = $schema->resultset("CD")->search({}, { order_by => 'cdid' });
14
15 my $rs_title = $rs->get_column('title');
16 my $rs_year = $rs->get_column('year');
17 my $max_year = $rs->get_column(\'MAX (year)');
18
19 is($rs_title->next, 'Spoonful of bees', "next okay");
20 is_deeply( [ sort $rs_year->func('DISTINCT') ], [ 1997, 1998, 1999, 2001 ],  "wantarray context okay");
21 ok ($max_year->next == $rs_year->max, q/get_column (\'FUNC') ok/);
22
23 my @all = $rs_title->all;
24 cmp_ok(scalar @all, '==', 5, "five titles returned");
25
26 cmp_ok($rs_year->max, '==', 2001, "max okay for year");
27 is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title");
28
29 cmp_ok($rs_year->sum, '==', 9996, "three artists returned");
30
31 my $reset_ret = $rs_year->reset;
32 is($rs_year->next, 1999, "reset okay");
33 is($reset_ret, undef, 'reset returns undef');
34
35 is($rs_year->first, 1999, "first okay");
36
37 # test +select/+as for single column
38 my $psrs = $schema->resultset('CD')->search({},
39     {
40         '+select'   => \'COUNT(*)',
41         '+as'       => 'count'
42     }
43 );
44 lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as additional column "count" present (scalar)');
45 dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception');
46
47 # test +select/+as for multiple columns
48 $psrs = $schema->resultset('CD')->search({},
49     {
50         '+select'   => [ \'COUNT(*)', 'title' ],
51         '+as'       => [ 'count', 'addedtitle' ]
52     }
53 );
54 lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as multiple additional columns, "count" column present');
55 lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present');
56
57 # test +select/+as for overriding a column
58 $psrs = $schema->resultset('CD')->search({},
59     {
60         'select'   => \"'The Final Countdown'",
61         'as'       => 'title'
62     }
63 );
64 is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"');
65
66 {
67   my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' });
68   my $rsc = $rs->get_column('year');
69   is( $rsc->{_parent_resultset}->{attrs}->{prefetch}, undef, 'prefetch wiped' );
70 }
71
72 # test sum()
73 is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly');
74
75 # test sum over search_related
76 my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' });
77 ok ($owner->books->count > 1, 'Owner Newton has multiple books');
78 is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books');
79
80
81 # make sure joined/prefetched get_column of a PK dtrt
82
83 $rs->reset;
84 my $j_rs = $rs->search ({}, { join => 'tracks' })->get_column ('cdid');
85 is_deeply (
86   [ $j_rs->all ],
87   [ map { my $c = $rs->next; ( ($c->id) x $c->tracks->count ) } (1 .. $rs->count) ],
88   'join properly explodes amount of rows from get_column',
89 );
90
91 $rs->reset;
92 my $p_rs = $rs->search ({}, { prefetch => 'tracks' })->get_column ('cdid');
93 is_deeply (
94   [ $p_rs->all ],
95   [ $rs->get_column ('cdid')->all ],
96   'prefetch properly collapses amount of rows from get_column',
97 );