8 my $schema = DBICTest->init_schema();
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
14 my $is_broken_sqlite = 0;
15 my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
16 split /\./, $schema->storage->dbh->get_info(18);
17 if( $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;
25 my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
27 cmp_ok(@art, '==', 3, "Three artists returned");
31 is($art->name, 'We Are Goth', "Correct order too");
33 $art->name('We Are In Rehab');
35 is($art->name, 'We Are In Rehab', "Accessor update ok");
37 is($art->get_column("name"), 'We Are In Rehab', 'And via get_column');
39 ok($art->update, 'Update run');
41 my $record_jp = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search(undef, { prefetch => 'cds' })->next;
43 ok($record_jp, "prefetch on same rel okay");
45 my $record_fn = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search({'cds.cdid' => '1'}, {join => 'artist_undirected_maps'})->next;
47 ok($record_fn, "funny join is okay");
49 @art = $schema->resultset("Artist")->search({ name => 'We Are In Rehab' });
51 cmp_ok(@art, '==', 1, "Changed artist returned by search");
53 cmp_ok($art[0]->artistid, '==', 3,'Correct artist too');
57 @art = $schema->resultset("Artist")->search({ });
59 cmp_ok(@art, '==', 2, 'And then there were two');
61 ok(!$art->in_storage, "It knows it's dead");
63 eval { $art->delete; };
65 ok($@, "Can't delete twice: $@");
67 is($art->name, 'We Are In Rehab', 'But the object is still live');
71 ok($art->in_storage, "Re-created");
73 @art = $schema->resultset("Artist")->search({ });
75 cmp_ok(@art, '==', 3, 'And now there are three again');
77 my $new = $schema->resultset("Artist")->create({ artistid => 4 });
79 cmp_ok($new->artistid, '==', 4, 'Create produced record ok');
81 @art = $schema->resultset("Artist")->search({ });
83 cmp_ok(@art, '==', 4, "Oh my god! There's four of them!");
85 $new->set_column('name' => 'Man With A Fork');
87 is($new->name, 'Man With A Fork', 'set_column ok');
89 $new->discard_changes;
91 ok(!defined $new->name, 'Discard ok');
93 $new->name('Man With A Spoon');
97 my $new_again = $schema->resultset("Artist")->find(4);
99 is($new_again->name, 'Man With A Spoon', 'Retrieved correctly');
101 is($new_again->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly');
103 # Test backwards compatibility
106 local $SIG{__WARN__} = sub { $warnings .= $_[0] };
108 my $artist_by_hash = $schema->resultset('Artist')->find(artistid => 4);
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');
111 like($warnings, qr/deprecated/, 'warned about deprecated find usage');
114 is($schema->resultset("Artist")->count, 4, 'count ok');
118 my $existing_obj = $schema->resultset('Artist')->find_or_new({
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');
125 my $new_obj = $schema->resultset('Artist')->find_or_new({
127 name => 'find_or_new',
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');
134 my $cd = $schema->resultset("CD")->find(1);
135 my %cols = $cd->get_columns;
137 cmp_ok(keys %cols, '==', 4, 'get_columns number of columns ok');
139 is($cols{title}, 'Spoonful of bees', 'get_columns values ok');
141 %cols = ( title => 'Forkful of bees', year => 2005);
142 $cd->set_columns(\%cols);
144 is($cd->title, 'Forkful of bees', 'set_columns ok');
146 is($cd->year, 2005, 'set_columns ok');
148 $cd->discard_changes;
150 # check whether ResultSource->columns returns columns in order originally supplied
151 my @cd = $schema->source("CD")->columns;
153 is_deeply( \@cd, [qw/cdid artist title year/], 'column order');
155 $cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { columns => ['title'] })->next;
156 is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly');
158 $cd = $schema->resultset("CD")->search(undef, { include_columns => [ 'artist.name' ], join => [ 'artist' ] })->find(1);
160 is($cd->title, 'Spoonful of bees', 'Correct CD returned with include');
161 is($cd->get_column('name'), 'Caterwauler McCrae', 'Additional column returned');
164 $new = $schema->resultset("Track")->new( {
168 title => 'Insert or Update',
170 $new->update_or_insert;
171 ok($new->in_storage, 'update_or_insert insert ok');
173 # test in update mode
175 $new->update_or_insert;
176 is( $schema->resultset("Track")->find(100)->pos, 5, 'update_or_insert update ok');
178 # get_inflated_columns w/relation and accessor alias
179 my %tdata = $new->get_inflated_columns;
180 is($tdata{'trackid'}, 100, 'got id');
181 isa_ok($tdata{'cd'}, 'DBICTest::CD', 'cd is CD object');
182 is($tdata{'cd'}->id, 1, 'cd object is id 1');
183 is($tdata{'position'}, 5, 'got position from pos');
184 is($tdata{'title'}, 'Insert or Update');
186 eval { $schema->class("Track")->load_components('DoesNotExist'); };
190 is($schema->class("Artist")->field_name_for->{name}, 'artist name', 'mk_classdata usage ok');
192 my $search = [ { 'tags.tag' => 'Cheesy' }, { 'tags.tag' => 'Blue' } ];
194 my( $or_rs ) = $schema->resultset("CD")->search_rs($search, { join => 'tags',
195 order_by => 'cdid' });
197 cmp_ok($or_rs->count, '==', 5, 'Search with OR ok');
199 my $distinct_rs = $schema->resultset("CD")->search($search, { join => 'tags', distinct => 1 });
200 cmp_ok($distinct_rs->all, '==', 4, 'DISTINCT search with OR ok');
203 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
204 if $is_broken_sqlite;
206 my $tcount = $schema->resultset("Track")->search(
209 select => {count => {distinct => ['position', 'title']}},
213 cmp_ok($tcount->next->get_column('count'), '==', 13, 'multiple column COUNT DISTINCT ok');
216 my $tag_rs = $schema->resultset('Tag')->search(
217 [ { 'me.tag' => 'Cheesy' }, { 'me.tag' => 'Blue' } ]);
219 my $rel_rs = $tag_rs->search_related('cd');
221 cmp_ok($rel_rs->count, '==', 5, 'Related search ok');
223 cmp_ok($or_rs->next->cdid, '==', $rel_rs->next->cdid, 'Related object ok');
227 my $tag = $schema->resultset('Tag')->search(
228 [ { 'me.tag' => 'Blue' } ], { cols=>[qw/tagid/] } )->next;
230 cmp_ok($tag->has_column_loaded('tagid'), '==', 1, 'Has tagid loaded');
231 cmp_ok($tag->has_column_loaded('tag'), '==', 0, 'Has not tag loaded');
233 ok($schema->storage(), 'Storage available');
236 my $rs = $schema->resultset("Artist")->search({
238 artistid => { '>=', 1 },
239 artistid => { '<', 3 }
243 $rs->update({ name => 'Test _cond_for_update_delete' });
247 $art = $schema->resultset("Artist")->find(1);
248 is($art->name, 'Test _cond_for_update_delete', 'updated first artist name');
250 $art = $schema->resultset("Artist")->find(2);
251 is($art->name, 'Test _cond_for_update_delete', 'updated second artist name');
256 # source_name should be set for normal modules
257 is($schema->source('CD')->source_name, 'CD', 'source_name is set to moniker');
259 # test the result source that sets source_name explictly
260 ok($schema->source('SourceNameArtists'), 'SourceNameArtists result source exists');
262 my @artsn = $schema->resultset('SourceNameArtists')->search({}, { order_by => 'name DESC' });
263 cmp_ok(@artsn, '==', 4, "Four artists returned");
265 # make sure subclasses that don't set source_name are ok
266 ok($schema->source('ArtistSubclass'), 'ArtistSubclass exists');
269 my $newbook = $schema->resultset( 'Bookmark' )->find(1);
273 my $newlink = $newbook->link;
275 ok(!$@, "stringify to false value doesn't cause error");
277 # test cascade_delete through many_to_many relations
279 my $art_del = $schema->resultset("Artist")->find({ artistid => 1 });
281 cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.');
282 cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.');
287 $schema->source("Artist")->{_columns}{'artistid'} = {};
288 $schema->source("Artist")->column_info_from_storage(1);
290 my $typeinfo = $schema->source("Artist")->column_info('artistid');
291 is($typeinfo->{data_type}, 'INTEGER', 'column_info ok');
292 $schema->source("Artist")->column_info('artistid');
293 ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set');
299 "source_info_key_A" => "source_info_value_A",
300 "source_info_key_B" => "source_info_value_B",
301 "source_info_key_C" => "source_info_value_C",
304 my $sinfo = $schema->source("Artist")->source_info;
306 is_deeply($sinfo, $expected, 'source_info data works');
309 # test remove_columns
311 is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year/]);
312 $schema->source('CD')->remove_columns('year');
313 is_deeply([$schema->source('CD')->columns], [qw/cdid artist title/]);
314 ok(! exists $schema->source('CD')->_columns->{'year'}, 'year still exists in _columns');
317 # test get_inflated_columns with objects
319 my $event = $schema->resultset('Event')->search->first;
320 my %edata = $event->get_inflated_columns;
321 is($edata{'id'}, $event->id, 'got id');
322 isa_ok($edata{'starts_at'}, 'DateTime', 'start_at is DateTime object');
323 isa_ok($edata{'created_on'}, 'DateTime', 'create_on DateTime object');
324 is($edata{'starts_at'}, $event->starts_at, 'got start date');
325 is($edata{'created_on'}, $event->created_on, 'got created date');