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