[Catalyst-commits] r10174 - in
Catalyst-Controller-DBIC-API/1.003/trunk:
lib/Catalyst/Controller/DBIC/API t/rpc t/var
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Sat May 16 12:55:42 GMT 2009
Author: lukes
Date: 2009-05-16 12:55:42 +0000 (Sat, 16 May 2009)
New Revision: 10174
Added:
Catalyst-Controller-DBIC-API/1.003/trunk/t/rpc/list_prefetch.t
Modified:
Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm
Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm
Catalyst-Controller-DBIC-API/1.003/trunk/t/var/DBIxClass.db
Log:
list_prefetch test added with JSON, CGI::Expand and normal params support
Modified: Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2009-05-16 11:51:55 UTC (rev 10173)
+++ Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2009-05-16 12:55:42 UTC (rev 10174)
@@ -42,6 +42,22 @@
$c->stash->{$self->rs_stash_key} = $c->model($self->class);
}
+# from Catalyst::Action::Serialize
+sub deserialize :ActionClass('Deserialize') {
+ my ($self, $c) = @_;
+
+ my $req_params = $self->expand_hash($c->req->params);
+ foreach my $param (qw/search list_count list_ordered_by list_grouped_by list_prefetch/) {
+ # these params can also be composed of JSON
+ eval {
+ my $deserialized = JSON::Any->from_json($req_params->{$param});
+ $req_params->{$param} = $deserialized;
+ };
+ }
+# use Data::Dumper; warn Dumper($req_params);
+ $c->stash->{_dbic_api}->{req_params} = $req_params;
+}
+
sub list :Private {
my ($self, $c) = @_;
@@ -58,9 +74,10 @@
my ($self, $c) = @_;
my $args = {};
- # do this before eventually calling expand_hash
- my $prefetch = (exists $c->req->params->{list_prefetch} ? JSON::Any->from_json($c->req->params->{list_prefetch}) : undef) || ($self->list_prefetch ? $self->list_prefetch : undef);
+ my $req_params = $c->stash->{_dbic_api}->{req_params};
+ my $prefetch = $req_params->{list_prefetch} || $self->list_prefetch || undef;
if ($prefetch) {
+ $prefetch = [$prefetch] unless ref $prefetch;
# validate the prefetch param against list_prefetch_allows
foreach my $prefetch_allows (@{$self->list_prefetch_allows}) {
if (eq_deeply($prefetch, $prefetch_allows)) {
@@ -73,8 +90,7 @@
$self->push_error($c, { message => "prefetch validation failed" });
}
}
-
- 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 {
@@ -124,8 +140,8 @@
$args->{select} = [map { ($_ =~ m/\./) ? $_ : "me.$_" } (ref $args->{select}) ? @{$args->{select}} : $args->{select}];
}
$args->{join} = $join;
- if ( my $a = $self->setup_dbic_args_method ) {
- my $format_action = $self->action_for($a);
+ if ( my $action = $self->setup_dbic_args_method ) {
+ my $format_action = $self->action_for($action);
if ( defined $format_action ) {
($params, $args) = @{$c->forward("/$format_action", [ $params, $args ])};
} else {
Modified: Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm 2009-05-16 11:51:55 UTC (rev 10173)
+++ Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm 2009-05-16 12:55:42 UTC (rev 10174)
@@ -82,12 +82,6 @@
$self->NEXT::begin($c);
}
-# from Catalyst::Action::Serialize
-sub deserialize :ActionClass('Deserialize') {
- my ($self, $c) = @_;
-
-}
-
sub object :Chained('setup') :Args(1) :PathPart('') :ActionClass('REST') {
my ($self, $c, $id) = @_;
Modified: Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm 2009-05-16 11:51:55 UTC (rev 10173)
+++ Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm 2009-05-16 12:55:42 UTC (rev 10174)
@@ -94,12 +94,6 @@
$self->NEXT::begin($c);
}
-# from Catalyst::Action::Serialize
-sub deserialize :ActionClass('Deserialize') {
- my ($self, $c) = @_;
-
-}
-
sub object :Chained('setup') :CaptureArgs(1) :PathPart('id') {
my ($self, $c, $id) = @_;
Added: Catalyst-Controller-DBIC-API/1.003/trunk/t/rpc/list_prefetch.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.003/trunk/t/rpc/list_prefetch.t (rev 0)
+++ Catalyst-Controller-DBIC-API/1.003/trunk/t/rpc/list_prefetch.t 2009-05-16 12:55:42 UTC (rev 10174)
@@ -0,0 +1,64 @@
+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 tests => 7;
+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 $cd_list_url = "$base/api/rpc/cd/list";
+
+foreach my $req_params ({ 'list_prefetch' => '["cds"]' }, { 'list_prefetch' => 'cds' }) {
+ my $uri = URI->new( $artist_list_url );
+ $uri->query_form($req_params);
+ my $req = GET( $uri, 'Accept' => 'text/x-json' );
+ $mech->request($req);
+ cmp_ok( $mech->status, '==', 200, 'search with simple prefetch request okay' );
+ my $rs = $schema->resultset('Artist')->search(undef, { prefetch => ['cds'] });
+ $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
+ my @rows = $rs->all;
+ my $expected_response = { list => \@rows, success => 'true' };
+ my $response = JSON::Syck::Load( $mech->content);
+ is_deeply( $expected_response, $response, 'correct data returned for search with simple prefetch specified as param' );
+}
+
+foreach my $req_params ({ 'list_prefetch' => '{"cds":"tracks"}' }, { 'list_prefetch.cds' => 'tracks' }) {
+ my $uri = URI->new( $artist_list_url );
+ $uri->query_form($req_params);
+ my $req = GET( $uri, 'Accept' => 'text/x-json' );
+ $mech->request($req);
+ cmp_ok( $mech->status, '==', 200, 'search with multi-level prefetch request okay' );
+ my $rs = $schema->resultset('Artist')->search(undef, { prefetch => {'cds' => 'tracks'} });
+ $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
+ my @rows = $rs->all;
+ my $expected_response = { list => \@rows, success => 'true' };
+ my $response = JSON::Syck::Load( $mech->content);
+ #use Data::Dumper; warn Dumper($response, $expected_response);
+ is_deeply( $expected_response, $response, 'correct data returned for search with multi-level prefetch specified as param' );
+}
+
+foreach my $req_params ({ 'list_prefetch' => '["artist"]' }, { 'list_prefetch' => 'artist' }) {
+ my $uri = URI->new( $cd_list_url );
+ $uri->query_form($req_params);
+ my $req = GET( $uri, 'Accept' => 'text/x-json' );
+ $mech->request($req);
+ cmp_ok( $mech->status, '==', 400, 'prefetch of artist not okay' );
+
+ my $expected_response = map { { $_->get_columns } } $schema->resultset('CD')->all;
+ my $response = JSON::Syck::Load( $mech->content);
+ is_deeply({ success => 'false',messages => ["prefetch validation failed"]}, $response, 'correct message returned' );
+}
+
Modified: Catalyst-Controller-DBIC-API/1.003/trunk/t/var/DBIxClass.db
===================================================================
(Binary files differ)
More information about the Catalyst-commits
mailing list