use JSON instead of JSON::Any to get rid of the CPAN Testers failures when only JSON...
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / list.t
1 use 5.6.0;
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7
8 my $base = 'http://localhost';
9
10 use RestTest;
11 use DBICTest;
12 use URI;
13 use Test::More;
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
16 use JSON;
17
18 my $json = JSON->new->utf8;
19
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
22
23 my $artist_list_url          = "$base/api/rest/artist";
24 my $filtered_artist_list_url = "$base/api/rest/bound_artist";
25 my $producer_list_url        = "$base/api/rest/producer";
26 my $cd_list_url              = "$base/api/rest/cd";
27 my $track_list_url           = "$base/api/rest/track";
28
29 # test open request
30 {
31     my $req = GET(
32         $artist_list_url,
33         {
34
35         },
36         'Accept' => 'text/x-json'
37     );
38     $mech->request($req);
39     cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
40     my @expected_response = map {
41         { $_->get_columns }
42     } $schema->resultset('Artist')->all;
43     my $response = $json->decode( $mech->content );
44     is_deeply(
45         $response,
46         { list => \@expected_response, success => 'true' },
47         'correct message returned'
48     );
49 }
50
51 {
52     my $uri = URI->new($artist_list_url);
53     $uri->query_form( { 'search.artistid' => 1 } );
54     my $req = GET( $uri, 'Accept' => 'text/x-json' );
55     $mech->request($req);
56     cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
57
58     my @expected_response = map {
59         { $_->get_columns }
60     } $schema->resultset('Artist')->search( { artistid => 1 } )->all;
61     my $response = $json->decode( $mech->content );
62     is_deeply(
63         $response,
64         { list => \@expected_response, success => 'true' },
65         'correct data returned'
66     );
67 }
68
69 {
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' );
73     $mech->request($req);
74     cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
75
76     my @expected_response = map {
77         { $_->get_columns }
78         } $schema->resultset('Artist')
79         ->search( { name => { LIKE => '%waul%' } } )->all;
80     my $response = $json->decode( $mech->content );
81     is_deeply(
82         $response,
83         { list => \@expected_response, success => 'true' },
84         'correct data returned for complex query'
85     );
86 }
87
88 {
89     my $uri = URI->new($producer_list_url);
90     my $req = GET( $uri, 'Accept' => 'text/x-json' );
91     $mech->request($req);
92     cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
93
94     my @expected_response = map {
95         { $_->get_columns }
96         } $schema->resultset('Producer')->search( {}, { select => ['name'] } )
97         ->all;
98     my $response = $json->decode( $mech->content );
99     is_deeply(
100         $response,
101         { list => \@expected_response, success => 'true' },
102         'correct data returned for class with list_returns specified'
103     );
104 }
105
106 {
107     my $uri = URI->new($artist_list_url);
108     $uri->query_form( { 'search.cds.title' => 'Forkful of bees' } );
109     my $req = GET( $uri, 'Accept' => 'text/x-json' );
110     $mech->request($req);
111     cmp_ok( $mech->status, '==', 200, 'search related request okay' );
112
113     my @expected_response = map {
114         { $_->get_columns }
115         } $schema->resultset('Artist')
116         ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } )
117         ->all;
118     my $response = $json->decode( $mech->content );
119     is_deeply(
120         $response,
121         { list => \@expected_response, success => 'true' },
122         'correct data returned for class with select specified'
123     );
124 }
125
126 {
127     my $uri = URI->new($artist_list_url);
128     $uri->query_form(
129         {   'search.cds.title'     => 'Forkful of bees',
130             'list_returns.0.count' => '*',
131             'as.0'                 => 'count'
132         }
133     );
134     my $req = GET( $uri, 'Accept' => 'text/x-json' );
135     $mech->request($req);
136     cmp_ok( $mech->status, '==', 200, 'search related request okay' );
137
138     my @expected_response = map {
139         { $_->get_columns }
140         } $schema->resultset('Artist')
141         ->search( { 'cds.title' => 'Forkful of bees' },
142         { select => [ { count => '*' } ], as => ['count'], join => 'cds' } )
143         ->all;
144     my $response = $json->decode( $mech->content );
145     is_deeply(
146         $response,
147         { list => \@expected_response, success => 'true' },
148         'correct data returned for count'
149     );
150 }
151
152 {
153     my $uri = URI->new($filtered_artist_list_url);
154     $uri->query_form( { 'search.artistid' => '2' } );
155     my $req = GET( $uri, 'Accept' => 'text/x-json' );
156     $mech->request($req);
157     cmp_ok( $mech->status, '==', 200, 'search related request okay' );
158     my $response          = $json->decode( $mech->content );
159     my @expected_response = map {
160         { $_->get_columns }
161     } $schema->resultset('Artist')->search( { 'artistid' => '1' } )->all;
162     is_deeply(
163         $response,
164         { list => \@expected_response, success => 'true' },
165         'correct data returned for class with setup_list_method specified'
166     );
167 }
168
169 {
170     my $uri = URI->new($cd_list_url);
171     $uri->query_form(
172         {   'search.tracks.position' => '1',
173             'search.artist.name'     => 'Caterwauler McCrae'
174         }
175     );
176     my $req = GET( $uri, 'Accept' => 'text/x-json' );
177     $mech->request($req);
178     cmp_ok( $mech->status, '==', 200, 'search multiple params request okay' );
179     my $response          = $json->decode( $mech->content );
180     my @expected_response = map {
181         { $_->get_columns }
182         } $schema->resultset('CD')->search(
183         {   'artist.name'     => 'Caterwauler McCrae',
184             'tracks.position' => 1,
185         },
186         { join => [qw/ artist tracks /], }
187         )->all;
188     is_deeply(
189         $response,
190         { list => \@expected_response, success => 'true' },
191         'correct data returned for multiple search params'
192     );
193 }
194
195 # page specified in controller config (RT#56226)
196 {
197     my $uri = URI->new($track_list_url);
198     $uri->query_form();
199     my $req = GET( $uri, 'Accept' => 'text/x-json' );
200     $mech->request($req);
201     cmp_ok( $mech->status, '==', 200, 'get first page ok' );
202     my $response          = $json->decode( $mech->content );
203     my @expected_response = map {
204         { $_->get_columns }
205     } $schema->resultset('Track')->search( undef, { page => 1, } )->all;
206     is_deeply(
207         $response,
208         { list => \@expected_response, success => 'true', totalcount => 15 },
209         'correct data returned for static configured paging'
210     );
211 }
212
213 {
214     my $uri = URI->new($artist_list_url);
215     $uri->query_form( { 'search.cds.track.title' => 'Suicidal' } );
216     my $req = GET( $uri, 'Accept' => 'text/x-json' );
217     $mech->request($req);
218     cmp_ok( $mech->status, '==', 400,
219         'attempt with nonexisting relationship fails' );
220     my $response = $json->decode( $mech->content );
221     is_deeply(
222         $response->{messages},
223         ['track is neither a relationship nor a column'],
224         'correct error message returned'
225     );
226 }
227
228 {
229     my $uri = URI->new($artist_list_url);
230     $uri->query_form( { 'search.cds.tracks.foo' => 'Bar' } );
231     my $req = GET( $uri, 'Accept' => 'text/x-json' );
232     $mech->request($req);
233     cmp_ok( $mech->status, '==', 400,
234         'attempt with nonexisting column fails' );
235     my $response = $json->decode( $mech->content );
236     is_deeply(
237         $response->{messages},
238         ['a database error has occured.'],
239         'correct error message returned'
240     );
241 }
242
243 {
244     my $uri = URI->new($artist_list_url);
245     $uri->query_form( { 'search.cds.tracks.title.like' => 'Boring%' } );
246     my $req = GET( $uri, 'Accept' => 'text/x-json' );
247     $mech->request($req);
248     cmp_ok( $mech->status, '==', 200, 'attempt with sql function ok' );
249     my $response          = $json->decode( $mech->content );
250     my @expected_response = map {
251         { $_->get_columns }
252         } $schema->resultset('Artist')
253         ->search( { 'tracks.title' => { 'like' => 'Boring%' }, },
254         { join => { cds => 'tracks' }, } )->all;
255     is_deeply(
256         $response,
257         { list => \@expected_response, success => 'true' },
258         'correct data returned for search with sql function'
259     );
260 }
261
262 done_testing();