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' );
42 is( $artist->count_related('cds'), 4, 'count_related ok' );
45 my $track = $schema->resultset("Track")->create( {
49 title => 'Hidden Track'
51 $track->set_from_related( cd => $cd );
53 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
54 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
56 is( $track->cd, 4, 'set_from_related ok' );
59 # update_from_related, the same as set_from_related, but it calls update afterwards
60 $track = $schema->resultset("Track")->create( {
64 title => 'Hidden Track'
66 $track->update_from_related( cd => $cd );
68 my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd;
70 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
71 is( $t_cd->cdid, 4, 'update_from_related ok' );
73 is( $t_cd, 4, 'update_from_related ok' );
76 # find_or_create_related with an existing record
77 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
78 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
80 # find_or_create_related creating a new record
81 $cd = $artist->find_or_create_related( 'cds', {
82 title => 'Greatest Hits',
85 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
86 @cds = $artist->search_related('cds');
87 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
89 $artist->delete_related( cds => { title => 'Greatest Hits' });
90 cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
93 skip "relationship checking needs fixing", 1;
94 # try to add a bogus relationship using the wrong cols
96 DBICTest::Schema::Artist->add_relationship(
97 tracks => 'DBICTest::Schema::Track',
98 { 'foreign.cd' => 'self.cdid' }
101 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
104 # another bogus relationship using no join condition
106 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
108 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
110 # many_to_many helper test
111 $cd = $schema->resultset("CD")->find(1);
112 my @producers = $cd->producers();
113 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
115 # test undirected many-to-many relationship (e.g. "related artists")
116 my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
117 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
119 $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
120 is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
122 my @art = $undir_maps->search_related('mapped_artists')->all;
124 cmp_ok(@art, '==', 2, "Both artist returned from map");