8 my $base = 'http://localhost';
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
18 my $json = JSON->new->utf8;
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
23 my $artist_list_url = "$base/api/rpc/artist/list";
24 my $producer_list_url = "$base/api/rpc/producer/list";
25 my $track_list_url = "$base/api/rpc/track/list";
26 my $cd_list_url = "$base/api/rpc/cd/list";
35 'Accept' => 'text/x-json'
38 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
40 my @expected_response = map {
42 } $schema->resultset('Artist')->all;
43 my $response = $json->decode( $mech->content );
46 { list => \@expected_response, success => 'true' },
47 'correct message returned'
52 my $uri = URI->new($artist_list_url);
53 $uri->query_form( { 'search.artistid' => 1 } );
54 my $req = GET( $uri, 'Accept' => 'text/x-json' );
56 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
58 my @expected_response = map {
60 } $schema->resultset('Artist')->search( { artistid => 1 } )->all;
61 my $response = $json->decode( $mech->content );
64 { list => \@expected_response, success => 'true' },
65 'correct data returned'
70 my $uri = URI->new($artist_list_url);
71 $uri->query_form( { 'search.name.LIKE' => '%waul%' } );
72 my $req = GET( $uri, 'Accept' => 'text/x-json' );
74 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
76 my @expected_response = map {
78 } $schema->resultset('Artist')
79 ->search( { name => { LIKE => '%waul%' } } )->all;
80 my $response = $json->decode( $mech->content );
83 { list => \@expected_response, success => 'true' },
84 'correct data returned for complex query'
89 my $uri = URI->new($artist_list_url);
91 { 'search.name.LIKE' => '%waul%',
92 'list_returns.0.count' => '*',
96 my $req = GET( $uri, 'Accept' => 'text/x-json' );
98 cmp_ok( $mech->status, '==', 200, 'attempt with basic count' );
100 my @expected_response = map {
102 } $schema->resultset('Artist')
103 ->search( { name => { LIKE => '%waul%' } },
104 { select => [ { count => '*' } ], as => ['count'] } )->all;
105 my $response = $json->decode( $mech->content );
108 { list => \@expected_response, success => 'true' },
109 'correct data returned for count'
114 my $uri = URI->new($producer_list_url);
115 my $req = GET( $uri, 'Accept' => 'text/x-json' );
116 $mech->request($req);
117 cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
119 my @expected_response = map {
121 } $schema->resultset('Producer')->search( {}, { select => ['name'] } )
123 my $response = $json->decode( $mech->content );
126 { list => \@expected_response, success => 'true' },
127 'correct data returned for class with list_returns specified'
132 my $uri = URI->new($artist_list_url);
133 $uri->query_form( { 'search.cds.title' => 'Forkful of bees' } );
134 my $req = GET( $uri, 'Accept' => 'text/x-json' );
135 $mech->request($req);
136 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
138 my @expected_response = map {
140 } $schema->resultset('Artist')
141 ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } )
143 my $response = $json->decode( $mech->content );
146 { list => \@expected_response, success => 'true' },
147 'correct data returned for class with list_returns specified'
152 my $uri = URI->new($track_list_url);
153 $uri->query_form( { 'list_ordered_by' => 'position' } );
154 my $req = GET( $uri, 'Accept' => 'text/x-json' );
155 $mech->request($req);
156 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
158 my @expected_response = map {
160 } $schema->resultset('Track')->search(
162 { group_by => 'position',
163 order_by => 'position ASC',
167 my $response = $json->decode( $mech->content );
170 { list => \@expected_response, success => 'true' },
171 'correct data returned for class with everything specified in class'
176 my $uri = URI->new($track_list_url);
178 { 'list_ordered_by' => 'cd',
179 'list_returns' => 'cd',
180 'list_grouped_by' => 'cd'
183 my $req = GET( $uri, 'Accept' => 'text/x-json' );
184 $mech->request($req);
185 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
187 my @expected_response = map {
189 } $schema->resultset('Track')
191 { group_by => 'cd', order_by => 'cd ASC', select => 'cd' } )->all;
192 my $response = $json->decode( $mech->content );
195 { list => \@expected_response, success => 'true' },
196 'correct data returned when everything overridden in query'
201 my $uri = URI->new($track_list_url);
202 $uri->query_form( { 'list_ordered_by' => 'cd', 'list_count' => 2 } );
203 my $req = GET( $uri, 'Accept' => 'text/x-json' );
204 $mech->request($req);
205 cmp_ok( $mech->status, '==', 200, 'list count request okay' );
207 my @expected_response = map {
209 } $schema->resultset('Track')->search(
211 { group_by => 'position',
212 order_by => 'position ASC',
213 select => 'position',
217 my $response = $json->decode( $mech->content );
220 { list => \@expected_response, success => 'true' },
221 'correct data returned'
226 my $uri = URI->new($track_list_url);
228 { 'list_ordered_by' => 'cd',
230 'list_page' => 'fgdg'
233 my $req = GET( $uri, 'Accept' => 'text/x-json' );
234 $mech->request($req);
235 cmp_ok( $mech->status, '==', 400,
236 'non numeric list_page request not okay' );
237 my $response = $json->decode( $mech->content );
238 is( $response->{success}, 'false', 'correct data returned' );
240 $response->{messages}->[0],
241 qr/Attribute \(page\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value fgdg/,
242 'correct data returned'
247 my $uri = URI->new($track_list_url);
249 { 'list_ordered_by' => 'cd',
250 'list_count' => 'sdsdf',
254 my $req = GET( $uri, 'Accept' => 'text/x-json' );
255 $mech->request($req);
256 cmp_ok( $mech->status, '==', 400,
257 'non numeric list_count request not okay' );
258 my $response = $json->decode( $mech->content );
259 is( $response->{success}, 'false', 'correct data returned' );
261 $response->{messages}->[0],
262 qr/Attribute \(count\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value sdsdf/,
263 'correct data returned'
269 my $uri = URI->new($track_list_url);
271 { 'list_ordered_by' => 'cd', 'list_count' => 2, 'list_page' => 2 } );
272 my $req = GET( $uri, 'Accept' => 'text/x-json' );
273 $mech->request($req);
274 cmp_ok( $mech->status, '==', 200, 'list count with page request okay' );
276 my @expected_response = map {
278 } $schema->resultset('Track')->search(
280 { group_by => 'position',
281 order_by => 'position ASC',
282 select => 'position',
287 my $response = $json->decode( $mech->content );
290 { list => \@expected_response, success => 'true', totalcount => 3 },
291 'correct data returned'
296 my $uri = URI->new($track_list_url);
297 $uri->query_form( { 'list_ordered_by' => 'cd', 'list_page' => 2 } );
298 my $req = GET( $uri, 'Accept' => 'text/x-json' );
299 $mech->request($req);
300 cmp_ok( $mech->status, '==', 400,
301 'list page without count returns error' );
302 my $response = $json->decode( $mech->content );
304 $response->{messages}->[0],
305 qr/a database error has occured/,
306 'correct data returned'
311 my $uri = URI->new($cd_list_url);
312 $uri->query_form( { 'search.artist.name' => 'Caterwauler McCrae' } );
313 my $req = GET( $uri, 'Accept' => 'text/x-json' );
314 $mech->request($req);
316 $mech->status, '==', 200,
317 'search on rel with same name column request okay'
321 my @expected_response = map {
323 } $schema->resultset('CD')->search( { 'me.artist' => 1 } )->all;
324 my $response = $json->decode( $mech->content );
327 { list => \@expected_response, success => 'true' },
328 'correct data returned for search on rel with same name column'
334 my $uri = URI->new($cd_list_url);
335 $uri->query_form( { 'search.artist' => 1 } );
336 my $req = GET( $uri, 'Accept' => 'text/x-json' );
337 $mech->request($req);
338 cmp_ok( $mech->status, '==', 200,
339 'search on column with same name rel request okay' );
341 my @expected_response = map {
343 } $schema->resultset('CD')->search( { 'me.artist' => 1 } )->all;
344 my $response = $json->decode( $mech->content );
347 { list => \@expected_response, success => 'true' },
348 'correct data returned for search on column with same name rel'
353 my $uri = URI->new($cd_list_url);
355 { 'search.title' => 'Spoonful of bees',
356 'search.tracks.position' => 1
359 my $req = GET( $uri, 'Accept' => 'text/x-json' );
360 $mech->request($req);
362 $mech->status, '==', 200,
363 'search on col which exists for me and related table okay'
367 my @expected_response = map {
369 } $schema->resultset('CD')
371 { 'me.title' => 'Spoonful of bees', 'tracks.position' => 1 },
372 { join => 'tracks' } )->all;
373 my $response = $json->decode( $mech->content );
376 { list => \@expected_response, success => 'true' },
377 'correct data returned for search on col which exists for me and related table'
383 my $uri = URI->new($cd_list_url);
384 $uri->query_form( { 'list_ordered_by' => 'invalid_column' } );
385 my $req = GET( $uri, 'Accept' => 'text/x-json' );
386 $mech->request($req);
388 $mech->status, '==', 400,
389 'order_by on non-existing col returns error'
393 my $response = $json->decode( $mech->content );
396 { messages => ['a database error has occured.'],
399 'error returned for order_by on non-existing col'
405 my $uri = URI->new($cd_list_url);
407 { 'list_ordered_by' => 'invalid_column',
412 my $req = GET( $uri, 'Accept' => 'text/x-json' );
413 $mech->request($req);
415 $mech->status, '==', 400,
416 'order_by on invalid col with paging returns error'
420 my $response = $json->decode( $mech->content );
423 { messages => ['a database error has occured.'],
426 'error returned for order_by on non-existing col with paging'