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