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