Merge 'trunk' into 'sqla_1.50_compat'
[dbsrgits/DBIx-Class-Historic.git] / t / 88result_set_column.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5d1fc7dc 5use Test::Exception;
70350518 6use lib qw(t/lib);
7use DBICTest;
8
a47e1233 9my $schema = DBICTest->init_schema();
2bb7b40b 10
66521001 11plan tests => 18;
2bb7b40b 12
58d387fe 13my $cd;
66521001 14my $rs = $cd = $schema->resultset("CD")->search({}, { order_by => 'cdid' });
2bb7b40b 15
16my $rs_title = $rs->get_column('title');
17my $rs_year = $rs->get_column('year');
20ea4813 18my $max_year = $rs->get_column(\'MAX (year)');
2bb7b40b 19
20is($rs_title->next, 'Spoonful of bees', "next okay");
5d62876f 21is_deeply( [ sort $rs_year->func('DISTINCT') ], [ 1997, 1998, 1999, 2001 ], "wantarray context okay");
20ea4813 22ok ($max_year->next == $rs_year->max, q/get_column (\'FUNC') ok/);
5d62876f 23
2bb7b40b 24my @all = $rs_title->all;
25cmp_ok(scalar @all, '==', 5, "five titles returned");
26
27cmp_ok($rs_year->max, '==', 2001, "max okay for year");
28is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title");
29
30cmp_ok($rs_year->sum, '==', 9996, "three artists returned");
31
66521001 32$rs_year->reset;
33is($rs_year->next, 1999, "reset okay");
34
35is($rs_year->first, 1999, "first okay");
36
5d1fc7dc 37# test +select/+as for single column
ae515736 38my $psrs = $schema->resultset('CD')->search({},
39 {
40 '+select' => \'COUNT(*)',
41 '+as' => 'count'
42 }
43);
5d1fc7dc 44lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as additional column "count" present (scalar)');
45dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception');
ae515736 46
5d1fc7dc 47# test +select/+as for multiple columns
ae515736 48$psrs = $schema->resultset('CD')->search({},
49 {
50 '+select' => [ \'COUNT(*)', 'title' ],
51 '+as' => [ 'count', 'addedtitle' ]
52 }
53);
5d1fc7dc 54lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as multiple additional columns, "count" column present');
55lives_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);
64is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"');
ae515736 65
3f6cc7e4 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}
cda5e082 71
72# test sum()
73is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly');
74
75# test sum over search_related
76my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' });
77ok ($owner->books->count > 1, 'Owner Newton has multiple books');
78is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books');