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; |
66e33361 |
9 | use DBIC::SqlMakerTest; |
70350518 |
10 | |
a47e1233 |
11 | my $schema = DBICTest->init_schema(); |
2bb7b40b |
12 | |
ee12e25a |
13 | my $rs = $schema->resultset("CD"); |
14 | |
15 | cmp_ok ( |
16 | $rs->count, |
17 | '!=', |
18 | $rs->search ({}, {columns => ['year'], distinct => 1})->count, |
19 | 'At least one year is the same in rs' |
20 | ); |
2bb7b40b |
21 | |
22 | my $rs_title = $rs->get_column('title'); |
23 | my $rs_year = $rs->get_column('year'); |
20ea4813 |
24 | my $max_year = $rs->get_column(\'MAX (year)'); |
2bb7b40b |
25 | |
26 | is($rs_title->next, 'Spoonful of bees', "next okay"); |
5d62876f |
27 | is_deeply( [ sort $rs_year->func('DISTINCT') ], [ 1997, 1998, 1999, 2001 ], "wantarray context okay"); |
20ea4813 |
28 | ok ($max_year->next == $rs_year->max, q/get_column (\'FUNC') ok/); |
5d62876f |
29 | |
2bb7b40b |
30 | my @all = $rs_title->all; |
31 | cmp_ok(scalar @all, '==', 5, "five titles returned"); |
32 | |
33 | cmp_ok($rs_year->max, '==', 2001, "max okay for year"); |
34 | is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title"); |
35 | |
36 | cmp_ok($rs_year->sum, '==', 9996, "three artists returned"); |
37 | |
b7c79955 |
38 | $rs_year->reset; |
66521001 |
39 | is($rs_year->next, 1999, "reset okay"); |
40 | |
41 | is($rs_year->first, 1999, "first okay"); |
42 | |
4e55c3ae |
43 | warnings_exist (sub { |
44 | is($rs_year->single, 1999, "single okay"); |
45 | }, qr/Query returned more than one row/, 'single warned'); |
46 | |
ee12e25a |
47 | |
48 | # test distinct propagation |
49 | is_deeply ( |
6f723769 |
50 | [sort $rs->search ({}, { distinct => 1 })->get_column ('year')->all], |
51 | [sort $rs_year->func('distinct')], |
ee12e25a |
52 | 'distinct => 1 is passed through properly', |
53 | ); |
54 | |
5d1fc7dc |
55 | # test +select/+as for single column |
ae515736 |
56 | my $psrs = $schema->resultset('CD')->search({}, |
57 | { |
472d7df3 |
58 | '+select' => \'MAX(year)', |
59 | '+as' => 'last_year' |
ae515736 |
60 | } |
61 | ); |
472d7df3 |
62 | lives_ok(sub { $psrs->get_column('last_year')->next }, '+select/+as additional column "last_year" present (scalar)'); |
5d1fc7dc |
63 | dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception'); |
ae515736 |
64 | |
66e33361 |
65 | # test +select/+as for overriding a column |
66 | $psrs = $schema->resultset('CD')->search({}, |
67 | { |
68 | 'select' => \"'The Final Countdown'", |
69 | 'as' => 'title' |
70 | } |
71 | ); |
72 | is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"'); |
73 | |
74 | |
5d1fc7dc |
75 | # test +select/+as for multiple columns |
ae515736 |
76 | $psrs = $schema->resultset('CD')->search({}, |
77 | { |
472d7df3 |
78 | '+select' => [ \'LENGTH(title) AS title_length', 'title' ], |
79 | '+as' => [ 'tlength', 'addedtitle' ] |
ae515736 |
80 | } |
81 | ); |
472d7df3 |
82 | lives_ok(sub { $psrs->get_column('tlength')->next }, '+select/+as multiple additional columns, "tlength" column present'); |
5d1fc7dc |
83 | lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present'); |
84 | |
66e33361 |
85 | # test that +select/+as specs do not leak |
86 | is_same_sql_bind ( |
87 | $psrs->get_column('year')->as_query, |
88 | '(SELECT me.year FROM cd me)', |
89 | [], |
90 | 'Correct SQL for get_column/as' |
5d1fc7dc |
91 | ); |
66e33361 |
92 | |
93 | is_same_sql_bind ( |
94 | $psrs->get_column('addedtitle')->as_query, |
95 | '(SELECT me.title FROM cd me)', |
96 | [], |
97 | 'Correct SQL for get_column/+as col' |
98 | ); |
99 | |
100 | is_same_sql_bind ( |
472d7df3 |
101 | $psrs->get_column('tlength')->as_query, |
102 | '(SELECT LENGTH(title) AS title_length FROM cd me)', |
66e33361 |
103 | [], |
104 | 'Correct SQL for get_column/+as func' |
105 | ); |
106 | |
472d7df3 |
107 | # test that order_by over a function forces a subquery |
108 | lives_ok ( sub { |
109 | is_deeply ( |
110 | [ $psrs->search ({}, { order_by => { -desc => 'title_length' } })->get_column ('title')->all ], |
111 | [ |
112 | "Generic Manufactured Singles", |
113 | "Come Be Depressed With Us", |
114 | "Caterwaulin' Blues", |
115 | "Spoonful of bees", |
116 | "Forkful of bees", |
117 | ], |
118 | 'Subquery count induced by aliased ordering function', |
119 | ); |
120 | }); |
121 | |
122 | # test for prefetch not leaking |
3f6cc7e4 |
123 | { |
124 | my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' }); |
125 | my $rsc = $rs->get_column('year'); |
126 | is( $rsc->{_parent_resultset}->{attrs}->{prefetch}, undef, 'prefetch wiped' ); |
127 | } |
cda5e082 |
128 | |
129 | # test sum() |
130 | is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly'); |
131 | |
132 | # test sum over search_related |
133 | my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' }); |
134 | ok ($owner->books->count > 1, 'Owner Newton has multiple books'); |
135 | is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books'); |
d8dbe471 |
136 | |
137 | |
138 | # make sure joined/prefetched get_column of a PK dtrt |
139 | |
140 | $rs->reset; |
141 | my $j_rs = $rs->search ({}, { join => 'tracks' })->get_column ('cdid'); |
142 | is_deeply ( |
143 | [ $j_rs->all ], |
144 | [ map { my $c = $rs->next; ( ($c->id) x $c->tracks->count ) } (1 .. $rs->count) ], |
145 | 'join properly explodes amount of rows from get_column', |
146 | ); |
147 | |
148 | $rs->reset; |
149 | my $p_rs = $rs->search ({}, { prefetch => 'tracks' })->get_column ('cdid'); |
150 | is_deeply ( |
151 | [ $p_rs->all ], |
152 | [ $rs->get_column ('cdid')->all ], |
153 | 'prefetch properly collapses amount of rows from get_column', |
154 | ); |
b7c79955 |
155 | |
156 | done_testing; |