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