6 # figure out if we've got a version of sqlite that is older than 3.2.6, in
7 # which case COUNT(DISTINCT()) doesn't work
8 my $is_broken_sqlite = 0;
9 my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
10 split /\./, $schema->storage->dbh->get_info(18);
11 if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
12 ( ($sqlite_major_ver < 3) ||
13 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
14 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
15 $is_broken_sqlite = 1;
19 my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
21 cmp_ok(@art, '==', 3, "Three artists returned");
25 is($art->name, 'We Are Goth', "Correct order too");
27 $art->name('We Are In Rehab');
29 is($art->name, 'We Are In Rehab', "Accessor update ok");
31 is($art->get_column("name"), 'We Are In Rehab', 'And via get_column');
33 ok($art->update, 'Update run');
35 my $record_jp = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search(undef, { prefetch => 'cds' })->next;
37 ok($record_jp, "prefetch on same rel okay");
39 my $record_fn = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search({'cds.cdid' => '1'}, {join => 'artist_undirected_maps'})->next;
41 ok($record_fn, "funny join is okay");
43 @art = $schema->resultset("Artist")->search({ name => 'We Are In Rehab' });
45 cmp_ok(@art, '==', 1, "Changed artist returned by search");
47 cmp_ok($art[0]->artistid, '==', 3,'Correct artist too');
51 @art = $schema->resultset("Artist")->search({ });
53 cmp_ok(@art, '==', 2, 'And then there were two');
55 ok(!$art->in_storage, "It knows it's dead");
57 eval { $art->delete; };
59 ok($@, "Can't delete twice: $@");
61 is($art->name, 'We Are In Rehab', 'But the object is still live');
65 ok($art->in_storage, "Re-created");
67 @art = $schema->resultset("Artist")->search({ });
69 cmp_ok(@art, '==', 3, 'And now there are three again');
71 my $new = $schema->resultset("Artist")->create({ artistid => 4 });
73 cmp_ok($new->artistid, '==', 4, 'Create produced record ok');
75 @art = $schema->resultset("Artist")->search({ });
77 cmp_ok(@art, '==', 4, "Oh my god! There's four of them!");
79 $new->set_column('name' => 'Man With A Fork');
81 is($new->name, 'Man With A Fork', 'set_column ok');
83 $new->discard_changes;
85 ok(!defined $new->name, 'Discard ok');
87 $new->name('Man With A Spoon');
91 $new_again = $schema->resultset("Artist")->find(4);
93 is($new_again->name, 'Man With A Spoon', 'Retrieved correctly');
95 is($new_again->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly');
97 # Test backwards compatibility
99 my $artist_by_hash = $schema->resultset('Artist')->find(artistid => 4);
100 is($artist_by_hash->name, 'Man With A Spoon', 'Retrieved correctly');
101 is($artist_by_hash->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly');
104 is($schema->resultset("Artist")->count, 4, 'count ok');
108 my $existing_obj = $schema->resultset('Artist')->find_or_new({
112 is($existing_obj->name, 'Man With A Spoon', 'find_or_new: found existing artist');
113 ok($existing_obj->in_storage, 'existing artist is in storage');
115 my $new_obj = $schema->resultset('Artist')->find_or_new({
117 name => 'find_or_new',
120 is($new_obj->name, 'find_or_new', 'find_or_new: instantiated a new artist');
121 ok(! $new_obj->in_storage, 'new artist is not in storage');
124 my $cd = $schema->resultset("CD")->find(1);
125 my %cols = $cd->get_columns;
127 cmp_ok(keys %cols, '==', 4, 'get_columns number of columns ok');
129 is($cols{title}, 'Spoonful of bees', 'get_columns values ok');
131 %cols = ( title => 'Forkful of bees', year => 2005);
132 $cd->set_columns(\%cols);
134 is($cd->title, 'Forkful of bees', 'set_columns ok');
136 is($cd->year, 2005, 'set_columns ok');
138 $cd->discard_changes;
140 # check whether ResultSource->columns returns columns in order originally supplied
141 my @cd = $schema->source("CD")->columns;
143 is_deeply( \@cd, [qw/cdid artist title year/], 'column order');
145 $cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { columns => ['title'] })->next;
146 is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly');
148 $cd = $schema->resultset("CD")->search(undef, { include_columns => [ 'artist.name' ], join => [ 'artist' ] })->find(1);
150 is($cd->title, 'Spoonful of bees', 'Correct CD returned with include');
151 is($cd->get_column('name'), 'Caterwauler McCrae', 'Additional column returned');
154 $new = $schema->resultset("Track")->new( {
158 title => 'Insert or Update',
160 $new->update_or_insert;
161 ok($new->in_storage, 'update_or_insert insert ok');
163 # test in update mode
165 $new->update_or_insert;
166 is( $schema->resultset("Track")->find(100)->pos, 5, 'update_or_insert update ok');
168 eval { $schema->class("Track")->load_components('DoesNotExist'); };
172 is($schema->class("Artist")->field_name_for->{name}, 'artist name', 'mk_classdata usage ok');
174 my $search = [ { 'tags.tag' => 'Cheesy' }, { 'tags.tag' => 'Blue' } ];
176 my( $or_rs ) = $schema->resultset("CD")->search_rs($search, { join => 'tags',
177 order_by => 'cdid' });
179 cmp_ok($or_rs->count, '==', 5, 'Search with OR ok');
181 my $distinct_rs = $schema->resultset("CD")->search($search, { join => 'tags', distinct => 1 });
182 cmp_ok($distinct_rs->all, '==', 4, 'DISTINCT search with OR ok');
185 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
186 if $is_broken_sqlite;
188 my $tcount = $schema->resultset("Track")->search(
191 select => {count => {distinct => ['position', 'title']}},
195 cmp_ok($tcount->next->get_column('count'), '==', 13, 'multiple column COUNT DISTINCT ok');
198 my $tag_rs = $schema->resultset('Tag')->search(
199 [ { 'me.tag' => 'Cheesy' }, { 'me.tag' => 'Blue' } ]);
201 my $rel_rs = $tag_rs->search_related('cd');
203 cmp_ok($rel_rs->count, '==', 5, 'Related search ok');
205 cmp_ok($or_rs->next->cdid, '==', $rel_rs->next->cdid, 'Related object ok');
209 my $tag = $schema->resultset('Tag')->search(
210 [ { 'me.tag' => 'Blue' } ], { cols=>[qw/tagid/] } )->next;
212 cmp_ok($tag->has_column_loaded('tagid'), '==', 1, 'Has tagid loaded');
213 cmp_ok($tag->has_column_loaded('tag'), '==', 0, 'Has not tag loaded');
215 ok($schema->storage(), 'Storage available');
218 my $rs = $schema->resultset("Artist")->search({
220 artistid => { '>=', 1 },
221 artistid => { '<', 3 }
225 $rs->update({ name => 'Test _cond_for_update_delete' });
229 $art = $schema->resultset("Artist")->find(1);
230 is($art->name, 'Test _cond_for_update_delete', 'updated first artist name');
232 $art = $schema->resultset("Artist")->find(2);
233 is($art->name, 'Test _cond_for_update_delete', 'updated second artist name');
238 # source_name should be set for normal modules
239 is($schema->source('CD')->source_name, 'CD', 'source_name is set to moniker');
241 # test the result source that sets source_name explictly
242 ok($schema->source('SourceNameArtists'), 'SourceNameArtists result source exists');
244 my @artsn = $schema->resultset('SourceNameArtists')->search({}, { order_by => 'name DESC' });
245 cmp_ok(@artsn, '==', 4, "Four artists returned");
248 # test cascade_delete through many_to_many relations
250 my $art_del = $schema->resultset("Artist")->find({ artistid => 1 });
252 cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.');
253 cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.');
258 $schema->source("Artist")->{_columns}{'artistid'} = {};
260 my $typeinfo = $schema->source("Artist")->column_info('artistid');
261 is($typeinfo->{data_type}, 'INTEGER', 'column_info ok');
262 $schema->source("Artist")->column_info('artistid');
263 ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set');
266 # test remove_columns
268 is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year/]);
269 $schema->source('CD')->remove_columns('year');
270 is_deeply([$schema->source('CD')->columns], [qw/cdid artist title/]);