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 | my $rs = $schema->resultset("CD")->search({}, { order_by => 'cdid' }); |
2bb7b40b |
12 | |
13 | my $rs_title = $rs->get_column('title'); |
14 | my $rs_year = $rs->get_column('year'); |
20ea4813 |
15 | my $max_year = $rs->get_column(\'MAX (year)'); |
2bb7b40b |
16 | |
17 | is($rs_title->next, 'Spoonful of bees', "next okay"); |
5d62876f |
18 | is_deeply( [ sort $rs_year->func('DISTINCT') ], [ 1997, 1998, 1999, 2001 ], "wantarray context okay"); |
20ea4813 |
19 | ok ($max_year->next == $rs_year->max, q/get_column (\'FUNC') ok/); |
5d62876f |
20 | |
2bb7b40b |
21 | my @all = $rs_title->all; |
22 | cmp_ok(scalar @all, '==', 5, "five titles returned"); |
23 | |
24 | cmp_ok($rs_year->max, '==', 2001, "max okay for year"); |
25 | is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title"); |
26 | |
27 | cmp_ok($rs_year->sum, '==', 9996, "three artists returned"); |
28 | |
b7c79955 |
29 | $rs_year->reset; |
66521001 |
30 | is($rs_year->next, 1999, "reset okay"); |
31 | |
32 | is($rs_year->first, 1999, "first okay"); |
33 | |
5d1fc7dc |
34 | # test +select/+as for single column |
ae515736 |
35 | my $psrs = $schema->resultset('CD')->search({}, |
36 | { |
37 | '+select' => \'COUNT(*)', |
38 | '+as' => 'count' |
39 | } |
40 | ); |
5d1fc7dc |
41 | lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as additional column "count" present (scalar)'); |
42 | dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception'); |
ae515736 |
43 | |
5d1fc7dc |
44 | # test +select/+as for multiple columns |
ae515736 |
45 | $psrs = $schema->resultset('CD')->search({}, |
46 | { |
47 | '+select' => [ \'COUNT(*)', 'title' ], |
48 | '+as' => [ 'count', 'addedtitle' ] |
49 | } |
50 | ); |
5d1fc7dc |
51 | lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as multiple additional columns, "count" column present'); |
52 | lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present'); |
53 | |
54 | # test +select/+as for overriding a column |
55 | $psrs = $schema->resultset('CD')->search({}, |
56 | { |
57 | 'select' => \"'The Final Countdown'", |
58 | 'as' => 'title' |
59 | } |
60 | ); |
61 | is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"'); |
ae515736 |
62 | |
3f6cc7e4 |
63 | { |
64 | my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' }); |
65 | my $rsc = $rs->get_column('year'); |
66 | is( $rsc->{_parent_resultset}->{attrs}->{prefetch}, undef, 'prefetch wiped' ); |
67 | } |
cda5e082 |
68 | |
69 | # test sum() |
70 | is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly'); |
71 | |
72 | # test sum over search_related |
73 | my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' }); |
74 | ok ($owner->books->count > 1, 'Owner Newton has multiple books'); |
75 | is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books'); |
d8dbe471 |
76 | |
77 | |
78 | # make sure joined/prefetched get_column of a PK dtrt |
79 | |
80 | $rs->reset; |
81 | my $j_rs = $rs->search ({}, { join => 'tracks' })->get_column ('cdid'); |
82 | is_deeply ( |
83 | [ $j_rs->all ], |
84 | [ map { my $c = $rs->next; ( ($c->id) x $c->tracks->count ) } (1 .. $rs->count) ], |
85 | 'join properly explodes amount of rows from get_column', |
86 | ); |
87 | |
88 | $rs->reset; |
89 | my $p_rs = $rs->search ({}, { prefetch => 'tracks' })->get_column ('cdid'); |
90 | is_deeply ( |
91 | [ $p_rs->all ], |
92 | [ $rs->get_column ('cdid')->all ], |
93 | 'prefetch properly collapses amount of rows from get_column', |
94 | ); |
b7c79955 |
95 | |
96 | done_testing; |