8 my $base = 'http://localhost';
13 use Test::WWW::Mechanize::Catalyst 'RestTest';
14 use HTTP::Request::Common;
17 my $json = JSON->new->utf8;
19 my $mech = Test::WWW::Mechanize::Catalyst->new;
20 ok( my $schema = DBICTest->init_schema(), 'got schema' );
22 my $artist_create_url = "$base/api/rest/artist";
23 my $producer_create_url = "$base/api/rest/producer";
25 # test validation when no params sent
27 my $test_data = $json->encode( { wrong_param => 'value' } );
28 my $req = PUT($artist_create_url);
29 $req->content_type('text/x-json');
31 do { use bytes; length($test_data) }
33 $req->content($test_data);
36 cmp_ok( $mech->status, '==', 400,
37 'attempt without required params caught' );
38 my $response = $json->decode( $mech->content );
40 $response->{messages}->[0],
41 qr/No value supplied for name and no default/,
42 'correct message returned'
46 # test default value used if default value exists
48 my $test_data = $json->encode( {} );
49 my $req = PUT($producer_create_url);
50 $req->content_type('text/x-json');
52 do { use bytes; length($test_data) }
54 $req->content($test_data);
56 cmp_ok( $mech->status, '==', 200,
57 'default value used when not supplied' );
58 ok( $schema->resultset('Producer')->find( { name => 'fred' } ),
59 'record created with default name' );
62 # test create works as expected when passing required value
64 my $test_data = $json->encode( { name => 'king luke' } );
65 my $req = PUT($producer_create_url);
66 $req->content_type('text/x-json');
68 do { use bytes; length($test_data) }
70 $req->content($test_data);
72 cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
74 $schema->resultset('Producer')->find( { name => 'king luke' } );
75 ok( $new_obj, 'record created with specified name' );
77 my $response = $json->decode( $mech->content );
80 { $new_obj->get_columns },
81 'json for new producer returned'
87 my $test_data = $json->encode(
88 { list => [ { name => 'king nperez' }, { name => 'queen perla' } ] }
90 my $req = PUT($producer_create_url);
91 $req->content_type('text/x-json');
93 do { use bytes; length($test_data) }
95 $req->content($test_data);
97 cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
99 $schema->resultset('Producer')
100 ->search( [ { name => 'king nperez' }, { name => 'queen perla' } ] );
101 ok( $rs, 'record created with specified name' );
103 my $response = $json->decode( $mech->content );
105 [ map { my %foo = $_->get_inflated_columns; \%foo; } $rs->all ];
106 is_deeply( $response->{list}, $expected,
107 'json for bulk create returned' );