8 my $cd = DBICTest->class("CD")->find(4);
9 my ($artist) = ($INC{'DBICTest/HelperRels'}
11 : $cd->search_related('artist'));
12 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
14 # has_many test with an order_by clause defined
15 $artist = DBICTest->class("Artist")->find(1);
16 my @cds = ($INC{'DBICTest/HelperRels'}
18 : $artist->search_related('cds'));
19 is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
21 # search_related with additional abstract query
22 @cds = ($INC{'DBICTest/HelperRels'}
23 ? $artist->cds({ title => { like => '%of%' } })
24 : $artist->search_related('cds', { title => { like => '%of%' } } )
26 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
28 # creating a related object
29 if ($INC{'DBICTest/HelperRels.pm'}) {
30 $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
32 $artist->create_related( 'cds', {
38 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
41 is( $artist->count_related('cds'), 4, 'count_related ok' );
44 my $track = DBICTest->class("Track")->create( {
48 title => 'Hidden Track'
50 $track->set_from_related( cd => $cd );
52 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
53 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
55 is( $track->cd, 4, 'set_from_related ok' );
58 # update_from_related, the same as set_from_related, but it calls update afterwards
59 $track = DBICTest->class("Track")->create( {
63 title => 'Hidden Track'
65 $track->update_from_related( cd => $cd );
67 my $t_cd = (DBICTest->class("Track")->search( cd => 4, position => 99 ))[0]->cd;
69 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
70 is( $t_cd->cdid, 4, 'update_from_related ok' );
72 is( $t_cd, 4, 'update_from_related ok' );
75 # find_or_create_related with an existing record
76 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
77 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
79 # find_or_create_related creating a new record
80 $cd = $artist->find_or_create_related( 'cds', {
81 title => 'Greatest Hits',
84 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
85 @cds = $artist->search_related('cds');
86 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
88 $artist->delete_related( cds => { title => 'Greatest Hits' });
89 cmp_ok( DBICTest->class("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
92 skip "relationship checking needs fixing", 1;
93 # try to add a bogus relationship using the wrong cols
95 DBICTest::Schema::Artist->add_relationship(
96 tracks => 'DBICTest::Schema::Track',
97 { 'foreign.cd' => 'self.cdid' }
100 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
103 # another bogus relationship using no join condition
105 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
107 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
109 # many_to_many helper test
110 $cd = DBICTest->class("CD")->find(1);
111 my @producers = $cd->producers();
112 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
114 # test undirected many-to-many relationship (e.g. "related artists")
115 my $undir_maps = DBICTest->class("Artist")->find(1)->artist_undirected_maps;
116 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
118 $undir_maps = DBICTest->class("Artist")->find(2)->artist_undirected_maps;
119 is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
121 my @art = $undir_maps->search_related('mapped_artists')->all;
123 cmp_ok(@art, '==', 2, "Both artist returned from map");