Version 2.008001
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / 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;
0b8c7370 11use Test::More;
d2739840 12use Test::WWW::Mechanize::Catalyst 'RestTest';
13use HTTP::Request::Common;
a5949bfd 14use JSON::MaybeXS;
0b0bf911 15
a5949bfd 16my $json = JSON::MaybeXS->new(utf8 => 1);
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
0b0bf911 24my $track_url = "$base/api/rest/track/";
25my $track_update_url = $track_url . $track->id;
0b8c7370 26my $tracks_update_url = $track_url;
d2739840 27
28# test invalid track id caught
29{
23bb3784 30 diag 'DBIx::Class warns about a non-numeric id which is ok because we test for that too';
0b0bf911 31 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
32 my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
33 my $test_data = $json->encode( { title => 'value' } );
34 my $req = POST( $incorrect_url, Content => $test_data );
35 $req->content_type('text/x-json');
36 $mech->request($req);
37
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 $test_data = $json->encode( { wrong_param => 'value' } );
60 my $req = POST( $track_update_url, Content => $test_data );
61 $req->content_type('text/x-json');
62 $mech->request($req);
63
64 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
65
66 my $response = $json->decode( $mech->content );
67 is_deeply( $response->{messages}, ['No valid keys passed'],
68 'correct message returned' );
69
70 $track->discard_changes;
71 is_deeply(
72 { $track->get_columns },
73 \%original_cols,
74 'no update occurred'
75 );
d2739840 76}
77
78{
0b0bf911 79 my $test_data = $json->encode( { title => undef } );
80 my $req = POST( $track_update_url, Content => $test_data );
81 $req->content_type('text/x-json');
82 $mech->request($req);
83 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
84
85 $track->discard_changes;
86 isnt( $track->title, $original_cols{title}, 'Title changed' );
87 is( $track->title, undef, 'Title changed to undef' );
d2739840 88}
89
90{
0b0bf911 91 my $test_data = $json->encode(
92 { title => 'monkey monkey', 'cd' => { year => 2009 } } );
93 my $req = POST( $track_update_url, Content => $test_data );
94 $req->content_type('text/x-json');
95 $mech->request($req);
d2739840 96
0b0bf911 97 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
d2739840 98
0b0bf911 99 $track->discard_changes;
100 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
101 is( $track->cd->year, 2009, 'related row updated' );
d2739840 102}
0b8c7370 103
257cb214 104# bulk update existing objects with database error
105{
106
107 # order to get a stable order of rows
108 my $tracks_rs =
109 $schema->resultset('Track')
110 ->search( undef, { order_by => 'trackid', rows => 3 } );
111 my @tracks = $tracks_rs->all;
112 my @tracks_new_data = map +{ id => $_->id, title => 'Track ' . $_->id },
113 @tracks;
114 # set position column to NULL to force error
115 $tracks_new_data[0]->{position} = undef;
116 my $test_data = $json->encode({ list => \@tracks_new_data });
117 my $req = PUT( $tracks_update_url, Content => $test_data );
118 $req->content_type('text/x-json');
119 $mech->request($req);
120 cmp_ok( $mech->status, '==', 400, 'Attempt to update three tracks fails' );
121 my $response = $json->decode( $mech->content );
a5949bfd 122 is( $response->{success}, JSON::MaybeXS::false,
257cb214 123 'success property returns unquoted false' );
124 like(
125 $response->{messages}->[0],
126 qr/a database error has occured/,
127 'correct message returned'
128 );
129
130 $tracks_rs->reset;
131 while ( my $track = $tracks_rs->next ) {
132 isnt( $track->title, 'Track ' . $track->id, 'Title unchanged' );
133 }
134}
135
7c8cb609 136# bulk_update existing objects
0b8c7370 137{
0b0bf911 138
139 # order to get a stable order of rows
140 my $tracks_rs =
141 $schema->resultset('Track')
142 ->search( undef, { order_by => 'trackid', rows => 3 } );
143 my $test_data = $json->encode(
144 { list => [
145 map +{ id => $_->id, title => 'Track ' . $_->id },
146 $tracks_rs->all
147 ]
148 }
149 );
150 my $req = PUT( $tracks_update_url, Content => $test_data );
151 $req->content_type('text/x-json');
152 $mech->request($req);
153 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
154
155 $tracks_rs->reset;
156 while ( my $track = $tracks_rs->next ) {
157 is( $track->title, 'Track ' . $track->id, 'Title changed' );
158 }
0b8c7370 159}
160
7c8cb609 161# bulk_update nonexisting objects
162{
0b0bf911 163
164 # order to get a stable order of rows
165 my $test_data = $json->encode(
166 { list => [
167 map +{ id => $_, title => 'Track ' . $_ },
168 ( 1000 .. 1002 )
169 ]
170 }
171 );
172 my $req = PUT( $tracks_update_url, Content => $test_data );
173 $req->content_type('text/x-json');
174 $mech->request($req);
175 cmp_ok( $mech->status, '==', 400,
176 'Attempt to update three nonexisting tracks fails' );
177 my $response = $json->decode( $mech->content );
a5949bfd 178 is( $response->{success}, JSON::MaybeXS::false,
0b0bf911 179 'success property returns unquoted false' );
180 like(
181 $response->{messages}->[0],
182 qr/No object found for id/,
183 'correct message returned'
184 );
7c8cb609 185}
186
0b8c7370 187done_testing();