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