336b7ffd0bef1defe4bc150a0d92d364c61bcd81
[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 ($@, qr/No such column, relationship, many-to-many helper accessor or generic accessor 'nonexisting'/, 'nonexisting column, accessor, relationship fails');
32
33 # creating new record linked to some old record
34 $updates = {
35 name => 'Test name 2',
36 viewings => [ { user_id => $owner->id } ],
37 owner => { id => $another_owner->id },
38 };
39
40 my $new_dvd = $dvd_rs->recursive_update( $updates );
41 # my $new_dvd = $dvd_rs->create( $updates );
42
43 is ( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' );
44 is ( $schema->resultset( 'User' )->count, $initial_user_count, "No new user created" );
45 is ( $new_dvd->name, 'Test name 2', 'Dvd name set' );
46 is ( $new_dvd->owner->id, $another_owner->id, 'Owner set' );
47 is ( $new_dvd->viewings->count, 1, 'Viewing created' );
48
49 # creating new records
50 $updates = {
51 #aaaa => undef,
52 tags => [ '2', { id => '3' } ],
53 name => 'Test name',
54 owner => $owner,
55 current_borrower => {
56 name => 'temp name',
57 username => 'temp name',
58 password => 'temp name',
59 },
60 liner_notes => {
61 notes => 'test note',
62 },
63 like_has_many => [
64 { key2 => 1 }
65 ],
66 like_has_many2 => [
67 {
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, $initial_user_count + 1, "One new user created" );
78 is ( $dvd->name, 'Test name', 'Dvd name set' );
79 is_deeply ( [ map {$_->id} $dvd->tags ], [ '2', '3' ], 'Tags set' );
80 is ( $dvd->owner->id, $owner->id, 'Owner set' );
81
82 is ( $dvd->current_borrower->name, 'temp name', 'Related record created' );
83 is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
84 ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'Test name', key2 => 1 } ), 'Twokeys created' );
85 my $onekey = $schema->resultset( 'Onekey' )->search( name => 'aaaaa' )->first;
86 ok ( $onekey, 'Onekey created' );
87 ok ( $schema->resultset( 'Twokeys_belongsto' )->find( { key1 => $onekey->id, key2 => 1 } ), 'Twokeys created' );
88
89 is ( $dvd->name, 'Test name', 'Dvd name set' );
90 # changing existing records
91
92 my $num_of_users = $user_rs->count;
93 $updates = {
94 id => $dvd->dvd_id, # id instead of dvd_id
95 ####aaaa => undef,
96 name => undef,
97 tags => [ ],
98 'owner' => $another_owner->id,
99 current_borrower => {
100 username => 'new name a',
101 name => 'new name a',
102 password => 'new password a',
103 },
104 liner_notes => {
105 notes => 'test note changed',
106 },
107
108 };
109 my $dvd_updated = $dvd_rs->recursive_update( $updates );
110
111 is ( $dvd_updated->dvd_id, $dvd->dvd_id, 'Pk from "id"' );
112 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
113 is ( $dvd_updated->name, undef, 'Dvd name deleted' );
114 is ( $dvd_updated->owner->id, $another_owner->id, 'Owner updated' );
115 is ( $dvd_updated->current_borrower->name, 'new name a', 'Related record modified' );
116 is ( $dvd_updated->tags->count, 0, 'Tags deleted' );
117 is ( $dvd_updated->liner_notes->notes, 'test note changed', 'might_have record changed' );
118
119 $new_dvd->update( { name => 'New Test Name' } );
120 $updates = {
121 id => $new_dvd->dvd_id, # id instead of dvd_id
122 like_has_many => [
123 { dvd_name => $dvd->name, key2 => 1 }
124 ],
125 };
126 $dvd_updated = $dvd_rs->recursive_update( $updates );
127 ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'New Test Name', key2 => 1 } ), 'Twokeys updated' );
128 ok ( !$schema->resultset( 'Twokeys' )->find( { dvd_name => $dvd->name, key2 => 1 } ), 'Twokeys updated' );
129
130 # repeatable
131
132 $updates = {
133 name => 'temp name',
134 username => 'temp username',
135 password => 'temp username',
136 owned_dvds =>[
137 {
138 'name' => 'temp name 1',
139 'tags' => [ 1, 2 ],
140 },
141 {
142 'name' => 'temp name 2',
143 'tags' => [ 2, 3 ],
144 }
145 ]
146 };
147
148 my $user = $user_rs->recursive_update( $updates );
149 is ( $schema->resultset( 'User' )->count, $initial_user_count + 2, "New user created" );
150 is ( $dvd_rs->count, $initial_dvd_count + 4, 'Dvds created' );
151 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
152 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
153 ok( $owned_dvds{'temp name 1'}, 'Name in a has_many related record saved' );
154 my @tags = $owned_dvds{'temp name 1'}->tags;
155 is( scalar @tags, 2, 'Tags in has_many related record saved' );
156 ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' );
157
158 # update has_many where foreign cols aren't nullable
159 $updates = {
160 id => $user->id,
161 address => {
162 street => "101 Main Street",
163 city => "Podunk",
164 state => "New York"
165 },
166 owned_dvds => [
167 {
168 id => 1,
169 },
170 ]
171 };
172 $user = $user_rs->recursive_update( $updates );
173 is( $schema->resultset( 'Address' )->search({ user_id => $user->id })->count, 1,
174 'the right number of addresses' );
175 $dvd = $dvd_rs->find( 1 );
176 is( $dvd->get_column( 'owner' ), $user->id, 'foreign key set' );
177
178 $dvd_rs->update( { current_borrower => $user->id } );
179 ok( $user->borrowed_dvds->count > 1, 'Precond' );
180 $updates = {
181 id => $user->id,
182 borrowed_dvds =>[
183 {
184 id => $dvd->id
185 },
186 ]
187 };
188 $user = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
189 resultset => $user_rs,
190 updates => $updates,
191 if_not_submitted => 'set_to_null',
192 );
193 is( $user->borrowed_dvds->count, 1, 'set_to_null' );
194
195 # has_many where foreign cols are nullable
196 $dvd_rs->update( { current_borrower => $user->id } );
197 $updates = {
198 id => $user->id,
199 borrowed_dvds => [
200 {
201 id => $dvd->id
202 },
203 ]
204 };
205 $user = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
206 resultset => $user_rs,
207 updates => $updates,
208 if_not_submitted => 'delete',
209 );
210 is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' );
211
212 @tags = $schema->resultset( 'Tag' )->search();
213 $dvd_updated = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
214 resultset => $schema->resultset( 'Dvd' ),
215 updates => {
216 id => $dvd->dvd_id, # id instead of dvd_id
217 tags => [ { id => $tags[0]->id, file => 'file0'}, { id => $tags[1]->id, file => 'file1' } ],
218 }
219 );
220 $tags[$_]->discard_changes for 0 .. 1;
221 is( $tags[0]->file, 'file0', 'file set in tag' );
222 is( $tags[1]->file, 'file1', 'file set in tag' );
223 my @rel_tags = $dvd_updated->tags;
224 is( scalar @rel_tags, 2, 'tags related' );
225 ok( $rel_tags[0]->file eq 'file0' || $rel_tags[0]->file eq 'file1', 'tags related' );
226
227 my $new_person = {
228 name => 'Amiri Barksdale',
229 username => 'amiri',
230 password => 'amiri',
231 };
232 ok(my $new_user = $user_rs->recursive_update( $new_person ));
233
234 #print STDERR Dumper $new_user;
235
236 # $updates = {
237 # name => 'Test name 1',
238 # };
239 # $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
240 # is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
241 }
This page took 0.059126 seconds and 3 git commands to generate.