Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5d1fc7dc |
5 | use Test::Exception; |
70350518 |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
8 | |
a47e1233 |
9 | my $schema = DBICTest->init_schema(); |
2bb7b40b |
10 | |
d8dbe471 |
11 | plan tests => 20; |
2bb7b40b |
12 | |
d8dbe471 |
13 | my $rs = $schema->resultset("CD")->search({}, { order_by => 'cdid' }); |
2bb7b40b |
14 | |
15 | my $rs_title = $rs->get_column('title'); |
16 | my $rs_year = $rs->get_column('year'); |
20ea4813 |
17 | my $max_year = $rs->get_column(\'MAX (year)'); |
2bb7b40b |
18 | |
19 | is($rs_title->next, 'Spoonful of bees', "next okay"); |
5d62876f |
20 | is_deeply( [ sort $rs_year->func('DISTINCT') ], [ 1997, 1998, 1999, 2001 ], "wantarray context okay"); |
20ea4813 |
21 | ok ($max_year->next == $rs_year->max, q/get_column (\'FUNC') ok/); |
5d62876f |
22 | |
2bb7b40b |
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 | |
66521001 |
31 | $rs_year->reset; |
32 | is($rs_year->next, 1999, "reset okay"); |
33 | |
34 | is($rs_year->first, 1999, "first okay"); |
35 | |
5d1fc7dc |
36 | # test +select/+as for single column |
ae515736 |
37 | my $psrs = $schema->resultset('CD')->search({}, |
38 | { |
39 | '+select' => \'COUNT(*)', |
40 | '+as' => 'count' |
41 | } |
42 | ); |
5d1fc7dc |
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'); |
ae515736 |
45 | |
5d1fc7dc |
46 | # test +select/+as for multiple columns |
ae515736 |
47 | $psrs = $schema->resultset('CD')->search({}, |
48 | { |
49 | '+select' => [ \'COUNT(*)', 'title' ], |
50 | '+as' => [ 'count', 'addedtitle' ] |
51 | } |
52 | ); |
5d1fc7dc |
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"'); |
ae515736 |
64 | |
3f6cc7e4 |
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 | } |
cda5e082 |
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'); |
d8dbe471 |
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 | ); |