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 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
46 my( $rs_from_list ) = $artist->search_related_rs('cds');
47 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
49 ( $rs_from_list ) = $artist->cds_rs();
50 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
53 is( $artist->count_related('cds'), 4, 'count_related ok' );
56 my $track = $schema->resultset("Track")->create( {
60 title => 'Hidden Track'
62 $track->set_from_related( cd => $cd );
64 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
66 $track->set_from_related( cd => undef );
68 ok( !defined($track->cd), 'set_from_related with undef ok');
71 local $TODO = 'accessing $object->rel and set_from_related';
72 my $track = $schema->resultset("Track")->new( {} );
74 $track->set_from_related( cd => $cd );
75 ok ($track->cd, 'set_from_related ok after using the accessor' );
78 # update_from_related, the same as set_from_related, but it calls update afterwards
79 $track = $schema->resultset("Track")->create( {
83 title => 'Hidden Track 2'
85 $track->update_from_related( cd => $cd );
87 my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd;
89 is( $t_cd->cdid, 4, 'update_from_related ok' );
91 # find_or_create_related with an existing record
92 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
93 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
95 # find_or_create_related creating a new record
96 $cd = $artist->find_or_create_related( 'cds', {
97 title => 'Greatest Hits',
100 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
102 @cds = $artist->search_related('cds');
103 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
105 $artist->delete_related( cds => { title => 'Greatest Hits' });
106 cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
108 # find_or_new_related with an existing record
109 $cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } );
110 is( $cd->year, 2005, 'find_or_new_related on existing record ok' );
111 ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' );
113 # find_or_new_related instantiating a new record
114 $cd = $artist->find_or_new_related( 'cds', {
115 title => 'Greatest Hits 2: Louder Than Ever',
118 is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' );
119 ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' );
122 my $newartist = $cd->find_or_new_related( 'artist', {
123 name => 'Random Boy Band Two',
126 is($newartist->name, 'Random Boy Band Two', 'find_or_new_related new artist record with id');
127 is($newartist->id, 200, 'find_or_new_related new artist id set');
130 local $TODO = "relationship checking needs fixing";
131 # try to add a bogus relationship using the wrong cols
133 DBICTest::Schema::Artist->add_relationship(
134 tracks => 'DBICTest::Schema::Track',
135 { 'foreign.cd' => 'self.cdid' }
138 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
141 # another bogus relationship using no join condition
143 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
145 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
147 # many_to_many helper tests
148 $cd = $schema->resultset("CD")->find(1);
149 my @producers = $cd->producers();
150 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
151 is( $cd->producers_sorted->next->name, 'Bob The Builder',
152 'sorted many_to_many ok' );
153 is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype',
154 'sorted many_to_many with search condition ok' );
156 $cd = $schema->resultset('CD')->find(2);
157 my $prod_rs = $cd->producers();
158 my $prod_before_count = $schema->resultset('Producer')->count;
159 is( $prod_rs->count, 0, "CD doesn't yet have any producers" );
160 my $prod = $schema->resultset('Producer')->find(1);
161 $cd->add_to_producers($prod);
162 is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' );
163 is( $prod_rs->first->name, 'Matt S Trout',
164 'many_to_many add_to_$rel($obj) ok' );
165 $cd->remove_from_producers($prod);
166 is( $schema->resultset('Producer')->find(1)->name, 'Matt S Trout',
167 "producer object exists after remove of link" );
168 is( $prod_rs->count, 0, 'many_to_many remove_from_$rel($obj) ok' );
169 $cd->add_to_producers({ name => 'Testy McProducer' });
170 is( $schema->resultset('Producer')->count, $prod_before_count+1,
171 'add_to_$rel($hash) inserted a new producer' );
172 is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($hash) count ok' );
173 is( $prod_rs->first->name, 'Testy McProducer',
174 'many_to_many add_to_$rel($hash) ok' );
175 $cd->add_to_producers({ name => 'Jack Black' });
176 is( $prod_rs->count(), 2, 'many_to_many add_to_$rel($hash) count ok' );
177 $cd->set_producers($schema->resultset('Producer')->all);
178 is( $cd->producers->count(), $prod_before_count+2,
179 'many_to_many set_$rel(@objs) count ok' );
180 $cd->set_producers($schema->resultset('Producer')->find(1));
181 is( $cd->producers->count(), 1, 'many_to_many set_$rel($obj) count ok' );
182 $cd->set_producers([$schema->resultset('Producer')->all]);
183 is( $cd->producers->count(), $prod_before_count+2,
184 'many_to_many set_$rel(\@objs) count ok' );
185 $cd->set_producers([$schema->resultset('Producer')->find(1)]);
186 is( $cd->producers->count(), 1, 'many_to_many set_$rel([$obj]) count ok' );
188 eval { $cd->remove_from_producers({ fake => 'hash' }); };
189 like( $@, qr/needs an object/, 'remove_from_$rel($hash) dies correctly' );
191 eval { $cd->add_to_producers(); };
192 like( $@, qr/needs an object or hashref/,
193 'add_to_$rel(undef) dies correctly' );
195 # many_to_many stresstest
196 my $twokey = $schema->resultset('TwoKeys')->find(1,1);
197 my $fourkey = $schema->resultset('FourKeys')->find(1,2,3,4);
199 is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
200 $twokey->add_to_fourkeys($fourkey, { autopilot => 'engaged' });
201 my $got_fourkey = $twokey->fourkeys({ sensors => 'online' })->first;
202 is( $twokey->fourkeys->count, 1, 'twokey has one fourkey' );
203 is( $got_fourkey->$_, $fourkey->$_,
204 'fourkeys row has the correct value for column '.$_ )
205 for (qw(foo bar hello goodbye sensors));
206 $twokey->remove_from_fourkeys($fourkey);
207 is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
208 is( $twokey->fourkeys_to_twokeys->count, 0,
209 'twokey has no links to fourkey' );
211 my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 });
212 is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded');
213 is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db');
215 $undef_artist_cd->related_resultset('artist')->new({name => 'foo'});
217 is( $@, '', "Object created on a resultset related to not yet inserted object");
219 my $def_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007, artist => undef });
220 is($def_artist_cd->has_column_loaded('artist'), 1, 'FK loaded');
221 is($def_artist_cd->search_related('artist')->count, 0, 'closed search on null FK');
223 # test undirected many-to-many relationship (e.g. "related artists")
224 my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
225 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
227 $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
228 is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
230 my $mapped_rs = $undir_maps->search_related('mapped_artists');
232 my @art = $mapped_rs->all;
234 cmp_ok(@art, '==', 2, "Both artist returned from map");
236 my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}});
238 cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition");
240 # check join through cascaded has_many relationships
241 $artist = $schema->resultset("Artist")->find(1);
242 my $trackset = $artist->cds->search_related('tracks');
243 # LEFT join means we also see the trackless additional album...
244 cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist");
246 # now see about updating eveything that belongs to artist 2 to artist 3
247 $artist = $schema->resultset("Artist")->find(2);
248 my $nartist = $schema->resultset("Artist")->find(3);
249 cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist");
250 cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist");
251 $artist->cds->update({artist => $nartist->id});
252 cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
253 cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");
255 my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode' });
256 my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave in Silence', 'year' => 1982});
259 $new_related_cd->insert;
261 is ($@, '', 'Staged insertion successful');
262 ok($new_artist->in_storage, 'artist inserted');
263 ok($new_related_cd->in_storage, 'new_related_cd inserted');
265 my $new_cd = $schema->resultset("CD")->new_result({});
266 my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',});
269 $new_related_artist->insert;
270 $new_cd->title( 'Misplaced Childhood' );
271 $new_cd->year ( 1985 );
272 # $new_cd->artist( $new_related_artist ); # For exact backward compatibility # not sure what this means
275 'Reversed staged insertion successful'
277 ok($new_related_artist->in_storage, 'related artist inserted');
278 ok($new_cd->in_storage, 'cd inserted');
280 # check if is_foreign_key_constraint attr is set
281 my $rs_normal = $schema->source('Track');
282 my $relinfo = $rs_normal->relationship_info ('cd');
283 cmp_ok($relinfo->{attrs}{is_foreign_key_constraint}, '==', 1, "is_foreign_key_constraint defined for belongs_to relationships.");
285 my $rs_overridden = $schema->source('ForceForeign');
286 my $relinfo_with_attr = $rs_overridden->relationship_info ('cd_3');
287 cmp_ok($relinfo_with_attr->{attrs}{is_foreign_key_constraint}, '==', 0, "is_foreign_key_constraint defined for belongs_to relationships with attr.");