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