add test for bulk update for existing objects with database error
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / update.t
CommitLineData
d2739840 1use strict;
2use warnings;
3
4use lib 't/lib';
5
6my $base = 'http://localhost';
7my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
8
9use RestTest;
10use DBICTest;
11use Test::More;
12use Test::WWW::Mechanize::Catalyst 'RestTest';
13use HTTP::Request::Common;
0b0bf911 14use JSON;
15
16my $json = JSON->new->utf8;
d2739840 17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 19ok( my $schema = DBICTest->init_schema(), 'got schema' );
d2739840 20
0b0bf911 21my $track = $schema->resultset('Track')->first;
d2739840 22my %original_cols = $track->get_columns;
23
24my $track_update_url = "$base/api/rpc/track/id/" . $track->id . "/update";
0b0bf911 25my $any_track_update_url =
26 "$base/api/rpc/any/track/id/" . $track->id . "/update";
0b8c7370 27my $tracks_update_url = "$base/api/rpc/track/update";
d2739840 28
29# test invalid track id caught
30{
23bb3784 31 diag 'DBIx::Class warns about a non-numeric id which is ok because we test for that too';
0b0bf911 32 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
33 my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
34 my $req = POST( $incorrect_url, { title => 'value' } );
35
36 $mech->request( $req, $content_type );
37 cmp_ok( $mech->status, '==', 400,
38 'Attempt with invalid track id caught' );
39
40 my $response = $json->decode( $mech->content );
41 like(
42 $response->{messages}->[0],
43 qr/No object found for id/,
44 'correct message returned'
45 );
46
47 $track->discard_changes;
48 is_deeply(
49 { $track->get_columns },
50 \%original_cols,
51 'no update occurred'
52 );
53 }
d2739840 54}
55
56# validation when no params sent
57{
0b0bf911 58 my $req = POST( $track_update_url, { wrong_param => 'value' } );
59 $mech->request( $req, $content_type );
60 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
61
62 my $response = $json->decode( $mech->content );
63 is_deeply( $response->{messages}, ['No valid keys passed'],
64 'correct message returned' );
65
66 $track->discard_changes;
67 is_deeply(
68 { $track->get_columns },
69 \%original_cols,
70 'no update occurred'
71 );
d2739840 72}
73
74{
0b0bf911 75 my $req = POST( $track_update_url, { wrong_param => 'value' } );
76 $mech->request( $req, $content_type );
77 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
78
79 my $response = $json->decode( $mech->content );
80 is_deeply( $response->{messages}, ['No valid keys passed'],
81 'correct message returned' );
82
83 $track->discard_changes;
84 is_deeply(
85 { $track->get_columns },
86 \%original_cols,
87 'no update occurred'
88 );
d2739840 89}
90
91{
0b0bf911 92 my $req = POST( $track_update_url, { title => undef } );
93 $mech->request( $req, $content_type );
94 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
95
96 $track->discard_changes;
97 isnt( $track->title, $original_cols{title}, 'Title changed' );
98 is( $track->title, '', 'Title changed to undef' );
d2739840 99}
100
101{
0b0bf911 102 my $req = POST( $track_update_url, { title => 'monkey monkey' } );
103 $mech->request( $req, $content_type );
104 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
105
106 $track->discard_changes;
107 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
d2739840 108}
109
110{
0b0bf911 111 my $req = POST(
112 $track_update_url,
113 { title => 'sheep sheep',
114 'cd.year' => '2009'
115 }
116 );
117 $mech->request( $req, $content_type );
118 cmp_ok( $mech->status, '==', 200,
119 'Update with key with value and related key okay' );
120
121 $track->discard_changes;
122 is( $track->title, 'sheep sheep', 'Title changed' );
123 is( $track->cd->year, '2009', 'Related field changed"' );
d2739840 124}
125
126{
0b0bf911 127 my $req = POST( $any_track_update_url, { title => 'baa' } );
128 $mech->request( $req, $content_type );
129 cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
130
131 $track->discard_changes;
132 is( $track->title, 'baa', 'Title changed' );
d2739840 133}
134
135{
0b0bf911 136 my $req = POST( $any_track_update_url, { position => '14' } );
137 $mech->request( $req, $content_type );
138 cmp_ok( $mech->status, '==', 200, 'Position update okay' );
139
140 $track->discard_changes;
141 is( $track->get_column('position'), '14', 'Position changed' );
d2739840 142}
143
7c8cb609 144# bulk_update existing objects
0b8c7370 145{
0b0bf911 146
147 # order to get a stable order of rows
148 my $tracks_rs =
149 $schema->resultset('Track')
150 ->search( undef, { order_by => 'trackid', rows => 3 } );
151 my $test_data = $json->encode(
152 { list => [
153 map +{ id => $_->id, title => 'Track ' . $_->id },
154 $tracks_rs->all
155 ]
156 }
157 );
158 my $req = POST( $tracks_update_url, Content => $test_data );
159 $req->content_type('text/x-json');
160 $mech->request($req);
161 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
162
163 $tracks_rs->reset;
164 while ( my $track = $tracks_rs->next ) {
165 is( $track->title, 'Track ' . $track->id, 'Title changed' );
166 }
0b8c7370 167}
168
7c8cb609 169# bulk_update nonexisting objects
170{
0b0bf911 171
172 # order to get a stable order of rows
173 my $test_data = $json->encode(
174 { list => [
175 map +{ id => $_, title => 'Track ' . $_ },
176 ( 1000 .. 1002 )
177 ]
178 }
179 );
180 my $req = POST( $tracks_update_url, Content => $test_data );
181 $req->content_type('text/x-json');
182 $mech->request($req);
183 cmp_ok( $mech->status, '==', 400,
184 'Attempt to update three nonexisting tracks fails' );
185 my $response = $json->decode( $mech->content );
186 is( $response->{success}, 'false',
187 'success property returns quoted false' );
188 like(
189 $response->{messages}->[0],
190 qr/No object found for id/,
191 'correct message returned'
192 );
7c8cb609 193}
194
d2739840 195done_testing();