Merge 'DBIx-Class-current' into 'resultset-new-refactor'
[dbsrgits/DBIx-Class.git] / t / run / 01core.tl
CommitLineData
0567538f 1sub run_tests {
1edaf6fe 2my $schema = shift;
0567538f 3
cf72e3cf 4plan tests => 59;
0567538f 5
f2de4889 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
8my $is_broken_sqlite = 0;
9my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
10 split /\./, $schema->storage->dbh->get_info(18);
11if( $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;
16}
17
0567538f 18
f9db5527 19my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
0567538f 20
21cmp_ok(@art, '==', 3, "Three artists returned");
22
23my $art = $art[0];
24
25is($art->name, 'We Are Goth', "Correct order too");
26
27$art->name('We Are In Rehab');
28
29is($art->name, 'We Are In Rehab', "Accessor update ok");
30
31is($art->get_column("name"), 'We Are In Rehab', 'And via get_column');
32
33ok($art->update, 'Update run');
34
6804d573 35my $record_jp = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search(undef, { prefetch => 'cds' })->next;
36
37ok($record_jp, "prefetch on same rel okay");
38
39my $record_fn = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search({'cds.cdid' => '1'}, {join => 'artist_undirected_maps'})->next;
40
41ok($record_fn, "funny join is okay");
42
f9db5527 43@art = $schema->resultset("Artist")->search({ name => 'We Are In Rehab' });
0567538f 44
45cmp_ok(@art, '==', 1, "Changed artist returned by search");
46
47cmp_ok($art[0]->artistid, '==', 3,'Correct artist too');
48
49$art->delete;
50
f9db5527 51@art = $schema->resultset("Artist")->search({ });
0567538f 52
53cmp_ok(@art, '==', 2, 'And then there were two');
54
55ok(!$art->in_storage, "It knows it's dead");
56
57eval { $art->delete; };
58
59ok($@, "Can't delete twice: $@");
60
61is($art->name, 'We Are In Rehab', 'But the object is still live');
62
63$art->insert;
64
65ok($art->in_storage, "Re-created");
66
f9db5527 67@art = $schema->resultset("Artist")->search({ });
0567538f 68
69cmp_ok(@art, '==', 3, 'And now there are three again');
70
f9db5527 71my $new = $schema->resultset("Artist")->create({ artistid => 4 });
0567538f 72
73cmp_ok($new->artistid, '==', 4, 'Create produced record ok');
74
f9db5527 75@art = $schema->resultset("Artist")->search({ });
0567538f 76
77cmp_ok(@art, '==', 4, "Oh my god! There's four of them!");
78
79$new->set_column('name' => 'Man With A Fork');
80
81is($new->name, 'Man With A Fork', 'set_column ok');
82
83$new->discard_changes;
84
85ok(!defined $new->name, 'Discard ok');
86
87$new->name('Man With A Spoon');
88
89$new->update;
90
f9db5527 91$new_again = $schema->resultset("Artist")->find(4);
0567538f 92
93is($new_again->name, 'Man With A Spoon', 'Retrieved correctly');
94
9bbd8963 95is($new_again->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly');
1f6715ab 96
a87eb971 97# Test backwards compatibility
98{
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');
102}
103
f9db5527 104is($schema->resultset("Artist")->count, 4, 'count ok');
0567538f 105
b3e1f1f5 106# test find_or_new
107{
108 my $existing_obj = $schema->resultset('Artist')->find_or_new({
109 artistid => 4,
110 });
111
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');
114
115 my $new_obj = $schema->resultset('Artist')->find_or_new({
116 artistid => 5,
117 name => 'find_or_new',
118 });
119
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');
122}
123
f9db5527 124my $cd = $schema->resultset("CD")->find(1);
076a6864 125my %cols = $cd->get_columns;
126
127cmp_ok(keys %cols, '==', 4, 'get_columns number of columns ok');
128
129is($cols{title}, 'Spoonful of bees', 'get_columns values ok');
130
131%cols = ( title => 'Forkful of bees', year => 2005);
132$cd->set_columns(\%cols);
133
134is($cd->title, 'Forkful of bees', 'set_columns ok');
135
136is($cd->year, 2005, 'set_columns ok');
137
138$cd->discard_changes;
139
20518cb4 140# check whether ResultSource->columns returns columns in order originally supplied
141my @cd = $schema->source("CD")->columns;
571dced3 142
20518cb4 143is_deeply( \@cd, [qw/cdid artist title year/], 'column order');
571dced3 144
82a96700 145$cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { columns => ['title'] })->next;
90f3f5ff 146is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly');
147
5ac6a044 148$cd = $schema->resultset("CD")->search(undef, { include_columns => [ 'artist.name' ], join => [ 'artist' ] })->find(1);
149
150is($cd->title, 'Spoonful of bees', 'Correct CD returned with include');
151is($cd->get_column('name'), 'Caterwauler McCrae', 'Additional column returned');
152
82a96700 153# update_or_insert
f9db5527 154$new = $schema->resultset("Track")->new( {
0567538f 155 trackid => 100,
156 cd => 1,
157 position => 1,
158 title => 'Insert or Update',
159} );
82a96700 160$new->update_or_insert;
161ok($new->in_storage, 'update_or_insert insert ok');
0567538f 162
163# test in update mode
91b0fbd7 164$new->pos(5);
82a96700 165$new->update_or_insert;
166is( $schema->resultset("Track")->find(100)->pos, 5, 'update_or_insert update ok');
0567538f 167
8c49f629 168eval { $schema->class("Track")->load_components('DoesNotExist'); };
0567538f 169
170ok $@, $@;
171
1edaf6fe 172is($schema->class("Artist")->field_name_for->{name}, 'artist name', 'mk_classdata usage ok');
90e6de6c 173
54540863 174my $search = [ { 'tags.tag' => 'Cheesy' }, { 'tags.tag' => 'Blue' } ];
175
5b89a768 176my( $or_rs ) = $schema->resultset("CD")->search_rs($search, { join => 'tags',
6aeb9185 177 order_by => 'cdid' });
54540863 178
6aeb9185 179cmp_ok($or_rs->count, '==', 5, 'Search with OR ok');
54540863 180
f9db5527 181my $distinct_rs = $schema->resultset("CD")->search($search, { join => 'tags', distinct => 1 });
6aeb9185 182cmp_ok($distinct_rs->all, '==', 4, 'DISTINCT search with OR ok');
183
f2de4889 184SKIP: {
185 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
186 if $is_broken_sqlite;
187
188 my $tcount = $schema->resultset("Track")->search(
286f32b3 189 {},
f2de4889 190 {
191 select => {count => {distinct => ['position', 'title']}},
192 as => ['count']
286f32b3 193 }
194 );
f2de4889 195 cmp_ok($tcount->next->get_column('count'), '==', 13, 'multiple column COUNT DISTINCT ok');
286f32b3 196
f2de4889 197}
f9db5527 198my $tag_rs = $schema->resultset('Tag')->search(
6aeb9185 199 [ { 'me.tag' => 'Cheesy' }, { 'me.tag' => 'Blue' } ]);
200
201my $rel_rs = $tag_rs->search_related('cd');
202
203cmp_ok($rel_rs->count, '==', 5, 'Related search ok');
204
205cmp_ok($or_rs->next->cdid, '==', $rel_rs->next->cdid, 'Related object ok');
a4731ae0 206$or_rs->reset;
207$rel_rs->reset;
a953d8d9 208
4c4ccf29 209my $tag = $schema->resultset('Tag')->search(
210 [ { 'me.tag' => 'Blue' } ], { cols=>[qw/tagid/] } )->next;
211
212cmp_ok($tag->has_column_loaded('tagid'), '==', 1, 'Has tagid loaded');
213cmp_ok($tag->has_column_loaded('tag'), '==', 0, 'Has not tag loaded');
214
a953d8d9 215ok($schema->storage(), 'Storage available');
216
16b4fd26 217{
218 my $rs = $schema->resultset("Artist")->search({
219 -and => [
220 artistid => { '>=', 1 },
221 artistid => { '<', 3 }
222 ]
223 });
224
225 $rs->update({ name => 'Test _cond_for_update_delete' });
226
227 my $art;
228
229 $art = $schema->resultset("Artist")->find(1);
230 is($art->name, 'Test _cond_for_update_delete', 'updated first artist name');
231
232 $art = $schema->resultset("Artist")->find(2);
233 is($art->name, 'Test _cond_for_update_delete', 'updated second artist name');
234}
235
825135d8 236# test source_name
237{
238 # source_name should be set for normal modules
239 is($schema->source('CD')->source_name, 'CD', 'source_name is set to moniker');
a4731ae0 240
825135d8 241 # test the result source that sets source_name explictly
242 ok($schema->source('SourceNameArtists'), 'SourceNameArtists result source exists');
0009fa49 243
825135d8 244 my @artsn = $schema->resultset('SourceNameArtists')->search({}, { order_by => 'name DESC' });
245 cmp_ok(@artsn, '==', 4, "Four artists returned");
246}
bab77431 247
9c2c91ea 248my $newbook = $schema->resultset( 'Bookmark' )->find(1);
249
250$@ = '';
251eval {
252my $newlink = $newbook->link;
253};
254ok(!$@, "stringify to false value doesn't cause error");
255
825135d8 256# test cascade_delete through many_to_many relations
257{
258 my $art_del = $schema->resultset("Artist")->find({ artistid => 1 });
259 $art_del->delete;
260 cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.');
261 cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.');
262}
bab77431 263
825135d8 264# test column_info
265{
266 $schema->source("Artist")->{_columns}{'artistid'} = {};
bab77431 267
825135d8 268 my $typeinfo = $schema->source("Artist")->column_info('artistid');
269 is($typeinfo->{data_type}, 'INTEGER', 'column_info ok');
270 $schema->source("Artist")->column_info('artistid');
271 ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set');
272}
bab77431 273
825135d8 274# test remove_columns
275{
276 is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year/]);
277 $schema->source('CD')->remove_columns('year');
278 is_deeply([$schema->source('CD')->columns], [qw/cdid artist title/]);
279}
bab77431 280
0567538f 281}
282
2831;