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 @art = $schema->resultset("Artist")->search({ name => 'We Are In Rehab' });
37 cmp_ok(@art, '==', 1, "Changed artist returned by search");
39 cmp_ok($art[0]->artistid, '==', 3,'Correct artist too');
43 @art = $schema->resultset("Artist")->search({ });
45 cmp_ok(@art, '==', 2, 'And then there were two');
47 ok(!$art->in_storage, "It knows it's dead");
49 eval { $art->delete; };
51 ok($@, "Can't delete twice: $@");
53 is($art->name, 'We Are In Rehab', 'But the object is still live');
57 ok($art->in_storage, "Re-created");
59 @art = $schema->resultset("Artist")->search({ });
61 cmp_ok(@art, '==', 3, 'And now there are three again');
63 my $new = $schema->resultset("Artist")->create({ artistid => 4 });
65 cmp_ok($new->artistid, '==', 4, 'Create produced record ok');
67 @art = $schema->resultset("Artist")->search({ });
69 cmp_ok(@art, '==', 4, "Oh my god! There's four of them!");
71 $new->set_column('name' => 'Man With A Fork');
73 is($new->name, 'Man With A Fork', 'set_column ok');
75 $new->discard_changes;
77 ok(!defined $new->name, 'Discard ok');
79 $new->name('Man With A Spoon');
83 $new_again = $schema->resultset("Artist")->find(4);
85 is($new_again->name, 'Man With A Spoon', 'Retrieved correctly');
87 is($new_again->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly');
89 is($schema->resultset("Artist")->count, 4, 'count ok');
91 my $cd = $schema->resultset("CD")->find(1);
92 my %cols = $cd->get_columns;
94 cmp_ok(keys %cols, '==', 4, 'get_columns number of columns ok');
96 is($cols{title}, 'Spoonful of bees', 'get_columns values ok');
98 %cols = ( title => 'Forkful of bees', year => 2005);
99 $cd->set_columns(\%cols);
101 is($cd->title, 'Forkful of bees', 'set_columns ok');
103 is($cd->year, 2005, 'set_columns ok');
105 $cd->discard_changes;
107 # check whether ResultSource->columns returns columns in order originally supplied
108 my @cd = $schema->source("CD")->columns;
110 is_deeply( \@cd, [qw/cdid artist title year/], 'column order');
112 $cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { columns => ['title'] })->next;
113 is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly');
115 $cd = $schema->resultset("CD")->search(undef, { include_columns => [ 'artist.name' ], join => [ 'artist' ] })->find(1);
117 is($cd->title, 'Spoonful of bees', 'Correct CD returned with include');
118 is($cd->get_column('name'), 'Caterwauler McCrae', 'Additional column returned');
121 $new = $schema->resultset("Track")->new( {
125 title => 'Insert or Update',
127 $new->update_or_insert;
128 ok($new->in_storage, 'update_or_insert insert ok');
130 # test in update mode
132 $new->update_or_insert;
133 is( $schema->resultset("Track")->find(100)->pos, 5, 'update_or_insert update ok');
135 eval { $schema->class("Track")->load_components('DoesNotExist'); };
139 is($schema->class("Artist")->field_name_for->{name}, 'artist name', 'mk_classdata usage ok');
141 my $search = [ { 'tags.tag' => 'Cheesy' }, { 'tags.tag' => 'Blue' } ];
143 my $or_rs = $schema->resultset("CD")->search($search, { join => 'tags',
144 order_by => 'cdid' });
146 cmp_ok($or_rs->count, '==', 5, 'Search with OR ok');
148 my $distinct_rs = $schema->resultset("CD")->search($search, { join => 'tags', distinct => 1 });
149 cmp_ok($distinct_rs->all, '==', 4, 'DISTINCT search with OR ok');
152 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
153 if $is_broken_sqlite;
155 my $tcount = $schema->resultset("Track")->search(
158 select => {count => {distinct => ['position', 'title']}},
162 cmp_ok($tcount->next->get_column('count'), '==', 13, 'multiple column COUNT DISTINCT ok');
165 my $tag_rs = $schema->resultset('Tag')->search(
166 [ { 'me.tag' => 'Cheesy' }, { 'me.tag' => 'Blue' } ]);
168 my $rel_rs = $tag_rs->search_related('cd');
170 cmp_ok($rel_rs->count, '==', 5, 'Related search ok');
172 cmp_ok($or_rs->next->cdid, '==', $rel_rs->next->cdid, 'Related object ok');
176 my $tag = $schema->resultset('Tag')->search(
177 [ { 'me.tag' => 'Blue' } ], { cols=>[qw/tagid/] } )->next;
179 cmp_ok($tag->has_column_loaded('tagid'), '==', 1, 'Has tagid loaded');
180 cmp_ok($tag->has_column_loaded('tag'), '==', 0, 'Has not tag loaded');
182 ok($schema->storage(), 'Storage available');
185 my $rs = $schema->resultset("Artist")->search({
187 artistid => { '>=', 1 },
188 artistid => { '<', 3 }
192 $rs->update({ name => 'Test _cond_for_update_delete' });
196 $art = $schema->resultset("Artist")->find(1);
197 is($art->name, 'Test _cond_for_update_delete', 'updated first artist name');
199 $art = $schema->resultset("Artist")->find(2);
200 is($art->name, 'Test _cond_for_update_delete', 'updated second artist name');
203 #test cascade_delete thru many_many relations
204 my $art_del = $schema->resultset("Artist")->find({ artistid => 1 });
206 cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.');
207 cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.');
209 $schema->source("Artist")->{_columns}{'artistid'} = {};
211 my $typeinfo = $schema->source("Artist")->column_info('artistid');
212 is($typeinfo->{data_type}, 'INTEGER', 'column_info ok');
213 $schema->source("Artist")->column_info('artistid');
214 ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set');
216 my $newbook = $schema->resultset( 'Bookmark' )->find(1);
220 my $newlink = $newbook->link;
222 ok(!$@, "stringify to false value doesn't cause error");