6 my $base = 'http://localhost';
12 use Test::WWW::Mechanize::Catalyst 'RestTest';
13 use HTTP::Request::Common;
17 my $json = JSON->new->utf8;
19 my $mech = Test::WWW::Mechanize::Catalyst->new;
20 ok( my $schema = DBICTest->init_schema(), 'got schema' );
22 my $artist_list_url = "$base/api/rest/artist";
23 my $filtered_artist_list_url = "$base/api/rest/bound_artist";
24 my $producer_list_url = "$base/api/rest/producer";
25 my $cd_list_url = "$base/api/rest/cd";
26 my $track_list_url = "$base/api/rest/track";
35 'Accept' => 'text/x-json'
38 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
39 my @expected_response = map {
41 } $schema->resultset('Artist')->all;
42 my $response = $json->decode( $mech->content );
45 { list => \@expected_response, success => 'true' },
46 'correct message returned'
51 my $uri = URI->new($artist_list_url);
52 $uri->query_form( { 'search.artistid' => 1 } );
53 my $req = GET( $uri, 'Accept' => 'text/x-json' );
55 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
57 my @expected_response = map {
59 } $schema->resultset('Artist')->search( { artistid => 1 } )->all;
60 my $response = $json->decode( $mech->content );
63 { list => \@expected_response, success => 'true' },
64 'correct data returned'
69 my $uri = URI->new($artist_list_url);
70 $uri->query_form( { 'search.name.LIKE' => '%waul%' } );
71 my $req = GET( $uri, 'Accept' => 'text/x-json' );
73 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
75 my @expected_response = map {
77 } $schema->resultset('Artist')
78 ->search( { name => { LIKE => '%waul%' } } )->all;
79 my $response = $json->decode( $mech->content );
82 { list => \@expected_response, success => 'true' },
83 'correct data returned for complex query'
88 my $uri = URI->new($producer_list_url);
89 my $req = GET( $uri, 'Accept' => 'text/x-json' );
91 cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
93 my @expected_response = map {
95 } $schema->resultset('Producer')->search( {}, { select => ['name'] } )
97 my $response = $json->decode( $mech->content );
100 { list => \@expected_response, success => 'true' },
101 'correct data returned for class with list_returns specified'
106 my $uri = URI->new($artist_list_url);
107 $uri->query_form( { 'search.cds.title' => 'Forkful of bees' } );
108 my $req = GET( $uri, 'Accept' => 'text/x-json' );
109 $mech->request($req);
110 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
112 my @expected_response = map {
114 } $schema->resultset('Artist')
115 ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } )
117 my $response = $json->decode( $mech->content );
120 { list => \@expected_response, success => 'true' },
121 'correct data returned for class with select specified'
126 my $uri = URI->new($artist_list_url);
128 { 'search.cds.title' => 'Forkful of bees',
129 'list_returns.0.count' => '*',
133 my $req = GET( $uri, 'Accept' => 'text/x-json' );
134 $mech->request($req);
135 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
137 my @expected_response = map {
139 } $schema->resultset('Artist')
140 ->search( { 'cds.title' => 'Forkful of bees' },
141 { select => [ { count => '*' } ], as => ['count'], join => 'cds' } )
143 my $response = $json->decode( $mech->content );
146 { list => \@expected_response, success => 'true' },
147 'correct data returned for count'
152 my $uri = URI->new($filtered_artist_list_url);
153 $uri->query_form( { 'search.artistid' => '2' } );
154 my $req = GET( $uri, 'Accept' => 'text/x-json' );
155 $mech->request($req);
156 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
157 my $response = $json->decode( $mech->content );
158 my @expected_response = map {
160 } $schema->resultset('Artist')->search( { 'artistid' => '1' } )->all;
163 { list => \@expected_response, success => 'true' },
164 'correct data returned for class with setup_list_method specified'
169 my $uri = URI->new($cd_list_url);
171 { 'search.tracks.position' => '1',
172 'search.artist.name' => 'Caterwauler McCrae'
175 my $req = GET( $uri, 'Accept' => 'text/x-json' );
176 $mech->request($req);
177 cmp_ok( $mech->status, '==', 200, 'search multiple params request okay' );
178 my $response = $json->decode( $mech->content );
179 my @expected_response = map {
181 } $schema->resultset('CD')->search(
182 { 'artist.name' => 'Caterwauler McCrae',
183 'tracks.position' => 1,
185 { join => [qw/ artist tracks /], }
189 { list => \@expected_response, success => 'true' },
190 'correct data returned for multiple search params'
194 # page specified in controller config (RT#56226)
196 my $uri = URI->new($track_list_url);
198 my $req = GET( $uri, 'Accept' => 'text/x-json' );
199 $mech->request($req);
200 cmp_ok( $mech->status, '==', 200, 'get first page ok' );
201 my $response = $json->decode( $mech->content );
202 my @expected_response = map {
204 } $schema->resultset('Track')->search( undef, { page => 1, } )->all;
208 # track does set use_json_boolean
209 { list => \@expected_response, success => JSON::true, totalcount => 15 },
210 'correct data returned for static configured paging'
220 title => [qw(Yowlin Howlin)],
226 title => [qw(Yowlin Howlin)],
233 title => [qw(Yowlin)],
234 title => [qw(Howlin)],
241 { title => [qw(Yowlin)] },
242 { title => [qw(Howlin)] },
314 for my $case ( @variants ) {
315 is $schema->resultset('Track')->search($case->{search})->count, 2, 'check -and|-or search param correctness';
317 my $uri = URI->new($track_list_url);
318 $uri->query_form( map { $_ => encode_json($case->{$_}) } keys %$case );
319 my $req = GET( $uri, 'Accept' => 'text/x-json' );
320 $mech->request($req);
321 cmp_ok( $mech->status, '==', 200, 'attempt with -or search okay' );
322 my $response = $json->decode( $mech->content );
323 my @expected_response = map {
325 } $schema->resultset('Track')->search($case->{search})->all;
328 # track does set use_json_boolean
329 { list => \@expected_response, success => JSON::true, totalcount => 2 },
330 'correct data returned for -and|-or search param'
332 or diag p($case) . p($response);
337 my $uri = URI->new($artist_list_url);
338 $uri->query_form( { 'search.cds.track.title' => 'Suicidal' } );
339 my $req = GET( $uri, 'Accept' => 'text/x-json' );
340 $mech->request($req);
341 cmp_ok( $mech->status, '==', 400,
342 'attempt with nonexisting relationship fails' );
343 my $response = $json->decode( $mech->content );
345 $response->{messages}->[0],
346 qr/unsupported value 'HASH\([^\)]+\)' for column 'track'/,
347 'correct error message returned'
352 my $uri = URI->new($artist_list_url);
353 $uri->query_form( { 'search.cds.tracks.foo' => 'Bar' } );
354 my $req = GET( $uri, 'Accept' => 'text/x-json' );
355 $mech->request($req);
356 cmp_ok( $mech->status, '==', 400,
357 'attempt with nonexisting column fails' );
358 my $response = $json->decode( $mech->content );
360 $response->{messages},
361 ['a database error has occured.'],
362 'correct error message returned'
367 my $uri = URI->new($artist_list_url);
368 $uri->query_form( { 'search.cds.tracks.title.like' => 'Boring%' } );
369 my $req = GET( $uri, 'Accept' => 'text/x-json' );
370 $mech->request($req);
371 cmp_ok( $mech->status, '==', 200, 'attempt with sql function ok' );
372 my $response = $json->decode( $mech->content );
373 my @expected_response = map {
375 } $schema->resultset('Artist')
376 ->search( { 'tracks.title' => { 'like' => 'Boring%' }, },
377 { join => { cds => 'tracks' }, } )->all;
381 # artist doesn't set use_json_boolean
382 { list => \@expected_response, success => 'true' },
383 'correct data returned for search with sql function'