test for fixed_fields
[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 DBIx::Class::ResultSet::RecursiveUpdate;
8
9 sub run_tests {
10 my $schema = shift;
11
12 plan tests => 47;
13
14 my $dvd_rs = $schema->resultset('Dvd');
15 my $user_rs = $schema->resultset('User');
16
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;
21 my $updates;
22
23 $dvd_rs->search( { dvd_id => 1 } )->recursive_update( {
24 owner => { username => 'aaa' }
25 },
26 [ 'dvd_id' ]
27 );
28
29 my $u = $user_rs->find( $dvd_rs->find( 1 )->owner->id );
30 is( $u->username, 'aaa', 'fixed_fields' );
31
32 # try to create with a not existing rel
33 $updates = {
34 name => 'Test name for nonexisting rel',
35 username => 'nonexisting_rel',
36 password => 'whatever',
37 nonexisting => { foo => 'bar' },
38 };
39 eval { my $nonexisting_user = $user_rs->recursive_update($updates); };
40 like(
41 $@,
42 qr/No such column, relationship, many-to-many helper accessor or generic accessor 'nonexisting'/,
43 'nonexisting column, accessor, relationship fails'
44 );
45
46 # creating new record linked to some old record
47 $updates = {
48 name => 'Test name 2',
49 viewings => [ { user_id => $owner->id } ],
50 owner => { id => $another_owner->id },
51 };
52
53 my $new_dvd = $dvd_rs->recursive_update($updates);
54
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' );
61
62 # creating new records
63 $updates = {
64
65 #aaaa => undef,
66 tags => [ '2', { id => '3' } ],
67 name => 'Test name',
68 owner => $owner,
69 current_borrower => {
70 name => 'temp name',
71 username => 'temp name',
72 password => 'temp name',
73 },
74 liner_notes => { notes => 'test note', },
75 like_has_many => [ { key2 => 1 } ],
76 like_has_many2 => [
77 { onekey => { name => 'aaaaa' },
78 key2 => 1
79 }
80 ],
81 };
82
83 my $dvd = $dvd_rs->recursive_update($updates);
84
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"
89 );
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' );
93
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 } ),
98 'Twokeys created'
99 );
100 my $onekey =
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'
106 );
107 TODO: {
108 local $TODO = 'value of fk from a multi relationship';
109 is( $dvd->twokeysfk, $onekey->id, 'twokeysfk in Dvd' );
110 };
111 is( $dvd->name, 'Test name', 'Dvd name set' );
112
113 # changing existing records
114 my $num_of_users = $user_rs->count;
115 $updates = {
116 id => $dvd->dvd_id, # id instead of dvd_id
117 #aaaa => undef,
118 name => undef,
119 tags => [],
120 'owner' => $another_owner->id,
121 current_borrower => {
122 username => 'new name a',
123 name => 'new name a',
124 password => 'new password a',
125 },
126 liner_notes => { notes => 'test note changed', },
127
128 };
129 my $dvd_updated = $dvd_rs->recursive_update($updates);
130
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"
135 );
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,
142 'test note changed',
143 'might_have record changed'
144 );
145
146 $new_dvd->update( { name => 'New Test Name' } );
147 $updates = {
148 id => $new_dvd->dvd_id, # id instead of dvd_id
149 like_has_many => [ { dvd_name => $dvd->name, key2 => 1 } ],
150 };
151 $dvd_updated = $dvd_rs->recursive_update($updates);
152 ok( $schema->resultset('Twokeys')
153 ->find( { dvd_name => 'New Test Name', key2 => 1 } ),
154 'Twokeys updated'
155 );
156 ok( !$schema->resultset('Twokeys')
157 ->find( { dvd_name => $dvd->name, key2 => 1 } ),
158 'Twokeys updated'
159 );
160
161 # repeatable
162 $updates = {
163 name => 'temp name',
164 username => 'temp username',
165 password => 'temp username',
166 owned_dvds => [
167 { 'name' => 'temp name 1',
168 'tags' => [ 1, 2 ],
169 },
170 { 'name' => 'temp name 2',
171 'tags' => [ 2, 3 ],
172 }
173 ]
174 };
175
176 my $user = $user_rs->recursive_update($updates);
177 is( $schema->resultset('User')->count,
178 $initial_user_count + 2,
179 "New user created"
180 );
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' );
190
191 # update has_many where foreign cols aren't nullable
192 $updates = {
193 id => $user->id,
194 address => {
195 street => "101 Main Street",
196 city => "Podunk",
197 state => "New York"
198 },
199 owned_dvds => [ { id => 1, }, ]
200 };
201 $user = $user_rs->recursive_update($updates);
202 is( $schema->resultset('Address')->search( { user_id => $user->id } )
203 ->count,
204 1,
205 'the right number of addresses'
206 );
207 $dvd = $dvd_rs->find(1);
208 is( $dvd->get_column('owner'), $user->id, 'foreign key set' );
209
210 $dvd_rs->update( { current_borrower => $user->id } );
211 ok( $user->borrowed_dvds->count > 1, 'Precond' );
212 $updates = {
213 id => $user->id,
214 borrowed_dvds => [ { id => $dvd->id }, ]
215 };
216 $user =
217 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
218 resultset => $user_rs,
219 updates => $updates,
220 if_not_submitted => 'set_to_null',
221 );
222 is( $user->borrowed_dvds->count, 1, 'set_to_null' );
223
224 # has_many where foreign cols are nullable
225 $dvd_rs->update( { current_borrower => $user->id } );
226 $updates = {
227 id => $user->id,
228 borrowed_dvds => [ { id => $dvd->id }, ]
229 };
230 $user =
231 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
232 resultset => $user_rs,
233 updates => $updates,
234 if_not_submitted => 'delete',
235 );
236 is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' );
237
238 @tags = $schema->resultset('Tag')->search();
239 $dvd_updated =
240 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
241 resultset => $schema->resultset('Dvd'),
242 updates => {
243 id => $dvd->dvd_id, # id instead of dvd_id
244 tags => [
245 { id => $tags[0]->id, file => 'file0' },
246 { id => $tags[1]->id, file => 'file1' }
247 ],
248 }
249 );
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',
256 'tags related' );
257
258 my $new_person = {
259 name => 'Amiri Barksdale',
260 username => 'amiri',
261 password => 'amiri',
262 };
263 ok( my $new_user = $user_rs->recursive_update($new_person) );
264
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');
269 $updates = {
270 id => $user->id,
271 owned_dvds => undef,
272 };
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' );
276
277 # $updates = {
278 # name => 'Test name 1',
279 # };
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' );
282 }
This page took 0.056824 seconds and 4 git commands to generate.