]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/blob - t/lib/RunTests.pm
perltidy
[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 => 42;
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 # try to create with a not existing rel
24 $updates = {
25 name => 'Test name for nonexisting rel',
26 username => 'nonexisting_rel',
27 password => 'whatever',
28 nonexisting => { foo => 'bar' },
29 };
30 eval { my $nonexisting_user = $user_rs->recursive_update($updates); };
31 like(
32 $@,
33 qr/No such column, relationship, many-to-many helper accessor or generic accessor 'nonexisting'/,
34 'nonexisting column, accessor, relationship fails'
35 );
36
37 # creating new record linked to some old record
38 $updates = {
39 name => 'Test name 2',
40 viewings => [ { user_id => $owner->id } ],
41 owner => { id => $another_owner->id },
42 };
43
44 my $new_dvd = $dvd_rs->recursive_update($updates);
45
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' );
52
53 # creating new records
54 $updates = {
55
56 #aaaa => undef,
57 tags => [ '2', { id => '3' } ],
58 name => 'Test name',
59 owner => $owner,
60 current_borrower => {
61 name => 'temp name',
62 username => 'temp name',
63 password => 'temp name',
64 },
65 liner_notes => { notes => 'test note', },
66 like_has_many => [ { key2 => 1 } ],
67 like_has_many2 => [
68 { onekey => { name => 'aaaaa' },
69 key2 => 1
70 }
71 ],
72 };
73
74 my $dvd = $dvd_rs->recursive_update($updates);
75
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"
80 );
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' );
84
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 } ),
89 'Twokeys created'
90 );
91 my $onekey =
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'
97 );
98 is( $dvd->name, 'Test name', 'Dvd name set' );
99
100 # changing existing records
101 my $num_of_users = $user_rs->count;
102 $updates = {
103 id => $dvd->dvd_id, # id instead of dvd_id
104 ####aaaa => undef,
105 name => undef,
106 tags => [],
107 'owner' => $another_owner->id,
108 current_borrower => {
109 username => 'new name a',
110 name => 'new name a',
111 password => 'new password a',
112 },
113 liner_notes => { notes => 'test note changed', },
114
115 };
116 my $dvd_updated = $dvd_rs->recursive_update($updates);
117
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"
122 );
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,
129 'test note changed',
130 'might_have record changed'
131 );
132
133 $new_dvd->update( { name => 'New Test Name' } );
134 $updates = {
135 id => $new_dvd->dvd_id, # id instead of dvd_id
136 like_has_many => [ { dvd_name => $dvd->name, key2 => 1 } ],
137 };
138 $dvd_updated = $dvd_rs->recursive_update($updates);
139 ok( $schema->resultset('Twokeys')
140 ->find( { dvd_name => 'New Test Name', key2 => 1 } ),
141 'Twokeys updated'
142 );
143 ok( !$schema->resultset('Twokeys')
144 ->find( { dvd_name => $dvd->name, key2 => 1 } ),
145 'Twokeys updated'
146 );
147
148 # repeatable
149 $updates = {
150 name => 'temp name',
151 username => 'temp username',
152 password => 'temp username',
153 owned_dvds => [
154 { 'name' => 'temp name 1',
155 'tags' => [ 1, 2 ],
156 },
157 { 'name' => 'temp name 2',
158 'tags' => [ 2, 3 ],
159 }
160 ]
161 };
162
163 my $user = $user_rs->recursive_update($updates);
164 is( $schema->resultset('User')->count,
165 $initial_user_count + 2,
166 "New user created"
167 );
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' );
177
178 # update has_many where foreign cols aren't nullable
179 $updates = {
180 id => $user->id,
181 address => {
182 street => "101 Main Street",
183 city => "Podunk",
184 state => "New York"
185 },
186 owned_dvds => [ { id => 1, }, ]
187 };
188 $user = $user_rs->recursive_update($updates);
189 is( $schema->resultset('Address')->search( { user_id => $user->id } )
190 ->count,
191 1,
192 'the right number of addresses'
193 );
194 $dvd = $dvd_rs->find(1);
195 is( $dvd->get_column('owner'), $user->id, 'foreign key set' );
196
197 # # delete has_many where foreign cols aren't nullable
198 # $updates = {
199 # id => $user->id,
200 # owned_dvds => undef,
201 # };
202 # $user = $user_rs->recursive_update( $updates );
203 # ok ( !$dvd_rs->find( 1 ), 'owned dvd deleted');
204
205 $dvd_rs->update( { current_borrower => $user->id } );
206 ok( $user->borrowed_dvds->count > 1, 'Precond' );
207 $updates = {
208 id => $user->id,
209 borrowed_dvds => [ { id => $dvd->id }, ]
210 };
211 $user =
212 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
213 resultset => $user_rs,
214 updates => $updates,
215 if_not_submitted => 'set_to_null',
216 );
217 is( $user->borrowed_dvds->count, 1, 'set_to_null' );
218
219 # has_many where foreign cols are nullable
220 $dvd_rs->update( { current_borrower => $user->id } );
221 $updates = {
222 id => $user->id,
223 borrowed_dvds => [ { id => $dvd->id }, ]
224 };
225 $user =
226 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
227 resultset => $user_rs,
228 updates => $updates,
229 if_not_submitted => 'delete',
230 );
231 is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' );
232
233 @tags = $schema->resultset('Tag')->search();
234 $dvd_updated =
235 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
236 resultset => $schema->resultset('Dvd'),
237 updates => {
238 id => $dvd->dvd_id, # id instead of dvd_id
239 tags => [
240 { id => $tags[0]->id, file => 'file0' },
241 { id => $tags[1]->id, file => 'file1' }
242 ],
243 }
244 );
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',
251 'tags related' );
252
253 my $new_person = {
254 name => 'Amiri Barksdale',
255 username => 'amiri',
256 password => 'amiri',
257 };
258 ok( my $new_user = $user_rs->recursive_update($new_person) );
259
260 #print STDERR Dumper $new_user;
261
262 # $updates = {
263 # name => 'Test name 1',
264 # };
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' );
267 }
This page took 0.052637 seconds and 5 git commands to generate.