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