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