6 my $base = 'http://localhost';
11 use Test::WWW::Mechanize::Catalyst 'RestTest';
12 use HTTP::Request::Common;
15 my $json = JSON->new->utf8;
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";
23 # test validation when no params sent
25 my $test_data = $json->encode( { wrong_param => 'value' } );
26 my $req = PUT($artist_create_url);
27 $req->content_type('text/x-json');
29 do { use bytes; length($test_data) }
31 $req->content($test_data);
34 cmp_ok( $mech->status, '==', 400,
35 'attempt without required params caught' );
36 my $response = $json->decode( $mech->content );
38 $response->{messages}->[0],
39 qr/No value supplied for name and no default/,
40 'correct message returned'
44 # test default value used if default value exists
46 my $test_data = $json->encode( {} );
47 my $req = PUT($producer_create_url);
48 $req->content_type('text/x-json');
50 do { use bytes; length($test_data) }
52 $req->content($test_data);
54 cmp_ok( $mech->status, '==', 200,
55 'default value used when not supplied' );
56 ok( $schema->resultset('Producer')->find( { name => 'fred' } ),
57 'record created with default name' );
60 # test create works as expected when passing required value
62 my $test_data = $json->encode( { name => 'king luke' } );
63 my $req = PUT($producer_create_url);
64 $req->content_type('text/x-json');
66 do { use bytes; length($test_data) }
68 $req->content($test_data);
70 cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
72 $schema->resultset('Producer')->find( { name => 'king luke' } );
73 ok( $new_obj, 'record created with specified name' );
75 my $response = $json->decode( $mech->content );
78 { $new_obj->get_columns },
79 'json for new producer returned'
85 my $test_data = $json->encode(
86 { list => [ { name => 'king nperez' }, { name => 'queen perla' } ] }
88 my $req = PUT($producer_create_url);
89 $req->content_type('text/x-json');
91 do { use bytes; length($test_data) }
93 $req->content($test_data);
95 cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
97 $schema->resultset('Producer')
98 ->search( [ { name => 'king nperez' }, { name => 'queen perla' } ] );
99 ok( $rs, 'record created with specified name' );
101 my $response = $json->decode( $mech->content );
103 [ map { my %foo = $_->get_inflated_columns; \%foo; } $rs->all ];
104 is_deeply( $response->{list}, $expected,
105 'json for bulk create returned' );