[Catalyst-commits] r7930 - in
Catalyst-Controller-DBIC-API/1.000/trunk:
lib/Catalyst/Controller/DBIC/API
t/lib/RestTest/Controller/API/REST t/rest t/var
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Sat Jun 14 10:17:52 BST 2008
Author: lukes
Date: 2008-06-14 10:17:51 +0100 (Sat, 14 Jun 2008)
New Revision: 7930
Added:
Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t
Modified:
Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm
Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/REST/Producer.pm
Catalyst-Controller-DBIC-API/1.000/trunk/t/var/DBIxClass.db
Log:
added rest list test
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2008-06-14 08:25:24 UTC (rev 7929)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2008-06-14 09:17:51 UTC (rev 7930)
@@ -52,8 +52,29 @@
$c->stash->{class_rs} = $c->model($self->class);
}
+=head2 list
+=cut
+sub list :Private {
+ my ($self, $c) = @_;
+
+ my $req_params = $self->expand_hash($c->req->params);
+
+ my $source = $c->stash->{class_rs}->result_source;
+ my @columns = (scalar(@{$self->list_returns})) ? @{$self->list_returns} : $source->columns;
+
+ my %search_params;
+ if ($req_params->{search}) {
+ %search_params = map { $_ => $req_params->{search}->{$_} } grep { exists $req_params->{search}->{$_} } $source->columns;
+ }
+
+ $c->stash->{class_rs} = $c->stash->{class_rs}->search(\%search_params, { select => \@columns });
+
+ my @ret = map { { $_->get_columns } } $c->stash->{class_rs}->all;
+ $c->stash->{response}->{list} = \@ret;
+}
+
=head2 create
Matches cols specified in create_requires and create_allows to $c->req->params and $c->stash->{params}
@@ -195,45 +216,7 @@
}
}
-sub split_name {
- my ( $class, $name ) = @_;
- if ( $name =~ /^ \w+ \[/x ) {
- return grep { defined } ( $name =~ /
- ^ (\w+) # root param
- | \[ (\w+) \] # nested
- /gx );
- } else {
- return $class->SUPER::split_name( $name );
- }
-}
-
-=head2 list
-
-=cut
-
-sub list {
- my ($self, $c) = @_;
-
- my $req_params = $self->expand_hash($c->req->params);
-
- my $source = $c->stash->{class_rs}->result_source;
- my @columns = (scalar(@{$self->list_returns})) ? @{$self->list_returns} : $source->columns;
-
- my %search_params;
- if ($req_params->{search}) {
- %search_params = map { $_ => $req_params->{search}->{$_} } grep { exists $req_params->{search}->{$_} } $source->columns;
- }
-
- $c->stash->{class_rs} = $c->stash->{class_rs}->search(\%search_params, { select => \@columns });
-
- my @ret = map { { $_->get_columns } } $c->stash->{class_rs}->all;
- $c->stash->{response}->{list} = \@ret;
-}
-
-
-
-
# =head2 add_to_rel
# finds a related row and then creates the many_to_many linking row using ->add_to_$rel
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm 2008-06-14 08:25:24 UTC (rev 7929)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm 2008-06-14 09:17:51 UTC (rev 7930)
@@ -52,7 +52,6 @@
my ($self, $c) = @_;
$c->forward('deserialize');
- use Data::Dumper; $c->log->debug(Dumper($c->req->data));
$c->req->params($c->req->data);
$self->NEXT::begin($c);
}
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/REST/Producer.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/REST/Producer.pm 2008-06-14 08:25:24 UTC (rev 7929)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/REST/Producer.pm 2008-06-14 09:17:51 UTC (rev 7930)
@@ -9,7 +9,8 @@
( action => { setup => { PathPart => 'producer', Chained => '/api/rest/rest_base' } },
class => 'RestTestDB::Producer',
create_requires => ['name'],
- update_allows => ['name']
+ update_allows => ['name'],
+ list_returns => ['name']
);
1;
Added: Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t (rev 0)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t 2008-06-14 09:17:51 UTC (rev 7930)
@@ -0,0 +1,82 @@
+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 => 11;
+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/rest/artist";
+my $producer_list_url = "$base/api/rest/producer";
+
+# test open request
+{
+ my $req = GET( $artist_list_url, {
+
+ }, 'Accept' => 'text/x-json' );
+ $mech->request($req);
+ cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
+
+ my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->all;
+ my $response = JSON::Syck::Load( $mech->content);
+ is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct message returned' );
+}
+
+{
+ my $uri = URI->new( $artist_list_url );
+ $uri->query_form({ 'search.artistid' => 1 });
+ 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({ artistid => 1 })->all;
+ my $response = JSON::Syck::Load( $mech->content);
+ is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned' );
+}
+
+{
+ my $uri = URI->new( $artist_list_url );
+ $uri->query_form({ 'search.artistid' => 1 });
+ 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({ artistid => 1 })->all;
+ my $response = JSON::Syck::Load( $mech->content);
+ is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned' );
+}
+
+{
+ 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);
+ cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
+
+ my @expected_response = map { { $_->get_columns } } $schema->resultset('Producer')->search({}, { select => ['name'] })->all;
+ my $response = JSON::Syck::Load( $mech->content);
+ is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for class with list_returns specified' );
+}
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/var/DBIxClass.db
===================================================================
(Binary files differ)
More information about the Catalyst-commits
mailing list