]>
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 } )
28 ->recursive_update( { owner
=> { username
=> 'aaa' } }, ['dvd_id'] );
30 my $u = $user_rs->find( $dvd_rs->find(1)->owner->id );
31 is( $u->username, 'aaa', 'fixed_fields pre 0.21 api ok' );
34 $dvd_rs->search( { dvd_id
=> 1 } )->recursive_update(
35 { owner
=> { username
=> 'bbb' } },
36 { fixed_fields
=> ['dvd_id'], }
39 $u = $user_rs->find( $dvd_rs->find(1)->owner->id );
40 is( $u->username, 'bbb', 'fixed_fields 0.21+ api ok' );
44 # try to create with a not existing rel
46 name
=> 'Test for nonexisting rel',
47 username
=> 'nonexisting_rel',
48 password
=> 'whatever',
49 nonexisting
=> { foo
=> 'bar' },
53 my $user = $user_rs->recursive_update($updates);
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' );
60 # for future use when we switch from warn to throw_exception
61 # eval { $user_rs->recursive_update($updates); };
64 # qr/No such column, relationship, many-to-many helper accessor or generic accessor 'nonexisting'/,
65 # 'nonexisting column, accessor, relationship fails'
71 # try to create with a not existing rel but suppressed warning
73 name
=> 'Test for nonexisting rel with suppressed warning',
74 username
=> 'suppressed_nonexisting_rel',
75 password
=> 'whatever',
76 nonexisting
=> { foo
=> 'bar' },
81 $user_rs->recursive_update( $updates,
82 { unknown_params_ok
=> 1 } );
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' );
92 # try to create with a not existing rel, suppressed warning but storage debugging
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' },
100 my $debug = $user_rs->result_source->storage->debug;
101 $user_rs->result_source->storage->debug(1);
105 $user_rs->recursive_update( $updates,
106 { unknown_params_ok
=> 1 } );
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' );
113 $user_rs->result_source->storage->debug($debug);
116 # creating new record linked to some old record
118 name
=> 'Test name 2',
119 viewings
=> [ { user_id
=> $owner->id } ],
120 owner
=> { id
=> $another_owner->id },
123 my $new_dvd = $dvd_rs->recursive_update($updates);
125 is( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' );
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' );
133 # creating new records
135 tags
=> [ '2', { id
=> '3' } ],
138 current_borrower
=> {
140 username
=> 'temp name',
141 password
=> 'temp name',
143 liner_notes
=> { notes
=> 'test note', },
144 like_has_many
=> [ { key2
=> 1 } ],
146 { onekey
=> { name
=> 'aaaaa' },
152 my $dvd = $dvd_rs->recursive_update($updates);
153 $expected_user_count++;
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' );
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 } ),
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'
176 local $TODO = 'value of fk from a multi relationship';
177 is( $dvd->twokeysfk, $onekey->id, 'twokeysfk in Dvd' );
179 is( $dvd->name, 'Test name', 'Dvd name set' );
181 # changing existing records
182 my $num_of_users = $user_rs->count;
184 id
=> $dvd->dvd_id, # id instead of dvd_id
187 owner
=> $another_owner->id,
188 current_borrower
=> {
189 username
=> 'new name a',
190 name
=> 'new name a',
191 password
=> 'new password a',
193 liner_notes
=> { notes
=> 'test note changed', },
196 my $dvd_updated = $dvd_rs->recursive_update($updates);
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,
209 'might_have record changed'
213 $dvd_rs->recursive_update( { id
=> $dvd->dvd_id, tags
=> [ 1, 2 ] } );
214 is_deeply
( [ map { $_->id } $dvd_with_tags->tags ], [ 1, 2 ],
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' );
221 $new_dvd->update( { name
=> 'New Test Name' } );
223 id
=> $new_dvd->dvd_id, # id instead of dvd_id
224 like_has_many
=> [ { dvd_name
=> $dvd->name, key2
=> 1 } ],
226 $dvd_updated = $dvd_rs->recursive_update($updates);
227 ok
( $schema->resultset('Twokeys')
228 ->find( { dvd_name
=> 'New Test Name', key2
=> 1 } ),
231 ok
( !$schema->resultset('Twokeys')
232 ->find( { dvd_name
=> $dvd->name, key2
=> 1 } ),
239 username
=> 'temp username',
240 password
=> 'temp username',
242 { 'name' => 'temp name 1',
245 { 'name' => 'temp name 2',
251 my $user = $user_rs->recursive_update($updates);
252 $expected_user_count++;
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' );
266 # update has_many where foreign cols aren't nullable
270 street
=> "101 Main Street",
274 owned_dvds
=> [ { id
=> 1, }, ]
276 $user = $user_rs->recursive_update($updates);
277 is( $schema->resultset('Address')->search( { user_id
=> $user->id } )
280 'the right number of addresses'
282 $dvd = $dvd_rs->find(1);
283 is( $dvd->get_column('owner'), $user->id, 'foreign key set' );
285 $dvd_rs->update( { current_borrower
=> $user->id } );
286 ok
( $user->borrowed_dvds->count > 1, 'Precond' );
289 borrowed_dvds
=> [ { id
=> $dvd->id }, ]
292 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
293 resultset
=> $user_rs,
295 if_not_submitted
=> 'set_to_null',
297 is( $user->borrowed_dvds->count, 1, 'set_to_null' );
299 # has_many where foreign cols are nullable
300 $dvd_rs->update( { current_borrower
=> $user->id } );
303 borrowed_dvds
=> [ { id
=> $dvd->id }, ]
306 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
307 resultset
=> $user_rs,
309 if_not_submitted
=> 'delete',
311 is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' );
313 @tags = $schema->resultset('Tag')->all;
315 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
316 resultset
=> $schema->resultset('Dvd'),
318 id
=> $dvd->dvd_id, # id instead of dvd_id
320 { id
=> $tags[0]->id, file
=> 'file0' },
321 { id
=> $tags[1]->id, file
=> 'file1' }
325 $tags[$_]->discard_changes for 0 .. 1;
326 is( $tags[0]->file, 'file0', 'file set in tag' );
327 is( $tags[1]->file, 'file1', 'file set in tag' );
328 my @rel_tags = $dvd_updated->tags;
329 is( scalar @rel_tags, 2, 'tags related' );
330 ok
( $rel_tags[0]->file eq 'file0' || $rel_tags[0]->file eq 'file1',
334 name
=> 'Amiri Barksdale',
338 ok
( my $new_user = $user_rs->recursive_update($new_person) );
340 # delete has_many where foreign cols aren't nullable
341 my $rs_user_dvd = $user->owned_dvds;
342 my @user_dvd_ids = map { $_->id } $rs_user_dvd->all;
343 is( $rs_user_dvd->count, 1, 'user owns 1 dvd' );
348 $user = $user_rs->recursive_update($updates);
349 is( $user->owned_dvds->count, 0, 'user owns no dvds' );
350 is( $dvd_rs->search( { dvd_id
=> { -in => \
@user_dvd_ids } } )->count,
351 0, 'owned dvds deleted' );
354 # name => 'Test name 1',
356 # $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
357 # is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
This page took 0.072006 seconds and 5 git commands to generate.