6 my $base = 'http://localhost';
7 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
12 use Test::WWW::Mechanize::Catalyst 'RestTest';
13 use HTTP::Request::Common;
16 my $json = JSON->new->utf8;
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok( my $schema = DBICTest->init_schema(), 'got schema' );
21 my $track = $schema->resultset('Track')->first;
22 my %original_cols = $track->get_columns;
24 my $track_url = "$base/api/rest/track/";
25 my $track_update_url = $track_url . $track->id;
26 my $tracks_update_url = $track_url;
28 my $artist_url = "$base/api/rest/artist/";
29 my $artist_update_url = $artist_url . "1";
31 # test invalid track id caught
33 diag 'DBIx::Class warns about a non-numeric id which is ok because we test for that too';
34 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
35 my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
36 my $test_data = $json->encode( { title => 'value' } );
37 my $req = POST( $incorrect_url, Content => $test_data );
38 $req->content_type('text/x-json');
41 cmp_ok( $mech->status, '==', 400,
42 'Attempt with invalid track id caught' );
44 my $response = $json->decode( $mech->content );
46 $response->{messages}->[0],
47 qr/No object found for id/,
48 'correct message returned'
51 $track->discard_changes;
53 { $track->get_columns },
60 # validation when no params sent
62 my $test_data = $json->encode( { wrong_param => 'value' } );
63 my $req = POST( $track_update_url, Content => $test_data );
64 $req->content_type('text/x-json');
67 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
69 my $response = $json->decode( $mech->content );
70 is_deeply( $response->{messages}, ['No valid keys passed'],
71 'correct message returned' );
73 $track->discard_changes;
75 { $track->get_columns },
82 my $test_data = $json->encode( { title => undef } );
83 my $req = POST( $track_update_url, Content => $test_data );
84 $req->content_type('text/x-json');
86 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
88 $track->discard_changes;
89 isnt( $track->title, $original_cols{title}, 'Title changed' );
90 is( $track->title, undef, 'Title changed to undef' );
94 my $test_data = $json->encode(
95 { title => 'monkey monkey', 'cd' => { year => 2009 } } );
96 my $req = POST( $track_update_url, Content => $test_data );
97 $req->content_type('text/x-json');
100 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
102 $track->discard_changes;
103 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
104 is( $track->cd->year, 2009, 'related row updated' );
108 my $test_data = $json->encode(
109 { name => 'Caterwauler B. McCrae',
113 title => 'All the bees are gone',
118 title => 'All the crops are gone',
125 my $req = POST( $artist_update_url, Content => $test_data );
126 $req->content_type('text/x-json');
127 $mech->request($req);
129 cmp_ok( $mech->status, '==', 200, 'Multi-row update returned 200 OK' );
131 my $artist = $schema->resultset('Artist')->search({ artistid => 1 });
132 ok ($artist->next->name eq "Caterwauler B. McCrae", "mutliple related row parent record update");
134 # make sure the old cds don't exist, it's possible we just inserted the new rows instead of updating them
135 my $old_cds = $artist->search_related('cds', { title => ['Spoonful of bees', 'Forkful of bees'] } )->count;
136 ok ($old_cds == 0, 'update performed update and not create on related rows');
138 my @cds = $artist->search_related('cds', { year => ['3030', '3031'] }, { order_by => 'year' })->all;
139 ok (@cds == 2, 'update modified proper number of related rows');
140 ok ($cds[0]->title eq 'All the bees are gone', 'update modified column on related row');
141 ok ($cds[1]->title eq 'All the crops are gone', 'update modified column on second related row');
144 # update related rows using only unique_constraint on CD vs. primary key
145 # update the year on constraint match
147 my $test_data = $json->encode(
148 { name => 'Caterwauler McCrae',
152 title => 'All the bees are gone',
157 title => 'All the crops are gone',
164 my $req = POST( $artist_update_url, Content => $test_data );
165 $req->content_type('text/x-json');
166 $mech->request($req);
168 cmp_ok( $mech->status, '==', 200, 'Multi-row unique constraint update returned 200 OK' );
170 my $artist = $schema->resultset('Artist')->search({ artistid => 1 });
171 ok ($artist->next->name eq "Caterwauler McCrae", "multi-row unique constraint related row parent record updated");
173 my $old_cds = $artist->search_related('cds', { year => ['3030', '3031'] }, { order_by => 'year' })->count;
174 ok ( $old_cds == 0, 'multi-row update with unique constraint updated year' );
176 my $cds = $artist->search_related('cds', { 'year' => 3032 } )->count;
177 ok ( $cds == 2, 'multi-row update with unique constraint okay' );
180 # bulk_update existing objects
183 # order to get a stable order of rows
185 $schema->resultset('Track')
186 ->search( undef, { order_by => 'trackid', rows => 3 } );
187 my $test_data = $json->encode(
189 map +{ id => $_->id, title => 'Track ' . $_->id },
194 my $req = PUT( $tracks_update_url, Content => $test_data );
195 $req->content_type('text/x-json');
196 $mech->request($req);
197 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
200 while ( my $track = $tracks_rs->next ) {
201 is( $track->title, 'Track ' . $track->id, 'Title changed' );
205 # bulk_update nonexisting objects
208 # order to get a stable order of rows
209 my $test_data = $json->encode(
211 map +{ id => $_, title => 'Track ' . $_ },
216 my $req = PUT( $tracks_update_url, Content => $test_data );
217 $req->content_type('text/x-json');
218 $mech->request($req);
219 cmp_ok( $mech->status, '==', 400,
220 'Attempt to update three nonexisting tracks fails' );
221 my $response = $json->decode( $mech->content );
222 is( $response->{success}, JSON::false,
223 'success property returns unquoted false' );
225 $response->{messages}->[0],
226 qr/No object found for id/,
227 'correct message returned'