Commit | Line | Data |
70350518 |
1 | use strict; |
2f926160 |
2 | use warnings; |
70350518 |
3 | |
4b8dcc58 |
4 | use Test::More; |
2f926160 |
5 | use Test::Exception; |
4b8dcc58 |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
8 | |
a47e1233 |
9 | my $schema = DBICTest->init_schema(); |
5efe4c79 |
10 | |
cef1bdda |
11 | plan tests => 74; |
0567538f |
12 | |
13 | # has_a test |
f9db5527 |
14 | my $cd = $schema->resultset("CD")->find(4); |
07037f89 |
15 | my ($artist) = ($INC{'DBICTest/HelperRels'} |
16 | ? $cd->artist |
17 | : $cd->search_related('artist')); |
0567538f |
18 | is($artist->name, 'Random Boy Band', 'has_a search_related ok'); |
19 | |
20 | # has_many test with an order_by clause defined |
f9db5527 |
21 | $artist = $schema->resultset("Artist")->find(1); |
07037f89 |
22 | my @cds = ($INC{'DBICTest/HelperRels'} |
23 | ? $artist->cds |
24 | : $artist->search_related('cds')); |
25 | is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' ); |
0567538f |
26 | |
27 | # search_related with additional abstract query |
07037f89 |
28 | @cds = ($INC{'DBICTest/HelperRels'} |
29 | ? $artist->cds({ title => { like => '%of%' } }) |
30 | : $artist->search_related('cds', { title => { like => '%of%' } } ) |
31 | ); |
0567538f |
32 | is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' ); |
33 | |
34 | # creating a related object |
07037f89 |
35 | if ($INC{'DBICTest/HelperRels.pm'}) { |
36 | $artist->add_to_cds({ title => 'Big Flop', year => 2005 }); |
37 | } else { |
38 | $artist->create_related( 'cds', { |
39 | title => 'Big Flop', |
40 | year => 2005, |
41 | } ); |
42 | } |
43 | |
c89815db |
44 | my $big_flop_cd = ($artist->search_related('cds'))[3]; |
45 | is( $big_flop_cd->title, 'Big Flop', 'create_related ok' ); |
46 | |
47 | { # make sure we are not making pointless select queries when a FK IS NULL |
48 | my $queries = 0; |
49 | $schema->storage->debugcb(sub { $queries++; }); |
50 | $schema->storage->debug(1); |
51 | $big_flop_cd->genre; #should not trigger a select query |
cef1bdda |
52 | is($queries, 0, 'No SELECT made for belongs_to if key IS NULL'); |
53 | $big_flop_cd->genre_inefficient; #should trigger a select query |
54 | is($queries, 1, 'SELECT made for belongs_to if key IS NULL when undef_on_null_fk disabled'); |
c89815db |
55 | $schema->storage->debug(0); |
56 | $schema->storage->debugcb(undef); |
57 | } |
0567538f |
58 | |
5b89a768 |
59 | my( $rs_from_list ) = $artist->search_related_rs('cds'); |
60 | is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' ); |
61 | |
62 | ( $rs_from_list ) = $artist->cds_rs(); |
63 | is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' ); |
64 | |
0567538f |
65 | # count_related |
66 | is( $artist->count_related('cds'), 4, 'count_related ok' ); |
67 | |
68 | # set_from_related |
f9db5527 |
69 | my $track = $schema->resultset("Track")->create( { |
0567538f |
70 | trackid => 1, |
71 | cd => 3, |
72 | position => 98, |
73 | title => 'Hidden Track' |
74 | } ); |
75 | $track->set_from_related( cd => $cd ); |
76 | |
70350518 |
77 | is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' ); |
0567538f |
78 | |
2c037e6b |
79 | $track->set_from_related( cd => undef ); |
80 | |
81 | ok( !defined($track->cd), 'set_from_related with undef ok'); |
82 | |
f77bad73 |
83 | TODO: { |
84 | local $TODO = 'accessing $object->rel and set_from_related'; |
85 | my $track = $schema->resultset("Track")->new( {} ); |
86 | $track->cd; |
87 | $track->set_from_related( cd => $cd ); |
88 | ok ($track->cd, 'set_from_related ok after using the accessor' ); |
89 | }; |
2c037e6b |
90 | |
0567538f |
91 | # update_from_related, the same as set_from_related, but it calls update afterwards |
f9db5527 |
92 | $track = $schema->resultset("Track")->create( { |
0567538f |
93 | trackid => 2, |
94 | cd => 3, |
95 | position => 99, |
365d06b7 |
96 | title => 'Hidden Track 2' |
0567538f |
97 | } ); |
98 | $track->update_from_related( cd => $cd ); |
99 | |
f9db5527 |
100 | my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd; |
0567538f |
101 | |
70350518 |
102 | is( $t_cd->cdid, 4, 'update_from_related ok' ); |
0567538f |
103 | |
104 | # find_or_create_related with an existing record |
105 | $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } ); |
106 | is( $cd->year, 2005, 'find_or_create_related on existing record ok' ); |
107 | |
108 | # find_or_create_related creating a new record |
109 | $cd = $artist->find_or_create_related( 'cds', { |
110 | title => 'Greatest Hits', |
111 | year => 2006, |
112 | } ); |
113 | is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' ); |
e02b9964 |
114 | |
0567538f |
115 | @cds = $artist->search_related('cds'); |
116 | is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' ); |
117 | |
87772e46 |
118 | $artist->delete_related( cds => { title => 'Greatest Hits' }); |
f9db5527 |
119 | cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' ); |
0567538f |
120 | |
b3e1f1f5 |
121 | # find_or_new_related with an existing record |
122 | $cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } ); |
123 | is( $cd->year, 2005, 'find_or_new_related on existing record ok' ); |
124 | ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' ); |
125 | |
126 | # find_or_new_related instantiating a new record |
127 | $cd = $artist->find_or_new_related( 'cds', { |
128 | title => 'Greatest Hits 2: Louder Than Ever', |
129 | year => 2007, |
130 | } ); |
131 | is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' ); |
132 | ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' ); |
133 | |
e02b9964 |
134 | $cd->artist(undef); |
135 | my $newartist = $cd->find_or_new_related( 'artist', { |
136 | name => 'Random Boy Band Two', |
137 | artistid => 200, |
138 | } ); |
e02b9964 |
139 | is($newartist->name, 'Random Boy Band Two', 'find_or_new_related new artist record with id'); |
140 | is($newartist->id, 200, 'find_or_new_related new artist id set'); |
141 | |
a0dd8679 |
142 | TODO: { |
143 | local $TODO = "relationship checking needs fixing"; |
87772e46 |
144 | # try to add a bogus relationship using the wrong cols |
145 | eval { |
146 | DBICTest::Schema::Artist->add_relationship( |
147 | tracks => 'DBICTest::Schema::Track', |
148 | { 'foreign.cd' => 'self.cdid' } |
149 | ); |
150 | }; |
151 | like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok'); |
152 | } |
153 | |
0567538f |
154 | # another bogus relationship using no join condition |
155 | eval { |
3712e4f4 |
156 | DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' ); |
0567538f |
157 | }; |
158 | like($@, qr/join condition/, 'failed when creating a rel without join condition, ok'); |
159 | |
b3f358b5 |
160 | # many_to_many helper tests |
f9db5527 |
161 | $cd = $schema->resultset("CD")->find(1); |
7411204b |
162 | my @producers = $cd->producers(); |
163 | is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' ); |
b3f358b5 |
164 | is( $cd->producers_sorted->next->name, 'Bob The Builder', |
165 | 'sorted many_to_many ok' ); |
166 | is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype', |
167 | 'sorted many_to_many with search condition ok' ); |
7411204b |
168 | |
3a868fb2 |
169 | $cd = $schema->resultset('CD')->find(2); |
b3f358b5 |
170 | my $prod_rs = $cd->producers(); |
171 | my $prod_before_count = $schema->resultset('Producer')->count; |
172 | is( $prod_rs->count, 0, "CD doesn't yet have any producers" ); |
3a868fb2 |
173 | my $prod = $schema->resultset('Producer')->find(1); |
174 | $cd->add_to_producers($prod); |
3a868fb2 |
175 | is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' ); |
b3f358b5 |
176 | is( $prod_rs->first->name, 'Matt S Trout', |
177 | 'many_to_many add_to_$rel($obj) ok' ); |
3a868fb2 |
178 | $cd->remove_from_producers($prod); |
b3f358b5 |
179 | is( $schema->resultset('Producer')->find(1)->name, 'Matt S Trout', |
180 | "producer object exists after remove of link" ); |
181 | is( $prod_rs->count, 0, 'many_to_many remove_from_$rel($obj) ok' ); |
303cf522 |
182 | $cd->add_to_producers({ name => 'Testy McProducer' }); |
b3f358b5 |
183 | is( $schema->resultset('Producer')->count, $prod_before_count+1, |
184 | 'add_to_$rel($hash) inserted a new producer' ); |
303cf522 |
185 | is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($hash) count ok' ); |
b3f358b5 |
186 | is( $prod_rs->first->name, 'Testy McProducer', |
187 | 'many_to_many add_to_$rel($hash) ok' ); |
188 | $cd->add_to_producers({ name => 'Jack Black' }); |
189 | is( $prod_rs->count(), 2, 'many_to_many add_to_$rel($hash) count ok' ); |
190 | $cd->set_producers($schema->resultset('Producer')->all); |
191 | is( $cd->producers->count(), $prod_before_count+2, |
192 | 'many_to_many set_$rel(@objs) count ok' ); |
193 | $cd->set_producers($schema->resultset('Producer')->find(1)); |
194 | is( $cd->producers->count(), 1, 'many_to_many set_$rel($obj) count ok' ); |
21c45f7b |
195 | $cd->set_producers([$schema->resultset('Producer')->all]); |
196 | is( $cd->producers->count(), $prod_before_count+2, |
197 | 'many_to_many set_$rel(\@objs) count ok' ); |
198 | $cd->set_producers([$schema->resultset('Producer')->find(1)]); |
199 | is( $cd->producers->count(), 1, 'many_to_many set_$rel([$obj]) count ok' ); |
303cf522 |
200 | |
201 | eval { $cd->remove_from_producers({ fake => 'hash' }); }; |
202 | like( $@, qr/needs an object/, 'remove_from_$rel($hash) dies correctly' ); |
203 | |
204 | eval { $cd->add_to_producers(); }; |
b3f358b5 |
205 | like( $@, qr/needs an object or hashref/, |
206 | 'add_to_$rel(undef) dies correctly' ); |
303cf522 |
207 | |
3bd6e3e0 |
208 | # many_to_many stresstest |
209 | my $twokey = $schema->resultset('TwoKeys')->find(1,1); |
210 | my $fourkey = $schema->resultset('FourKeys')->find(1,2,3,4); |
211 | |
212 | is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' ); |
213 | $twokey->add_to_fourkeys($fourkey, { autopilot => 'engaged' }); |
214 | my $got_fourkey = $twokey->fourkeys({ sensors => 'online' })->first; |
215 | is( $twokey->fourkeys->count, 1, 'twokey has one fourkey' ); |
216 | is( $got_fourkey->$_, $fourkey->$_, |
217 | 'fourkeys row has the correct value for column '.$_ ) |
218 | for (qw(foo bar hello goodbye sensors)); |
219 | $twokey->remove_from_fourkeys($fourkey); |
220 | is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' ); |
b3f358b5 |
221 | is( $twokey->fourkeys_to_twokeys->count, 0, |
222 | 'twokey has no links to fourkey' ); |
223 | |
ac8a5ba4 |
224 | my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 }); |
225 | is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded'); |
370f2ba2 |
226 | is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db'); |
2c5c07ec |
227 | eval{ |
228 | $undef_artist_cd->related_resultset('artist')->new({name => 'foo'}); |
229 | }; |
230 | is( $@, '', "Object created on a resultset related to not yet inserted object"); |
231 | |
ac8a5ba4 |
232 | my $def_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007, artist => undef }); |
233 | is($def_artist_cd->has_column_loaded('artist'), 1, 'FK loaded'); |
234 | is($def_artist_cd->search_related('artist')->count, 0, 'closed search on null FK'); |
3a868fb2 |
235 | |
5efe4c79 |
236 | # test undirected many-to-many relationship (e.g. "related artists") |
f9db5527 |
237 | my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps; |
5efe4c79 |
238 | is($undir_maps->count, 1, 'found 1 undirected map for artist 1'); |
239 | |
f9db5527 |
240 | $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps; |
5efe4c79 |
241 | is($undir_maps->count, 1, 'found 1 undirected map for artist 2'); |
242 | |
ad3d2d7c |
243 | my $mapped_rs = $undir_maps->search_related('mapped_artists'); |
244 | |
245 | my @art = $mapped_rs->all; |
5efe4c79 |
246 | |
247 | cmp_ok(@art, '==', 2, "Both artist returned from map"); |
248 | |
ad3d2d7c |
249 | my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}}); |
250 | |
251 | cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition"); |
252 | |
b8d4bd90 |
253 | # check join through cascaded has_many relationships |
254 | $artist = $schema->resultset("Artist")->find(1); |
255 | my $trackset = $artist->cds->search_related('tracks'); |
256 | # LEFT join means we also see the trackless additional album... |
257 | cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist"); |
ad3d2d7c |
258 | |
0f57d214 |
259 | # now see about updating eveything that belongs to artist 2 to artist 3 |
260 | $artist = $schema->resultset("Artist")->find(2); |
261 | my $nartist = $schema->resultset("Artist")->find(3); |
262 | cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist"); |
263 | cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist"); |
264 | $artist->cds->update({artist => $nartist->id}); |
265 | cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist"); |
266 | cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist"); |
267 | |
8c368cf3 |
268 | my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode' }); |
8c368cf3 |
269 | my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave in Silence', 'year' => 1982}); |
270 | eval { |
271 | $new_artist->insert; |
272 | $new_related_cd->insert; |
273 | }; |
4295b5d4 |
274 | is ($@, '', 'Staged insertion successful'); |
275 | ok($new_artist->in_storage, 'artist inserted'); |
276 | ok($new_related_cd->in_storage, 'new_related_cd inserted'); |
6bf6ba2f |
277 | |
a7be8807 |
278 | TODO: { |
279 | local $TODO = "TODOify for multicreate branch"; |
2f926160 |
280 | my $new_cd = $schema->resultset("CD")->new_result({}); |
281 | my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',}); |
282 | lives_ok ( |
283 | sub { |
284 | $new_related_artist->insert; |
285 | $new_cd->title( 'Misplaced Childhood' ); |
286 | $new_cd->year ( 1985 ); |
287 | # $new_cd->artist( $new_related_artist ); # For exact backward compatibility # not sure what this means |
288 | $new_cd->insert; |
289 | }, |
290 | 'Reversed staged insertion successful' |
291 | ); |
292 | ok($new_related_artist->in_storage, 'related artist inserted'); |
293 | ok($new_cd->in_storage, 'cd inserted'); |
294 | |
6bf6ba2f |
295 | # check if is_foreign_key_constraint attr is set |
296 | my $rs_normal = $schema->source('Track'); |
297 | my $relinfo = $rs_normal->relationship_info ('cd'); |
298 | cmp_ok($relinfo->{attrs}{is_foreign_key_constraint}, '==', 1, "is_foreign_key_constraint defined for belongs_to relationships."); |
299 | |
300 | my $rs_overridden = $schema->source('ForceForeign'); |
301 | my $relinfo_with_attr = $rs_overridden->relationship_info ('cd_3'); |
302 | cmp_ok($relinfo_with_attr->{attrs}{is_foreign_key_constraint}, '==', 0, "is_foreign_key_constraint defined for belongs_to relationships with attr."); |
a7be8807 |
303 | } |