6 my $base = 'http://localhost';
11 use Test::WWW::Mechanize::Catalyst 'RestTest';
12 use HTTP::Request::Common;
15 my $json = JSON::MaybeXS->new(utf8 => 1);
17 my $mech = Test::WWW::Mechanize::Catalyst->new;
18 ok( my $schema = DBICTest->init_schema(), 'got schema' );
20 my $artist_create_url = "$base/api/rest/artist";
21 my $producer_create_url = "$base/api/rest/producer";
22 my $track_create_url = "$base/api/rest/track";
24 # test validation when no params sent
26 my $test_data = $json->encode( { wrong_param => 'value' } );
27 my $req = PUT($artist_create_url);
28 $req->content_type('text/x-json');
30 do { use bytes; length($test_data) }
32 $req->content($test_data);
35 cmp_ok( $mech->status, '==', 400,
36 'attempt without required params caught' );
37 my $response = $json->decode( $mech->content );
39 $response->{messages}->[0],
40 qr/No value supplied for name and no default/,
41 'correct message returned'
45 # test default value used if default value exists
47 my $test_data = $json->encode( {} );
48 my $req = PUT($producer_create_url);
49 $req->content_type('text/x-json');
51 do { use bytes; length($test_data) }
53 $req->content($test_data);
55 cmp_ok( $mech->status, '==', 200,
56 'default value used when not supplied' );
57 ok( $schema->resultset('Producer')->find( { name => 'fred' } ),
58 'record created with default name' );
61 # test create works as expected when passing required value
63 my $test_data = $json->encode( { name => 'king luke' } );
64 my $req = PUT($producer_create_url);
65 $req->content_type('text/x-json');
67 do { use bytes; length($test_data) }
69 $req->content($test_data);
71 cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
73 $schema->resultset('Producer')->find( { name => 'king luke' } );
74 ok( $new_obj, 'record created with specified name' );
76 my $response = $json->decode( $mech->content );
79 { $new_obj->get_columns },
80 'json for new producer returned'
86 my $test_data = $json->encode(
87 { list => [ { name => 'king nperez' }, { name => 'queen perla' } ] }
89 my $req = PUT($producer_create_url);
90 $req->content_type('text/x-json');
92 do { use bytes; length($test_data) }
94 $req->content($test_data);
96 cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
98 $schema->resultset('Producer')
99 ->search( [ { name => 'king nperez' }, { name => 'queen perla' } ] );
100 ok( $rs, 'record created with specified name' );
102 my $response = $json->decode( $mech->content );
104 [ map { my %foo = $_->get_inflated_columns; \%foo; } $rs->all ];
105 is_deeply( $response->{list}, $expected,
106 'json for bulk create returned' );
109 # test bulk create with database error
111 my $test_data = $json->encode({
120 # set position column to NULL to force error
126 #my $req = PUT( $tracks_update_url, Content => $test_data );
127 my $req = PUT($track_create_url);
128 $req->content_type('text/x-json');
129 $req->content_length(
130 do { use bytes; length($test_data) }
132 $req->content($test_data);
133 $mech->request($req);
134 cmp_ok( $mech->status, '==', 400, 'bulk request with one invalid object fails' );
136 $schema->resultset('Track')
137 ->search( [ { cd => 2 } ] );
138 is( $rs->count, 3, 'no records created' );
140 my $response = $json->decode( $mech->content );
141 is( $response->{success}, JSON::MaybeXS::false,
142 'success property returns unquoted false' );
144 $response->{messages}->[0],
145 qr/a database error has occured/,
146 'correct message returned'