]>
Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/blob - t/lib/RunTests.pm
d0fce5b74b594cac099cd64bd7eca35c3517fdad
3 use Exporter
'import'; # gives you Exporter's import() method directly
4 @EXPORT = qw(run_tests);
7 use DBIx
::Class
::ResultSet
::RecursiveUpdate
;
14 my $dvd_rs = $schema->resultset('Dvd');
15 my $user_rs = $schema->resultset('User');
17 my $owner = $user_rs->next;
18 my $another_owner = $user_rs->next;
19 my $initial_user_count = $user_rs->count;
20 my $initial_dvd_count = $dvd_rs->count;
23 $dvd_rs->search( { dvd_id
=> 1 } )->recursive_update( {
24 owner
=> { username
=> 'aaa' }
29 my $u = $user_rs->find( $dvd_rs->find( 1 )->owner->id );
30 is( $u->username, 'aaa', 'fixed_fields' );
32 # try to create with a not existing rel
34 name
=> 'Test name for nonexisting rel',
35 username
=> 'nonexisting_rel',
36 password
=> 'whatever',
37 nonexisting
=> { foo
=> 'bar' },
39 eval { my $nonexisting_user = $user_rs->recursive_update($updates); };
42 qr/No such column, relationship, many-to-many helper accessor or generic accessor 'nonexisting'/,
43 'nonexisting column, accessor, relationship fails'
46 # creating new record linked to some old record
48 name
=> 'Test name 2',
49 viewings
=> [ { user_id
=> $owner->id } ],
50 owner
=> { id
=> $another_owner->id },
53 my $new_dvd = $dvd_rs->recursive_update($updates);
55 is( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' );
56 is( $schema->resultset('User')->count,
57 $initial_user_count, "No new user created" );
58 is( $new_dvd->name, 'Test name 2', 'Dvd name set' );
59 is( $new_dvd->owner->id, $another_owner->id, 'Owner set' );
60 is( $new_dvd->viewings->count, 1, 'Viewing created' );
62 # creating new records
66 tags
=> [ '2', { id
=> '3' } ],
71 username
=> 'temp name',
72 password
=> 'temp name',
74 liner_notes
=> { notes
=> 'test note', },
75 like_has_many
=> [ { key2
=> 1 } ],
77 { onekey
=> { name
=> 'aaaaa' },
83 my $dvd = $dvd_rs->recursive_update($updates);
85 is( $dvd_rs->count, $initial_dvd_count + 2, 'Dvd created' );
86 is( $schema->resultset('User')->count,
87 $initial_user_count + 1,
88 "One new user created"
90 is( $dvd->name, 'Test name', 'Dvd name set' );
91 is_deeply
( [ map { $_->id } $dvd->tags ], [ '2', '3' ], 'Tags set' );
92 is( $dvd->owner->id, $owner->id, 'Owner set' );
94 is( $dvd->current_borrower->name, 'temp name', 'Related record created' );
95 is( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
96 ok
( $schema->resultset('Twokeys')
97 ->find( { dvd_name
=> 'Test name', key2
=> 1 } ),
101 $schema->resultset('Onekey')->search( name
=> 'aaaaa' )->first;
102 ok
( $onekey, 'Onekey created' );
103 ok
( $schema->resultset('Twokeys_belongsto')
104 ->find( { key1
=> $onekey->id, key2
=> 1 } ),
105 'Twokeys_belongsto created'
108 local $TODO = 'value of fk from a multi relationship';
109 is( $dvd->twokeysfk, $onekey->id, 'twokeysfk in Dvd' );
111 is( $dvd->name, 'Test name', 'Dvd name set' );
113 # changing existing records
114 my $num_of_users = $user_rs->count;
116 id
=> $dvd->dvd_id, # id instead of dvd_id
120 'owner' => $another_owner->id,
121 current_borrower
=> {
122 username
=> 'new name a',
123 name
=> 'new name a',
124 password
=> 'new password a',
126 liner_notes
=> { notes
=> 'test note changed', },
129 my $dvd_updated = $dvd_rs->recursive_update($updates);
131 is( $dvd_updated->dvd_id, $dvd->dvd_id, 'Pk from "id"' );
132 is( $schema->resultset('User')->count,
133 $initial_user_count + 1,
134 "No new user created"
136 is( $dvd_updated->name, undef, 'Dvd name deleted' );
137 is( $dvd_updated->owner->id, $another_owner->id, 'Owner updated' );
138 is( $dvd_updated->current_borrower->name,
139 'new name a', 'Related record modified' );
140 is( $dvd_updated->tags->count, 0, 'Tags deleted' );
141 is( $dvd_updated->liner_notes->notes,
143 'might_have record changed'
146 $new_dvd->update( { name
=> 'New Test Name' } );
148 id
=> $new_dvd->dvd_id, # id instead of dvd_id
149 like_has_many
=> [ { dvd_name
=> $dvd->name, key2
=> 1 } ],
151 $dvd_updated = $dvd_rs->recursive_update($updates);
152 ok
( $schema->resultset('Twokeys')
153 ->find( { dvd_name
=> 'New Test Name', key2
=> 1 } ),
156 ok
( !$schema->resultset('Twokeys')
157 ->find( { dvd_name
=> $dvd->name, key2
=> 1 } ),
164 username
=> 'temp username',
165 password
=> 'temp username',
167 { 'name' => 'temp name 1',
170 { 'name' => 'temp name 2',
176 my $user = $user_rs->recursive_update($updates);
177 is( $schema->resultset('User')->count,
178 $initial_user_count + 2,
181 is( $dvd_rs->count, $initial_dvd_count + 4, 'Dvds created' );
182 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
183 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
184 ok
( $owned_dvds{'temp name 1'},
185 'Name in a has_many related record saved' );
186 my @tags = $owned_dvds{'temp name 1'}->tags;
187 is( scalar @tags, 2, 'Tags in has_many related record saved' );
188 ok
( $owned_dvds{'temp name 2'},
189 'Second name in a has_many related record saved' );
191 # update has_many where foreign cols aren't nullable
195 street
=> "101 Main Street",
199 owned_dvds
=> [ { id
=> 1, }, ]
201 $user = $user_rs->recursive_update($updates);
202 is( $schema->resultset('Address')->search( { user_id
=> $user->id } )
205 'the right number of addresses'
207 $dvd = $dvd_rs->find(1);
208 is( $dvd->get_column('owner'), $user->id, 'foreign key set' );
210 $dvd_rs->update( { current_borrower
=> $user->id } );
211 ok
( $user->borrowed_dvds->count > 1, 'Precond' );
214 borrowed_dvds
=> [ { id
=> $dvd->id }, ]
217 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
218 resultset
=> $user_rs,
220 if_not_submitted
=> 'set_to_null',
222 is( $user->borrowed_dvds->count, 1, 'set_to_null' );
224 # has_many where foreign cols are nullable
225 $dvd_rs->update( { current_borrower
=> $user->id } );
228 borrowed_dvds
=> [ { id
=> $dvd->id }, ]
231 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
232 resultset
=> $user_rs,
234 if_not_submitted
=> 'delete',
236 is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' );
238 @tags = $schema->resultset('Tag')->search();
240 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
241 resultset
=> $schema->resultset('Dvd'),
243 id
=> $dvd->dvd_id, # id instead of dvd_id
245 { id
=> $tags[0]->id, file
=> 'file0' },
246 { id
=> $tags[1]->id, file
=> 'file1' }
250 $tags[$_]->discard_changes for 0 .. 1;
251 is( $tags[0]->file, 'file0', 'file set in tag' );
252 is( $tags[1]->file, 'file1', 'file set in tag' );
253 my @rel_tags = $dvd_updated->tags;
254 is( scalar @rel_tags, 2, 'tags related' );
255 ok
( $rel_tags[0]->file eq 'file0' || $rel_tags[0]->file eq 'file1',
259 name
=> 'Amiri Barksdale',
263 ok
( my $new_user = $user_rs->recursive_update($new_person) );
265 # delete has_many where foreign cols aren't nullable
266 my $rs_user_dvd = $user->owned_dvds;
267 my @user_dvd_ids = map { $_->id } $rs_user_dvd->all;
268 is( $rs_user_dvd->count, 1, 'user owns 1 dvd');
273 $user = $user_rs->recursive_update($updates);
274 is( $user->owned_dvds->count, 0, 'user owns no dvds');
275 is( $dvd_rs->search({ dvd_id
=> {-in => \
@user_dvd_ids }})->count, 0, 'owned dvds deleted' );
278 # name => 'Test name 1',
280 # $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
281 # is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
This page took 0.053923 seconds and 3 git commands to generate.