New SQLA::Test handles () differences *much* better, thus fixing the tests
[dbsrgits/DBIx-Class-Historic.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
c49fcf72 11plan tests => 71;
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 {
c49fcf72 38 my $big_flop = $artist->create_related( 'cds', {
07037f89 39 title => 'Big Flop',
40 year => 2005,
41 } );
c49fcf72 42 lives_ok { $big_flop->genre} "Don't throw exception when col is not loaded after insert";
07037f89 43}
44
c89815db 45my $big_flop_cd = ($artist->search_related('cds'))[3];
46is( $big_flop_cd->title, 'Big Flop', 'create_related ok' );
47
48{ # make sure we are not making pointless select queries when a FK IS NULL
49 my $queries = 0;
50 $schema->storage->debugcb(sub { $queries++; });
51 $schema->storage->debug(1);
52 $big_flop_cd->genre; #should not trigger a select query
cef1bdda 53 is($queries, 0, 'No SELECT made for belongs_to if key IS NULL');
54 $big_flop_cd->genre_inefficient; #should trigger a select query
55 is($queries, 1, 'SELECT made for belongs_to if key IS NULL when undef_on_null_fk disabled');
c89815db 56 $schema->storage->debug(0);
57 $schema->storage->debugcb(undef);
58}
0567538f 59
5b89a768 60my( $rs_from_list ) = $artist->search_related_rs('cds');
61is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
62
63( $rs_from_list ) = $artist->cds_rs();
64is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
65
0567538f 66# count_related
67is( $artist->count_related('cds'), 4, 'count_related ok' );
68
69# set_from_related
f9db5527 70my $track = $schema->resultset("Track")->create( {
0567538f 71 trackid => 1,
72 cd => 3,
73 position => 98,
74 title => 'Hidden Track'
75} );
76$track->set_from_related( cd => $cd );
77
70350518 78is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
0567538f 79
2c037e6b 80$track->set_from_related( cd => undef );
81
82ok( !defined($track->cd), 'set_from_related with undef ok');
83
f77bad73 84TODO: {
85 local $TODO = 'accessing $object->rel and set_from_related';
86 my $track = $schema->resultset("Track")->new( {} );
87 $track->cd;
88 $track->set_from_related( cd => $cd );
89 ok ($track->cd, 'set_from_related ok after using the accessor' );
90};
2c037e6b 91
0567538f 92# update_from_related, the same as set_from_related, but it calls update afterwards
f9db5527 93$track = $schema->resultset("Track")->create( {
0567538f 94 trackid => 2,
95 cd => 3,
365d06b7 96 title => 'Hidden Track 2'
0567538f 97} );
98$track->update_from_related( cd => $cd );
99
d6df786a 100my $t_cd = ($schema->resultset("Track")->search( cd => 4, title => 'Hidden Track 2' ))[0]->cd;
0567538f 101
70350518 102is( $t_cd->cdid, 4, 'update_from_related ok' );
0567538f 103
104# find_or_create_related with an existing record
105$cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
106is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
107
108# find_or_create_related creating a new record
109$cd = $artist->find_or_create_related( 'cds', {
110 title => 'Greatest Hits',
111 year => 2006,
112} );
113is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
e02b9964 114
0567538f 115@cds = $artist->search_related('cds');
116is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
117
87772e46 118$artist->delete_related( cds => { title => 'Greatest Hits' });
f9db5527 119cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
0567538f 120
b3e1f1f5 121# find_or_new_related with an existing record
122$cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } );
123is( $cd->year, 2005, 'find_or_new_related on existing record ok' );
124ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' );
125
126# find_or_new_related instantiating a new record
127$cd = $artist->find_or_new_related( 'cds', {
128 title => 'Greatest Hits 2: Louder Than Ever',
129 year => 2007,
130} );
131is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' );
132ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' );
133
e02b9964 134$cd->artist(undef);
135my $newartist = $cd->find_or_new_related( 'artist', {
136 name => 'Random Boy Band Two',
137 artistid => 200,
138} );
e02b9964 139is($newartist->name, 'Random Boy Band Two', 'find_or_new_related new artist record with id');
140is($newartist->id, 200, 'find_or_new_related new artist id set');
141
f623c6ee 142lives_ok(
143 sub {
144 my $new_bookmark = $schema->resultset("Bookmark")->new_result( {} );
145 my $new_related_link = $new_bookmark->new_related( 'link', {} );
146 },
147 'No back rel'
148);
149
150
a0dd8679 151TODO: {
152 local $TODO = "relationship checking needs fixing";
87772e46 153 # try to add a bogus relationship using the wrong cols
154 eval {
155 DBICTest::Schema::Artist->add_relationship(
156 tracks => 'DBICTest::Schema::Track',
157 { 'foreign.cd' => 'self.cdid' }
158 );
159 };
160 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
161}
162
0567538f 163# another bogus relationship using no join condition
164eval {
3712e4f4 165 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
0567538f 166};
167like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
168
b3f358b5 169# many_to_many helper tests
f9db5527 170$cd = $schema->resultset("CD")->find(1);
7411204b 171my @producers = $cd->producers();
172is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
b3f358b5 173is( $cd->producers_sorted->next->name, 'Bob The Builder',
174 'sorted many_to_many ok' );
175is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype',
176 'sorted many_to_many with search condition ok' );
7411204b 177
3a868fb2 178$cd = $schema->resultset('CD')->find(2);
b3f358b5 179my $prod_rs = $cd->producers();
180my $prod_before_count = $schema->resultset('Producer')->count;
181is( $prod_rs->count, 0, "CD doesn't yet have any producers" );
3a868fb2 182my $prod = $schema->resultset('Producer')->find(1);
183$cd->add_to_producers($prod);
3a868fb2 184is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' );
b3f358b5 185is( $prod_rs->first->name, 'Matt S Trout',
186 'many_to_many add_to_$rel($obj) ok' );
3a868fb2 187$cd->remove_from_producers($prod);
b3f358b5 188is( $schema->resultset('Producer')->find(1)->name, 'Matt S Trout',
189 "producer object exists after remove of link" );
190is( $prod_rs->count, 0, 'many_to_many remove_from_$rel($obj) ok' );
303cf522 191$cd->add_to_producers({ name => 'Testy McProducer' });
b3f358b5 192is( $schema->resultset('Producer')->count, $prod_before_count+1,
193 'add_to_$rel($hash) inserted a new producer' );
303cf522 194is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($hash) count ok' );
b3f358b5 195is( $prod_rs->first->name, 'Testy McProducer',
196 'many_to_many add_to_$rel($hash) ok' );
197$cd->add_to_producers({ name => 'Jack Black' });
198is( $prod_rs->count(), 2, 'many_to_many add_to_$rel($hash) count ok' );
199$cd->set_producers($schema->resultset('Producer')->all);
200is( $cd->producers->count(), $prod_before_count+2,
201 'many_to_many set_$rel(@objs) count ok' );
202$cd->set_producers($schema->resultset('Producer')->find(1));
203is( $cd->producers->count(), 1, 'many_to_many set_$rel($obj) count ok' );
21c45f7b 204$cd->set_producers([$schema->resultset('Producer')->all]);
205is( $cd->producers->count(), $prod_before_count+2,
206 'many_to_many set_$rel(\@objs) count ok' );
207$cd->set_producers([$schema->resultset('Producer')->find(1)]);
208is( $cd->producers->count(), 1, 'many_to_many set_$rel([$obj]) count ok' );
303cf522 209
210eval { $cd->remove_from_producers({ fake => 'hash' }); };
211like( $@, qr/needs an object/, 'remove_from_$rel($hash) dies correctly' );
212
213eval { $cd->add_to_producers(); };
b3f358b5 214like( $@, qr/needs an object or hashref/,
215 'add_to_$rel(undef) dies correctly' );
303cf522 216
3bd6e3e0 217# many_to_many stresstest
218my $twokey = $schema->resultset('TwoKeys')->find(1,1);
219my $fourkey = $schema->resultset('FourKeys')->find(1,2,3,4);
220
221is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
222$twokey->add_to_fourkeys($fourkey, { autopilot => 'engaged' });
223my $got_fourkey = $twokey->fourkeys({ sensors => 'online' })->first;
224is( $twokey->fourkeys->count, 1, 'twokey has one fourkey' );
225is( $got_fourkey->$_, $fourkey->$_,
226 'fourkeys row has the correct value for column '.$_ )
227 for (qw(foo bar hello goodbye sensors));
228$twokey->remove_from_fourkeys($fourkey);
229is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' );
b3f358b5 230is( $twokey->fourkeys_to_twokeys->count, 0,
231 'twokey has no links to fourkey' );
232
ac8a5ba4 233my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 });
234is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded');
370f2ba2 235is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db');
2c5c07ec 236eval{
237 $undef_artist_cd->related_resultset('artist')->new({name => 'foo'});
238};
239is( $@, '', "Object created on a resultset related to not yet inserted object");
981299aa 240lives_ok{
241 $schema->resultset('Artwork')->new_result({})->cd;
242} 'undef_on_null_fk does not choke on empty conds';
243
ac8a5ba4 244my $def_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007, artist => undef });
245is($def_artist_cd->has_column_loaded('artist'), 1, 'FK loaded');
246is($def_artist_cd->search_related('artist')->count, 0, 'closed search on null FK');
3a868fb2 247
5efe4c79 248# test undirected many-to-many relationship (e.g. "related artists")
f9db5527 249my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
5efe4c79 250is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
251
f9db5527 252$undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
5efe4c79 253is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
254
ad3d2d7c 255my $mapped_rs = $undir_maps->search_related('mapped_artists');
256
257my @art = $mapped_rs->all;
5efe4c79 258
259cmp_ok(@art, '==', 2, "Both artist returned from map");
260
ad3d2d7c 261my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}});
262
263cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition");
264
b8d4bd90 265# check join through cascaded has_many relationships
266$artist = $schema->resultset("Artist")->find(1);
267my $trackset = $artist->cds->search_related('tracks');
268# LEFT join means we also see the trackless additional album...
269cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist");
ad3d2d7c 270
0f57d214 271# now see about updating eveything that belongs to artist 2 to artist 3
272$artist = $schema->resultset("Artist")->find(2);
273my $nartist = $schema->resultset("Artist")->find(3);
274cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist");
275cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist");
276$artist->cds->update({artist => $nartist->id});
277cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
278cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");
279
6bf6ba2f 280# check if is_foreign_key_constraint attr is set
281my $rs_normal = $schema->source('Track');
282my $relinfo = $rs_normal->relationship_info ('cd');
283cmp_ok($relinfo->{attrs}{is_foreign_key_constraint}, '==', 1, "is_foreign_key_constraint defined for belongs_to relationships.");
284
285my $rs_overridden = $schema->source('ForceForeign');
286my $relinfo_with_attr = $rs_overridden->relationship_info ('cd_3');
287cmp_ok($relinfo_with_attr->{attrs}{is_foreign_key_constraint}, '==', 0, "is_foreign_key_constraint defined for belongs_to relationships with attr.");