fixed test failure in generic.t with Moose > 2.02
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / 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_update_url = "$base/api/rpc/track/id/" . $track->id . "/update";
25 my $any_track_update_url =
26     "$base/api/rpc/any/track/id/" . $track->id . "/update";
27 my $tracks_update_url = "$base/api/rpc/track/update";
28
29 # test invalid track id caught
30 {
31     foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
32         my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
33         my $req = POST( $incorrect_url, { title => 'value' } );
34
35         $mech->request( $req, $content_type );
36         cmp_ok( $mech->status, '==', 400,
37             'Attempt with invalid track id caught' );
38
39         my $response = $json->decode( $mech->content );
40         like(
41             $response->{messages}->[0],
42             qr/No object found for id/,
43             'correct message returned'
44         );
45
46         $track->discard_changes;
47         is_deeply(
48             { $track->get_columns },
49             \%original_cols,
50             'no update occurred'
51         );
52     }
53 }
54
55 # validation when no params sent
56 {
57     my $req = POST( $track_update_url, { wrong_param => 'value' } );
58     $mech->request( $req, $content_type );
59     cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
60
61     my $response = $json->decode( $mech->content );
62     is_deeply( $response->{messages}, ['No valid keys passed'],
63         'correct message returned' );
64
65     $track->discard_changes;
66     is_deeply(
67         { $track->get_columns },
68         \%original_cols,
69         'no update occurred'
70     );
71 }
72
73 {
74     my $req = POST( $track_update_url, { wrong_param => 'value' } );
75     $mech->request( $req, $content_type );
76     cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
77
78     my $response = $json->decode( $mech->content );
79     is_deeply( $response->{messages}, ['No valid keys passed'],
80         'correct message returned' );
81
82     $track->discard_changes;
83     is_deeply(
84         { $track->get_columns },
85         \%original_cols,
86         'no update occurred'
87     );
88 }
89
90 {
91     my $req = POST( $track_update_url, { title => undef } );
92     $mech->request( $req, $content_type );
93     cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
94
95     $track->discard_changes;
96     isnt( $track->title, $original_cols{title}, 'Title changed' );
97     is( $track->title, '', 'Title changed to undef' );
98 }
99
100 {
101     my $req = POST( $track_update_url, { title => 'monkey monkey' } );
102     $mech->request( $req, $content_type );
103     cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
104
105     $track->discard_changes;
106     is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
107 }
108
109 {
110     my $req = POST(
111         $track_update_url,
112         {   title     => 'sheep sheep',
113             'cd.year' => '2009'
114         }
115     );
116     $mech->request( $req, $content_type );
117     cmp_ok( $mech->status, '==', 200,
118         'Update with key with value and related key okay' );
119
120     $track->discard_changes;
121     is( $track->title,    'sheep sheep', 'Title changed' );
122     is( $track->cd->year, '2009',        'Related field changed"' );
123 }
124
125 {
126     my $req = POST( $any_track_update_url, { title => 'baa' } );
127     $mech->request( $req, $content_type );
128     cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
129
130     $track->discard_changes;
131     is( $track->title, 'baa', 'Title changed' );
132 }
133
134 {
135     my $req = POST( $any_track_update_url, { position => '14' } );
136     $mech->request( $req, $content_type );
137     cmp_ok( $mech->status, '==', 200, 'Position update okay' );
138
139     $track->discard_changes;
140     is( $track->get_column('position'), '14', 'Position changed' );
141 }
142
143 # bulk_update existing objects
144 {
145
146     # order to get a stable order of rows
147     my $tracks_rs =
148         $schema->resultset('Track')
149         ->search( undef, { order_by => 'trackid', rows => 3 } );
150     my $test_data = $json->encode(
151         {   list => [
152                 map +{ id => $_->id, title => 'Track ' . $_->id },
153                 $tracks_rs->all
154             ]
155         }
156     );
157     my $req = POST( $tracks_update_url, Content => $test_data );
158     $req->content_type('text/x-json');
159     $mech->request($req);
160     cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
161
162     $tracks_rs->reset;
163     while ( my $track = $tracks_rs->next ) {
164         is( $track->title, 'Track ' . $track->id, 'Title changed' );
165     }
166 }
167
168 # bulk_update nonexisting objects
169 {
170
171     # order to get a stable order of rows
172     my $test_data = $json->encode(
173         {   list => [
174                 map +{ id => $_, title => 'Track ' . $_ },
175                 ( 1000 .. 1002 )
176             ]
177         }
178     );
179     my $req = POST( $tracks_update_url, Content => $test_data );
180     $req->content_type('text/x-json');
181     $mech->request($req);
182     cmp_ok( $mech->status, '==', 400,
183         'Attempt to update three nonexisting tracks fails' );
184     my $response = $json->decode( $mech->content );
185     is( $response->{success}, 'false',
186         'success property returns quoted false' );
187     like(
188         $response->{messages}->[0],
189         qr/No object found for id/,
190         'correct message returned'
191     );
192 }
193
194 done_testing();