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