8 my $schema = DBICTest->init_schema();
13 my $cd = $schema->resultset("CD")->find(4);
14 my ($artist) = ($INC{'DBICTest/HelperRels'}
16 : $cd->search_related('artist'));
17 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
19 # has_many test with an order_by clause defined
20 $artist = $schema->resultset("Artist")->find(1);
21 my @cds = ($INC{'DBICTest/HelperRels'}
23 : $artist->search_related('cds'));
24 is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
26 # search_related with additional abstract query
27 @cds = ($INC{'DBICTest/HelperRels'}
28 ? $artist->cds({ title => { like => '%of%' } })
29 : $artist->search_related('cds', { title => { like => '%of%' } } )
31 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
33 # creating a related object
34 if ($INC{'DBICTest/HelperRels.pm'}) {
35 $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
37 $artist->create_related( 'cds', {
43 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
45 my( $rs_from_list ) = $artist->search_related_rs('cds');
46 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
48 ( $rs_from_list ) = $artist->cds_rs();
49 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
52 is( $artist->count_related('cds'), 4, 'count_related ok' );
55 my $track = $schema->resultset("Track")->create( {
59 title => 'Hidden Track'
61 $track->set_from_related( cd => $cd );
63 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
65 $track->set_from_related( cd => undef );
67 ok( !defined($track->cd), 'set_from_related with undef ok');
70 local $TODO = 'accessing $object->rel and set_from_related';
71 my $track = $schema->resultset("Track")->new( {} );
73 $track->set_from_related( cd => $cd );
74 ok ($track->cd, 'set_from_related ok after using the accessor' );
77 # update_from_related, the same as set_from_related, but it calls update afterwards
78 $track = $schema->resultset("Track")->create( {
82 title => 'Hidden Track 2'
84 $track->update_from_related( cd => $cd );
86 my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd;
88 is( $t_cd->cdid, 4, 'update_from_related ok' );
90 # find_or_create_related with an existing record
91 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
92 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
94 # find_or_create_related creating a new record
95 $cd = $artist->find_or_create_related( 'cds', {
96 title => 'Greatest Hits',
99 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
101 @cds = $artist->search_related('cds');
102 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
104 $artist->delete_related( cds => { title => 'Greatest Hits' });
105 cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
107 # find_or_new_related with an existing record
108 $cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } );
109 is( $cd->year, 2005, 'find_or_new_related on existing record ok' );
110 ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' );
112 # find_or_new_related instantiating a new record
113 $cd = $artist->find_or_new_related( 'cds', {
114 title => 'Greatest Hits 2: Louder Than Ever',
117 is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' );
118 ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' );
121 my $newartist = $cd->find_or_new_related( 'artist', {
122 name => 'Random Boy Band Two',
125 is($newartist->name, 'Random Boy Band Two', 'find_or_new_related new artist record with id');
126 is($newartist->id, 200, 'find_or_new_related new artist id set');
129 local $TODO = "relationship checking needs fixing";
130 # try to add a bogus relationship using the wrong cols
132 DBICTest::Schema::Artist->add_relationship(
133 tracks => 'DBICTest::Schema::Track',
134 { 'foreign.cd' => 'self.cdid' }
137 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
140 # another bogus relationship using no join condition
142 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
144 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
146 # many_to_many helper tests
147 $cd = $schema->resultset("CD")->find(1);
148 my @producers = $cd->producers();
149 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
150 is( $cd->producers_sorted->next->name, 'Bob The Builder',
151 'sorted many_to_many ok' );
152 is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype',
153 'sorted many_to_many with search condition ok' );
155 $cd = $schema->resultset('CD')->find(2);
156 my $prod_rs = $cd->producers();
157 my $prod_before_count = $schema->resultset('Producer')->count;
158 is( $prod_rs->count, 0, "CD doesn't yet have any producers" );
159 my $prod = $schema->resultset('Producer')->find(1);
160 $cd->add_to_producers($prod);
161 is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' );
162 is( $prod_rs->first->name, 'Matt S Trout',
163 'many_to_many add_to_$rel($obj) ok' );
164 $cd->remove_from_producers($prod);
165 is( $schema->resultset('Producer')->find(1)->name, 'Matt S Trout',
166 "producer object exists after remove of link" );
167 is( $prod_rs->count, 0, 'many_to_many remove_from_$rel($obj) ok' );
168 $cd->add_to_producers({ name => 'Testy McProducer' });
169 is( $schema->resultset('Producer')->count, $prod_before_count+1,
170 'add_to_$rel($hash) inserted a new producer' );
171 is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($hash) count ok' );
172 is( $prod_rs->first->name, 'Testy McProducer',
173 'many_to_many add_to_$rel($hash) ok' );
174 $cd->add_to_producers({ name => 'Jack Black' });
175 is( $prod_rs->count(), 2, 'many_to_many add_to_$rel($hash) count ok' );
176 $cd->set_producers($schema->resultset('Producer')->all);
177 is( $cd->producers->count(), $prod_before_count+2,
178 'many_to_many set_$rel(@objs) count ok' );
179 $cd->set_producers($schema->resultset('Producer')->find(1));
180 is( $cd->producers->count(), 1, 'many_to_many set_$rel($obj) count ok' );
181 $cd->set_producers([$schema->resultset('Producer')->all]);
182 is( $cd->producers->count(), $prod_before_count+2,
183 'many_to_many set_$rel(\@objs) count ok' );
184 $cd->set_producers([$schema->resultset('Producer')->find(1)]);
185 is( $cd->producers->count(), 1, 'many_to_many set_$rel([$obj]) count ok' );
187 eval { $cd->remove_from_producers({ fake => 'hash' }); };
188 like( $@, qr/needs an object/, 'remove_from_$rel($hash) dies correctly' );
190 eval { $cd->add_to_producers(); };
191 like( $@, qr/needs an object or hashref/,
192 'add_to_$rel(undef) dies correctly' );
194 # many_to_many stresstest
195 my $twokey = $schema->resultset('TwoKeys')->find(1,1);
196 my $fourkey = $schema->resultset('FourKeys')->find(1,2,3,4);
198 is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
199 $twokey->add_to_fourkeys($fourkey, { autopilot => 'engaged' });
200 my $got_fourkey = $twokey->fourkeys({ sensors => 'online' })->first;
201 is( $twokey->fourkeys->count, 1, 'twokey has one fourkey' );
202 is( $got_fourkey->$_, $fourkey->$_,
203 'fourkeys row has the correct value for column '.$_ )
204 for (qw(foo bar hello goodbye sensors));
205 $twokey->remove_from_fourkeys($fourkey);
206 is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
207 is( $twokey->fourkeys_to_twokeys->count, 0,
208 'twokey has no links to fourkey' );
210 my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 });
211 is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded');
212 is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db');
214 $undef_artist_cd->related_resultset('artist')->new({name => 'foo'});
216 is( $@, '', "Object created on a resultset related to not yet inserted object");
218 my $def_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007, artist => undef });
219 is($def_artist_cd->has_column_loaded('artist'), 1, 'FK loaded');
220 is($def_artist_cd->search_related('artist')->count, 0, 'closed search on null FK');
222 # test undirected many-to-many relationship (e.g. "related artists")
223 my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
224 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
226 $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
227 is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
229 my $mapped_rs = $undir_maps->search_related('mapped_artists');
231 my @art = $mapped_rs->all;
233 cmp_ok(@art, '==', 2, "Both artist returned from map");
235 my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}});
237 cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition");
239 # check join through cascaded has_many relationships
240 $artist = $schema->resultset("Artist")->find(1);
241 my $trackset = $artist->cds->search_related('tracks');
242 # LEFT join means we also see the trackless additional album...
243 cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist");
245 # now see about updating eveything that belongs to artist 2 to artist 3
246 $artist = $schema->resultset("Artist")->find(2);
247 my $nartist = $schema->resultset("Artist")->find(3);
248 cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist");
249 cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist");
250 $artist->cds->update({artist => $nartist->id});
251 cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
252 cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");
254 my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode' });
255 my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave in Silence', 'year' => 1982});
258 $new_related_cd->insert;
260 is ($@, '', 'Staged insertion successful');
261 ok($new_artist->in_storage, 'artist inserted');
262 ok($new_related_cd->in_storage, 'new_related_cd inserted');
264 # check if is_foreign_key_constraint attr is set
265 my $rs_normal = $schema->source('Track');
266 my $relinfo = $rs_normal->relationship_info ('cd');
267 cmp_ok($relinfo->{attrs}{is_foreign_key_constraint}, '==', 1, "is_foreign_key_constraint defined for belongs_to relationships.");
269 my $rs_overridden = $schema->source('ForceForeign');
270 my $relinfo_with_attr = $rs_overridden->relationship_info ('cd_3');
271 cmp_ok($relinfo_with_attr->{attrs}{is_foreign_key_constraint}, '==', 0, "is_foreign_key_constraint defined for belongs_to relationships with attr.");