c5c1c6ab0c6779f7ea23e3ca4620711574fbf575
[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 => 53;
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 my $dvd_with_tags = $dvd_rs->recursive_update({ id => $dvd->dvd_id, tags => [1, 2] });
185 is_deeply( [ map { $_->id } $dvd_with_tags->tags ], [ 1, 2 ], 'Tags set' );
186 my $dvd_without_tags = $dvd_rs->recursive_update({ id => $dvd->dvd_id, tags => undef });
187 is( $dvd_without_tags->tags->count, 0, 'Tags deleted when m2m accessor set to undef' );
188
189 $new_dvd->update( { name => 'New Test Name' } );
190 $updates = {
191 id => $new_dvd->dvd_id, # id instead of dvd_id
192 like_has_many => [ { dvd_name => $dvd->name, key2 => 1 } ],
193 };
194 $dvd_updated = $dvd_rs->recursive_update($updates);
195 ok( $schema->resultset('Twokeys')
196 ->find( { dvd_name => 'New Test Name', key2 => 1 } ),
197 'Twokeys updated'
198 );
199 ok( !$schema->resultset('Twokeys')
200 ->find( { dvd_name => $dvd->name, key2 => 1 } ),
201 'Twokeys updated'
202 );
203
204 # repeatable
205 $updates = {
206 name => 'temp name',
207 username => 'temp username',
208 password => 'temp username',
209 owned_dvds => [
210 { 'name' => 'temp name 1',
211 'tags' => [ 1, 2 ],
212 },
213 { 'name' => 'temp name 2',
214 'tags' => [ 2, 3 ],
215 }
216 ]
217 };
218
219 my $user = $user_rs->recursive_update($updates);
220 $expected_user_count++;
221
222 is( $schema->resultset('User')->count,
223 $expected_user_count, "New user created" );
224 is( $dvd_rs->count, $initial_dvd_count + 4, 'Dvds created' );
225 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
226 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
227 ok( $owned_dvds{'temp name 1'},
228 'Name in a has_many related record saved' );
229 my @tags = $owned_dvds{'temp name 1'}->tags;
230 is( scalar @tags, 2, 'Tags in has_many related record saved' );
231 ok( $owned_dvds{'temp name 2'},
232 'Second name in a has_many related record saved' );
233
234 # update has_many where foreign cols aren't nullable
235 $updates = {
236 id => $user->id,
237 address => {
238 street => "101 Main Street",
239 city => "Podunk",
240 state => "New York"
241 },
242 owned_dvds => [ { id => 1, }, ]
243 };
244 $user = $user_rs->recursive_update($updates);
245 is( $schema->resultset('Address')->search( { user_id => $user->id } )
246 ->count,
247 1,
248 'the right number of addresses'
249 );
250 $dvd = $dvd_rs->find(1);
251 is( $dvd->get_column('owner'), $user->id, 'foreign key set' );
252
253 $dvd_rs->update( { current_borrower => $user->id } );
254 ok( $user->borrowed_dvds->count > 1, 'Precond' );
255 $updates = {
256 id => $user->id,
257 borrowed_dvds => [ { id => $dvd->id }, ]
258 };
259 $user =
260 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
261 resultset => $user_rs,
262 updates => $updates,
263 if_not_submitted => 'set_to_null',
264 );
265 is( $user->borrowed_dvds->count, 1, 'set_to_null' );
266
267 # has_many where foreign cols are nullable
268 $dvd_rs->update( { current_borrower => $user->id } );
269 $updates = {
270 id => $user->id,
271 borrowed_dvds => [ { id => $dvd->id }, ]
272 };
273 $user =
274 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
275 resultset => $user_rs,
276 updates => $updates,
277 if_not_submitted => 'delete',
278 );
279 is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' );
280
281 @tags = $schema->resultset('Tag')->all;
282 $dvd_updated =
283 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
284 resultset => $schema->resultset('Dvd'),
285 updates => {
286 id => $dvd->dvd_id, # id instead of dvd_id
287 tags => [
288 { id => $tags[0]->id, file => 'file0' },
289 { id => $tags[1]->id, file => 'file1' }
290 ],
291 }
292 );
293 $tags[$_]->discard_changes for 0 .. 1;
294 is( $tags[0]->file, 'file0', 'file set in tag' );
295 is( $tags[1]->file, 'file1', 'file set in tag' );
296 my @rel_tags = $dvd_updated->tags;
297 is( scalar @rel_tags, 2, 'tags related' );
298 ok( $rel_tags[0]->file eq 'file0' || $rel_tags[0]->file eq 'file1',
299 'tags related' );
300
301 my $new_person = {
302 name => 'Amiri Barksdale',
303 username => 'amiri',
304 password => 'amiri',
305 };
306 ok( my $new_user = $user_rs->recursive_update($new_person) );
307
308 # delete has_many where foreign cols aren't nullable
309 my $rs_user_dvd = $user->owned_dvds;
310 my @user_dvd_ids = map { $_->id } $rs_user_dvd->all;
311 is( $rs_user_dvd->count, 1, 'user owns 1 dvd' );
312 $updates = {
313 id => $user->id,
314 owned_dvds => undef,
315 };
316 $user = $user_rs->recursive_update($updates);
317 is( $user->owned_dvds->count, 0, 'user owns no dvds' );
318 is( $dvd_rs->search( { dvd_id => { -in => \@user_dvd_ids } } )->count,
319 0, 'owned dvds deleted' );
320
321 # $updates = {
322 # name => 'Test name 1',
323 # };
324 # $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
325 # is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
326 }
This page took 0.060637 seconds and 3 git commands to generate.