[Catalyst-commits] r9525 - in
Catalyst-Controller-DBIC-API/1.001/trunk:
lib/Catalyst/Controller/DBIC lib/Catalyst/Controller/DBIC/API t/rpc
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Wed Mar 18 19:42:41 GMT 2009
Author: lukes
Date: 2009-03-18 19:42:41 +0000 (Wed, 18 Mar 2009)
New Revision: 9525
Added:
Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list_json_search.t
Modified:
Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm
Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list.t
Log:
added better JSON test
Modified: Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2009-03-18 19:05:30 UTC (rev 9524)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2009-03-18 19:42:41 UTC (rev 9525)
@@ -36,7 +36,9 @@
sub list :Private {
my ($self, $c) = @_;
- my ($params, $args) = @{$c->forward('generate_dbic_search_args')};
+ my $ret = $c->forward('generate_dbic_search_args');
+ return unless ($ret && ref $ret);
+ my ($params, $args) = @{$ret};
return if $self->get_errors($c);
$c->stash->{$self->rs_stash_key} = $c->stash->{$self->rs_stash_key}->search($params, $args);
@@ -47,6 +49,18 @@
my ($self, $c) = @_;
my $req_params = (grep { ref $_ } values %{$c->req->params}) ? $c->req->params : $self->expand_hash($c->req->params);
+ # if expand_hash didn't do anything, try json
+ unless (exists $req_params->{search} && ref $req_params->{search} eq 'HASH') {
+ eval {
+ $req_params->{search} = exists $c->req->params->{search} ? JSON::Any->from_json($c->req->params->{search}) : undef;
+ };
+ if ($@) {
+ # json didn't work either. looks like it's screwed.
+ $self->push_error($c, { message => "can not parse search arg" });
+ return;
+ }
+ }
+
if ( my $a = $self->setup_list_method ) {
my $setup_action = $self->action_for($a);
if ( defined $setup_action ) {
@@ -59,15 +73,9 @@
my ($params, $join);
my $args = {};
- if (exists $req_params->{search}
- && ref $req_params->{search} eq 'HASH'
- ) {
- ($params, $join) = $self->_format_search($c, { params => $req_params->{search}, source => $source }) if ($req_params->{search});
- }
- # assume json if search wasn't expanded by expand_hash
- else {
- $params = exists $c->req->params->{search} ? JSON::Any->from_json($c->req->params->{search}) : undef;
- }
+
+ ($params, $join) = $self->_format_search($c, { params => $req_params->{search}, source => $source }) if ($req_params->{search});
+
$args->{group_by} = $req_params->{list_grouped_by} || ((scalar(@{$self->list_grouped_by})) ? $self->list_grouped_by : undef);
$args->{order_by} = $req_params->{list_ordered_by} || ((scalar(@{$self->list_ordered_by})) ? $self->list_ordered_by : undef);
$args->{rows} = $req_params->{list_count} || $self->list_count;
Modified: Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm 2009-03-18 19:05:30 UTC (rev 9524)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm 2009-03-18 19:42:41 UTC (rev 9525)
@@ -231,7 +231,7 @@
?search.name=fred&search.cd.artist=luke
OR
- ?search='{"name":"fred","cd.artist":"luke"}'
+ ?search='{"name":"fred","cd": {"artist":"luke"}}'
Would result in this search (where 'name' is a column of the schema class, 'cd' is a relation of the schema class and 'artist' is a column of the related class):
Modified: Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list.t 2009-03-18 19:05:30 UTC (rev 9524)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list.t 2009-03-18 19:42:41 UTC (rev 9525)
@@ -60,19 +60,8 @@
is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for complex query' );
}
+exit;
{
- 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::Syck::Load( $mech->content);
- is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for complex query' );
-}
-
-{
my $uri = URI->new( $producer_list_url );
my $req = GET( $uri, 'Accept' => 'text/x-json' );
$mech->request($req);
Added: Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list_json_search.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list_json_search.t (rev 0)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list_json_search.t 2009-03-18 19:42:41 UTC (rev 9525)
@@ -0,0 +1,57 @@
+use 5.6.0;
+
+use strict;
+use warnings;
+
+use lib 't/lib';
+
+my $base = 'http://localhost';
+
+use RestTest;
+use DBICTest;
+use URI;
+use Test::More qw(no_plan);
+use Test::WWW::Mechanize::Catalyst 'RestTest';
+use HTTP::Request::Common;
+use JSON::Syck;
+
+my $mech = Test::WWW::Mechanize::Catalyst->new;
+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 $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::Syck::Load( $mech->content);
+ is_deeply( { messages => ['can not parse search arg'], success => 'false' }, $response, '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::Syck::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::Syck::Load( $mech->content);
+ is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for complex query' );
+}
More information about the Catalyst-commits
mailing list