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