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