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