6 my $cd = DBICTest::CD->find(4);
7 my ($artist) = $cd->search_related('artist');
8 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
10 # has_many test with an order_by clause defined
11 $artist = DBICTest::Artist->find(1);
12 is( ($artist->search_related('cds'))[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
14 # search_related with additional abstract query
15 my @cds = $artist->search_related('cds', { title => { like => '%of%' } } );
16 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
18 # creating a related object
19 $artist->create_related( 'cds', {
23 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
26 is( $artist->count_related('cds'), 4, 'count_related ok' );
29 my $track = DBICTest::Track->create( {
33 title => 'Hidden Track'
35 $track->set_from_related( cd => $cd );
37 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
38 is($track->cd->cdid, 4, 'set_from_related ok' );
40 is( $track->cd, 4, 'set_from_related ok' );
43 # update_from_related, the same as set_from_related, but it calls update afterwards
44 $track = DBICTest::Track->create( {
48 title => 'Hidden Track'
50 $track->update_from_related( cd => $cd );
52 my $t_cd = (DBICTest::Track->search( cd => 4, position => 99 ))[0]->cd;
54 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
55 is( $t_cd->cdid, 4, 'update_from_related ok' );
57 is( $t_cd, 4, 'update_from_related ok' );
60 # find_or_create_related with an existing record
61 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
62 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
64 # find_or_create_related creating a new record
65 $cd = $artist->find_or_create_related( 'cds', {
66 title => 'Greatest Hits',
69 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
70 @cds = $artist->search_related('cds');
71 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
74 #skip 'Need to add delete_related', 1;
76 $artist->delete_related( cds => { title => 'Greatest Hits' });
77 cmp_ok( DBICTest::CD->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
80 # try to add a bogus relationship using the wrong cols
82 $artist->add_relationship(
83 tracks => 'DBICTest::Track',
84 { 'foreign.cd' => 'self.cdid' }
87 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
89 # another bogus relationship using no join condition
91 $artist->add_relationship( tracks => 'DBICTest::Track' );
93 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');