Merge 'trunk' into 'multiple_version_upgrade'
[dbsrgits/DBIx-Class.git] / t / 88result_set_column.t
CommitLineData
70350518 1use strict;
4e55c3ae 2use warnings;
70350518 3
4use Test::More;
4e55c3ae 5use Test::Warn;
5d1fc7dc 6use Test::Exception;
70350518 7use lib qw(t/lib);
8use DBICTest;
66e33361 9use DBIC::SqlMakerTest;
70350518 10
a47e1233 11my $schema = DBICTest->init_schema();
2bb7b40b 12
ee12e25a 13my $rs = $schema->resultset("CD");
14
15cmp_ok (
16 $rs->count,
17 '!=',
18 $rs->search ({}, {columns => ['year'], distinct => 1})->count,
19 'At least one year is the same in rs'
20);
2bb7b40b 21
22my $rs_title = $rs->get_column('title');
23my $rs_year = $rs->get_column('year');
20ea4813 24my $max_year = $rs->get_column(\'MAX (year)');
2bb7b40b 25
26is($rs_title->next, 'Spoonful of bees', "next okay");
5d62876f 27is_deeply( [ sort $rs_year->func('DISTINCT') ], [ 1997, 1998, 1999, 2001 ], "wantarray context okay");
20ea4813 28ok ($max_year->next == $rs_year->max, q/get_column (\'FUNC') ok/);
5d62876f 29
2bb7b40b 30my @all = $rs_title->all;
31cmp_ok(scalar @all, '==', 5, "five titles returned");
32
33cmp_ok($rs_year->max, '==', 2001, "max okay for year");
34is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title");
35
36cmp_ok($rs_year->sum, '==', 9996, "three artists returned");
37
b7c79955 38$rs_year->reset;
66521001 39is($rs_year->next, 1999, "reset okay");
40
41is($rs_year->first, 1999, "first okay");
42
4e55c3ae 43warnings_exist (sub {
44 is($rs_year->single, 1999, "single okay");
45}, qr/Query returned more than one row/, 'single warned');
46
ee12e25a 47
48# test distinct propagation
49is_deeply (
50 [$rs->search ({}, { distinct => 1 })->get_column ('year')->all],
51 [$rs_year->func('distinct')],
52 'distinct => 1 is passed through properly',
53);
54
5d1fc7dc 55# test +select/+as for single column
ae515736 56my $psrs = $schema->resultset('CD')->search({},
57 {
58 '+select' => \'COUNT(*)',
59 '+as' => 'count'
60 }
61);
5d1fc7dc 62lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as additional column "count" present (scalar)');
63dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception');
ae515736 64
66e33361 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);
72is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"');
73
74
5d1fc7dc 75# test +select/+as for multiple columns
ae515736 76$psrs = $schema->resultset('CD')->search({},
77 {
78 '+select' => [ \'COUNT(*)', 'title' ],
79 '+as' => [ 'count', 'addedtitle' ]
80 }
81);
5d1fc7dc 82lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as multiple additional columns, "count" column present');
83lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present');
84
66e33361 85# test that +select/+as specs do not leak
86is_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'
5d1fc7dc 91);
66e33361 92
93is_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
100is_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
ae515736 107
3f6cc7e4 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}
cda5e082 113
114# test sum()
115is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly');
116
117# test sum over search_related
118my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' });
119ok ($owner->books->count > 1, 'Owner Newton has multiple books');
120is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books');
d8dbe471 121
122
123# make sure joined/prefetched get_column of a PK dtrt
124
125$rs->reset;
126my $j_rs = $rs->search ({}, { join => 'tracks' })->get_column ('cdid');
127is_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;
134my $p_rs = $rs->search ({}, { prefetch => 'tracks' })->get_column ('cdid');
135is_deeply (
136 [ $p_rs->all ],
137 [ $rs->get_column ('cdid')->all ],
138 'prefetch properly collapses amount of rows from get_column',
139);
b7c79955 140
141done_testing;