From: Alex Howarth Date: Wed, 12 Jun 2013 20:31:21 +0000 (-0400) Subject: Fix generate_rs to set model to contents of stash->{class} if present and add tests. X-Git-Tag: 2.005001~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Controller-DBIC-API.git;a=commitdiff_plain;h=07b00970f381c386ff40e4f13d5ab3c8bcf72c43 Fix generate_rs to set model to contents of stash->{class} if present and add tests. Signed-off-by: Alex Howarth --- diff --git a/lib/Catalyst/Controller/DBIC/API.pm b/lib/Catalyst/Controller/DBIC/API.pm index 7db1d8c..b595fb4 100644 --- a/lib/Catalyst/Controller/DBIC/API.pm +++ b/lib/Catalyst/Controller/DBIC/API.pm @@ -202,7 +202,7 @@ sub generate_rs { my ($self, $c) = @_; - return $c->model($self->class); + return $c->model($self->class || $c->stash->{class}); } =method_protected inflate_request diff --git a/t/lib/RestTest/Controller/API/REST/StashedClass.pm b/t/lib/RestTest/Controller/API/REST/StashedClass.pm new file mode 100644 index 0000000..804e7ae --- /dev/null +++ b/t/lib/RestTest/Controller/API/REST/StashedClass.pm @@ -0,0 +1,13 @@ +package RestTest::Controller::API::REST::StashedClass; +use Moose; +BEGIN { extends 'Catalyst::Controller::DBIC::API::REST' } + +use namespace::autoclean; + +sub setup :Chained('/api/rest/rest_base') :CaptureArgs(1) :PathPart('stashedclass') { + my ($self, $c, $class) = @_; + $c->stash->{class} = $class; + $self->next::method($c); +} + +1; diff --git a/t/rest/stashedclass.t b/t/rest/stashedclass.t new file mode 100644 index 0000000..84abd26 --- /dev/null +++ b/t/rest/stashedclass.t @@ -0,0 +1,56 @@ +use strict; +use warnings; + +use lib 't/lib'; + +my $base = 'http://localhost'; + +use RestTest; +use DBICTest; +use URI; +use Test::More; +use Test::WWW::Mechanize::Catalyst 'RestTest'; +use HTTP::Request::Common; +use JSON; +use Data::Dumper; + +my $json = JSON->new->utf8; + +my $mech = Test::WWW::Mechanize::Catalyst->new; +ok( my $schema = DBICTest->init_schema(), 'got schema' ); + +my $base_url = "$base/api/rest/stashedclass"; + +# test cd +{ + my $class = 'RestTestDB::CD'; + my $req = GET( "$base_url/$class", {}, 'Accept' => 'text/x-json'); + $mech->request($req); + cmp_ok( $mech->status, '==', '200', "status OK" ); + my $response = $json->decode( $mech->content ); + is($response->{success}, 'true', 'success'); + is(scalar( @{$response->{list}} ), 6, 'six results'); +} + +# test author +{ + my $class = 'RestTestDB::Artist'; + my $req = GET( "$base_url/$class", {}, 'Accept' => 'text/x-json'); + $mech->request($req); + cmp_ok( $mech->status, '==', '200', "status OK" ); + my $response = $json->decode( $mech->content ); + is($response->{success}, 'true', 'success'); + is(scalar( @{$response->{list}} ), 3, 'three results'); +} + +# test non-existent class +{ + my $class = 'Foo::Bar::Baz'; + my $req = GET( "$base_url/$class", {}, 'Accept' => 'text/x-json'); + $mech->request($req); + cmp_ok( $mech->status, '==', '400', "status 400" ); + my $response = $json->decode( $mech->content ); + like($response->{messages}[0], qr/current_result_set.*does not pass the type constraint/, 'invalid class'); +} + +done_testing();