X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Controller-DBIC-API.git;a=blobdiff_plain;f=t%2Frpc%2Flist_json_search.t;h=dab6b7e5a424243980202dca38c0c83578908e5b;hp=089f5b7d92a701dd757736f886a8d7f1460e5b57;hb=HEAD;hpb=d273984026646e5b57c052deef3fcb9121122060 diff --git a/t/rpc/list_json_search.t b/t/rpc/list_json_search.t index 089f5b7..dab6b7e 100644 --- a/t/rpc/list_json_search.t +++ b/t/rpc/list_json_search.t @@ -1,5 +1,3 @@ -use 5.6.0; - use strict; use warnings; @@ -13,58 +11,84 @@ use URI; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON::Any; +use JSON::MaybeXS; + +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; -ok(my $schema = DBICTest->init_schema(), 'got schema'); +ok( my $schema = DBICTest->init_schema(), 'got schema' ); my $artist_list_url = "$base/api/rpc/artist/list"; -my $base_rs = $schema->resultset('Track')->search({}, { select => [qw/me.title me.position/], order_by => 'position' }); +my $base_rs = + $schema->resultset('Track') + ->search( {}, + { select => [qw/me.title me.position/], order_by => 'position' } ); { - my $uri = URI->new( $artist_list_url ); - $uri->query_form({ 'search' => '{"gibberish}' }); - my $req = GET( $uri, 'Accept' => 'text/x-json' ); - $mech->request($req); - cmp_ok( $mech->status, '==', 400, 'attempt with gibberish json not okay' ); - my $response = JSON::Any->Load( $mech->content); - is($response->{success}, 'false', 'correct data returned for gibberish in search' ); - like($response->{messages}->[0], qr/Attribute \(search\) does not pass the type constraint because/, 'correct data returned for gibberish in search' ); + my $uri = URI->new($artist_list_url); + $uri->query_form( { 'search' => '{"gibberish}' } ); + my $req = GET( $uri, 'Accept' => 'text/x-json' ); + $mech->request($req); + cmp_ok( $mech->status, '==', 400, + 'attempt with gibberish json not okay' ); + my $response = $json->decode( $mech->content ); + is( $response->{success}, 'false', + 'correct data returned for gibberish in search' ); + like( + $response->{messages}->[0], + qr/Attribute \(search\) does not pass the type constraint because/, + 'correct data returned for gibberish in search' + ); } { - my $uri = URI->new( $artist_list_url ); - $uri->query_form({ 'search' => '{"name":{"LIKE":"%waul%"}}' }); - my $req = GET( $uri, 'Accept' => 'text/x-json' ); - $mech->request($req); - cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' ); - - my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }})->all; - my $response = JSON::Any->Load( $mech->content); - is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for complex query' ); + my $uri = URI->new($artist_list_url); + $uri->query_form( { 'search' => '{"name":{"LIKE":"%waul%"}}' } ); + my $req = GET( $uri, 'Accept' => 'text/x-json' ); + $mech->request($req); + cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' ); + + my @expected_response = map { + { $_->get_columns } + } $schema->resultset('Artist') + ->search( { name => { LIKE => '%waul%' } } )->all; + my $response = $json->decode( $mech->content ); + is_deeply( { list => \@expected_response, success => 'true' }, + $response, 'correct data returned for complex query' ); } { - my $uri = URI->new( $artist_list_url ); - $uri->query_form({ 'search' => '{ "cds": { "title": "Spoonful of bees" }}' }); - my $req = GET( $uri, 'Accept' => 'text/x-json' ); - $mech->request($req); - cmp_ok( $mech->status, '==', 200, 'attempt with related search okay' ); - my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Spoonful of bees' }, { join => 'cds' })->all; - my $response = JSON::Any->Load( $mech->content); - is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for complex query' ); + my $uri = URI->new($artist_list_url); + $uri->query_form( + { 'search' => '{ "cds": { "title": "Spoonful of bees" }}' } ); + my $req = GET( $uri, 'Accept' => 'text/x-json' ); + $mech->request($req); + cmp_ok( $mech->status, '==', 200, 'attempt with related search okay' ); + my @expected_response = map { + { $_->get_columns } + } $schema->resultset('Artist') + ->search( { 'cds.title' => 'Spoonful of bees' }, { join => 'cds' } ) + ->all; + my $response = $json->decode( $mech->content ); + is_deeply( { list => \@expected_response, success => 'true' }, + $response, 'correct data returned for complex query' ); } { - my $uri = URI->new( $artist_list_url ); - $uri->query_form({ 'search.name' => '{"LIKE":"%waul%"}' }); - my $req = GET( $uri, 'Accept' => 'text/x-json' ); - $mech->request($req); - cmp_ok( $mech->status, '==', 200, 'attempt with mixed CGI::Expand + JSON search okay' ); - - my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }})->all; - my $response = JSON::Any->Load( $mech->content); - is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for complex query' ); + my $uri = URI->new($artist_list_url); + $uri->query_form( { 'search.name' => '{"LIKE":"%waul%"}' } ); + my $req = GET( $uri, 'Accept' => 'text/x-json' ); + $mech->request($req); + cmp_ok( $mech->status, '==', 200, + 'attempt with mixed CGI::Expand + JSON search okay' ); + + my @expected_response = map { + { $_->get_columns } + } $schema->resultset('Artist') + ->search( { name => { LIKE => '%waul%' } } )->all; + my $response = $json->decode( $mech->content ); + is_deeply( { list => \@expected_response, success => 'true' }, + $response, 'correct data returned for complex query' ); } done_testing();