Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
7 | |
ae515736 |
8 | my $schema = DBICTest->init_schema(); |
0567538f |
9 | |
ade8df5b |
10 | plan tests => 78; |
0567538f |
11 | |
03a1819f |
12 | eval { require DateTime::Format::MySQL }; |
13 | my $NO_DTFM = $@ ? 1 : 0; |
14 | |
f2de4889 |
15 | # figure out if we've got a version of sqlite that is older than 3.2.6, in |
16 | # which case COUNT(DISTINCT()) doesn't work |
17 | my $is_broken_sqlite = 0; |
18 | my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) = |
19 | split /\./, $schema->storage->dbh->get_info(18); |
20 | if( $schema->storage->dbh->get_info(17) eq 'SQLite' && |
21 | ( ($sqlite_major_ver < 3) || |
22 | ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) || |
23 | ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) { |
24 | $is_broken_sqlite = 1; |
25 | } |
26 | |
0567538f |
27 | |
f9db5527 |
28 | my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); |
0567538f |
29 | |
30 | cmp_ok(@art, '==', 3, "Three artists returned"); |
31 | |
32 | my $art = $art[0]; |
33 | |
34 | is($art->name, 'We Are Goth', "Correct order too"); |
35 | |
36 | $art->name('We Are In Rehab'); |
37 | |
38 | is($art->name, 'We Are In Rehab', "Accessor update ok"); |
39 | |
40 | is($art->get_column("name"), 'We Are In Rehab', 'And via get_column'); |
41 | |
42 | ok($art->update, 'Update run'); |
43 | |
ae515736 |
44 | my $record_jp = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search(undef, { prefetch => 'cds' })->next; |
45 | |
46 | ok($record_jp, "prefetch on same rel okay"); |
47 | |
48 | my $record_fn = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search({'cds.cdid' => '1'}, {join => 'artist_undirected_maps'})->next; |
49 | |
50 | ok($record_fn, "funny join is okay"); |
51 | |
f9db5527 |
52 | @art = $schema->resultset("Artist")->search({ name => 'We Are In Rehab' }); |
0567538f |
53 | |
54 | cmp_ok(@art, '==', 1, "Changed artist returned by search"); |
55 | |
56 | cmp_ok($art[0]->artistid, '==', 3,'Correct artist too'); |
57 | |
58 | $art->delete; |
59 | |
f9db5527 |
60 | @art = $schema->resultset("Artist")->search({ }); |
0567538f |
61 | |
62 | cmp_ok(@art, '==', 2, 'And then there were two'); |
63 | |
64 | ok(!$art->in_storage, "It knows it's dead"); |
65 | |
66 | eval { $art->delete; }; |
67 | |
68 | ok($@, "Can't delete twice: $@"); |
69 | |
70 | is($art->name, 'We Are In Rehab', 'But the object is still live'); |
71 | |
72 | $art->insert; |
73 | |
74 | ok($art->in_storage, "Re-created"); |
75 | |
f9db5527 |
76 | @art = $schema->resultset("Artist")->search({ }); |
0567538f |
77 | |
78 | cmp_ok(@art, '==', 3, 'And now there are three again'); |
79 | |
f9db5527 |
80 | my $new = $schema->resultset("Artist")->create({ artistid => 4 }); |
0567538f |
81 | |
82 | cmp_ok($new->artistid, '==', 4, 'Create produced record ok'); |
83 | |
f9db5527 |
84 | @art = $schema->resultset("Artist")->search({ }); |
0567538f |
85 | |
86 | cmp_ok(@art, '==', 4, "Oh my god! There's four of them!"); |
87 | |
88 | $new->set_column('name' => 'Man With A Fork'); |
89 | |
90 | is($new->name, 'Man With A Fork', 'set_column ok'); |
91 | |
92 | $new->discard_changes; |
93 | |
94 | ok(!defined $new->name, 'Discard ok'); |
95 | |
96 | $new->name('Man With A Spoon'); |
97 | |
98 | $new->update; |
99 | |
70350518 |
100 | my $new_again = $schema->resultset("Artist")->find(4); |
0567538f |
101 | |
102 | is($new_again->name, 'Man With A Spoon', 'Retrieved correctly'); |
103 | |
9bbd8963 |
104 | is($new_again->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly'); |
1f6715ab |
105 | |
a87eb971 |
106 | # Test backwards compatibility |
107 | { |
13e6ab63 |
108 | my $warnings = ''; |
109 | local $SIG{__WARN__} = sub { $warnings .= $_[0] }; |
110 | |
111 | my $artist_by_hash = $schema->resultset('Artist')->find(artistid => 4); |
a87eb971 |
112 | is($artist_by_hash->name, 'Man With A Spoon', 'Retrieved correctly'); |
113 | is($artist_by_hash->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly'); |
13e6ab63 |
114 | like($warnings, qr/deprecated/, 'warned about deprecated find usage'); |
a87eb971 |
115 | } |
116 | |
f9db5527 |
117 | is($schema->resultset("Artist")->count, 4, 'count ok'); |
0567538f |
118 | |
b3e1f1f5 |
119 | # test find_or_new |
120 | { |
121 | my $existing_obj = $schema->resultset('Artist')->find_or_new({ |
122 | artistid => 4, |
123 | }); |
124 | |
125 | is($existing_obj->name, 'Man With A Spoon', 'find_or_new: found existing artist'); |
126 | ok($existing_obj->in_storage, 'existing artist is in storage'); |
127 | |
128 | my $new_obj = $schema->resultset('Artist')->find_or_new({ |
129 | artistid => 5, |
130 | name => 'find_or_new', |
131 | }); |
132 | |
133 | is($new_obj->name, 'find_or_new', 'find_or_new: instantiated a new artist'); |
134 | ok(! $new_obj->in_storage, 'new artist is not in storage'); |
135 | } |
136 | |
f9db5527 |
137 | my $cd = $schema->resultset("CD")->find(1); |
076a6864 |
138 | my %cols = $cd->get_columns; |
139 | |
140 | cmp_ok(keys %cols, '==', 4, 'get_columns number of columns ok'); |
141 | |
142 | is($cols{title}, 'Spoonful of bees', 'get_columns values ok'); |
143 | |
144 | %cols = ( title => 'Forkful of bees', year => 2005); |
145 | $cd->set_columns(\%cols); |
146 | |
147 | is($cd->title, 'Forkful of bees', 'set_columns ok'); |
148 | |
149 | is($cd->year, 2005, 'set_columns ok'); |
150 | |
151 | $cd->discard_changes; |
152 | |
20518cb4 |
153 | # check whether ResultSource->columns returns columns in order originally supplied |
154 | my @cd = $schema->source("CD")->columns; |
571dced3 |
155 | |
20518cb4 |
156 | is_deeply( \@cd, [qw/cdid artist title year/], 'column order'); |
571dced3 |
157 | |
82a96700 |
158 | $cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { columns => ['title'] })->next; |
90f3f5ff |
159 | is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly'); |
160 | |
5ac6a044 |
161 | $cd = $schema->resultset("CD")->search(undef, { include_columns => [ 'artist.name' ], join => [ 'artist' ] })->find(1); |
162 | |
163 | is($cd->title, 'Spoonful of bees', 'Correct CD returned with include'); |
164 | is($cd->get_column('name'), 'Caterwauler McCrae', 'Additional column returned'); |
165 | |
82a96700 |
166 | # update_or_insert |
f9db5527 |
167 | $new = $schema->resultset("Track")->new( { |
0567538f |
168 | trackid => 100, |
169 | cd => 1, |
365d06b7 |
170 | position => 4, |
0567538f |
171 | title => 'Insert or Update', |
43556c5d |
172 | last_updated_on => '1973-07-19 12:01:02' |
0567538f |
173 | } ); |
82a96700 |
174 | $new->update_or_insert; |
175 | ok($new->in_storage, 'update_or_insert insert ok'); |
0567538f |
176 | |
177 | # test in update mode |
91b0fbd7 |
178 | $new->pos(5); |
82a96700 |
179 | $new->update_or_insert; |
180 | is( $schema->resultset("Track")->find(100)->pos, 5, 'update_or_insert update ok'); |
0567538f |
181 | |
ba4a6453 |
182 | # get_inflated_columns w/relation and accessor alias |
03a1819f |
183 | SKIP: { |
184 | skip "This test requires DateTime::Format::MySQL", 8 if $NO_DTFM; |
185 | |
186 | isa_ok($new->updated_date, 'DateTime', 'have inflated object via accessor'); |
187 | my %tdata = $new->get_inflated_columns; |
188 | is($tdata{'trackid'}, 100, 'got id'); |
189 | isa_ok($tdata{'cd'}, 'DBICTest::CD', 'cd is CD object'); |
190 | is($tdata{'cd'}->id, 1, 'cd object is id 1'); |
191 | is($tdata{'position'}, 5, 'got position from pos'); |
192 | is($tdata{'title'}, 'Insert or Update'); |
193 | is($tdata{'last_updated_on'}, '1973-07-19T12:01:02'); |
194 | isa_ok($tdata{'last_updated_on'}, 'DateTime', 'inflated accessored column'); |
195 | } |
ba4a6453 |
196 | |
8c49f629 |
197 | eval { $schema->class("Track")->load_components('DoesNotExist'); }; |
0567538f |
198 | |
199 | ok $@, $@; |
200 | |
1edaf6fe |
201 | is($schema->class("Artist")->field_name_for->{name}, 'artist name', 'mk_classdata usage ok'); |
90e6de6c |
202 | |
54540863 |
203 | my $search = [ { 'tags.tag' => 'Cheesy' }, { 'tags.tag' => 'Blue' } ]; |
204 | |
5b89a768 |
205 | my( $or_rs ) = $schema->resultset("CD")->search_rs($search, { join => 'tags', |
6aeb9185 |
206 | order_by => 'cdid' }); |
54540863 |
207 | |
6aeb9185 |
208 | cmp_ok($or_rs->count, '==', 5, 'Search with OR ok'); |
54540863 |
209 | |
f9db5527 |
210 | my $distinct_rs = $schema->resultset("CD")->search($search, { join => 'tags', distinct => 1 }); |
6aeb9185 |
211 | cmp_ok($distinct_rs->all, '==', 4, 'DISTINCT search with OR ok'); |
212 | |
f2de4889 |
213 | SKIP: { |
214 | skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1 |
215 | if $is_broken_sqlite; |
216 | |
217 | my $tcount = $schema->resultset("Track")->search( |
286f32b3 |
218 | {}, |
f2de4889 |
219 | { |
220 | select => {count => {distinct => ['position', 'title']}}, |
221 | as => ['count'] |
286f32b3 |
222 | } |
223 | ); |
f2de4889 |
224 | cmp_ok($tcount->next->get_column('count'), '==', 13, 'multiple column COUNT DISTINCT ok'); |
286f32b3 |
225 | |
f2de4889 |
226 | } |
f9db5527 |
227 | my $tag_rs = $schema->resultset('Tag')->search( |
6aeb9185 |
228 | [ { 'me.tag' => 'Cheesy' }, { 'me.tag' => 'Blue' } ]); |
229 | |
230 | my $rel_rs = $tag_rs->search_related('cd'); |
231 | |
232 | cmp_ok($rel_rs->count, '==', 5, 'Related search ok'); |
233 | |
234 | cmp_ok($or_rs->next->cdid, '==', $rel_rs->next->cdid, 'Related object ok'); |
a4731ae0 |
235 | $or_rs->reset; |
236 | $rel_rs->reset; |
a953d8d9 |
237 | |
4c4ccf29 |
238 | my $tag = $schema->resultset('Tag')->search( |
239 | [ { 'me.tag' => 'Blue' } ], { cols=>[qw/tagid/] } )->next; |
240 | |
241 | cmp_ok($tag->has_column_loaded('tagid'), '==', 1, 'Has tagid loaded'); |
242 | cmp_ok($tag->has_column_loaded('tag'), '==', 0, 'Has not tag loaded'); |
243 | |
a953d8d9 |
244 | ok($schema->storage(), 'Storage available'); |
245 | |
16b4fd26 |
246 | { |
247 | my $rs = $schema->resultset("Artist")->search({ |
248 | -and => [ |
249 | artistid => { '>=', 1 }, |
250 | artistid => { '<', 3 } |
251 | ] |
252 | }); |
253 | |
254 | $rs->update({ name => 'Test _cond_for_update_delete' }); |
255 | |
256 | my $art; |
257 | |
258 | $art = $schema->resultset("Artist")->find(1); |
259 | is($art->name, 'Test _cond_for_update_delete', 'updated first artist name'); |
260 | |
261 | $art = $schema->resultset("Artist")->find(2); |
262 | is($art->name, 'Test _cond_for_update_delete', 'updated second artist name'); |
263 | } |
264 | |
825135d8 |
265 | # test source_name |
266 | { |
267 | # source_name should be set for normal modules |
268 | is($schema->source('CD')->source_name, 'CD', 'source_name is set to moniker'); |
a4731ae0 |
269 | |
825135d8 |
270 | # test the result source that sets source_name explictly |
271 | ok($schema->source('SourceNameArtists'), 'SourceNameArtists result source exists'); |
0009fa49 |
272 | |
825135d8 |
273 | my @artsn = $schema->resultset('SourceNameArtists')->search({}, { order_by => 'name DESC' }); |
274 | cmp_ok(@artsn, '==', 4, "Four artists returned"); |
b1fb2c94 |
275 | |
276 | # make sure subclasses that don't set source_name are ok |
93405cf0 |
277 | ok($schema->source('ArtistSubclass'), 'ArtistSubclass exists'); |
825135d8 |
278 | } |
bab77431 |
279 | |
9c2c91ea |
280 | my $newbook = $schema->resultset( 'Bookmark' )->find(1); |
281 | |
282 | $@ = ''; |
283 | eval { |
284 | my $newlink = $newbook->link; |
285 | }; |
286 | ok(!$@, "stringify to false value doesn't cause error"); |
287 | |
825135d8 |
288 | # test cascade_delete through many_to_many relations |
289 | { |
290 | my $art_del = $schema->resultset("Artist")->find({ artistid => 1 }); |
291 | $art_del->delete; |
292 | cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.'); |
293 | cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.'); |
294 | } |
bab77431 |
295 | |
825135d8 |
296 | # test column_info |
297 | { |
298 | $schema->source("Artist")->{_columns}{'artistid'} = {}; |
d9916234 |
299 | $schema->source("Artist")->column_info_from_storage(1); |
bab77431 |
300 | |
825135d8 |
301 | my $typeinfo = $schema->source("Artist")->column_info('artistid'); |
302 | is($typeinfo->{data_type}, 'INTEGER', 'column_info ok'); |
303 | $schema->source("Artist")->column_info('artistid'); |
304 | ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set'); |
305 | } |
bab77431 |
306 | |
a48e92d7 |
307 | # test source_info |
308 | { |
309 | my $expected = { |
310 | "source_info_key_A" => "source_info_value_A", |
311 | "source_info_key_B" => "source_info_value_B", |
312 | "source_info_key_C" => "source_info_value_C", |
313 | }; |
314 | |
315 | my $sinfo = $schema->source("Artist")->source_info; |
316 | |
317 | is_deeply($sinfo, $expected, 'source_info data works'); |
318 | } |
319 | |
825135d8 |
320 | # test remove_columns |
321 | { |
322 | is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year/]); |
323 | $schema->source('CD')->remove_columns('year'); |
324 | is_deeply([$schema->source('CD')->columns], [qw/cdid artist title/]); |
a918d901 |
325 | ok(! exists $schema->source('CD')->_columns->{'year'}, 'year still exists in _columns'); |
825135d8 |
326 | } |
bab77431 |
327 | |
ba4a6453 |
328 | # test get_inflated_columns with objects |
03a1819f |
329 | SKIP: { |
330 | skip "This test requires DateTime::Format::MySQL", 5 if $NO_DTFM; |
ba4a6453 |
331 | my $event = $schema->resultset('Event')->search->first; |
332 | my %edata = $event->get_inflated_columns; |
333 | is($edata{'id'}, $event->id, 'got id'); |
334 | isa_ok($edata{'starts_at'}, 'DateTime', 'start_at is DateTime object'); |
335 | isa_ok($edata{'created_on'}, 'DateTime', 'create_on DateTime object'); |
336 | is($edata{'starts_at'}, $event->starts_at, 'got start date'); |
337 | is($edata{'created_on'}, $event->created_on, 'got created date'); |
338 | } |
339 | |
ade8df5b |
340 | # test resultsource->table return value when setting |
341 | { |
342 | my $class = $schema->class('Event'); |
343 | diag $class; |
344 | my $table = $class->table($class->table); |
345 | is($table, $class->table, '->table($table) returns $table'); |
346 | } |