Add search_rs to ResultSet and a new {$rel}_rs accessor to has_many.
[dbsrgits/DBIx-Class.git] / t / run / 06relationship.tl
CommitLineData
0567538f 1sub run_tests {
1edaf6fe 2my $schema = shift;
5efe4c79 3
4use strict;
5use warnings;
5b89a768 6plan tests => 32;
0567538f 7
8# has_a test
f9db5527 9my $cd = $schema->resultset("CD")->find(4);
07037f89 10my ($artist) = ($INC{'DBICTest/HelperRels'}
11 ? $cd->artist
12 : $cd->search_related('artist'));
0567538f 13is($artist->name, 'Random Boy Band', 'has_a search_related ok');
14
15# has_many test with an order_by clause defined
f9db5527 16$artist = $schema->resultset("Artist")->find(1);
07037f89 17my @cds = ($INC{'DBICTest/HelperRels'}
18 ? $artist->cds
19 : $artist->search_related('cds'));
20is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
0567538f 21
22# search_related with additional abstract query
07037f89 23@cds = ($INC{'DBICTest/HelperRels'}
24 ? $artist->cds({ title => { like => '%of%' } })
25 : $artist->search_related('cds', { title => { like => '%of%' } } )
26 );
0567538f 27is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
28
29# creating a related object
07037f89 30if ($INC{'DBICTest/HelperRels.pm'}) {
31 $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
32} else {
33 $artist->create_related( 'cds', {
34 title => 'Big Flop',
35 year => 2005,
36 } );
37}
38
0567538f 39is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
40
5b89a768 41my( $rs_from_list ) = $artist->search_related_rs('cds');
42is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
43
44( $rs_from_list ) = $artist->cds_rs();
45is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
46
0567538f 47# count_related
48is( $artist->count_related('cds'), 4, 'count_related ok' );
49
50# set_from_related
f9db5527 51my $track = $schema->resultset("Track")->create( {
0567538f 52 trackid => 1,
53 cd => 3,
54 position => 98,
55 title => 'Hidden Track'
56} );
57$track->set_from_related( cd => $cd );
58
2c037e6b 59if ($INC{'DBICTest/HelperRels.pm'}) { # expect inflated object
1e3bc087 60 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
0567538f 61} else {
62 is( $track->cd, 4, 'set_from_related ok' );
63}
64
2c037e6b 65$track->set_from_related( cd => undef );
66
67ok( !defined($track->cd), 'set_from_related with undef ok');
68
69
0567538f 70# update_from_related, the same as set_from_related, but it calls update afterwards
f9db5527 71$track = $schema->resultset("Track")->create( {
0567538f 72 trackid => 2,
73 cd => 3,
74 position => 99,
75 title => 'Hidden Track'
76} );
77$track->update_from_related( cd => $cd );
78
f9db5527 79my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd;
0567538f 80
81if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
82 is( $t_cd->cdid, 4, 'update_from_related ok' );
83} else {
84 is( $t_cd, 4, 'update_from_related ok' );
85}
86
87# find_or_create_related with an existing record
88$cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
89is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
90
91# find_or_create_related creating a new record
92$cd = $artist->find_or_create_related( 'cds', {
93 title => 'Greatest Hits',
94 year => 2006,
95} );
96is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
97@cds = $artist->search_related('cds');
98is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
99
87772e46 100$artist->delete_related( cds => { title => 'Greatest Hits' });
f9db5527 101cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
0567538f 102
b3e1f1f5 103# find_or_new_related with an existing record
104$cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } );
105is( $cd->year, 2005, 'find_or_new_related on existing record ok' );
106ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' );
107
108# find_or_new_related instantiating a new record
109$cd = $artist->find_or_new_related( 'cds', {
110 title => 'Greatest Hits 2: Louder Than Ever',
111 year => 2007,
112} );
113is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' );
114ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' );
115
87772e46 116SKIP: {
117 skip "relationship checking needs fixing", 1;
118 # try to add a bogus relationship using the wrong cols
119 eval {
120 DBICTest::Schema::Artist->add_relationship(
121 tracks => 'DBICTest::Schema::Track',
122 { 'foreign.cd' => 'self.cdid' }
123 );
124 };
125 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
126}
127
0567538f 128# another bogus relationship using no join condition
129eval {
3712e4f4 130 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
0567538f 131};
132like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
133
7411204b 134# many_to_many helper test
f9db5527 135$cd = $schema->resultset("CD")->find(1);
7411204b 136my @producers = $cd->producers();
137is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
0f6ac8bb 138is( $cd->producers_sorted->next->name, 'Bob The Builder', 'sorted many_to_many ok' );
139is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype', 'sorted many_to_many with search condition ok' );
7411204b 140
5efe4c79 141# test undirected many-to-many relationship (e.g. "related artists")
f9db5527 142my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
5efe4c79 143is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
144
f9db5527 145$undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
5efe4c79 146is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
147
ad3d2d7c 148my $mapped_rs = $undir_maps->search_related('mapped_artists');
149
150my @art = $mapped_rs->all;
5efe4c79 151
152cmp_ok(@art, '==', 2, "Both artist returned from map");
153
ad3d2d7c 154my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}});
155
156cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition");
157
b8d4bd90 158# check join through cascaded has_many relationships
159$artist = $schema->resultset("Artist")->find(1);
160my $trackset = $artist->cds->search_related('tracks');
161# LEFT join means we also see the trackless additional album...
162cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist");
ad3d2d7c 163
0f57d214 164# now see about updating eveything that belongs to artist 2 to artist 3
165$artist = $schema->resultset("Artist")->find(2);
166my $nartist = $schema->resultset("Artist")->find(3);
167cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist");
168cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist");
169$artist->cds->update({artist => $nartist->id});
170cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
171cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");
172
0567538f 173}
174
1751;