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