better money value comparison in tests
[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 => 20;
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 $rs_year->reset;
32 is($rs_year->next, 1999, "reset okay");
33
34 is($rs_year->first, 1999, "first okay");
35
36 # test +select/+as for single column
37 my $psrs = $schema->resultset('CD')->search({},
38     {
39         '+select'   => \'COUNT(*)',
40         '+as'       => 'count'
41     }
42 );
43 lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as additional column "count" present (scalar)');
44 dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception');
45
46 # test +select/+as for multiple columns
47 $psrs = $schema->resultset('CD')->search({},
48     {
49         '+select'   => [ \'COUNT(*)', 'title' ],
50         '+as'       => [ 'count', 'addedtitle' ]
51     }
52 );
53 lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as multiple additional columns, "count" column present');
54 lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present');
55
56 # test +select/+as for overriding a column
57 $psrs = $schema->resultset('CD')->search({},
58     {
59         'select'   => \"'The Final Countdown'",
60         'as'       => 'title'
61     }
62 );
63 is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"');
64
65 {
66   my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' });
67   my $rsc = $rs->get_column('year');
68   is( $rsc->{_parent_resultset}->{attrs}->{prefetch}, undef, 'prefetch wiped' );
69 }
70
71 # test sum()
72 is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly');
73
74 # test sum over search_related
75 my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' });
76 ok ($owner->books->count > 1, 'Owner Newton has multiple books');
77 is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books');
78
79
80 # make sure joined/prefetched get_column of a PK dtrt
81
82 $rs->reset;
83 my $j_rs = $rs->search ({}, { join => 'tracks' })->get_column ('cdid');
84 is_deeply (
85   [ $j_rs->all ],
86   [ map { my $c = $rs->next; ( ($c->id) x $c->tracks->count ) } (1 .. $rs->count) ],
87   'join properly explodes amount of rows from get_column',
88 );
89
90 $rs->reset;
91 my $p_rs = $rs->search ({}, { prefetch => 'tracks' })->get_column ('cdid');
92 is_deeply (
93   [ $p_rs->all ],
94   [ $rs->get_column ('cdid')->all ],
95   'prefetch properly collapses amount of rows from get_column',
96 );