-use 5.6.0;
-
use strict;
use warnings;
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' );
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
{
'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::MaybeXS::false,
+ 'success property returns unquoted false' );
+ like(
+ $response->{messages}->[0],
+ qr/a database error has occured/,
+ 'correct message returned'
+ );
+}
+
done_testing();