9 my $cd = $schema->resultset("CD")->find(4);
10 my ($artist) = ($INC{'DBICTest/HelperRels'}
12 : $cd->search_related('artist'));
13 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
15 # has_many test with an order_by clause defined
16 $artist = $schema->resultset("Artist")->find(1);
17 my @cds = ($INC{'DBICTest/HelperRels'}
19 : $artist->search_related('cds'));
20 is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
22 # search_related with additional abstract query
23 @cds = ($INC{'DBICTest/HelperRels'}
24 ? $artist->cds({ title => { like => '%of%' } })
25 : $artist->search_related('cds', { title => { like => '%of%' } } )
27 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
29 # creating a related object
30 if ($INC{'DBICTest/HelperRels.pm'}) {
31 $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
33 $artist->create_related( 'cds', {
39 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
41 my( $rs_from_list ) = $artist->search_related_rs('cds');
42 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
44 ( $rs_from_list ) = $artist->cds_rs();
45 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
48 is( $artist->count_related('cds'), 4, 'count_related ok' );
51 my $track = $schema->resultset("Track")->create( {
55 title => 'Hidden Track'
57 $track->set_from_related( cd => $cd );
59 if ($INC{'DBICTest/HelperRels.pm'}) { # expect inflated object
60 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
62 is( $track->cd, 4, 'set_from_related ok' );
65 $track->set_from_related( cd => undef );
67 ok( !defined($track->cd), 'set_from_related with undef ok');
70 # update_from_related, the same as set_from_related, but it calls update afterwards
71 $track = $schema->resultset("Track")->create( {
75 title => 'Hidden Track'
77 $track->update_from_related( cd => $cd );
79 my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd;
81 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
82 is( $t_cd->cdid, 4, 'update_from_related ok' );
84 is( $t_cd, 4, 'update_from_related ok' );
87 # find_or_create_related with an existing record
88 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
89 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
91 # find_or_create_related creating a new record
92 $cd = $artist->find_or_create_related( 'cds', {
93 title => 'Greatest Hits',
96 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
97 @cds = $artist->search_related('cds');
98 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
100 $artist->delete_related( cds => { title => 'Greatest Hits' });
101 cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
103 # find_or_new_related with an existing record
104 $cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } );
105 is( $cd->year, 2005, 'find_or_new_related on existing record ok' );
106 ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' );
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',
113 is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' );
114 ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' );
117 skip "relationship checking needs fixing", 1;
118 # try to add a bogus relationship using the wrong cols
120 DBICTest::Schema::Artist->add_relationship(
121 tracks => 'DBICTest::Schema::Track',
122 { 'foreign.cd' => 'self.cdid' }
125 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
128 # another bogus relationship using no join condition
130 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
132 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
134 # many_to_many helper test
135 $cd = $schema->resultset("CD")->find(1);
136 my @producers = $cd->producers();
137 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
138 is( $cd->producers_sorted->next->name, 'Bob The Builder', 'sorted many_to_many ok' );
139 is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype', 'sorted many_to_many with search condition ok' );
141 # test undirected many-to-many relationship (e.g. "related artists")
142 my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
143 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
145 $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
146 is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
148 my $mapped_rs = $undir_maps->search_related('mapped_artists');
150 my @art = $mapped_rs->all;
152 cmp_ok(@art, '==', 2, "Both artist returned from map");
154 my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}});
156 cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition");
158 # check join through cascaded has_many relationships
159 $artist = $schema->resultset("Artist")->find(1);
160 my $trackset = $artist->cds->search_related('tracks');
161 # LEFT join means we also see the trackless additional album...
162 cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist");
164 # now see about updating eveything that belongs to artist 2 to artist 3
165 $artist = $schema->resultset("Artist")->find(2);
166 my $nartist = $schema->resultset("Artist")->find(3);
167 cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist");
168 cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist");
169 $artist->cds->update({artist => $nartist->id});
170 cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
171 cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");