removed perl 5.6.0 requirement from test files
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / update.t
1 use strict;
2 use warnings;
3
4 use lib 't/lib';
5
6 my $base = 'http://localhost';
7 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
8
9 use RestTest;
10 use DBICTest;
11 use Test::More;
12 use Test::WWW::Mechanize::Catalyst 'RestTest';
13 use HTTP::Request::Common;
14 use JSON;
15
16 my $json = JSON->new->utf8;
17
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok( my $schema = DBICTest->init_schema(), 'got schema' );
20
21 my $track         = $schema->resultset('Track')->first;
22 my %original_cols = $track->get_columns;
23
24 my $track_url         = "$base/api/rest/track/";
25 my $track_update_url  = $track_url . $track->id;
26 my $tracks_update_url = $track_url;
27
28 # test invalid track id caught
29 {
30     foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
31         my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
32         my $test_data     = $json->encode( { title => 'value' } );
33         my $req           = POST( $incorrect_url, Content => $test_data );
34         $req->content_type('text/x-json');
35         $mech->request($req);
36
37         cmp_ok( $mech->status, '==', 400,
38             'Attempt with invalid track id caught' );
39
40         my $response = $json->decode( $mech->content );
41         like(
42             $response->{messages}->[0],
43             qr/No object found for id/,
44             'correct message returned'
45         );
46
47         $track->discard_changes;
48         is_deeply(
49             { $track->get_columns },
50             \%original_cols,
51             'no update occurred'
52         );
53     }
54 }
55
56 # validation when no params sent
57 {
58     my $test_data = $json->encode( { wrong_param => 'value' } );
59     my $req = POST( $track_update_url, Content => $test_data );
60     $req->content_type('text/x-json');
61     $mech->request($req);
62
63     cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
64
65     my $response = $json->decode( $mech->content );
66     is_deeply( $response->{messages}, ['No valid keys passed'],
67         'correct message returned' );
68
69     $track->discard_changes;
70     is_deeply(
71         { $track->get_columns },
72         \%original_cols,
73         'no update occurred'
74     );
75 }
76
77 {
78     my $test_data = $json->encode( { title => undef } );
79     my $req = POST( $track_update_url, Content => $test_data );
80     $req->content_type('text/x-json');
81     $mech->request($req);
82     cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
83
84     $track->discard_changes;
85     isnt( $track->title, $original_cols{title}, 'Title changed' );
86     is( $track->title, undef, 'Title changed to undef' );
87 }
88
89 {
90     my $test_data = $json->encode(
91         { title => 'monkey monkey', 'cd' => { year => 2009 } } );
92     my $req = POST( $track_update_url, Content => $test_data );
93     $req->content_type('text/x-json');
94     $mech->request($req);
95
96     cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
97
98     $track->discard_changes;
99     is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
100     is( $track->cd->year, 2009, 'related row updated' );
101 }
102
103 # bulk_update existing objects
104 {
105
106     # order to get a stable order of rows
107     my $tracks_rs =
108         $schema->resultset('Track')
109         ->search( undef, { order_by => 'trackid', rows => 3 } );
110     my $test_data = $json->encode(
111         {   list => [
112                 map +{ id => $_->id, title => 'Track ' . $_->id },
113                 $tracks_rs->all
114             ]
115         }
116     );
117     my $req = PUT( $tracks_update_url, Content => $test_data );
118     $req->content_type('text/x-json');
119     $mech->request($req);
120     cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
121
122     $tracks_rs->reset;
123     while ( my $track = $tracks_rs->next ) {
124         is( $track->title, 'Track ' . $track->id, 'Title changed' );
125     }
126 }
127
128 # bulk_update nonexisting objects
129 {
130
131     # order to get a stable order of rows
132     my $test_data = $json->encode(
133         {   list => [
134                 map +{ id => $_, title => 'Track ' . $_ },
135                 ( 1000 .. 1002 )
136             ]
137         }
138     );
139     my $req = PUT( $tracks_update_url, Content => $test_data );
140     $req->content_type('text/x-json');
141     $mech->request($req);
142     cmp_ok( $mech->status, '==', 400,
143         'Attempt to update three nonexisting tracks fails' );
144     my $response = $json->decode( $mech->content );
145     is( $response->{success}, JSON::false,
146         'success property returns unquoted false' );
147     like(
148         $response->{messages}->[0],
149         qr/No object found for id/,
150         'correct message returned'
151     );
152 }
153
154 done_testing();