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