9 my $schema = DBICTest->init_schema();
14 my $cd = $schema->resultset("CD")->find(4);
15 my ($artist) = ($INC{'DBICTest/HelperRels'}
17 : $cd->search_related('artist'));
18 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
20 # has_many test with an order_by clause defined
21 $artist = $schema->resultset("Artist")->find(1);
22 my @cds = ($INC{'DBICTest/HelperRels'}
24 : $artist->search_related('cds'));
25 is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
27 # search_related with additional abstract query
28 @cds = ($INC{'DBICTest/HelperRels'}
29 ? $artist->cds({ title => { like => '%of%' } })
30 : $artist->search_related('cds', { title => { like => '%of%' } } )
32 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
34 # creating a related object
35 if ($INC{'DBICTest/HelperRels.pm'}) {
36 $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
38 $artist->create_related( 'cds', {
44 my $big_flop_cd = ($artist->search_related('cds'))[3];
45 is( $big_flop_cd->title, 'Big Flop', 'create_related ok' );
47 { # make sure we are not making pointless select queries when a FK IS NULL
49 $schema->storage->debugcb(sub { $queries++; });
50 $schema->storage->debug(1);
51 $big_flop_cd->genre; #should not trigger a select query
52 is($queries, 0, 'No SELECT made for belongs_to if key IS NULL');
53 $big_flop_cd->genre_inefficient; #should trigger a select query
54 is($queries, 1, 'SELECT made for belongs_to if key IS NULL when undef_on_null_fk disabled');
55 $schema->storage->debug(0);
56 $schema->storage->debugcb(undef);
59 my( $rs_from_list ) = $artist->search_related_rs('cds');
60 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
62 ( $rs_from_list ) = $artist->cds_rs();
63 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
66 is( $artist->count_related('cds'), 4, 'count_related ok' );
69 my $track = $schema->resultset("Track")->create( {
73 title => 'Hidden Track'
75 $track->set_from_related( cd => $cd );
77 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
79 $track->set_from_related( cd => undef );
81 ok( !defined($track->cd), 'set_from_related with undef ok');
84 local $TODO = 'accessing $object->rel and set_from_related';
85 my $track = $schema->resultset("Track")->new( {} );
87 $track->set_from_related( cd => $cd );
88 ok ($track->cd, 'set_from_related ok after using the accessor' );
91 # update_from_related, the same as set_from_related, but it calls update afterwards
92 $track = $schema->resultset("Track")->create( {
95 title => 'Hidden Track 2'
97 $track->update_from_related( cd => $cd );
99 my $t_cd = ($schema->resultset("Track")->search( cd => 4, title => 'Hidden Track 2' ))[0]->cd;
101 is( $t_cd->cdid, 4, 'update_from_related ok' );
103 # find_or_create_related with an existing record
104 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
105 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
107 # find_or_create_related creating a new record
108 $cd = $artist->find_or_create_related( 'cds', {
109 title => 'Greatest Hits',
112 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
114 @cds = $artist->search_related('cds');
115 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
117 $artist->delete_related( cds => { title => 'Greatest Hits' });
118 cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
120 # find_or_new_related with an existing record
121 $cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } );
122 is( $cd->year, 2005, 'find_or_new_related on existing record ok' );
123 ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' );
125 # find_or_new_related instantiating a new record
126 $cd = $artist->find_or_new_related( 'cds', {
127 title => 'Greatest Hits 2: Louder Than Ever',
130 is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' );
131 ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' );
134 my $newartist = $cd->find_or_new_related( 'artist', {
135 name => 'Random Boy Band Two',
138 is($newartist->name, 'Random Boy Band Two', 'find_or_new_related new artist record with id');
139 is($newartist->id, 200, 'find_or_new_related new artist id set');
142 local $TODO = "relationship checking needs fixing";
143 # try to add a bogus relationship using the wrong cols
145 DBICTest::Schema::Artist->add_relationship(
146 tracks => 'DBICTest::Schema::Track',
147 { 'foreign.cd' => 'self.cdid' }
150 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
153 # another bogus relationship using no join condition
155 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
157 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
159 # many_to_many helper tests
160 $cd = $schema->resultset("CD")->find(1);
161 my @producers = $cd->producers();
162 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
163 is( $cd->producers_sorted->next->name, 'Bob The Builder',
164 'sorted many_to_many ok' );
165 is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype',
166 'sorted many_to_many with search condition ok' );
168 $cd = $schema->resultset('CD')->find(2);
169 my $prod_rs = $cd->producers();
170 my $prod_before_count = $schema->resultset('Producer')->count;
171 is( $prod_rs->count, 0, "CD doesn't yet have any producers" );
172 my $prod = $schema->resultset('Producer')->find(1);
173 $cd->add_to_producers($prod);
174 is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' );
175 is( $prod_rs->first->name, 'Matt S Trout',
176 'many_to_many add_to_$rel($obj) ok' );
177 $cd->remove_from_producers($prod);
178 is( $schema->resultset('Producer')->find(1)->name, 'Matt S Trout',
179 "producer object exists after remove of link" );
180 is( $prod_rs->count, 0, 'many_to_many remove_from_$rel($obj) ok' );
181 $cd->add_to_producers({ name => 'Testy McProducer' });
182 is( $schema->resultset('Producer')->count, $prod_before_count+1,
183 'add_to_$rel($hash) inserted a new producer' );
184 is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($hash) count ok' );
185 is( $prod_rs->first->name, 'Testy McProducer',
186 'many_to_many add_to_$rel($hash) ok' );
187 $cd->add_to_producers({ name => 'Jack Black' });
188 is( $prod_rs->count(), 2, 'many_to_many add_to_$rel($hash) count ok' );
189 $cd->set_producers($schema->resultset('Producer')->all);
190 is( $cd->producers->count(), $prod_before_count+2,
191 'many_to_many set_$rel(@objs) count ok' );
192 $cd->set_producers($schema->resultset('Producer')->find(1));
193 is( $cd->producers->count(), 1, 'many_to_many set_$rel($obj) count ok' );
194 $cd->set_producers([$schema->resultset('Producer')->all]);
195 is( $cd->producers->count(), $prod_before_count+2,
196 'many_to_many set_$rel(\@objs) count ok' );
197 $cd->set_producers([$schema->resultset('Producer')->find(1)]);
198 is( $cd->producers->count(), 1, 'many_to_many set_$rel([$obj]) count ok' );
200 eval { $cd->remove_from_producers({ fake => 'hash' }); };
201 like( $@, qr/needs an object/, 'remove_from_$rel($hash) dies correctly' );
203 eval { $cd->add_to_producers(); };
204 like( $@, qr/needs an object or hashref/,
205 'add_to_$rel(undef) dies correctly' );
207 # many_to_many stresstest
208 my $twokey = $schema->resultset('TwoKeys')->find(1,1);
209 my $fourkey = $schema->resultset('FourKeys')->find(1,2,3,4);
211 is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
212 $twokey->add_to_fourkeys($fourkey, { autopilot => 'engaged' });
213 my $got_fourkey = $twokey->fourkeys({ sensors => 'online' })->first;
214 is( $twokey->fourkeys->count, 1, 'twokey has one fourkey' );
215 is( $got_fourkey->$_, $fourkey->$_,
216 'fourkeys row has the correct value for column '.$_ )
217 for (qw(foo bar hello goodbye sensors));
218 $twokey->remove_from_fourkeys($fourkey);
219 is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
220 is( $twokey->fourkeys_to_twokeys->count, 0,
221 'twokey has no links to fourkey' );
223 my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 });
224 is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded');
225 is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db');
227 $undef_artist_cd->related_resultset('artist')->new({name => 'foo'});
229 is( $@, '', "Object created on a resultset related to not yet inserted object");
231 $schema->resultset('Artwork')->new_result({})->cd;
232 } 'undef_on_null_fk does not choke on empty conds';
234 my $def_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007, artist => undef });
235 is($def_artist_cd->has_column_loaded('artist'), 1, 'FK loaded');
236 is($def_artist_cd->search_related('artist')->count, 0, 'closed search on null FK');
238 # test undirected many-to-many relationship (e.g. "related artists")
239 my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
240 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
242 $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
243 is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
245 my $mapped_rs = $undir_maps->search_related('mapped_artists');
247 my @art = $mapped_rs->all;
249 cmp_ok(@art, '==', 2, "Both artist returned from map");
251 my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}});
253 cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition");
255 # check join through cascaded has_many relationships
256 $artist = $schema->resultset("Artist")->find(1);
257 my $trackset = $artist->cds->search_related('tracks');
258 # LEFT join means we also see the trackless additional album...
259 cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist");
261 # now see about updating eveything that belongs to artist 2 to artist 3
262 $artist = $schema->resultset("Artist")->find(2);
263 my $nartist = $schema->resultset("Artist")->find(3);
264 cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist");
265 cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist");
266 $artist->cds->update({artist => $nartist->id});
267 cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
268 cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");
270 # check if is_foreign_key_constraint attr is set
271 my $rs_normal = $schema->source('Track');
272 my $relinfo = $rs_normal->relationship_info ('cd');
273 cmp_ok($relinfo->{attrs}{is_foreign_key_constraint}, '==', 1, "is_foreign_key_constraint defined for belongs_to relationships.");
275 my $rs_overridden = $schema->source('ForceForeign');
276 my $relinfo_with_attr = $rs_overridden->relationship_info ('cd_3');
277 cmp_ok($relinfo_with_attr->{attrs}{is_foreign_key_constraint}, '==', 0, "is_foreign_key_constraint defined for belongs_to relationships with attr.");