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