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