removed perl 5.6.0 requirement from test files
[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{
0b0bf911 31 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
32 my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
33 my $req = POST( $incorrect_url, { title => 'value' } );
34
35 $mech->request( $req, $content_type );
36 cmp_ok( $mech->status, '==', 400,
37 'Attempt with invalid track id caught' );
38
39 my $response = $json->decode( $mech->content );
40 like(
41 $response->{messages}->[0],
42 qr/No object found for id/,
43 'correct message returned'
44 );
45
46 $track->discard_changes;
47 is_deeply(
48 { $track->get_columns },
49 \%original_cols,
50 'no update occurred'
51 );
52 }
d2739840 53}
54
55# validation when no params sent
56{
0b0bf911 57 my $req = POST( $track_update_url, { wrong_param => 'value' } );
58 $mech->request( $req, $content_type );
59 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
60
61 my $response = $json->decode( $mech->content );
62 is_deeply( $response->{messages}, ['No valid keys passed'],
63 'correct message returned' );
64
65 $track->discard_changes;
66 is_deeply(
67 { $track->get_columns },
68 \%original_cols,
69 'no update occurred'
70 );
d2739840 71}
72
73{
0b0bf911 74 my $req = POST( $track_update_url, { wrong_param => 'value' } );
75 $mech->request( $req, $content_type );
76 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
77
78 my $response = $json->decode( $mech->content );
79 is_deeply( $response->{messages}, ['No valid keys passed'],
80 'correct message returned' );
81
82 $track->discard_changes;
83 is_deeply(
84 { $track->get_columns },
85 \%original_cols,
86 'no update occurred'
87 );
d2739840 88}
89
90{
0b0bf911 91 my $req = POST( $track_update_url, { title => undef } );
92 $mech->request( $req, $content_type );
93 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
94
95 $track->discard_changes;
96 isnt( $track->title, $original_cols{title}, 'Title changed' );
97 is( $track->title, '', 'Title changed to undef' );
d2739840 98}
99
100{
0b0bf911 101 my $req = POST( $track_update_url, { title => 'monkey monkey' } );
102 $mech->request( $req, $content_type );
103 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
104
105 $track->discard_changes;
106 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
d2739840 107}
108
109{
0b0bf911 110 my $req = POST(
111 $track_update_url,
112 { title => 'sheep sheep',
113 'cd.year' => '2009'
114 }
115 );
116 $mech->request( $req, $content_type );
117 cmp_ok( $mech->status, '==', 200,
118 'Update with key with value and related key okay' );
119
120 $track->discard_changes;
121 is( $track->title, 'sheep sheep', 'Title changed' );
122 is( $track->cd->year, '2009', 'Related field changed"' );
d2739840 123}
124
125{
0b0bf911 126 my $req = POST( $any_track_update_url, { title => 'baa' } );
127 $mech->request( $req, $content_type );
128 cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
129
130 $track->discard_changes;
131 is( $track->title, 'baa', 'Title changed' );
d2739840 132}
133
134{
0b0bf911 135 my $req = POST( $any_track_update_url, { position => '14' } );
136 $mech->request( $req, $content_type );
137 cmp_ok( $mech->status, '==', 200, 'Position update okay' );
138
139 $track->discard_changes;
140 is( $track->get_column('position'), '14', 'Position changed' );
d2739840 141}
142
7c8cb609 143# bulk_update existing objects
0b8c7370 144{
0b0bf911 145
146 # order to get a stable order of rows
147 my $tracks_rs =
148 $schema->resultset('Track')
149 ->search( undef, { order_by => 'trackid', rows => 3 } );
150 my $test_data = $json->encode(
151 { list => [
152 map +{ id => $_->id, title => 'Track ' . $_->id },
153 $tracks_rs->all
154 ]
155 }
156 );
157 my $req = POST( $tracks_update_url, Content => $test_data );
158 $req->content_type('text/x-json');
159 $mech->request($req);
160 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
161
162 $tracks_rs->reset;
163 while ( my $track = $tracks_rs->next ) {
164 is( $track->title, 'Track ' . $track->id, 'Title changed' );
165 }
0b8c7370 166}
167
7c8cb609 168# bulk_update nonexisting objects
169{
0b0bf911 170
171 # order to get a stable order of rows
172 my $test_data = $json->encode(
173 { list => [
174 map +{ id => $_, title => 'Track ' . $_ },
175 ( 1000 .. 1002 )
176 ]
177 }
178 );
179 my $req = POST( $tracks_update_url, Content => $test_data );
180 $req->content_type('text/x-json');
181 $mech->request($req);
182 cmp_ok( $mech->status, '==', 400,
183 'Attempt to update three nonexisting tracks fails' );
184 my $response = $json->decode( $mech->content );
185 is( $response->{success}, 'false',
186 'success property returns quoted false' );
187 like(
188 $response->{messages}->[0],
189 qr/No object found for id/,
190 'correct message returned'
191 );
7c8cb609 192}
193
d2739840 194done_testing();