Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
3 | |
4b8dcc58 |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
7 | |
a47e1233 |
8 | my $schema = DBICTest->init_schema(); |
5efe4c79 |
9 | |
ac8a5ba4 |
10 | plan tests => 62; |
0567538f |
11 | |
12 | # has_a test |
f9db5527 |
13 | my $cd = $schema->resultset("CD")->find(4); |
07037f89 |
14 | my ($artist) = ($INC{'DBICTest/HelperRels'} |
15 | ? $cd->artist |
16 | : $cd->search_related('artist')); |
0567538f |
17 | is($artist->name, 'Random Boy Band', 'has_a search_related ok'); |
18 | |
19 | # has_many test with an order_by clause defined |
f9db5527 |
20 | $artist = $schema->resultset("Artist")->find(1); |
07037f89 |
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' ); |
0567538f |
25 | |
26 | # search_related with additional abstract query |
07037f89 |
27 | @cds = ($INC{'DBICTest/HelperRels'} |
28 | ? $artist->cds({ title => { like => '%of%' } }) |
29 | : $artist->search_related('cds', { title => { like => '%of%' } } ) |
30 | ); |
0567538f |
31 | is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' ); |
32 | |
33 | # creating a related object |
07037f89 |
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 | |
0567538f |
43 | is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' ); |
44 | |
5b89a768 |
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 | |
0567538f |
51 | # count_related |
52 | is( $artist->count_related('cds'), 4, 'count_related ok' ); |
53 | |
54 | # set_from_related |
f9db5527 |
55 | my $track = $schema->resultset("Track")->create( { |
0567538f |
56 | trackid => 1, |
57 | cd => 3, |
58 | position => 98, |
59 | title => 'Hidden Track' |
60 | } ); |
61 | $track->set_from_related( cd => $cd ); |
62 | |
70350518 |
63 | is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' ); |
0567538f |
64 | |
2c037e6b |
65 | $track->set_from_related( cd => undef ); |
66 | |
67 | ok( !defined($track->cd), 'set_from_related with undef ok'); |
68 | |
69 | |
0567538f |
70 | # update_from_related, the same as set_from_related, but it calls update afterwards |
f9db5527 |
71 | $track = $schema->resultset("Track")->create( { |
0567538f |
72 | trackid => 2, |
73 | cd => 3, |
74 | position => 99, |
365d06b7 |
75 | title => 'Hidden Track 2' |
0567538f |
76 | } ); |
77 | $track->update_from_related( cd => $cd ); |
78 | |
f9db5527 |
79 | my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd; |
0567538f |
80 | |
70350518 |
81 | is( $t_cd->cdid, 4, 'update_from_related ok' ); |
0567538f |
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' ); |
e02b9964 |
93 | |
0567538f |
94 | @cds = $artist->search_related('cds'); |
95 | is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' ); |
96 | |
87772e46 |
97 | $artist->delete_related( cds => { title => 'Greatest Hits' }); |
f9db5527 |
98 | cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' ); |
0567538f |
99 | |
b3e1f1f5 |
100 | # find_or_new_related with an existing record |
101 | $cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } ); |
102 | is( $cd->year, 2005, 'find_or_new_related on existing record ok' ); |
103 | ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' ); |
104 | |
105 | # find_or_new_related instantiating a new record |
106 | $cd = $artist->find_or_new_related( 'cds', { |
107 | title => 'Greatest Hits 2: Louder Than Ever', |
108 | year => 2007, |
109 | } ); |
110 | is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' ); |
111 | ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' ); |
112 | |
e02b9964 |
113 | # print STDERR Data::Dumper::Dumper($cd->get_columns); |
114 | # $cd->result_source->schema->storage->debug(1); |
115 | $cd->artist(undef); |
116 | my $newartist = $cd->find_or_new_related( 'artist', { |
117 | name => 'Random Boy Band Two', |
118 | artistid => 200, |
119 | } ); |
ac8a5ba4 |
120 | # $cd->result_source->schema->storage->debug(0); |
e02b9964 |
121 | is($newartist->name, 'Random Boy Band Two', 'find_or_new_related new artist record with id'); |
122 | is($newartist->id, 200, 'find_or_new_related new artist id set'); |
123 | |
87772e46 |
124 | SKIP: { |
125 | skip "relationship checking needs fixing", 1; |
126 | # try to add a bogus relationship using the wrong cols |
127 | eval { |
128 | DBICTest::Schema::Artist->add_relationship( |
129 | tracks => 'DBICTest::Schema::Track', |
130 | { 'foreign.cd' => 'self.cdid' } |
131 | ); |
132 | }; |
133 | like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok'); |
134 | } |
135 | |
0567538f |
136 | # another bogus relationship using no join condition |
137 | eval { |
3712e4f4 |
138 | DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' ); |
0567538f |
139 | }; |
140 | like($@, qr/join condition/, 'failed when creating a rel without join condition, ok'); |
141 | |
b3f358b5 |
142 | # many_to_many helper tests |
f9db5527 |
143 | $cd = $schema->resultset("CD")->find(1); |
7411204b |
144 | my @producers = $cd->producers(); |
145 | is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' ); |
b3f358b5 |
146 | is( $cd->producers_sorted->next->name, 'Bob The Builder', |
147 | 'sorted many_to_many ok' ); |
148 | is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype', |
149 | 'sorted many_to_many with search condition ok' ); |
7411204b |
150 | |
3a868fb2 |
151 | $cd = $schema->resultset('CD')->find(2); |
b3f358b5 |
152 | my $prod_rs = $cd->producers(); |
153 | my $prod_before_count = $schema->resultset('Producer')->count; |
154 | is( $prod_rs->count, 0, "CD doesn't yet have any producers" ); |
3a868fb2 |
155 | my $prod = $schema->resultset('Producer')->find(1); |
156 | $cd->add_to_producers($prod); |
3a868fb2 |
157 | is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' ); |
b3f358b5 |
158 | is( $prod_rs->first->name, 'Matt S Trout', |
159 | 'many_to_many add_to_$rel($obj) ok' ); |
3a868fb2 |
160 | $cd->remove_from_producers($prod); |
b3f358b5 |
161 | is( $schema->resultset('Producer')->find(1)->name, 'Matt S Trout', |
162 | "producer object exists after remove of link" ); |
163 | is( $prod_rs->count, 0, 'many_to_many remove_from_$rel($obj) ok' ); |
303cf522 |
164 | $cd->add_to_producers({ name => 'Testy McProducer' }); |
b3f358b5 |
165 | is( $schema->resultset('Producer')->count, $prod_before_count+1, |
166 | 'add_to_$rel($hash) inserted a new producer' ); |
303cf522 |
167 | is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($hash) count ok' ); |
b3f358b5 |
168 | is( $prod_rs->first->name, 'Testy McProducer', |
169 | 'many_to_many add_to_$rel($hash) ok' ); |
170 | $cd->add_to_producers({ name => 'Jack Black' }); |
171 | is( $prod_rs->count(), 2, 'many_to_many add_to_$rel($hash) count ok' ); |
172 | $cd->set_producers($schema->resultset('Producer')->all); |
173 | is( $cd->producers->count(), $prod_before_count+2, |
174 | 'many_to_many set_$rel(@objs) count ok' ); |
175 | $cd->set_producers($schema->resultset('Producer')->find(1)); |
176 | is( $cd->producers->count(), 1, 'many_to_many set_$rel($obj) count ok' ); |
21c45f7b |
177 | $cd->set_producers([$schema->resultset('Producer')->all]); |
178 | is( $cd->producers->count(), $prod_before_count+2, |
179 | 'many_to_many set_$rel(\@objs) count ok' ); |
180 | $cd->set_producers([$schema->resultset('Producer')->find(1)]); |
181 | is( $cd->producers->count(), 1, 'many_to_many set_$rel([$obj]) count ok' ); |
303cf522 |
182 | |
183 | eval { $cd->remove_from_producers({ fake => 'hash' }); }; |
184 | like( $@, qr/needs an object/, 'remove_from_$rel($hash) dies correctly' ); |
185 | |
186 | eval { $cd->add_to_producers(); }; |
b3f358b5 |
187 | like( $@, qr/needs an object or hashref/, |
188 | 'add_to_$rel(undef) dies correctly' ); |
303cf522 |
189 | |
3bd6e3e0 |
190 | # many_to_many stresstest |
191 | my $twokey = $schema->resultset('TwoKeys')->find(1,1); |
192 | my $fourkey = $schema->resultset('FourKeys')->find(1,2,3,4); |
193 | |
194 | is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' ); |
195 | $twokey->add_to_fourkeys($fourkey, { autopilot => 'engaged' }); |
196 | my $got_fourkey = $twokey->fourkeys({ sensors => 'online' })->first; |
197 | is( $twokey->fourkeys->count, 1, 'twokey has one fourkey' ); |
198 | is( $got_fourkey->$_, $fourkey->$_, |
199 | 'fourkeys row has the correct value for column '.$_ ) |
200 | for (qw(foo bar hello goodbye sensors)); |
201 | $twokey->remove_from_fourkeys($fourkey); |
202 | is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' ); |
b3f358b5 |
203 | is( $twokey->fourkeys_to_twokeys->count, 0, |
204 | 'twokey has no links to fourkey' ); |
205 | |
ac8a5ba4 |
206 | my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 }); |
207 | is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded'); |
208 | is($undef_artist_cd->search_related('artist')->count, 3, 'open search on undef FK'); |
209 | |
210 | my $def_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007, artist => undef }); |
211 | is($def_artist_cd->has_column_loaded('artist'), 1, 'FK loaded'); |
212 | is($def_artist_cd->search_related('artist')->count, 0, 'closed search on null FK'); |
3a868fb2 |
213 | |
5efe4c79 |
214 | # test undirected many-to-many relationship (e.g. "related artists") |
f9db5527 |
215 | my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps; |
5efe4c79 |
216 | is($undir_maps->count, 1, 'found 1 undirected map for artist 1'); |
217 | |
f9db5527 |
218 | $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps; |
5efe4c79 |
219 | is($undir_maps->count, 1, 'found 1 undirected map for artist 2'); |
220 | |
ad3d2d7c |
221 | my $mapped_rs = $undir_maps->search_related('mapped_artists'); |
222 | |
223 | my @art = $mapped_rs->all; |
5efe4c79 |
224 | |
225 | cmp_ok(@art, '==', 2, "Both artist returned from map"); |
226 | |
ad3d2d7c |
227 | my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}}); |
228 | |
229 | cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition"); |
230 | |
b8d4bd90 |
231 | # check join through cascaded has_many relationships |
232 | $artist = $schema->resultset("Artist")->find(1); |
233 | my $trackset = $artist->cds->search_related('tracks'); |
234 | # LEFT join means we also see the trackless additional album... |
235 | cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist"); |
ad3d2d7c |
236 | |
0f57d214 |
237 | # now see about updating eveything that belongs to artist 2 to artist 3 |
238 | $artist = $schema->resultset("Artist")->find(2); |
239 | my $nartist = $schema->resultset("Artist")->find(3); |
240 | cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist"); |
241 | cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist"); |
242 | $artist->cds->update({artist => $nartist->id}); |
243 | cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist"); |
244 | cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist"); |
245 | |