* Fixed test cases of ResultSetColumn vs. +select/+as (they used to unconditionally...
[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
5d1fc7dc 11plan tests => 16;
2bb7b40b 12
58d387fe 13my $cd;
2bb7b40b 14my $rs = $cd = $schema->resultset("CD")->search({});
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
5d1fc7dc 32# test +select/+as for single column
ae515736 33my $psrs = $schema->resultset('CD')->search({},
34 {
35 '+select' => \'COUNT(*)',
36 '+as' => 'count'
37 }
38);
5d1fc7dc 39lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as additional column "count" present (scalar)');
40dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception');
ae515736 41
5d1fc7dc 42# test +select/+as for multiple columns
ae515736 43$psrs = $schema->resultset('CD')->search({},
44 {
45 '+select' => [ \'COUNT(*)', 'title' ],
46 '+as' => [ 'count', 'addedtitle' ]
47 }
48);
5d1fc7dc 49lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as multiple additional columns, "count" column present');
50lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present');
51
52# test +select/+as for overriding a column
53$psrs = $schema->resultset('CD')->search({},
54 {
55 'select' => \"'The Final Countdown'",
56 'as' => 'title'
57 }
58);
59is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"');
ae515736 60
3f6cc7e4 61{
62 my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' });
63 my $rsc = $rs->get_column('year');
64 is( $rsc->{_parent_resultset}->{attrs}->{prefetch}, undef, 'prefetch wiped' );
65}
cda5e082 66
67# test sum()
68is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly');
69
70# test sum over search_related
71my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' });
72ok ($owner->books->count > 1, 'Owner Newton has multiple books');
73is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books');