add_relationship, relationship_info, relationships moved to ResultSource
[dbsrgits/DBIx-Class.git] / t / run / 06relationship.tl
CommitLineData
0567538f 1sub run_tests {
2
7411204b 3plan tests => 14;
0567538f 4
5# has_a test
3712e4f4 6my $cd = DBICTest->class("CD")->find(4);
07037f89 7my ($artist) = ($INC{'DBICTest/HelperRels'}
8 ? $cd->artist
9 : $cd->search_related('artist'));
0567538f 10is($artist->name, 'Random Boy Band', 'has_a search_related ok');
11
12# has_many test with an order_by clause defined
3712e4f4 13$artist = DBICTest->class("Artist")->find(1);
07037f89 14my @cds = ($INC{'DBICTest/HelperRels'}
15 ? $artist->cds
16 : $artist->search_related('cds'));
17is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
0567538f 18
19# search_related with additional abstract query
07037f89 20@cds = ($INC{'DBICTest/HelperRels'}
21 ? $artist->cds({ title => { like => '%of%' } })
22 : $artist->search_related('cds', { title => { like => '%of%' } } )
23 );
0567538f 24is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
25
26# creating a related object
07037f89 27if ($INC{'DBICTest/HelperRels.pm'}) {
28 $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
29} else {
30 $artist->create_related( 'cds', {
31 title => 'Big Flop',
32 year => 2005,
33 } );
34}
35
0567538f 36is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
37
38# count_related
39is( $artist->count_related('cds'), 4, 'count_related ok' );
40
41# set_from_related
3712e4f4 42my $track = DBICTest->class("Track")->create( {
0567538f 43 trackid => 1,
44 cd => 3,
45 position => 98,
46 title => 'Hidden Track'
47} );
48$track->set_from_related( cd => $cd );
49
50if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
1e3bc087 51 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
0567538f 52} else {
53 is( $track->cd, 4, 'set_from_related ok' );
54}
55
56# update_from_related, the same as set_from_related, but it calls update afterwards
3712e4f4 57$track = DBICTest->class("Track")->create( {
0567538f 58 trackid => 2,
59 cd => 3,
60 position => 99,
61 title => 'Hidden Track'
62} );
63$track->update_from_related( cd => $cd );
64
3712e4f4 65my $t_cd = (DBICTest->class("Track")->search( cd => 4, position => 99 ))[0]->cd;
0567538f 66
67if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
68 is( $t_cd->cdid, 4, 'update_from_related ok' );
69} else {
70 is( $t_cd, 4, 'update_from_related ok' );
71}
72
73# find_or_create_related with an existing record
74$cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
75is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
76
77# find_or_create_related creating a new record
78$cd = $artist->find_or_create_related( 'cds', {
79 title => 'Greatest Hits',
80 year => 2006,
81} );
82is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
83@cds = $artist->search_related('cds');
84is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
85
86SKIP: {
87 #skip 'Need to add delete_related', 1;
88 # delete_related
89 $artist->delete_related( cds => { title => 'Greatest Hits' });
3712e4f4 90 cmp_ok( DBICTest->class("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
0567538f 91};
92
93# try to add a bogus relationship using the wrong cols
94eval {
3712e4f4 95 DBICTest::Schema::Artist->add_relationship(
8452e496 96 tracks => 'DBICTest::Schema::Track',
0567538f 97 { 'foreign.cd' => 'self.cdid' }
98 );
99};
100like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
101
102# another bogus relationship using no join condition
103eval {
3712e4f4 104 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
0567538f 105};
106like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
107
7411204b 108# many_to_many helper test
109$cd = DBICTest->class("CD")->find(1);
110my @producers = $cd->producers();
111is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
112
0567538f 113}
114
1151;