]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/blob - t/lib/RunTests.pm
fixed updating of nullable has_many rels (RT#65561)
[chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate] / t / lib / RunTests.pm
1 # -*- perl -*-
2 package RunTests;
3 use Exporter 'import'; # gives you Exporter's import() method directly
4 @EXPORT = qw(run_tests);
5 use strict;
6 use warnings;
7 use Test::More;
8 use Test::Warn;
9 use DBIx::Class::ResultSet::RecursiveUpdate;
10
11 sub run_tests {
12 my $schema = shift;
13
14 plan tests => 59;
15
16 my $dvd_rs = $schema->resultset('Dvd');
17 my $user_rs = $schema->resultset('User');
18
19 my $owner = $user_rs->next;
20 my $another_owner = $user_rs->next;
21 my $initial_user_count = $user_rs->count;
22 my $expected_user_count = $initial_user_count;
23 my $initial_dvd_count = $dvd_rs->count;
24 my $updates;
25
26 # pre 0.21 api
27 $dvd_rs->search( { dvd_id => 1 } )
28 ->recursive_update( { owner => { username => 'aaa' } }, ['dvd_id'] );
29
30 my $u = $user_rs->find( $dvd_rs->find(1)->owner->id );
31 is( $u->username, 'aaa', 'fixed_fields pre 0.21 api ok' );
32
33 # 0.21+ api
34 $dvd_rs->search( { dvd_id => 1 } )->recursive_update(
35 { owner => { username => 'bbb' } },
36 { fixed_fields => ['dvd_id'], }
37 );
38
39 $u = $user_rs->find( $dvd_rs->find(1)->owner->id );
40 is( $u->username, 'bbb', 'fixed_fields 0.21+ api ok' );
41
42 {
43
44 # try to create with a not existing rel
45 my $updates = {
46 name => 'Test for nonexisting rel',
47 username => 'nonexisting_rel',
48 password => 'whatever',
49 nonexisting => { foo => 'bar' },
50 };
51
52 warning_like {
53 my $user = $user_rs->recursive_update($updates);
54 }
55 qr/No such column, relationship, many-to-many helper accessor or generic accessor 'nonexisting'/,
56 'nonexisting column, accessor, relationship warns';
57 $expected_user_count++;
58 is( $user_rs->count, $expected_user_count, 'User created' );
59
60 # for future use when we switch from warn to throw_exception
61 # eval { $user_rs->recursive_update($updates); };
62 # like(
63 # $@,
64 # qr/No such column, relationship, many-to-many helper accessor or generic accessor 'nonexisting'/,
65 # 'nonexisting column, accessor, relationship fails'
66 # );
67 }
68
69 {
70
71 # try to create with a not existing rel but suppressed warning
72 my $updates = {
73 name => 'Test for nonexisting rel with suppressed warning',
74 username => 'suppressed_nonexisting_rel',
75 password => 'whatever',
76 nonexisting => { foo => 'bar' },
77 };
78
79 warning_is {
80 my $user =
81 $user_rs->recursive_update( $updates,
82 { unknown_params_ok => 1 } );
83 }
84 "",
85 "nonexisting column, accessor, relationship doesn't warn with unknown_params_ok";
86 $expected_user_count++;
87 is( $user_rs->count, $expected_user_count, 'User created' );
88 }
89
90 {
91
92 # try to create with a not existing rel, suppressed warning but storage debugging
93 my $updates = {
94 name => 'Test for nonexisting rel with suppressed warning but storage debugging',
95 username => 'suppressed_nonexisting_rel_with_storage_debug',
96 password => 'whatever',
97 nonexisting => { foo => 'bar' },
98 };
99
100 my $debug = $user_rs->result_source->storage->debug;
101 $user_rs->result_source->storage->debug(1);
102
103 warning_like {
104 my $user =
105 $user_rs->recursive_update( $updates,
106 { unknown_params_ok => 1 } );
107 }
108 qr/No such column, relationship, many-to-many helper accessor or generic accessor 'nonexisting'/,
109 "nonexisting column, accessor, relationship doesn't warn with unknown_params_ok";
110 $expected_user_count++;
111 is( $user_rs->count, $expected_user_count, 'User created' );
112
113 $user_rs->result_source->storage->debug($debug);
114 }
115
116 # creating new record linked to some old record
117 $updates = {
118 name => 'Test name 2',
119 viewings => [ { user_id => $owner->id } ],
120 owner => { id => $another_owner->id },
121 };
122
123 my $new_dvd = $dvd_rs->recursive_update($updates);
124
125 is( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' );
126
127 is( $schema->resultset('User')->count,
128 $expected_user_count, "No new user created" );
129 is( $new_dvd->name, 'Test name 2', 'Dvd name set' );
130 is( $new_dvd->owner->id, $another_owner->id, 'Owner set' );
131 is( $new_dvd->viewings->count, 1, 'Viewing created' );
132
133 # creating new records
134 $updates = {
135 tags => [ '2', { id => '3' } ],
136 name => 'Test name',
137 owner => $owner,
138 current_borrower => {
139 name => 'temp name',
140 username => 'temp name',
141 password => 'temp name',
142 },
143 liner_notes => { notes => 'test note', },
144 like_has_many => [ { key2 => 1 } ],
145 like_has_many2 => [
146 { onekey => { name => 'aaaaa' },
147 key2 => 1
148 }
149 ],
150 };
151
152 my $dvd = $dvd_rs->recursive_update($updates);
153 $expected_user_count++;
154
155 is( $dvd_rs->count, $initial_dvd_count + 2, 'Dvd created' );
156 is( $schema->resultset('User')->count,
157 $expected_user_count, "One new user created" );
158 is( $dvd->name, 'Test name', 'Dvd name set' );
159 is_deeply( [ map { $_->id } $dvd->tags ], [ '2', '3' ], 'Tags set' );
160 is( $dvd->owner->id, $owner->id, 'Owner set' );
161
162 is( $dvd->current_borrower->name, 'temp name', 'Related record created' );
163 is( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
164 ok( $schema->resultset('Twokeys')
165 ->find( { dvd_name => 'Test name', key2 => 1 } ),
166 'Twokeys created'
167 );
168 my $onekey =
169 $schema->resultset('Onekey')->search({ name => 'aaaaa' })->first;
170 ok( $onekey, 'Onekey created' );
171 ok( $schema->resultset('Twokeys_belongsto')
172 ->find( { key1 => $onekey->id, key2 => 1 } ),
173 'Twokeys_belongsto created'
174 );
175 TODO: {
176 local $TODO = 'value of fk from a multi relationship';
177 is( $dvd->twokeysfk, $onekey->id, 'twokeysfk in Dvd' );
178 }
179 is( $dvd->name, 'Test name', 'Dvd name set' );
180
181 # changing existing records
182 my $num_of_users = $user_rs->count;
183 $updates = {
184 id => $dvd->dvd_id, # id instead of dvd_id
185 name => undef,
186 tags => [],
187 owner => $another_owner->id,
188 current_borrower => {
189 username => 'new name a',
190 name => 'new name a',
191 password => 'new password a',
192 },
193 liner_notes => { notes => 'test note changed', },
194
195 };
196 my $dvd_updated = $dvd_rs->recursive_update($updates);
197
198 is( $dvd_updated->dvd_id, $dvd->dvd_id, 'Pk from "id"' );
199 is( $schema->resultset('User')->count,
200 $expected_user_count, "No new user created" );
201 is( $dvd_updated->name, undef, 'Dvd name deleted' );
202 is( $dvd_updated->get_column('owner'),
203 $another_owner->id, 'Owner updated' );
204 is( $dvd_updated->current_borrower->name,
205 'new name a', 'Related record modified' );
206 is( $dvd_updated->tags->count, 0, 'Tags deleted' );
207 is( $dvd_updated->liner_notes->notes,
208 'test note changed',
209 'might_have record changed'
210 );
211
212 my $dvd_with_tags =
213 $dvd_rs->recursive_update( { id => $dvd->dvd_id, tags => [ 1, 2 ] } );
214 is_deeply( [ map { $_->id } $dvd_with_tags->tags ], [ 1, 2 ],
215 'Tags set' );
216 my $dvd_without_tags =
217 $dvd_rs->recursive_update( { id => $dvd->dvd_id, tags => undef } );
218 is( $dvd_without_tags->tags->count,
219 0, 'Tags deleted when m2m accessor set to undef' );
220
221 $new_dvd->update( { name => 'New Test Name' } );
222 $updates = {
223 id => $new_dvd->dvd_id, # id instead of dvd_id
224 like_has_many => [ { dvd_name => $dvd->name, key2 => 1 } ],
225 };
226 $dvd_updated = $dvd_rs->recursive_update($updates);
227 ok( $schema->resultset('Twokeys')
228 ->find( { dvd_name => 'New Test Name', key2 => 1 } ),
229 'Twokeys updated'
230 );
231 ok( !$schema->resultset('Twokeys')
232 ->find( { dvd_name => $dvd->name, key2 => 1 } ),
233 'Twokeys updated'
234 );
235
236 # repeatable
237 $updates = {
238 name => 'temp name',
239 username => 'temp username',
240 password => 'temp username',
241 owned_dvds => [
242 { 'name' => 'temp name 1',
243 'tags' => [ 1, 2 ],
244 },
245 { 'name' => 'temp name 2',
246 'tags' => [ 2, 3 ],
247 }
248 ]
249 };
250
251 my $user = $user_rs->recursive_update($updates);
252 $expected_user_count++;
253
254 is( $schema->resultset('User')->count,
255 $expected_user_count, "New user created" );
256 is( $dvd_rs->count, $initial_dvd_count + 4, 'Dvds created' );
257 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
258 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
259 ok( $owned_dvds{'temp name 1'},
260 'Name in a has_many related record saved' );
261 my @tags = $owned_dvds{'temp name 1'}->tags;
262 is( scalar @tags, 2, 'Tags in has_many related record saved' );
263 ok( $owned_dvds{'temp name 2'},
264 'Second name in a has_many related record saved' );
265
266 # update has_many where foreign cols aren't nullable
267 $updates = {
268 id => $user->id,
269 address => {
270 street => "101 Main Street",
271 city => "Podunk",
272 state => "New York"
273 },
274 owned_dvds => [ { id => 1, }, ]
275 };
276 $user = $user_rs->recursive_update($updates);
277 is( $schema->resultset('Address')->search( { user_id => $user->id } )
278 ->count,
279 1,
280 'the right number of addresses'
281 );
282 $dvd = $dvd_rs->find(1);
283 is( $dvd->get_column('owner'), $user->id, 'foreign key set' );
284
285 # has_many where foreign cols are nullable
286 my $available_dvd_rs = $dvd_rs->search({ current_borrower => undef });
287 $dvd_rs->update( { current_borrower => $user->id } );
288 ok( $user->borrowed_dvds->count > 1, 'Precond' );
289 $updates = {
290 id => $user->id,
291 borrowed_dvds => [ { id => $dvd->id }, ]
292 };
293 $user =
294 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
295 resultset => $user_rs,
296 updates => $updates,
297 if_not_submitted => 'set_to_null',
298 );
299 is( $user->borrowed_dvds->count, 1, 'borrowed_dvds update with if_not_submitted => set_to_null ok' );
300 is( $available_dvd_rs->count, 5, "previously borrowed dvds weren't deleted");
301
302 $dvd_rs->update( { current_borrower => $user->id } );
303 $user =
304 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
305 resultset => $user_rs,
306 updates => $updates,
307 );
308 is( $user->borrowed_dvds->count, 1, 'borrowed_dvds update without if_not_submitted ok' );
309 is( $available_dvd_rs->count, 5, "previously borrowed dvds weren't deleted");
310
311 $dvd_rs->update( { current_borrower => $user->id } );
312 $user =
313 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
314 resultset => $user_rs,
315 updates => $updates,
316 if_not_submitted => 'delete',
317 );
318 is( $user->borrowed_dvds->count, 1, 'borrowed_dvds update with if_not_submitted => delete ok' );
319 is( $dvd_rs->count, 1, 'all dvds except the one borrowed by the user were deleted');
320
321 @tags = $schema->resultset('Tag')->all;
322 $dvd_updated =
323 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
324 resultset => $schema->resultset('Dvd'),
325 updates => {
326 id => $dvd->dvd_id, # id instead of dvd_id
327 tags => [
328 { id => $tags[0]->id, file => 'file0' },
329 { id => $tags[1]->id, file => 'file1' }
330 ],
331 }
332 );
333 $tags[$_]->discard_changes for 0 .. 1;
334 is( $tags[0]->file, 'file0', 'file set in tag' );
335 is( $tags[1]->file, 'file1', 'file set in tag' );
336 my @rel_tags = $dvd_updated->tags;
337 is( scalar @rel_tags, 2, 'tags related' );
338 ok( $rel_tags[0]->file eq 'file0' || $rel_tags[0]->file eq 'file1',
339 'tags related' );
340
341 my $new_person = {
342 name => 'Amiri Barksdale',
343 username => 'amiri',
344 password => 'amiri',
345 };
346 ok( my $new_user = $user_rs->recursive_update($new_person) );
347
348 # delete has_many where foreign cols aren't nullable
349 my $rs_user_dvd = $user->owned_dvds;
350 my @user_dvd_ids = map { $_->id } $rs_user_dvd->all;
351 is( $rs_user_dvd->count, 1, 'user owns 1 dvd' );
352 $updates = {
353 id => $user->id,
354 owned_dvds => undef,
355 };
356 $user = $user_rs->recursive_update($updates);
357 is( $user->owned_dvds->count, 0, 'user owns no dvds' );
358 is( $dvd_rs->search( { dvd_id => { -in => \@user_dvd_ids } } )->count,
359 0, 'owned dvds deleted' );
360
361 # $updates = {
362 # name => 'Test name 1',
363 # };
364 # $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
365 # is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
366 }
This page took 0.062152 seconds and 4 git commands to generate.