8 my $base = 'http://localhost';
13 use Test::WWW::Mechanize::Catalyst 'RestTest';
14 use HTTP::Request::Common;
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::Any->Dump({ 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, 'attempt without required params caught' );
35 my $response = JSON::Any->Load( $mech->content);
36 like($response->{messages}->[0], qr/No value supplied for name and no default/, 'correct message returned' );
39 # test default value used if default value exists
41 my $test_data = JSON::Any->Dump({});
42 my $req = PUT( $producer_create_url );
43 $req->content_type('text/x-json');
45 do { use bytes; length( $test_data ) }
47 $req->content( $test_data );
49 cmp_ok( $mech->status, '==', 200, 'default value used when not supplied' );
50 ok($schema->resultset('Producer')->find({ name => 'fred' }), 'record created with default name');
53 # test create works as expected when passing required value
55 my $test_data = JSON::Any->Dump({ name => 'king luke' });
56 my $req = PUT( $producer_create_url );
57 $req->content_type('text/x-json');
59 do { use bytes; length( $test_data ) }
61 $req->content( $test_data );
63 cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
64 my $new_obj = $schema->resultset('Producer')->find({ name => 'king luke' });
65 ok($new_obj, 'record created with specified name');
67 my $response = JSON::Any->Load( $mech->content);
68 is_deeply( $response->{list}, { $new_obj->get_columns }, 'json for new producer returned' );
73 my $test_data = JSON::Any->Dump({ list => [{ name => 'king nperez' }, { name => 'queen perla'}] });
74 my $req = PUT( $producer_create_url );
75 $req->content_type('text/x-json');
77 do { use bytes; length( $test_data ) }
79 $req->content( $test_data );
81 cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
82 my $rs = $schema->resultset('Producer')->search([ { name => 'king nperez' }, { name => 'queen perla' } ]);
83 ok($rs, 'record created with specified name');
85 my $response = JSON::Any->Load( $mech->content);
86 my $expected = [ map { my %foo = $_->get_inflated_columns; \%foo; } $rs->all ];
87 is_deeply( $response->{list}, $expected, 'json for bulk create returned' );