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