Trunk passes tests again - todoify everything multicreate related to branch it out...
[dbsrgits/DBIx-Class.git] / t / 66relationship.t
CommitLineData
70350518 1use strict;
2f926160 2use warnings;
70350518 3
4b8dcc58 4use Test::More;
2f926160 5use Test::Exception;
4b8dcc58 6use lib qw(t/lib);
7use DBICTest;
8
a47e1233 9my $schema = DBICTest->init_schema();
5efe4c79 10
2f926160 11plan tests => 72;
0567538f 12
13# has_a test
f9db5527 14my $cd = $schema->resultset("CD")->find(4);
07037f89 15my ($artist) = ($INC{'DBICTest/HelperRels'}
16 ? $cd->artist
17 : $cd->search_related('artist'));
0567538f 18is($artist->name, 'Random Boy Band', 'has_a search_related ok');
19
20# has_many test with an order_by clause defined
f9db5527 21$artist = $schema->resultset("Artist")->find(1);
07037f89 22my @cds = ($INC{'DBICTest/HelperRels'}
23 ? $artist->cds
24 : $artist->search_related('cds'));
25is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
0567538f 26
27# search_related with additional abstract query
07037f89 28@cds = ($INC{'DBICTest/HelperRels'}
29 ? $artist->cds({ title => { like => '%of%' } })
30 : $artist->search_related('cds', { title => { like => '%of%' } } )
31 );
0567538f 32is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
33
34# creating a related object
07037f89 35if ($INC{'DBICTest/HelperRels.pm'}) {
36 $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
37} else {
38 $artist->create_related( 'cds', {
39 title => 'Big Flop',
40 year => 2005,
41 } );
42}
43
0567538f 44is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
45
5b89a768 46my( $rs_from_list ) = $artist->search_related_rs('cds');
47is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
48
49( $rs_from_list ) = $artist->cds_rs();
50is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
51
0567538f 52# count_related
53is( $artist->count_related('cds'), 4, 'count_related ok' );
54
55# set_from_related
f9db5527 56my $track = $schema->resultset("Track")->create( {
0567538f 57 trackid => 1,
58 cd => 3,
59 position => 98,
60 title => 'Hidden Track'
61} );
62$track->set_from_related( cd => $cd );
63
70350518 64is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
0567538f 65
2c037e6b 66$track->set_from_related( cd => undef );
67
68ok( !defined($track->cd), 'set_from_related with undef ok');
69
f77bad73 70TODO: {
71 local $TODO = 'accessing $object->rel and set_from_related';
72 my $track = $schema->resultset("Track")->new( {} );
73 $track->cd;
74 $track->set_from_related( cd => $cd );
75 ok ($track->cd, 'set_from_related ok after using the accessor' );
76};
2c037e6b 77
0567538f 78# update_from_related, the same as set_from_related, but it calls update afterwards
f9db5527 79$track = $schema->resultset("Track")->create( {
0567538f 80 trackid => 2,
81 cd => 3,
82 position => 99,
365d06b7 83 title => 'Hidden Track 2'
0567538f 84} );
85$track->update_from_related( cd => $cd );
86
f9db5527 87my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd;
0567538f 88
70350518 89is( $t_cd->cdid, 4, 'update_from_related ok' );
0567538f 90
91# find_or_create_related with an existing record
92$cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
93is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
94
95# find_or_create_related creating a new record
96$cd = $artist->find_or_create_related( 'cds', {
97 title => 'Greatest Hits',
98 year => 2006,
99} );
100is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
e02b9964 101
0567538f 102@cds = $artist->search_related('cds');
103is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
104
87772e46 105$artist->delete_related( cds => { title => 'Greatest Hits' });
f9db5527 106cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
0567538f 107
b3e1f1f5 108# find_or_new_related with an existing record
109$cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } );
110is( $cd->year, 2005, 'find_or_new_related on existing record ok' );
111ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' );
112
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',
116 year => 2007,
117} );
118is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' );
119ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' );
120
e02b9964 121$cd->artist(undef);
122my $newartist = $cd->find_or_new_related( 'artist', {
123 name => 'Random Boy Band Two',
124 artistid => 200,
125} );
e02b9964 126is($newartist->name, 'Random Boy Band Two', 'find_or_new_related new artist record with id');
127is($newartist->id, 200, 'find_or_new_related new artist id set');
128
a0dd8679 129TODO: {
130 local $TODO = "relationship checking needs fixing";
87772e46 131 # try to add a bogus relationship using the wrong cols
132 eval {
133 DBICTest::Schema::Artist->add_relationship(
134 tracks => 'DBICTest::Schema::Track',
135 { 'foreign.cd' => 'self.cdid' }
136 );
137 };
138 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
139}
140
0567538f 141# another bogus relationship using no join condition
142eval {
3712e4f4 143 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
0567538f 144};
145like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
146
b3f358b5 147# many_to_many helper tests
f9db5527 148$cd = $schema->resultset("CD")->find(1);
7411204b 149my @producers = $cd->producers();
150is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
b3f358b5 151is( $cd->producers_sorted->next->name, 'Bob The Builder',
152 'sorted many_to_many ok' );
153is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype',
154 'sorted many_to_many with search condition ok' );
7411204b 155
3a868fb2 156$cd = $schema->resultset('CD')->find(2);
b3f358b5 157my $prod_rs = $cd->producers();
158my $prod_before_count = $schema->resultset('Producer')->count;
159is( $prod_rs->count, 0, "CD doesn't yet have any producers" );
3a868fb2 160my $prod = $schema->resultset('Producer')->find(1);
161$cd->add_to_producers($prod);
3a868fb2 162is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' );
b3f358b5 163is( $prod_rs->first->name, 'Matt S Trout',
164 'many_to_many add_to_$rel($obj) ok' );
3a868fb2 165$cd->remove_from_producers($prod);
b3f358b5 166is( $schema->resultset('Producer')->find(1)->name, 'Matt S Trout',
167 "producer object exists after remove of link" );
168is( $prod_rs->count, 0, 'many_to_many remove_from_$rel($obj) ok' );
303cf522 169$cd->add_to_producers({ name => 'Testy McProducer' });
b3f358b5 170is( $schema->resultset('Producer')->count, $prod_before_count+1,
171 'add_to_$rel($hash) inserted a new producer' );
303cf522 172is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($hash) count ok' );
b3f358b5 173is( $prod_rs->first->name, 'Testy McProducer',
174 'many_to_many add_to_$rel($hash) ok' );
175$cd->add_to_producers({ name => 'Jack Black' });
176is( $prod_rs->count(), 2, 'many_to_many add_to_$rel($hash) count ok' );
177$cd->set_producers($schema->resultset('Producer')->all);
178is( $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));
181is( $cd->producers->count(), 1, 'many_to_many set_$rel($obj) count ok' );
21c45f7b 182$cd->set_producers([$schema->resultset('Producer')->all]);
183is( $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)]);
186is( $cd->producers->count(), 1, 'many_to_many set_$rel([$obj]) count ok' );
303cf522 187
188eval { $cd->remove_from_producers({ fake => 'hash' }); };
189like( $@, qr/needs an object/, 'remove_from_$rel($hash) dies correctly' );
190
191eval { $cd->add_to_producers(); };
b3f358b5 192like( $@, qr/needs an object or hashref/,
193 'add_to_$rel(undef) dies correctly' );
303cf522 194
3bd6e3e0 195# many_to_many stresstest
196my $twokey = $schema->resultset('TwoKeys')->find(1,1);
197my $fourkey = $schema->resultset('FourKeys')->find(1,2,3,4);
198
199is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
200$twokey->add_to_fourkeys($fourkey, { autopilot => 'engaged' });
201my $got_fourkey = $twokey->fourkeys({ sensors => 'online' })->first;
202is( $twokey->fourkeys->count, 1, 'twokey has one fourkey' );
203is( $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);
207is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
b3f358b5 208is( $twokey->fourkeys_to_twokeys->count, 0,
209 'twokey has no links to fourkey' );
210
ac8a5ba4 211my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 });
212is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded');
370f2ba2 213is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db');
2c5c07ec 214eval{
215 $undef_artist_cd->related_resultset('artist')->new({name => 'foo'});
216};
217is( $@, '', "Object created on a resultset related to not yet inserted object");
218
ac8a5ba4 219my $def_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007, artist => undef });
220is($def_artist_cd->has_column_loaded('artist'), 1, 'FK loaded');
221is($def_artist_cd->search_related('artist')->count, 0, 'closed search on null FK');
3a868fb2 222
5efe4c79 223# test undirected many-to-many relationship (e.g. "related artists")
f9db5527 224my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
5efe4c79 225is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
226
f9db5527 227$undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
5efe4c79 228is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
229
ad3d2d7c 230my $mapped_rs = $undir_maps->search_related('mapped_artists');
231
232my @art = $mapped_rs->all;
5efe4c79 233
234cmp_ok(@art, '==', 2, "Both artist returned from map");
235
ad3d2d7c 236my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}});
237
238cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition");
239
b8d4bd90 240# check join through cascaded has_many relationships
241$artist = $schema->resultset("Artist")->find(1);
242my $trackset = $artist->cds->search_related('tracks');
243# LEFT join means we also see the trackless additional album...
244cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist");
ad3d2d7c 245
0f57d214 246# now see about updating eveything that belongs to artist 2 to artist 3
247$artist = $schema->resultset("Artist")->find(2);
248my $nartist = $schema->resultset("Artist")->find(3);
249cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist");
250cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist");
251$artist->cds->update({artist => $nartist->id});
252cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
253cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");
254
8c368cf3 255my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode' });
8c368cf3 256my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave in Silence', 'year' => 1982});
257eval {
258 $new_artist->insert;
259 $new_related_cd->insert;
260};
4295b5d4 261is ($@, '', 'Staged insertion successful');
262ok($new_artist->in_storage, 'artist inserted');
263ok($new_related_cd->in_storage, 'new_related_cd inserted');
6bf6ba2f 264
a7be8807 265TODO: {
266local $TODO = "TODOify for multicreate branch";
2f926160 267my $new_cd = $schema->resultset("CD")->new_result({});
268my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',});
269lives_ok (
270 sub {
271 $new_related_artist->insert;
272 $new_cd->title( 'Misplaced Childhood' );
273 $new_cd->year ( 1985 );
274# $new_cd->artist( $new_related_artist ); # For exact backward compatibility # not sure what this means
275 $new_cd->insert;
276 },
277 'Reversed staged insertion successful'
278);
279ok($new_related_artist->in_storage, 'related artist inserted');
280ok($new_cd->in_storage, 'cd inserted');
281
6bf6ba2f 282# check if is_foreign_key_constraint attr is set
283my $rs_normal = $schema->source('Track');
284my $relinfo = $rs_normal->relationship_info ('cd');
285cmp_ok($relinfo->{attrs}{is_foreign_key_constraint}, '==', 1, "is_foreign_key_constraint defined for belongs_to relationships.");
286
287my $rs_overridden = $schema->source('ForceForeign');
288my $relinfo_with_attr = $rs_overridden->relationship_info ('cd_3');
289cmp_ok($relinfo_with_attr->{attrs}{is_foreign_key_constraint}, '==', 0, "is_foreign_key_constraint defined for belongs_to relationships with attr.");
a7be8807 290}