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