Added preliminary tests for new helpers
[dbsrgits/DBIx-Class.git] / t / 66relationship.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 plan tests => 35;
11
12 # has_a test
13 my $cd = $schema->resultset("CD")->find(4);
14 my ($artist) = ($INC{'DBICTest/HelperRels'}
15                   ? $cd->artist
16                   : $cd->search_related('artist'));
17 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
18
19 # has_many test with an order_by clause defined
20 $artist = $schema->resultset("Artist")->find(1);
21 my @cds = ($INC{'DBICTest/HelperRels'}
22              ? $artist->cds
23              : $artist->search_related('cds'));
24 is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
25
26 # search_related with additional abstract query
27 @cds = ($INC{'DBICTest/HelperRels'}
28           ? $artist->cds({ title => { like => '%of%' } })
29           : $artist->search_related('cds', { title => { like => '%of%' } } )
30        );
31 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
32
33 # creating a related object
34 if ($INC{'DBICTest/HelperRels.pm'}) {
35   $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
36 } else {
37   $artist->create_related( 'cds', {
38       title => 'Big Flop',
39       year => 2005,
40   } );
41 }
42
43 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
44
45 my( $rs_from_list ) = $artist->search_related_rs('cds');
46 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
47
48 ( $rs_from_list ) = $artist->cds_rs();
49 is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
50
51 # count_related
52 is( $artist->count_related('cds'), 4, 'count_related ok' );
53
54 # set_from_related
55 my $track = $schema->resultset("Track")->create( {
56   trackid => 1,
57   cd => 3,
58   position => 98,
59   title => 'Hidden Track'
60 } );
61 $track->set_from_related( cd => $cd );
62
63 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
64
65 $track->set_from_related( cd => undef );
66
67 ok( !defined($track->cd), 'set_from_related with undef ok');
68
69
70 # update_from_related, the same as set_from_related, but it calls update afterwards
71 $track = $schema->resultset("Track")->create( {
72   trackid => 2,
73   cd => 3,
74   position => 99,
75   title => 'Hidden Track'
76 } );
77 $track->update_from_related( cd => $cd );
78
79 my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd;
80
81 is( $t_cd->cdid, 4, 'update_from_related ok' );
82
83 # find_or_create_related with an existing record
84 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
85 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
86
87 # find_or_create_related creating a new record
88 $cd = $artist->find_or_create_related( 'cds', {
89   title => 'Greatest Hits',
90   year => 2006,
91 } );
92 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
93 @cds = $artist->search_related('cds');
94 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
95
96 $artist->delete_related( cds => { title => 'Greatest Hits' });
97 cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
98
99 # find_or_new_related with an existing record
100 $cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } );
101 is( $cd->year, 2005, 'find_or_new_related on existing record ok' );
102 ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' );
103
104 # find_or_new_related instantiating a new record
105 $cd = $artist->find_or_new_related( 'cds', {
106   title => 'Greatest Hits 2: Louder Than Ever',
107   year => 2007,
108 } );
109 is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' );
110 ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' );
111
112 SKIP: {
113   skip "relationship checking needs fixing", 1;
114   # try to add a bogus relationship using the wrong cols
115   eval {
116       DBICTest::Schema::Artist->add_relationship(
117           tracks => 'DBICTest::Schema::Track',
118           { 'foreign.cd' => 'self.cdid' }
119       );
120   };
121   like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
122 }
123   
124 # another bogus relationship using no join condition
125 eval {
126     DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
127 };
128 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
129
130 # many_to_many helper test
131 $cd = $schema->resultset("CD")->find(1);
132 my @producers = $cd->producers();
133 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
134 is( $cd->producers_sorted->next->name, 'Bob The Builder', 'sorted many_to_many ok' );
135 is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype', 'sorted many_to_many with search condition ok' );
136
137 # test new many_to_many helpers
138 $cd = $schema->resultset('CD')->find(2);
139 my $prod = $schema->resultset('Producer')->find(1);
140 $cd->add_to_producers($prod);
141 my $prod_rs = $cd->producers();
142 is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' );
143 is( $prod_rs->first->name, 'Matt S Trout', 'many_to_many add_to_$rel($obj) ok' );
144 $cd->remove_from_producers($prod);
145 is( $cd->producers->count, 0, 'many_to_many remove_from_$rel($obj) ok' );
146
147 # test undirected many-to-many relationship (e.g. "related artists")
148 my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
149 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
150
151 $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
152 is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
153
154 my $mapped_rs = $undir_maps->search_related('mapped_artists');
155
156 my @art = $mapped_rs->all;
157
158 cmp_ok(@art, '==', 2, "Both artist returned from map");
159
160 my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}});
161
162 cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition");
163
164 # check join through cascaded has_many relationships
165 $artist = $schema->resultset("Artist")->find(1);
166 my $trackset = $artist->cds->search_related('tracks');
167 # LEFT join means we also see the trackless additional album...
168 cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist");
169
170 # now see about updating eveything that belongs to artist 2 to artist 3
171 $artist = $schema->resultset("Artist")->find(2);
172 my $nartist = $schema->resultset("Artist")->find(3);
173 cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist");
174 cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist");
175 $artist->cds->update({artist => $nartist->id});
176 cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
177 cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");
178