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