a7a6eb6838c7b83c579fb76edc8ea67880642c7f
[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
207         # track does set use_json_boolean
208         { list => \@expected_response, success => JSON::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
258         # artist doesn't set use_json_boolean
259         { list => \@expected_response, success => 'true' },
260         'correct data returned for search with sql function'
261     );
262 }
263
264 done_testing();