use Dist::Zilla::PluginBundle::Git
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / update.t
index 7cd6ce5..3d8bae2 100644 (file)
@@ -11,9 +11,9 @@ use DBICTest;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON;
+use JSON::MaybeXS;
 
-my $json = JSON->new->utf8;
+my $json = JSON::MaybeXS->new(utf8 => 1);
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
 ok( my $schema = DBICTest->init_schema(), 'got schema' );
@@ -101,6 +101,38 @@ my $tracks_update_url = $track_url;
     is( $track->cd->year, 2009, 'related row updated' );
 }
 
+# bulk update existing objects with database error
+{
+
+    # order to get a stable order of rows
+    my $tracks_rs =
+        $schema->resultset('Track')
+        ->search( undef, { order_by => 'trackid', rows => 3 } );
+    my @tracks = $tracks_rs->all;
+    my @tracks_new_data = map +{ id => $_->id, title => 'Track ' . $_->id },
+        @tracks;
+    # set position column to NULL to force error
+    $tracks_new_data[0]->{position} = undef;
+    my $test_data = $json->encode({ list => \@tracks_new_data });
+    my $req = PUT( $tracks_update_url, Content => $test_data );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400, 'Attempt to update three tracks fails' );
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, JSON::MaybeXS::false,
+        'success property returns unquoted false' );
+    like(
+        $response->{messages}->[0],
+        qr/a database error has occured/,
+        'correct message returned'
+    );
+
+    $tracks_rs->reset;
+    while ( my $track = $tracks_rs->next ) {
+        isnt( $track->title, 'Track ' . $track->id, 'Title unchanged' );
+    }
+}
+
 # bulk_update existing objects
 {
 
@@ -143,7 +175,7 @@ my $tracks_update_url = $track_url;
     cmp_ok( $mech->status, '==', 400,
         'Attempt to update three nonexisting tracks fails' );
     my $response = $json->decode( $mech->content );
-    is( $response->{success}, JSON::false,
+    is( $response->{success}, JSON::MaybeXS::false,
         'success property returns unquoted false' );
     like(
         $response->{messages}->[0],