add_relationship, relationship_info, relationships moved to ResultSource
[dbsrgits/DBIx-Class.git] / t / run / 06relationship.tl
1 sub run_tests {
2   
3 plan tests => 14;
4
5 # has_a test
6 my $cd = DBICTest->class("CD")->find(4);
7 my ($artist) = ($INC{'DBICTest/HelperRels'}
8                   ? $cd->artist
9                   : $cd->search_related('artist'));
10 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
11
12 # has_many test with an order_by clause defined
13 $artist = DBICTest->class("Artist")->find(1);
14 my @cds = ($INC{'DBICTest/HelperRels'}
15              ? $artist->cds
16              : $artist->search_related('cds'));
17 is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
18
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%' } } )
23        );
24 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
25
26 # creating a related object
27 if ($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
36 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
37
38 # count_related
39 is( $artist->count_related('cds'), 4, 'count_related ok' );
40
41 # set_from_related
42 my $track = DBICTest->class("Track")->create( {
43   trackid => 1,
44   cd => 3,
45   position => 98,
46   title => 'Hidden Track'
47 } );
48 $track->set_from_related( cd => $cd );
49
50 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
51   is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
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
57 $track = DBICTest->class("Track")->create( {
58   trackid => 2,
59   cd => 3,
60   position => 99,
61   title => 'Hidden Track'
62 } );
63 $track->update_from_related( cd => $cd );
64
65 my $t_cd = (DBICTest->class("Track")->search( cd => 4, position => 99 ))[0]->cd;
66
67 if ($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' } );
75 is( $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 } );
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' );
85
86 SKIP: {
87     #skip 'Need to add delete_related', 1;
88     # delete_related
89     $artist->delete_related( cds => { title => 'Greatest Hits' });
90     cmp_ok( DBICTest->class("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
91 };
92
93 # try to add a bogus relationship using the wrong cols
94 eval {
95     DBICTest::Schema::Artist->add_relationship(
96         tracks => 'DBICTest::Schema::Track',
97         { 'foreign.cd' => 'self.cdid' }
98     );
99 };
100 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
101
102 # another bogus relationship using no join condition
103 eval {
104     DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
105 };
106 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
107
108 # many_to_many helper test
109 $cd = DBICTest->class("CD")->find(1);
110 my @producers = $cd->producers();
111 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
112
113 }
114
115 1;