]>
Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/blob - t/lib/RunTests.pm
3 use Exporter
'import'; # gives you Exporter's import() method directly
4 @EXPORT = qw(run_tests);
9 use DBIx
::Class
::ResultSet
::RecursiveUpdate
;
16 my $dvd_rs = $schema->resultset('Dvd');
17 my $user_rs = $schema->resultset('User');
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;
27 $dvd_rs->search( { dvd_id
=> 1 } )->recursive_update( {
28 owner
=> { username
=> 'aaa' }
33 my $u = $user_rs->find( $dvd_rs->find( 1 )->owner->id );
34 is( $u->username, 'aaa', 'fixed_fields pre 0.21 api ok' );
37 $dvd_rs->search( { dvd_id
=> 1 } )->recursive_update( {
38 owner
=> { username
=> 'bbb' }
41 fixed_fields
=> [ 'dvd_id' ],
45 $u = $user_rs->find( $dvd_rs->find( 1 )->owner->id );
46 is( $u->username, 'bbb', 'fixed_fields 0.21+ api ok' );
50 # try to create with a not existing rel
52 name
=> 'Test for nonexisting rel',
53 username
=> 'nonexisting_rel',
54 password
=> 'whatever',
55 nonexisting
=> { foo
=> 'bar' },
59 my $user = $user_rs->recursive_update($updates);
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' );
66 # for future use when we switch from warn to throw_exception
67 # eval { $user_rs->recursive_update($updates); };
70 # qr/No such column, relationship, many-to-many helper accessor or generic accessor 'nonexisting'/,
71 # 'nonexisting column, accessor, relationship fails'
77 # try to create with a not existing rel but suppressed warning
79 name
=> 'Test for nonexisting rel with suppressed warning',
80 username
=> 'suppressed_nonexisting_rel',
81 password
=> 'whatever',
82 nonexisting
=> { foo
=> 'bar' },
87 $user_rs->recursive_update( $updates,
88 { unknown_params_ok
=> 1 } );
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' );
98 # try to create with a not existing rel, suppressed warning but storage debugging
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' },
106 my $debug = $user_rs->result_source->storage->debug;
107 $user_rs->result_source->storage->debug(1);
111 $user_rs->recursive_update( $updates,
112 { unknown_params_ok
=> 1 } );
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' );
119 $user_rs->result_source->storage->debug($debug);
122 # creating new record linked to some old record
124 name
=> 'Test name 2',
125 viewings
=> [ { user_id
=> $owner->id } ],
126 owner
=> { id
=> $another_owner->id },
129 my $new_dvd = $dvd_rs->recursive_update($updates);
131 is( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' );
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' );
139 # creating new records
141 tags
=> [ '2', { id
=> '3' } ],
144 current_borrower
=> {
146 username
=> 'temp name',
147 password
=> 'temp name',
149 liner_notes
=> { notes
=> 'test note', },
150 like_has_many
=> [ { key2
=> 1 } ],
152 { onekey
=> { name
=> 'aaaaa' },
158 my $dvd = $dvd_rs->recursive_update($updates);
159 $expected_user_count++;
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' );
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 } ),
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'
182 local $TODO = 'value of fk from a multi relationship';
183 is( $dvd->twokeysfk, $onekey->id, 'twokeysfk in Dvd' );
185 is( $dvd->name, 'Test name', 'Dvd name set' );
187 # changing existing records
188 my $num_of_users = $user_rs->count;
190 id
=> $dvd->dvd_id, # id instead of dvd_id
193 owner
=> $another_owner->id,
194 current_borrower
=> {
195 username
=> 'new name a',
196 name
=> 'new name a',
197 password
=> 'new password a',
199 liner_notes
=> { notes
=> 'test note changed', },
202 my $dvd_updated = $dvd_rs->recursive_update($updates);
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,
214 'might_have record changed'
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' );
222 $new_dvd->update( { name
=> 'New Test Name' } );
224 id
=> $new_dvd->dvd_id, # id instead of dvd_id
225 like_has_many
=> [ { dvd_name
=> $dvd->name, key2
=> 1 } ],
227 $dvd_updated = $dvd_rs->recursive_update($updates);
228 ok
( $schema->resultset('Twokeys')
229 ->find( { dvd_name
=> 'New Test Name', key2
=> 1 } ),
232 ok
( !$schema->resultset('Twokeys')
233 ->find( { dvd_name
=> $dvd->name, key2
=> 1 } ),
240 username
=> 'temp username',
241 password
=> 'temp username',
243 { 'name' => 'temp name 1',
246 { 'name' => 'temp name 2',
252 my $user = $user_rs->recursive_update($updates);
253 $expected_user_count++;
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' );
267 # update has_many where foreign cols aren't nullable
271 street
=> "101 Main Street",
275 owned_dvds
=> [ { id
=> 1, }, ]
277 $user = $user_rs->recursive_update($updates);
278 is( $schema->resultset('Address')->search( { user_id
=> $user->id } )
281 'the right number of addresses'
283 $dvd = $dvd_rs->find(1);
284 is( $dvd->get_column('owner'), $user->id, 'foreign key set' );
286 $dvd_rs->update( { current_borrower
=> $user->id } );
287 ok
( $user->borrowed_dvds->count > 1, 'Precond' );
290 borrowed_dvds
=> [ { id
=> $dvd->id }, ]
293 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
294 resultset
=> $user_rs,
296 if_not_submitted
=> 'set_to_null',
298 is( $user->borrowed_dvds->count, 1, 'set_to_null' );
300 # has_many where foreign cols are nullable
301 $dvd_rs->update( { current_borrower
=> $user->id } );
304 borrowed_dvds
=> [ { id
=> $dvd->id }, ]
307 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
308 resultset
=> $user_rs,
310 if_not_submitted
=> 'delete',
312 is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' );
314 @tags = $schema->resultset('Tag')->all;
316 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
317 resultset
=> $schema->resultset('Dvd'),
319 id
=> $dvd->dvd_id, # id instead of dvd_id
321 { id
=> $tags[0]->id, file
=> 'file0' },
322 { id
=> $tags[1]->id, file
=> 'file1' }
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',
335 name
=> 'Amiri Barksdale',
339 ok
( my $new_user = $user_rs->recursive_update($new_person) );
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' );
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' );
355 # name => 'Test name 1',
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' );
This page took 0.065465 seconds and 5 git commands to generate.