]>
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);
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 # try to create with a not existing rel
25 name
=> 'Test name for nonexisting rel',
26 username
=> 'nonexisting_rel',
27 password
=> 'whatever',
28 nonexisting
=> { foo
=> 'bar' },
30 eval { my $nonexisting_user = $user_rs->recursive_update($updates); };
33 qr/No such column, relationship, many-to-many helper accessor or generic accessor 'nonexisting'/,
34 'nonexisting column, accessor, relationship fails'
37 # creating new record linked to some old record
39 name
=> 'Test name 2',
40 viewings
=> [ { user_id
=> $owner->id } ],
41 owner
=> { id
=> $another_owner->id },
44 my $new_dvd = $dvd_rs->recursive_update($updates);
46 is( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' );
47 is( $schema->resultset('User')->count,
48 $initial_user_count, "No new user created" );
49 is( $new_dvd->name, 'Test name 2', 'Dvd name set' );
50 is( $new_dvd->owner->id, $another_owner->id, 'Owner set' );
51 is( $new_dvd->viewings->count, 1, 'Viewing created' );
53 # creating new records
57 tags
=> [ '2', { id
=> '3' } ],
62 username
=> 'temp name',
63 password
=> 'temp name',
65 liner_notes
=> { notes
=> 'test note', },
66 like_has_many
=> [ { key2
=> 1 } ],
68 { onekey
=> { name
=> 'aaaaa' },
74 my $dvd = $dvd_rs->recursive_update($updates);
76 is( $dvd_rs->count, $initial_dvd_count + 2, 'Dvd created' );
77 is( $schema->resultset('User')->count,
78 $initial_user_count + 1,
79 "One new user created"
81 is( $dvd->name, 'Test name', 'Dvd name set' );
82 is_deeply
( [ map { $_->id } $dvd->tags ], [ '2', '3' ], 'Tags set' );
83 is( $dvd->owner->id, $owner->id, 'Owner set' );
85 is( $dvd->current_borrower->name, 'temp name', 'Related record created' );
86 is( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
87 ok
( $schema->resultset('Twokeys')
88 ->find( { dvd_name
=> 'Test name', key2
=> 1 } ),
92 $schema->resultset('Onekey')->search( name
=> 'aaaaa' )->first;
93 ok
( $onekey, 'Onekey created' );
94 ok
( $schema->resultset('Twokeys_belongsto')
95 ->find( { key1
=> $onekey->id, key2
=> 1 } ),
96 'Twokeys_belongsto created'
98 is( $dvd->name, 'Test name', 'Dvd name set' );
100 # changing existing records
101 my $num_of_users = $user_rs->count;
103 id
=> $dvd->dvd_id, # id instead of dvd_id
107 'owner' => $another_owner->id,
108 current_borrower
=> {
109 username
=> 'new name a',
110 name
=> 'new name a',
111 password
=> 'new password a',
113 liner_notes
=> { notes
=> 'test note changed', },
116 my $dvd_updated = $dvd_rs->recursive_update($updates);
118 is( $dvd_updated->dvd_id, $dvd->dvd_id, 'Pk from "id"' );
119 is( $schema->resultset('User')->count,
120 $initial_user_count + 1,
121 "No new user created"
123 is( $dvd_updated->name, undef, 'Dvd name deleted' );
124 is( $dvd_updated->owner->id, $another_owner->id, 'Owner updated' );
125 is( $dvd_updated->current_borrower->name,
126 'new name a', 'Related record modified' );
127 is( $dvd_updated->tags->count, 0, 'Tags deleted' );
128 is( $dvd_updated->liner_notes->notes,
130 'might_have record changed'
133 $new_dvd->update( { name
=> 'New Test Name' } );
135 id
=> $new_dvd->dvd_id, # id instead of dvd_id
136 like_has_many
=> [ { dvd_name
=> $dvd->name, key2
=> 1 } ],
138 $dvd_updated = $dvd_rs->recursive_update($updates);
139 ok
( $schema->resultset('Twokeys')
140 ->find( { dvd_name
=> 'New Test Name', key2
=> 1 } ),
143 ok
( !$schema->resultset('Twokeys')
144 ->find( { dvd_name
=> $dvd->name, key2
=> 1 } ),
151 username
=> 'temp username',
152 password
=> 'temp username',
154 { 'name' => 'temp name 1',
157 { 'name' => 'temp name 2',
163 my $user = $user_rs->recursive_update($updates);
164 is( $schema->resultset('User')->count,
165 $initial_user_count + 2,
168 is( $dvd_rs->count, $initial_dvd_count + 4, 'Dvds created' );
169 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
170 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
171 ok
( $owned_dvds{'temp name 1'},
172 'Name in a has_many related record saved' );
173 my @tags = $owned_dvds{'temp name 1'}->tags;
174 is( scalar @tags, 2, 'Tags in has_many related record saved' );
175 ok
( $owned_dvds{'temp name 2'},
176 'Second name in a has_many related record saved' );
178 # update has_many where foreign cols aren't nullable
182 street
=> "101 Main Street",
186 owned_dvds
=> [ { id
=> 1, }, ]
188 $user = $user_rs->recursive_update($updates);
189 is( $schema->resultset('Address')->search( { user_id
=> $user->id } )
192 'the right number of addresses'
194 $dvd = $dvd_rs->find(1);
195 is( $dvd->get_column('owner'), $user->id, 'foreign key set' );
197 # # delete has_many where foreign cols aren't nullable
200 # owned_dvds => undef,
202 # $user = $user_rs->recursive_update( $updates );
203 # ok ( !$dvd_rs->find( 1 ), 'owned dvd deleted');
205 $dvd_rs->update( { current_borrower
=> $user->id } );
206 ok
( $user->borrowed_dvds->count > 1, 'Precond' );
209 borrowed_dvds
=> [ { id
=> $dvd->id }, ]
212 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
213 resultset
=> $user_rs,
215 if_not_submitted
=> 'set_to_null',
217 is( $user->borrowed_dvds->count, 1, 'set_to_null' );
219 # has_many where foreign cols are nullable
220 $dvd_rs->update( { current_borrower
=> $user->id } );
223 borrowed_dvds
=> [ { id
=> $dvd->id }, ]
226 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
227 resultset
=> $user_rs,
229 if_not_submitted
=> 'delete',
231 is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' );
233 @tags = $schema->resultset('Tag')->search();
235 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
236 resultset
=> $schema->resultset('Dvd'),
238 id
=> $dvd->dvd_id, # id instead of dvd_id
240 { id
=> $tags[0]->id, file
=> 'file0' },
241 { id
=> $tags[1]->id, file
=> 'file1' }
245 $tags[$_]->discard_changes for 0 .. 1;
246 is( $tags[0]->file, 'file0', 'file set in tag' );
247 is( $tags[1]->file, 'file1', 'file set in tag' );
248 my @rel_tags = $dvd_updated->tags;
249 is( scalar @rel_tags, 2, 'tags related' );
250 ok
( $rel_tags[0]->file eq 'file0' || $rel_tags[0]->file eq 'file1',
254 name
=> 'Amiri Barksdale',
258 ok
( my $new_user = $user_rs->recursive_update($new_person) );
260 #print STDERR Dumper $new_user;
263 # name => 'Test name 1',
265 # $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
266 # is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
This page took 0.053993 seconds and 4 git commands to generate.