Improved join condition possiblities - arrayrefs of hashrefs now work for OR
[dbsrgits/DBIx-Class.git] / t / run / 06relationship.tl
1 sub run_tests {
2
3 use strict;
4 use warnings;  
5 plan tests => 17;
6
7 # has_a test
8 my $cd = DBICTest->class("CD")->find(4);
9 my ($artist) = ($INC{'DBICTest/HelperRels'}
10                   ? $cd->artist
11                   : $cd->search_related('artist'));
12 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
13
14 # has_many test with an order_by clause defined
15 $artist = DBICTest->class("Artist")->find(1);
16 my @cds = ($INC{'DBICTest/HelperRels'}
17              ? $artist->cds
18              : $artist->search_related('cds'));
19 is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
20
21 # search_related with additional abstract query
22 @cds = ($INC{'DBICTest/HelperRels'}
23           ? $artist->cds({ title => { like => '%of%' } })
24           : $artist->search_related('cds', { title => { like => '%of%' } } )
25        );
26 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
27
28 # creating a related object
29 if ($INC{'DBICTest/HelperRels.pm'}) {
30   $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
31 } else {
32   $artist->create_related( 'cds', {
33       title => 'Big Flop',
34       year => 2005,
35   } );
36 }
37
38 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
39
40 # count_related
41 is( $artist->count_related('cds'), 4, 'count_related ok' );
42
43 # set_from_related
44 my $track = DBICTest->class("Track")->create( {
45   trackid => 1,
46   cd => 3,
47   position => 98,
48   title => 'Hidden Track'
49 } );
50 $track->set_from_related( cd => $cd );
51
52 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
53   is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
54 } else {
55   is( $track->cd, 4, 'set_from_related ok' );
56 }
57
58 # update_from_related, the same as set_from_related, but it calls update afterwards
59 $track = DBICTest->class("Track")->create( {
60   trackid => 2,
61   cd => 3,
62   position => 99,
63   title => 'Hidden Track'
64 } );
65 $track->update_from_related( cd => $cd );
66
67 my $t_cd = (DBICTest->class("Track")->search( cd => 4, position => 99 ))[0]->cd;
68
69 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
70   is( $t_cd->cdid, 4, 'update_from_related ok' );
71 } else {
72   is( $t_cd, 4, 'update_from_related ok' );
73 }
74
75 # find_or_create_related with an existing record
76 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
77 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
78
79 # find_or_create_related creating a new record
80 $cd = $artist->find_or_create_related( 'cds', {
81   title => 'Greatest Hits',
82   year => 2006,
83 } );
84 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
85 @cds = $artist->search_related('cds');
86 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
87
88 $artist->delete_related( cds => { title => 'Greatest Hits' });
89 cmp_ok( DBICTest->class("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
90
91 SKIP: {
92   skip "relationship checking needs fixing", 1;
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   
103 # another bogus relationship using no join condition
104 eval {
105     DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
106 };
107 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
108
109 # many_to_many helper test
110 $cd = DBICTest->class("CD")->find(1);
111 my @producers = $cd->producers();
112 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
113
114 # test undirected many-to-many relationship (e.g. "related artists")
115 my $undir_maps = DBICTest->class("Artist")->find(1)->artist_undirected_maps;
116 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
117
118 $undir_maps = DBICTest->class("Artist")->find(2)->artist_undirected_maps;
119 is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
120
121 my @art = $undir_maps->search_related('mapped_artists')->all;
122
123 cmp_ok(@art, '==', 2, "Both artist returned from map");
124
125 }
126
127 1;