6 my $cd = DBICTest::CD->find(4);
7 my ($artist) = ($INC{'DBICTest/HelperRels'}
9 : $cd->search_related('artist'));
10 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
12 # has_many test with an order_by clause defined
13 $artist = DBICTest::Artist->find(1);
14 my @cds = ($INC{'DBICTest/HelperRels'}
16 : $artist->search_related('cds'));
17 is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
19 # search_related with additional abstract query
20 @cds = ($INC{'DBICTest/HelperRels'}
21 ? $artist->cds({ title => { like => '%of%' } })
22 : $artist->search_related('cds', { title => { like => '%of%' } } )
24 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
26 # creating a related object
27 if ($INC{'DBICTest/HelperRels.pm'}) {
28 $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
30 $artist->create_related( 'cds', {
36 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
39 is( $artist->count_related('cds'), 4, 'count_related ok' );
42 my $track = DBICTest::Track->create( {
46 title => 'Hidden Track'
48 $track->set_from_related( cd => $cd );
50 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
51 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
53 is( $track->cd, 4, 'set_from_related ok' );
56 # update_from_related, the same as set_from_related, but it calls update afterwards
57 $track = DBICTest::Track->create( {
61 title => 'Hidden Track'
63 $track->update_from_related( cd => $cd );
65 my $t_cd = (DBICTest::Track->search( cd => 4, position => 99 ))[0]->cd;
67 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
68 is( $t_cd->cdid, 4, 'update_from_related ok' );
70 is( $t_cd, 4, 'update_from_related ok' );
73 # find_or_create_related with an existing record
74 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
75 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
77 # find_or_create_related creating a new record
78 $cd = $artist->find_or_create_related( 'cds', {
79 title => 'Greatest Hits',
82 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
83 @cds = $artist->search_related('cds');
84 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
87 #skip 'Need to add delete_related', 1;
89 $artist->delete_related( cds => { title => 'Greatest Hits' });
90 cmp_ok( DBICTest::CD->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
93 # try to add a bogus relationship using the wrong cols
95 $artist->add_relationship(
96 tracks => 'DBICTest::Track',
97 { 'foreign.cd' => 'self.cdid' }
100 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
102 # another bogus relationship using no join condition
104 $artist->add_relationship( tracks => 'DBICTest::Track' );
106 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');