use JSON instead of JSON::Any to get rid of the CPAN Testers failures when only JSON...
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / create.t
CommitLineData
d2739840 1use 5.6.0;
2
3use strict;
4use warnings;
5
6use lib 't/lib';
7
8my $base = 'http://localhost';
9
10use RestTest;
11use DBICTest;
12use Test::More;
13use Test::WWW::Mechanize::Catalyst 'RestTest';
14use HTTP::Request::Common;
0b0bf911 15use JSON;
16
17my $json = JSON->new->utf8;
d2739840 18
19my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 20ok( my $schema = DBICTest->init_schema(), 'got schema' );
d2739840 21
0b0bf911 22my $artist_create_url = "$base/api/rest/artist";
d2739840 23my $producer_create_url = "$base/api/rest/producer";
24
25# test validation when no params sent
26{
0b0bf911 27 my $test_data = $json->encode( { wrong_param => 'value' } );
28 my $req = PUT($artist_create_url);
29 $req->content_type('text/x-json');
30 $req->content_length(
31 do { use bytes; length($test_data) }
32 );
33 $req->content($test_data);
34 $mech->request($req);
35
36 cmp_ok( $mech->status, '==', 400,
37 'attempt without required params caught' );
38 my $response = $json->decode( $mech->content );
39 like(
40 $response->{messages}->[0],
41 qr/No value supplied for name and no default/,
42 'correct message returned'
43 );
d2739840 44}
45
46# test default value used if default value exists
47{
0b0bf911 48 my $test_data = $json->encode( {} );
49 my $req = PUT($producer_create_url);
50 $req->content_type('text/x-json');
51 $req->content_length(
52 do { use bytes; length($test_data) }
53 );
54 $req->content($test_data);
55 $mech->request($req);
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' );
d2739840 60}
61
62# test create works as expected when passing required value
63{
0b0bf911 64 my $test_data = $json->encode( { name => 'king luke' } );
65 my $req = PUT($producer_create_url);
66 $req->content_type('text/x-json');
67 $req->content_length(
68 do { use bytes; length($test_data) }
69 );
70 $req->content($test_data);
71 $mech->request($req);
72 cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
73 my $new_obj =
74 $schema->resultset('Producer')->find( { name => 'king luke' } );
75 ok( $new_obj, 'record created with specified name' );
d2739840 76
0b0bf911 77 my $response = $json->decode( $mech->content );
78 is_deeply(
79 $response->{list},
80 { $new_obj->get_columns },
81 'json for new producer returned'
82 );
d2739840 83}
84
85# test bulk create
86{
0b0bf911 87 my $test_data = $json->encode(
88 { list => [ { name => 'king nperez' }, { name => 'queen perla' } ] }
89 );
90 my $req = PUT($producer_create_url);
91 $req->content_type('text/x-json');
92 $req->content_length(
93 do { use bytes; length($test_data) }
94 );
95 $req->content($test_data);
96 $mech->request($req);
97 cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
98 my $rs =
99 $schema->resultset('Producer')
100 ->search( [ { name => 'king nperez' }, { name => 'queen perla' } ] );
101 ok( $rs, 'record created with specified name' );
d2739840 102
0b0bf911 103 my $response = $json->decode( $mech->content );
104 my $expected =
105 [ map { my %foo = $_->get_inflated_columns; \%foo; } $rs->all ];
106 is_deeply( $response->{list}, $expected,
107 'json for bulk create returned' );
d2739840 108}
109
110done_testing();