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