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/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";
34 'Accept' => 'text/x-json'
37 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($producer_list_url);
88 my $req = GET( $uri, 'Accept' => 'text/x-json' );
90 cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
92 my @expected_response = map {
94 } $schema->resultset('Producer')->search( {}, { select => ['name'] } )
96 my $response = $json->decode( $mech->content );
99 { list => \@expected_response, success => 'true' },
100 'correct data returned for class with list_returns specified'
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' );
111 my @expected_response = map {
113 } $schema->resultset('Artist')
114 ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } )
116 my $response = $json->decode( $mech->content );
119 { list => \@expected_response, success => 'true' },
120 'correct data returned for class with select specified'
125 my $uri = URI->new($artist_list_url);
127 { 'search.cds.title' => 'Forkful of bees',
128 'list_returns.0.count' => '*',
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' },
140 { select => [ { count => '*' } ], as => ['count'], join => 'cds' } )
142 my $response = $json->decode( $mech->content );
145 { list => \@expected_response, success => 'true' },
146 'correct data returned for count'
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 {
159 } $schema->resultset('Artist')->search( { 'artistid' => '1' } )->all;
162 { list => \@expected_response, success => 'true' },
163 'correct data returned for class with setup_list_method specified'
168 my $uri = URI->new($cd_list_url);
170 { 'search.tracks.position' => '1',
171 'search.artist.name' => 'Caterwauler McCrae'
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 {
180 } $schema->resultset('CD')->search(
181 { 'artist.name' => 'Caterwauler McCrae',
182 'tracks.position' => 1,
184 { join => [qw/ artist tracks /], }
188 { list => \@expected_response, success => 'true' },
189 'correct data returned for multiple search params'
193 # page specified in controller config (RT#56226)
195 my $uri = URI->new($track_list_url);
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 {
203 } $schema->resultset('Track')->search( undef, { page => 1, } )->all;
206 { list => \@expected_response, success => 'true', totalcount => 15 },
207 'correct data returned for static configured paging'
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 );
220 $response->{messages},
221 ['track is neither a relationship nor a column'],
222 'correct error message returned'
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 );
235 $response->{messages},
236 ['a database error has occured.'],
237 'correct error message returned'
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 {
250 } $schema->resultset('Artist')
251 ->search( { 'tracks.title' => { 'like' => 'Boring%' }, },
252 { join => { cds => 'tracks' }, } )->all;
255 { list => \@expected_response, success => 'true' },
256 'correct data returned for search with sql function'