6 my $base = 'http://localhost';
12 use Test::WWW::Mechanize::Catalyst 'RestTest';
13 use HTTP::Request::Common;
16 my $json = JSON->new->utf8;
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok( my $schema = DBICTest->init_schema(), 'got schema' );
21 my $artist_list_url = "$base/api/rpc/artist/list";
22 my $producer_list_url = "$base/api/rpc/producer/list";
23 my $track_list_url = "$base/api/rpc/track/list";
24 my $cd_list_url = "$base/api/rpc/cd/list";
33 'Accept' => 'text/x-json'
36 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
38 my @expected_response = map {
40 } $schema->resultset('Artist')->all;
41 my $response = $json->decode( $mech->content );
44 { list => \@expected_response, success => 'true' },
45 'correct message returned'
50 my $uri = URI->new($artist_list_url);
51 $uri->query_form( { 'search.artistid' => 1 } );
52 my $req = GET( $uri, 'Accept' => 'text/x-json' );
54 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
56 my @expected_response = map {
58 } $schema->resultset('Artist')->search( { artistid => 1 } )->all;
59 my $response = $json->decode( $mech->content );
62 { list => \@expected_response, success => 'true' },
63 'correct data returned'
68 my $uri = URI->new($artist_list_url);
69 $uri->query_form( { 'search.name.LIKE' => '%waul%' } );
70 my $req = GET( $uri, 'Accept' => 'text/x-json' );
72 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
74 my @expected_response = map {
76 } $schema->resultset('Artist')
77 ->search( { name => { LIKE => '%waul%' } } )->all;
78 my $response = $json->decode( $mech->content );
81 { list => \@expected_response, success => 'true' },
82 'correct data returned for complex query'
87 my $uri = URI->new($artist_list_url);
89 { 'search.name.LIKE' => '%waul%',
90 'list_returns.0.count' => '*',
94 my $req = GET( $uri, 'Accept' => 'text/x-json' );
96 cmp_ok( $mech->status, '==', 200, 'attempt with basic count' );
98 my @expected_response = map {
100 } $schema->resultset('Artist')
101 ->search( { name => { LIKE => '%waul%' } },
102 { select => [ { count => '*' } ], as => ['count'] } )->all;
103 my $response = $json->decode( $mech->content );
106 { list => \@expected_response, success => 'true' },
107 'correct data returned for count'
112 my $uri = URI->new($producer_list_url);
113 my $req = GET( $uri, 'Accept' => 'text/x-json' );
114 $mech->request($req);
115 cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
117 my @expected_response = map {
119 } $schema->resultset('Producer')->search( {}, { select => ['name'] } )
121 my $response = $json->decode( $mech->content );
124 { list => \@expected_response, success => 'true' },
125 'correct data returned for class with list_returns specified'
130 my $uri = URI->new($artist_list_url);
131 $uri->query_form( { 'search.cds.title' => 'Forkful of bees' } );
132 my $req = GET( $uri, 'Accept' => 'text/x-json' );
133 $mech->request($req);
134 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
136 my @expected_response = map {
138 } $schema->resultset('Artist')
139 ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } )
141 my $response = $json->decode( $mech->content );
144 { list => \@expected_response, success => 'true' },
145 'correct data returned for class with list_returns specified'
150 my $uri = URI->new($track_list_url);
151 $uri->query_form( { 'list_ordered_by' => 'position' } );
152 my $req = GET( $uri, 'Accept' => 'text/x-json' );
153 $mech->request($req);
154 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
156 my @expected_response = map {
158 } $schema->resultset('Track')->search(
160 { group_by => 'position',
161 order_by => 'position ASC',
165 my $response = $json->decode( $mech->content );
168 { list => \@expected_response, success => 'true' },
169 'correct data returned for class with everything specified in class'
174 my $uri = URI->new($track_list_url);
176 { 'list_ordered_by' => 'cd',
177 'list_returns' => 'cd',
178 'list_grouped_by' => 'cd'
181 my $req = GET( $uri, 'Accept' => 'text/x-json' );
182 $mech->request($req);
183 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
185 my @expected_response = map {
187 } $schema->resultset('Track')
189 { group_by => 'cd', order_by => 'cd ASC', select => 'cd' } )->all;
190 my $response = $json->decode( $mech->content );
193 { list => \@expected_response, success => 'true' },
194 'correct data returned when everything overridden in query'
199 my $uri = URI->new($track_list_url);
200 $uri->query_form( { 'list_ordered_by' => 'cd', 'list_count' => 2 } );
201 my $req = GET( $uri, 'Accept' => 'text/x-json' );
202 $mech->request($req);
203 cmp_ok( $mech->status, '==', 200, 'list count request okay' );
205 my @expected_response = map {
207 } $schema->resultset('Track')->search(
209 { group_by => 'position',
210 order_by => 'position ASC',
211 select => 'position',
215 my $response = $json->decode( $mech->content );
218 { list => \@expected_response, success => 'true' },
219 'correct data returned'
224 my $uri = URI->new($track_list_url);
226 { 'list_ordered_by' => 'cd',
228 'list_page' => 'fgdg'
231 my $req = GET( $uri, 'Accept' => 'text/x-json' );
232 $mech->request($req);
233 cmp_ok( $mech->status, '==', 400,
234 'non numeric list_page request not okay' );
235 my $response = $json->decode( $mech->content );
236 is( $response->{success}, 'false', 'correct data returned' );
238 $response->{messages}->[0],
239 qr/Attribute \(page\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value (")?fgdg(")?/,
240 'correct data returned'
245 my $uri = URI->new($track_list_url);
247 { 'list_ordered_by' => 'cd',
248 'list_count' => 'sdsdf',
252 my $req = GET( $uri, 'Accept' => 'text/x-json' );
253 $mech->request($req);
254 cmp_ok( $mech->status, '==', 400,
255 'non numeric list_count request not okay' );
256 my $response = $json->decode( $mech->content );
257 is( $response->{success}, 'false', 'correct data returned' );
259 $response->{messages}->[0],
260 qr/Attribute \(count\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value (")?sdsdf(")?/,
261 'correct data returned'
267 my $uri = URI->new($track_list_url);
269 { 'list_ordered_by' => 'cd', 'list_count' => 2, 'list_page' => 2 } );
270 my $req = GET( $uri, 'Accept' => 'text/x-json' );
271 $mech->request($req);
272 cmp_ok( $mech->status, '==', 200, 'list count with page request okay' );
274 my @expected_response = map {
276 } $schema->resultset('Track')->search(
278 { group_by => 'position',
279 order_by => 'position ASC',
280 select => 'position',
285 my $response = $json->decode( $mech->content );
288 { list => \@expected_response, success => 'true', totalcount => 3 },
289 'correct data returned'
294 my $uri = URI->new($track_list_url);
295 $uri->query_form( { 'list_ordered_by' => 'cd', 'list_page' => 2 } );
296 my $req = GET( $uri, 'Accept' => 'text/x-json' );
297 $mech->request($req);
298 cmp_ok( $mech->status, '==', 400,
299 'list page without count returns error' );
300 my $response = $json->decode( $mech->content );
302 $response->{messages}->[0],
303 qr/a database error has occured/,
304 'correct data returned'
309 my $uri = URI->new($cd_list_url);
310 $uri->query_form( { 'search.artist.name' => 'Caterwauler McCrae' } );
311 my $req = GET( $uri, 'Accept' => 'text/x-json' );
312 $mech->request($req);
314 $mech->status, '==', 200,
315 'search on rel with same name column request okay'
319 my @expected_response = map {
321 } $schema->resultset('CD')->search( { 'me.artist' => 1 } )->all;
322 my $response = $json->decode( $mech->content );
325 { list => \@expected_response, success => 'true' },
326 'correct data returned for search on rel with same name column'
332 my $uri = URI->new($cd_list_url);
333 $uri->query_form( { 'search.artist' => 1 } );
334 my $req = GET( $uri, 'Accept' => 'text/x-json' );
335 $mech->request($req);
336 cmp_ok( $mech->status, '==', 200,
337 'search on column with same name rel request okay' );
339 my @expected_response = map {
341 } $schema->resultset('CD')->search( { 'me.artist' => 1 } )->all;
342 my $response = $json->decode( $mech->content );
345 { list => \@expected_response, success => 'true' },
346 'correct data returned for search on column with same name rel'
351 my $uri = URI->new($cd_list_url);
353 { 'search.title' => 'Spoonful of bees',
354 'search.tracks.position' => 1
357 my $req = GET( $uri, 'Accept' => 'text/x-json' );
358 $mech->request($req);
360 $mech->status, '==', 200,
361 'search on col which exists for me and related table okay'
365 my @expected_response = map {
367 } $schema->resultset('CD')
369 { 'me.title' => 'Spoonful of bees', 'tracks.position' => 1 },
370 { join => 'tracks' } )->all;
371 my $response = $json->decode( $mech->content );
374 { list => \@expected_response, success => 'true' },
375 'correct data returned for search on col which exists for me and related table'
381 my $uri = URI->new($cd_list_url);
382 $uri->query_form( { 'list_ordered_by' => 'invalid_column' } );
383 my $req = GET( $uri, 'Accept' => 'text/x-json' );
384 $mech->request($req);
386 $mech->status, '==', 400,
387 'order_by on non-existing col returns error'
391 my $response = $json->decode( $mech->content );
394 { messages => ['a database error has occured.'],
397 'error returned for order_by on non-existing col'
403 my $uri = URI->new($cd_list_url);
405 { 'list_ordered_by' => 'invalid_column',
410 my $req = GET( $uri, 'Accept' => 'text/x-json' );
411 $mech->request($req);
413 $mech->status, '==', 400,
414 'order_by on invalid col with paging returns error'
418 my $response = $json->decode( $mech->content );
421 { messages => ['a database error has occured.'],
424 'error returned for order_by on non-existing col with paging'