use Dist::Zilla::PluginBundle::Git
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / list.t
CommitLineData
d2739840 1use strict;
2use warnings;
3
4use lib 't/lib';
5
6my $base = 'http://localhost';
7
8use RestTest;
9use DBICTest;
10use URI;
11use Test::More;
12use Test::WWW::Mechanize::Catalyst 'RestTest';
13use HTTP::Request::Common;
a5949bfd 14use JSON::MaybeXS;
0b0bf911 15
a5949bfd 16my $json = JSON::MaybeXS->new(utf8 => 1);
d2739840 17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 19ok( my $schema = DBICTest->init_schema(), 'got schema' );
d2739840 20
0b0bf911 21my $artist_list_url = "$base/api/rpc/artist/list";
d2739840 22my $producer_list_url = "$base/api/rpc/producer/list";
0b0bf911 23my $track_list_url = "$base/api/rpc/track/list";
24my $cd_list_url = "$base/api/rpc/cd/list";
d2739840 25
26# test open request
27{
0b0bf911 28 my $req = GET(
29 $artist_list_url,
30 {
31
32 },
33 'Accept' => 'text/x-json'
34 );
35 $mech->request($req);
36 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
37
38 my @expected_response = map {
39 { $_->get_columns }
40 } $schema->resultset('Artist')->all;
41 my $response = $json->decode( $mech->content );
42 is_deeply(
43 $response,
44 { list => \@expected_response, success => 'true' },
45 'correct message returned'
46 );
d2739840 47}
48
49{
0b0bf911 50 my $uri = URI->new($artist_list_url);
51 $uri->query_form( { 'search.artistid' => 1 } );
52 my $req = GET( $uri, 'Accept' => 'text/x-json' );
53 $mech->request($req);
54 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
55
56 my @expected_response = map {
57 { $_->get_columns }
58 } $schema->resultset('Artist')->search( { artistid => 1 } )->all;
59 my $response = $json->decode( $mech->content );
60 is_deeply(
61 $response,
62 { list => \@expected_response, success => 'true' },
63 'correct data returned'
64 );
d2739840 65}
66
67{
0b0bf911 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' );
71 $mech->request($req);
72 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
73
74 my @expected_response = map {
75 { $_->get_columns }
76 } $schema->resultset('Artist')
77 ->search( { name => { LIKE => '%waul%' } } )->all;
78 my $response = $json->decode( $mech->content );
79 is_deeply(
80 $response,
81 { list => \@expected_response, success => 'true' },
82 'correct data returned for complex query'
83 );
d2739840 84}
85
86{
0b0bf911 87 my $uri = URI->new($artist_list_url);
88 $uri->query_form(
89 { 'search.name.LIKE' => '%waul%',
90 'list_returns.0.count' => '*',
91 'as.0' => 'count'
92 }
93 );
94 my $req = GET( $uri, 'Accept' => 'text/x-json' );
95 $mech->request($req);
96 cmp_ok( $mech->status, '==', 200, 'attempt with basic count' );
97
98 my @expected_response = map {
99 { $_->get_columns }
100 } $schema->resultset('Artist')
101 ->search( { name => { LIKE => '%waul%' } },
102 { select => [ { count => '*' } ], as => ['count'] } )->all;
103 my $response = $json->decode( $mech->content );
104 is_deeply(
105 $response,
106 { list => \@expected_response, success => 'true' },
107 'correct data returned for count'
108 );
d2739840 109}
110
111{
0b0bf911 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' );
116
117 my @expected_response = map {
118 { $_->get_columns }
119 } $schema->resultset('Producer')->search( {}, { select => ['name'] } )
120 ->all;
121 my $response = $json->decode( $mech->content );
122 is_deeply(
123 $response,
124 { list => \@expected_response, success => 'true' },
125 'correct data returned for class with list_returns specified'
126 );
d2739840 127}
128
d2739840 129{
0b0bf911 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' );
135
136 my @expected_response = map {
137 { $_->get_columns }
138 } $schema->resultset('Artist')
139 ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } )
140 ->all;
141 my $response = $json->decode( $mech->content );
142 is_deeply(
143 $response,
144 { list => \@expected_response, success => 'true' },
145 'correct data returned for class with list_returns specified'
146 );
d2739840 147}
148
149{
0b0bf911 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' );
155
156 my @expected_response = map {
157 { $_->get_columns }
158 } $schema->resultset('Track')->search(
159 {},
160 { group_by => 'position',
161 order_by => 'position ASC',
162 select => 'position'
163 }
164 )->all;
165 my $response = $json->decode( $mech->content );
166 is_deeply(
167 $response,
168 { list => \@expected_response, success => 'true' },
169 'correct data returned for class with everything specified in class'
170 );
d2739840 171}
172
173{
0b0bf911 174 my $uri = URI->new($track_list_url);
175 $uri->query_form(
176 { 'list_ordered_by' => 'cd',
177 'list_returns' => 'cd',
178 'list_grouped_by' => 'cd'
179 }
180 );
181 my $req = GET( $uri, 'Accept' => 'text/x-json' );
182 $mech->request($req);
183 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
184
185 my @expected_response = map {
186 { $_->get_columns }
187 } $schema->resultset('Track')
188 ->search( {},
189 { group_by => 'cd', order_by => 'cd ASC', select => 'cd' } )->all;
190 my $response = $json->decode( $mech->content );
191 is_deeply(
192 $response,
193 { list => \@expected_response, success => 'true' },
194 'correct data returned when everything overridden in query'
195 );
d2739840 196}
197
198{
0b0bf911 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' );
204
205 my @expected_response = map {
206 { $_->get_columns }
207 } $schema->resultset('Track')->search(
208 {},
209 { group_by => 'position',
210 order_by => 'position ASC',
211 select => 'position',
212 rows => 2
213 }
214 )->all;
215 my $response = $json->decode( $mech->content );
216 is_deeply(
217 $response,
218 { list => \@expected_response, success => 'true' },
219 'correct data returned'
220 );
d2739840 221}
222
223{
0b0bf911 224 my $uri = URI->new($track_list_url);
225 $uri->query_form(
226 { 'list_ordered_by' => 'cd',
227 'list_count' => 2,
228 'list_page' => 'fgdg'
229 }
230 );
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' );
237 like(
238 $response->{messages}->[0],
8c7199a1 239 qr/Attribute \(page\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value (")?fgdg(")?/,
0b0bf911 240 'correct data returned'
241 );
d2739840 242}
243
244{
0b0bf911 245 my $uri = URI->new($track_list_url);
246 $uri->query_form(
247 { 'list_ordered_by' => 'cd',
248 'list_count' => 'sdsdf',
249 'list_page' => 2
250 }
251 );
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' );
258 like(
259 $response->{messages}->[0],
8c7199a1 260 qr/Attribute \(count\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value (")?sdsdf(")?/,
0b0bf911 261 'correct data returned'
262 );
263
d2739840 264}
265
266{
0b0bf911 267 my $uri = URI->new($track_list_url);
268 $uri->query_form(
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' );
273
274 my @expected_response = map {
275 { $_->get_columns }
276 } $schema->resultset('Track')->search(
277 {},
278 { group_by => 'position',
279 order_by => 'position ASC',
280 select => 'position',
281 rows => 2,
282 page => 2
283 }
284 )->all;
285 my $response = $json->decode( $mech->content );
286 is_deeply(
287 $response,
288 { list => \@expected_response, success => 'true', totalcount => 3 },
289 'correct data returned'
290 );
d2739840 291}
292
293{
0b0bf911 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 );
301 like(
302 $response->{messages}->[0],
303 qr/a database error has occured/,
304 'correct data returned'
305 );
d2739840 306}
307
308{
0b0bf911 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);
313 if (cmp_ok(
314 $mech->status, '==', 200,
315 'search on rel with same name column request okay'
316 )
317 )
318 {
319 my @expected_response = map {
320 { $_->get_columns }
321 } $schema->resultset('CD')->search( { 'me.artist' => 1 } )->all;
322 my $response = $json->decode( $mech->content );
323 is_deeply(
324 $response,
325 { list => \@expected_response, success => 'true' },
326 'correct data returned for search on rel with same name column'
327 );
328 }
d2739840 329}
330
331{
0b0bf911 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' );
338
339 my @expected_response = map {
340 { $_->get_columns }
341 } $schema->resultset('CD')->search( { 'me.artist' => 1 } )->all;
342 my $response = $json->decode( $mech->content );
343 is_deeply(
344 $response,
345 { list => \@expected_response, success => 'true' },
346 'correct data returned for search on column with same name rel'
347 );
d2739840 348}
349
350{
0b0bf911 351 my $uri = URI->new($cd_list_url);
352 $uri->query_form(
353 { 'search.title' => 'Spoonful of bees',
354 'search.tracks.position' => 1
355 }
356 );
357 my $req = GET( $uri, 'Accept' => 'text/x-json' );
358 $mech->request($req);
359 if (cmp_ok(
360 $mech->status, '==', 200,
361 'search on col which exists for me and related table okay'
362 )
363 )
364 {
365 my @expected_response = map {
366 { $_->get_columns }
367 } $schema->resultset('CD')
368 ->search(
369 { 'me.title' => 'Spoonful of bees', 'tracks.position' => 1 },
370 { join => 'tracks' } )->all;
371 my $response = $json->decode( $mech->content );
372 is_deeply(
373 $response,
374 { list => \@expected_response, success => 'true' },
375 'correct data returned for search on col which exists for me and related table'
376 );
377 }
d2739840 378}
379
380{
0b0bf911 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);
385 if (cmp_ok(
386 $mech->status, '==', 400,
387 'order_by on non-existing col returns error'
388 )
389 )
390 {
391 my $response = $json->decode( $mech->content );
392 is_deeply(
393 $response,
394 { messages => ['a database error has occured.'],
395 success => 'false'
396 },
397 'error returned for order_by on non-existing col'
398 );
399 }
d2739840 400}
401
402{
0b0bf911 403 my $uri = URI->new($cd_list_url);
404 $uri->query_form(
405 { 'list_ordered_by' => 'invalid_column',
406 'list_count' => 2,
407 'list_page' => 1
408 }
409 );
410 my $req = GET( $uri, 'Accept' => 'text/x-json' );
411 $mech->request($req);
412 if (cmp_ok(
413 $mech->status, '==', 400,
414 'order_by on invalid col with paging returns error'
415 )
416 )
417 {
418 my $response = $json->decode( $mech->content );
419 is_deeply(
420 $response,
421 { messages => ['a database error has occured.'],
422 success => 'false'
423 },
424 'error returned for order_by on non-existing col with paging'
425 );
426 }
d2739840 427}
428
429done_testing();