8e5afa8e3bc0e83b01aa125ea8497345140cceff
[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 => 36;
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 # creating new record linked to some old record
24 $updates = {
25 name => 'Test name 2',
26 viewings => [ { user_id => $owner->id } ],
27 owner => { id => $another_owner->id },
28 };
29
30 my $new_dvd = $dvd_rs->recursive_update( $updates );
31 # my $new_dvd = $dvd_rs->create( $updates );
32
33 is ( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' );
34 is ( $schema->resultset( 'User' )->count, $initial_user_count, "No new user created" );
35 is ( $new_dvd->name, 'Test name 2', 'Dvd name set' );
36 is ( $new_dvd->owner->id, $another_owner->id, 'Owner set' );
37 is ( $new_dvd->viewings->count, 1, 'Viewing created' );
38
39 # creating new records
40 $updates = {
41 aaaa => undef,
42 tags => [ '2', { id => '3' } ],
43 name => 'Test name',
44 owner => $owner,
45 current_borrower => {
46 name => 'temp name',
47 username => 'temp name',
48 password => 'temp name',
49 },
50 liner_notes => {
51 notes => 'test note',
52 },
53 like_has_many => [
54 { key2 => 1 }
55 ],
56 like_has_many2 => [
57 {
58 onekey => { name => 'aaaaa' },
59 key2 => 1
60 }
61 ],
62 };
63
64 my $dvd = $dvd_rs->recursive_update( $updates );
65 ;
66 is ( $dvd_rs->count, $initial_dvd_count + 2, 'Dvd created' );
67 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "One new user created" );
68 is ( $dvd->name, 'Test name', 'Dvd name set' );
69 is_deeply ( [ map {$_->id} $dvd->tags ], [ '2', '3' ], 'Tags set' );
70 is ( $dvd->owner->id, $owner->id, 'Owner set' );
71
72 is ( $dvd->current_borrower->name, 'temp name', 'Related record created' );
73 is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
74 ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'Test name', key2 => 1 } ), 'Twokeys created' );
75 my $onekey = $schema->resultset( 'Onekey' )->search( name => 'aaaaa' )->first;
76 ok ( $onekey, 'Onekey created' );
77 ok ( $schema->resultset( 'Twokeys_belongsto' )->find( { key1 => $onekey->id, key2 => 1 } ), 'Twokeys created' );
78
79 is ( $dvd->name, 'Test name', 'Dvd name set' );
80 # changing existing records
81
82 my $num_of_users = $user_rs->count;
83 $updates = {
84 id => $dvd->dvd_id, # id instead of dvd_id
85 aaaa => undef,
86 name => undef,
87 tags => [ ],
88 'owner' => $another_owner->id,
89 current_borrower => {
90 username => 'new name a',
91 name => 'new name a',
92 password => 'new password a',
93 },
94 liner_notes => {
95 notes => 'test note changed',
96 },
97
98 };
99 my $dvd_updated = $dvd_rs->recursive_update( $updates );
100
101 is ( $dvd_updated->dvd_id, $dvd->dvd_id, 'Pk from "id"' );
102 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
103 is ( $dvd_updated->name, undef, 'Dvd name deleted' );
104 is ( $dvd_updated->owner->id, $another_owner->id, 'Owner updated' );
105 is ( $dvd_updated->current_borrower->name, 'new name a', 'Related record modified' );
106 is ( $dvd_updated->tags->count, 0, 'Tags deleted' );
107 is ( $dvd_updated->liner_notes->notes, 'test note changed', 'might_have record changed' );
108
109 $new_dvd->update( { name => 'New Test Name' } );
110 $updates = {
111 id => $new_dvd->dvd_id, # id instead of dvd_id
112 like_has_many => [
113 { dvd_name => $dvd->name, key2 => 1 }
114 ],
115 };
116 my $dvd_updated = $dvd_rs->recursive_update( $updates );
117 ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'New Test Name', key2 => 1 } ), 'Twokeys updated' );
118 ok ( !$schema->resultset( 'Twokeys' )->find( { dvd_name => $dvd->name, key2 => 1 } ), 'Twokeys updated' );
119
120 # repeatable
121
122 $updates = {
123 name => 'temp name',
124 username => 'temp username',
125 password => 'temp username',
126 owned_dvds =>[
127 {
128 'name' => 'temp name 1',
129 'tags' => [ 1, 2 ],
130 },
131 {
132 'name' => 'temp name 2',
133 'tags' => [ 2, 3 ],
134 }
135 ]
136 };
137
138 my $user = $user_rs->recursive_update( $updates );
139 is ( $schema->resultset( 'User' )->count, $initial_user_count + 2, "New user created" );
140 is ( $dvd_rs->count, $initial_dvd_count + 4, 'Dvds created' );
141 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
142 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
143 ok( $owned_dvds{'temp name 1'}, 'Name in a has_many related record saved' );
144 my @tags = $owned_dvds{'temp name 1'}->tags;
145 is( scalar @tags, 2, 'Tags in has_many related record saved' );
146 ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' );
147
148 $updates = {
149 id => $user->id,
150 address => {
151 street => "101 Main Street",
152 city => "Podunk",
153 state => "New York"
154 },
155 owned_dvds =>[
156 {
157 id => 1,
158 },
159 ]
160 };
161 $user = $user_rs->recursive_update( $updates );
162 $user = $user_rs->recursive_update( $updates );
163 is( $schema->resultset( 'Address' )->search({ user_id => $user->id })->count, 1,
164 'the right number of addresses' );
165 $dvd = $dvd_rs->find( 1 );
166 is( $dvd->get_column( 'owner' ), $user->id, 'foreign key set' );
167
168 $dvd_rs->update( { current_borrower => $user->id } );
169 ok( $user->borrowed_dvds->count > 1, 'Precond' );
170 $updates = {
171 id => $user->id,
172 borrowed_dvds =>[
173 {
174 id => $dvd->id
175 },
176 ]
177 };
178 $user = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
179 resultset => $user_rs,
180 updates => $updates,
181 if_not_submitted => 'set_to_null',
182 );
183 is( $user->borrowed_dvds->count, 1, 'set_to_null' );
184
185 $dvd_rs->update( { current_borrower => $user->id } );
186 $updates = {
187 id => $user->id,
188 borrowed_dvds =>[
189 {
190 id => $dvd->id
191 },
192 ]
193 };
194 $user = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
195 resultset => $user_rs,
196 updates => $updates,
197 if_not_submitted => 'delete',
198 );
199 is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' );
200
201
202 # $updates = {
203 # name => 'Test name 1',
204 # };
205 # $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
206 # is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
207 }
This page took 0.047989 seconds and 3 git commands to generate.