X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Controller-DBIC-API.git;a=blobdiff_plain;f=t%2Frest%2Flist.t;h=b63cc9894f0375c4d672bd79437de8284de39e2a;hp=acb8872fa5bc09bb3787b240bd98b71f97663b43;hb=4cb8623abade7b9f100b3892df93ddcfb1168e01;hpb=0b0bf9111127c9fdad1e456169ec4cdd15b160f9 diff --git a/t/rest/list.t b/t/rest/list.t index acb8872..b63cc98 100644 --- a/t/rest/list.t +++ b/t/rest/list.t @@ -1,5 +1,3 @@ -use 5.6.0; - use strict; use warnings; @@ -14,6 +12,7 @@ use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; use JSON; +use Data::Printer; my $json = JSON->new->utf8; @@ -205,11 +204,135 @@ my $track_list_url = "$base/api/rest/track"; } $schema->resultset('Track')->search( undef, { page => 1, } )->all; is_deeply( $response, - { list => \@expected_response, success => 'true', totalcount => 15 }, + + # track does set use_json_boolean + { list => \@expected_response, success => JSON::true, totalcount => 15 }, 'correct data returned for static configured paging' ); } +# -and|-or condition +{ + my @variants = ( + # -or + { + search => { + title => [qw(Yowlin Howlin)], + }, + }, + { + search => { + -or => [ + title => [qw(Yowlin Howlin)], + ], + }, + }, + { + search => { + -or => [ + title => [qw(Yowlin)], + title => [qw(Howlin)], + ], + }, + }, + { + search => { + -or => [ + { title => [qw(Yowlin)] }, + { title => [qw(Howlin)] }, + ], + }, + }, + # -and + { + search => { + cd => 2, + position => [1, 2], + }, + }, + { + search => { + -and => [ + cd => 2, + position => [1, 2], + ], + }, + }, + # -and & -or + { + search => { + -or => [ + -and => [ + cd => 2, + position => [0, 1], + ], + -and => [ + cd => 2, + position => [0, 2], + ], + ], + }, + }, + { + search => { + -or => [ + { + -and => [ + cd => 2, + position => [0, 1], + ], + }, + { + -and => [ + cd => 2, + position => [0, 2], + ], + }, + ], + }, + }, + { + search => { + -or => [ + { + -and => [ + cd => 2, + position => [0, 1], + ], + }, + { + -and => [ + cd => 2, + position => [0, 2], + ], + }, + ], + }, + }, + ); + + for my $case ( @variants ) { + is $schema->resultset('Track')->search($case->{search})->count, 2, 'check -and|-or search param correctness'; + + my $uri = URI->new($track_list_url); + $uri->query_form( map { $_ => encode_json($case->{$_}) } keys %$case ); + my $req = GET( $uri, 'Accept' => 'text/x-json' ); + $mech->request($req); + cmp_ok( $mech->status, '==', 200, 'attempt with -or search okay' ); + my $response = $json->decode( $mech->content ); + my @expected_response = map { + { $_->get_columns } + } $schema->resultset('Track')->search($case->{search})->all; + is_deeply( + $response, + # track does set use_json_boolean + { list => \@expected_response, success => JSON::true, totalcount => 2 }, + 'correct data returned for -and|-or search param' + ) + or diag p($case) . p($response); + } +} + { my $uri = URI->new($artist_list_url); $uri->query_form( { 'search.cds.track.title' => 'Suicidal' } ); @@ -218,9 +341,9 @@ my $track_list_url = "$base/api/rest/track"; cmp_ok( $mech->status, '==', 400, 'attempt with nonexisting relationship fails' ); my $response = $json->decode( $mech->content ); - is_deeply( - $response->{messages}, - ['track is neither a relationship nor a column'], + like( + $response->{messages}->[0], + qr/unsupported value 'HASH\([^\)]+\)' for column 'track'/, 'correct error message returned' ); } @@ -254,6 +377,8 @@ my $track_list_url = "$base/api/rest/track"; { join => { cds => 'tracks' }, } )->all; is_deeply( $response, + + # artist doesn't set use_json_boolean { list => \@expected_response, success => 'true' }, 'correct data returned for search with sql function' );