5 my $schema = DBICTest::init_schema();
12 my $cd = $schema->resultset("CD")->find(4);
13 my ($artist) = ($INC{'DBICTest/HelperRels'}
15 : $cd->search_related('artist'));
16 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
18 # has_many test with an order_by clause defined
19 $artist = $schema->resultset("Artist")->find(1);
20 my @cds = ($INC{'DBICTest/HelperRels'}
22 : $artist->search_related('cds'));
23 is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
25 # search_related with additional abstract query
26 @cds = ($INC{'DBICTest/HelperRels'}
27 ? $artist->cds({ title => { like => '%of%' } })
28 : $artist->search_related('cds', { title => { like => '%of%' } } )
30 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
32 # creating a related object
33 if ($INC{'DBICTest/HelperRels.pm'}) {
34 $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
36 $artist->create_related( 'cds', {
42 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
44 my( $rs_from_list ) = $artist->search_related_rs('cds');
45 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
47 ( $rs_from_list ) = $artist->cds_rs();
48 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
51 is( $artist->count_related('cds'), 4, 'count_related ok' );
54 my $track = $schema->resultset("Track")->create( {
58 title => 'Hidden Track'
60 $track->set_from_related( cd => $cd );
62 if ($INC{'DBICTest/HelperRels.pm'}) { # expect inflated object
63 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
65 is( $track->cd, 4, 'set_from_related ok' );
68 $track->set_from_related( cd => undef );
70 ok( !defined($track->cd), 'set_from_related with undef ok');
73 # update_from_related, the same as set_from_related, but it calls update afterwards
74 $track = $schema->resultset("Track")->create( {
78 title => 'Hidden Track'
80 $track->update_from_related( cd => $cd );
82 my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd;
84 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
85 is( $t_cd->cdid, 4, 'update_from_related ok' );
87 is( $t_cd, 4, 'update_from_related ok' );
90 # find_or_create_related with an existing record
91 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
92 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
94 # find_or_create_related creating a new record
95 $cd = $artist->find_or_create_related( 'cds', {
96 title => 'Greatest Hits',
99 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
100 @cds = $artist->search_related('cds');
101 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
103 $artist->delete_related( cds => { title => 'Greatest Hits' });
104 cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
106 # find_or_new_related with an existing record
107 $cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } );
108 is( $cd->year, 2005, 'find_or_new_related on existing record ok' );
109 ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' );
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',
116 is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' );
117 ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' );
120 skip "relationship checking needs fixing", 1;
121 # try to add a bogus relationship using the wrong cols
123 DBICTest::Schema::Artist->add_relationship(
124 tracks => 'DBICTest::Schema::Track',
125 { 'foreign.cd' => 'self.cdid' }
128 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
131 # another bogus relationship using no join condition
133 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
135 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
137 # many_to_many helper test
138 $cd = $schema->resultset("CD")->find(1);
139 my @producers = $cd->producers();
140 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
141 is( $cd->producers_sorted->next->name, 'Bob The Builder', 'sorted many_to_many ok' );
142 is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype', 'sorted many_to_many with search condition ok' );
144 # test undirected many-to-many relationship (e.g. "related artists")
145 my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
146 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
148 $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
149 is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
151 my $mapped_rs = $undir_maps->search_related('mapped_artists');
153 my @art = $mapped_rs->all;
155 cmp_ok(@art, '==', 2, "Both artist returned from map");
157 my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}});
159 cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition");
161 # check join through cascaded has_many relationships
162 $artist = $schema->resultset("Artist")->find(1);
163 my $trackset = $artist->cds->search_related('tracks');
164 # LEFT join means we also see the trackless additional album...
165 cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist");
167 # now see about updating eveything that belongs to artist 2 to artist 3
168 $artist = $schema->resultset("Artist")->find(2);
169 my $nartist = $schema->resultset("Artist")->find(3);
170 cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist");
171 cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist");
172 $artist->cds->update({artist => $nartist->id});
173 cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
174 cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");