add test for bulk create with database error
Alexander Hartmaier [Wed, 13 Jun 2018 17:24:41 +0000 (19:24 +0200)]
t/rest/create.t

index ba850c1..eff1bfa 100644 (file)
@@ -19,6 +19,7 @@ ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
 my $artist_create_url   = "$base/api/rest/artist";
 my $producer_create_url = "$base/api/rest/producer";
+my $track_create_url = "$base/api/rest/track";
 
 # test validation when no params sent
 {
@@ -105,4 +106,45 @@ my $producer_create_url = "$base/api/rest/producer";
         'json for bulk create returned' );
 }
 
+# test bulk create with database error
+{
+    my $test_data = $json->encode({
+        list => [
+            {
+                cd => 2,
+                position => 4,
+                title => "Foobar",
+            },
+            {
+                cd => 2,
+                # set position column to NULL to force error
+                position => undef,
+                title => "Foobar",
+            },
+        ]
+    });
+    #my $req = PUT( $tracks_update_url, Content => $test_data );
+    my $req = PUT($track_create_url);
+    $req->content_type('text/x-json');
+    $req->content_length(
+        do { use bytes; length($test_data) }
+    );
+    $req->content($test_data);
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400, 'bulk request with one invalid object fails' );
+    my $rs =
+        $schema->resultset('Track')
+        ->search( [ { cd => 2 } ] );
+    is( $rs->count, 3, 'no records created' );
+
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, JSON::false,
+        'success property returns unquoted false' );
+    like(
+        $response->{messages}->[0],
+        qr/a database error has occured/,
+        'correct message returned'
+    );
+}
+
 done_testing();