[Catalyst-commits] r14386 - in
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk: .
lib/CatalystX/CRUD/Model lib/CatalystX/CRUD/Object t t/lib/My
t/lib/MyApp/Controller t/lib/MyApp/Controller/REST
karpet at dev.catalyst.perl.org
karpet at dev.catalyst.perl.org
Mon Nov 5 04:45:47 GMT 2012
Author: karpet
Date: 2012-11-05 04:45:47 +0000 (Mon, 05 Nov 2012)
New Revision: 14386
Added:
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/02-rest.t
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/REST/
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/REST/Foo.pm
Modified:
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/Changes
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/MANIFEST
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/Makefile.PL
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Model/RDBO.pm
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Object/RDBO.pm
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/01-rdbo.t
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/My/DB.pm
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/Foo.pm
Log:
port tests to CatalystX::CRUD::Controller::REST
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/Changes
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/Changes 2012-11-05 03:13:42 UTC (rev 14385)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/Changes 2012-11-05 04:45:47 UTC (rev 14386)
@@ -82,3 +82,7 @@
0.23 31 Oct 2012
* add find_related() method
+
+0.24 xxx
+ * add tests for CatalystX::CRUD::Controller::REST
+
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/MANIFEST
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/MANIFEST 2012-11-05 03:13:42 UTC (rev 14385)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/MANIFEST 2012-11-05 04:45:47 UTC (rev 14386)
@@ -1,22 +1,25 @@
Changes
+lib/CatalystX/CRUD/Model/RDBO.pm
+lib/CatalystX/CRUD/Object/RDBO.pm
+Makefile.PL
MANIFEST
-Makefile.PL
README
-lib/CatalystX/CRUD/Model/RDBO.pm
-lib/CatalystX/CRUD/Object/RDBO.pm
t/00-load.t
t/01-rdbo.t
+t/02-rest.t
t/boilerplate.t
-t/pod-coverage.t
-t/pod.t
-t/lib/My/Foo.pm
-t/lib/MyApp.pm
-t/lib/MyApp/Model/Foo.pm
-t/lib/MyApp/Object.pm
t/lib/My/Bar.pm
t/lib/My/DB.pm
+t/lib/My/Foo.pm
t/lib/My/FooBar.pm
+t/lib/MyApp.pm
+t/lib/MyApp/Base/RDBO.pm
t/lib/MyApp/Controller/Foo.pm
+t/lib/MyApp/Controller/REST/Foo.pm
t/lib/MyApp/Form/Foo.pm
t/lib/MyApp/Model/Bar.pm
-t/lib/MyApp/Base/RDBO.pm
+t/lib/MyApp/Model/Foo.pm
+t/lib/MyApp/Object.pm
+t/lib/MyApp/script/myapp_server.pl
+t/pod-coverage.t
+t/pod.t
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/Makefile.PL
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/Makefile.PL 2012-11-05 03:13:42 UTC (rev 14385)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/Makefile.PL 2012-11-05 04:45:47 UTC (rev 14386)
@@ -13,7 +13,7 @@
'Data::Dump' => 0, # for testing
'Rose::DB::Object' => 0,
'Rose::DBx::Object::MoreHelpers' => 0,
- 'CatalystX::CRUD' => 0.53,
+ 'CatalystX::CRUD' => 0.54,
'Catalyst::Runtime' => 5.8,
'Rose::DBx::TestDB' => 0,
'Sort::SQL' => 0.05,
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Model/RDBO.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Model/RDBO.pm 2012-11-05 03:13:42 UTC (rev 14385)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Model/RDBO.pm 2012-11-05 04:45:47 UTC (rev 14386)
@@ -8,7 +8,7 @@
use Carp;
use Data::Dump qw( dump );
-our $VERSION = '0.23';
+our $VERSION = '0.23_01';
__PACKAGE__->mk_ro_accessors(
qw( name manager treat_like_int load_with related_load_with ));
@@ -356,7 +356,13 @@
my $fpk = $meta->{map_to}->[1];
my $args = [ $fpk => $foreign_pk_value ];
my $r = $obj->$method( query => $args );
- return $r;
+
+ # save ourselves lots of method-call overhead.
+ my $class = $self->object_class;
+
+ # delegate
+ my @wrapped = map { $class->new( delegate => $_ ) } @$r;
+ return wantarray ? @wrapped : \@wrapped;
}
=head2 add_related( I<obj>, I<rel_name>, I<foreign_value> )
@@ -433,10 +439,12 @@
my $meta = $self->_get_rel_meta( $obj, $rel_name );
my $fpk = $meta->{map_to}->[1];
$obj->$addmethod( { $fpk => $fk_val } );
- $obj->save;
+ my $rt = $obj->save;
# so next access reflects change.
$obj->forget_related($rel_name);
+
+ return $rt;
}
sub rm_related {
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Object/RDBO.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Object/RDBO.pm 2012-11-05 03:13:42 UTC (rev 14385)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Object/RDBO.pm 2012-11-05 04:45:47 UTC (rev 14386)
@@ -3,8 +3,12 @@
use warnings;
use base qw( CatalystX::CRUD::Object );
-our $VERSION = '0.23';
+# help for serialize()
+use Rose::DB::Object::Helpers qw( column_values_as_json );
+use JSON;
+our $VERSION = '0.23_01';
+
=head1 NAME
CatalystX::CRUD::Object::RDBO - Rose::DB::Object implementation of CatalystX::CRUD::Object
@@ -90,6 +94,27 @@
shift->delegate->delete(@_);
}
+=head2 is_new
+
+Calls not_found() on the RDBO delegate.
+
+=cut
+
+sub is_new {
+ shift->delegate->not_found();
+}
+
+=head2 serialize
+
+Returns column/value pairs for RDBO delegate with all DateTime
+et al objects serialized to strings.
+
+=cut
+
+sub serialize {
+ return decode_json( column_values_as_json( shift->delegate ) );
+}
+
1;
__END__
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/01-rdbo.t
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/01-rdbo.t 2012-11-05 03:13:42 UTC (rev 14385)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/01-rdbo.t 2012-11-05 04:45:47 UTC (rev 14386)
@@ -1,3 +1,6 @@
+#!/usr/bin/env perl
+use strict;
+
use Test::More tests => 22;
BEGIN {
Added: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/02-rest.t
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/02-rest.t (rev 0)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/02-rest.t 2012-11-05 04:45:47 UTC (rev 14386)
@@ -0,0 +1,167 @@
+#!/usr/bin/env perl
+use strict;
+
+use Test::More tests => 25;
+
+BEGIN {
+ use lib qw( ../../CatalystX-CRUD/trunk/lib );
+ use_ok('CatalystX::CRUD::Model::RDBO');
+ use_ok('CatalystX::CRUD::Object::RDBO');
+ use_ok('Rose::DBx::TestDB');
+ use_ok('Rose::DB::Object');
+}
+
+SKIP: {
+ # if we do not have required modules, skip everything
+ eval { require CatalystX::CRUD::Controller::REST; use JSON; };
+ if ($@) {
+ warn "$@";
+ skip "CatalystX::CRUD::Controller::REST not installed", 18;
+ }
+
+ use lib qw( t/lib );
+ use Catalyst::Test 'MyApp';
+ use Data::Dump qw( dump );
+ use HTTP::Request::Common;
+
+ diag( "testing against Catalyst-Runtime version "
+ . $Catalyst::Runtime::VERSION );
+
+ my $res;
+
+ ok( $res = request('/rest/foo/1'), "GET /rest/1" );
+ is( $res->code, 200, "get 200" );
+ is_deeply(
+ decode_json( $res->content ),
+ { "name" => "blue", "id" => 1 },
+ "get foo/1"
+ );
+
+ ok( $res = request('/rest/foo/1/bars/2'), "GET /foo/1/bars/2" );
+ is( $res->code, 404, "GET related does not yet exist" );
+ is_deeply(
+ decode_json( $res->content ),
+ { error => "No such bars with id '2'" },
+ "GET related 404 error"
+ );
+
+ # add a new foobar
+ ok( $res = request( POST( '/rest/foo/1/bars/2', [] ) ),
+ "POST /foo/1/bars/2/add" );
+
+ is( $res->code, 204, "POST add related OK" );
+
+ ok( $res = request('/rest/foo/1/bars/2'), "GET /foo/1/bars/2" );
+ is( $res->code, 200, "GET related now exists" );
+
+ #diag( dump decode_json( $res->content ) );
+ is_deeply(
+ decode_json( $res->content ),
+ [ { id => 2, name => 'red' } ],
+ "GET related 200"
+ );
+
+ # remove an old foobar
+ ok( $res = request(
+ POST( '/rest/foo/1/bars/1', [ 'x-tunneled-method' => 'DELETE' ] )
+ ),
+ "DELETE /foo/1/bars/1"
+ );
+
+ is( $res->code, 204, "DELETE related foobar" );
+
+ ok( $res = request('/rest/foo/search?id=1&cxc-order=id'),
+ "search id=1 with order" );
+
+ #diag( dump decode_json( $res->content ) );
+
+ is_deeply(
+ decode_json( $res->content ),
+ { count => 1,
+ query => {
+ limit => 50,
+ offset => 0,
+ plain_query => { id => [1] },
+ plain_query_str => "(id='1')",
+ query => [ "id", 1 ],
+ query_obj => "(id='1')",
+ sort_by => "t1.id ASC",
+ sort_order => [ { id => "ASC" } ],
+ },
+ results => [ { id => 1, name => "blue" } ],
+ },
+ "search query with order dir assumed"
+ );
+
+ #dump $res;
+
+ ok( $res = request('/rest/foo/search?id=1&cxc-sort=id&cxc-dir=desc'),
+ "search id=1 with sort/dir" );
+
+ #diag( dump decode_json( $res->content ) );
+
+ is_deeply(
+ decode_json( $res->content ),
+ { count => 1,
+ query => {
+ limit => 50,
+ offset => 0,
+ plain_query => { id => [1] },
+ plain_query_str => "(id='1')",
+ query => [ "id", 1 ],
+ query_obj => "(id='1')",
+ sort_by => "t1.id DESC",
+ sort_order => [ { id => "DESC" } ],
+ },
+ results => [ { id => 1, name => "blue" } ],
+ },
+ "search query with explicit order/dir"
+ );
+
+ ok( $res = request('/rest/foo/search?id=1'), "search id=1 with no sort" );
+
+ #diag( dump decode_json( $res->content ) );
+
+ is_deeply(
+ decode_json( $res->content ),
+ { count => 1,
+ query => {
+ limit => 50,
+ offset => 0,
+ plain_query => { id => [1] },
+ plain_query_str => "(id='1')",
+ query => [ "id", 1 ],
+ query_obj => "(id='1')",
+ sort_by => "t1.id DESC",
+ sort_order => [ { id => "DESC" } ],
+ },
+ results => [ { id => 1, name => "blue" } ],
+ },
+ "search query with default PK order"
+ );
+
+ # test multiple sort
+ ok( $res = request('/rest/foo/search?id=1&cxc-order=id+desc+name+asc'),
+ "search id=1 with 2-column sort" );
+
+ #diag( dump decode_json( $res->content ) );
+
+ is_deeply(
+ decode_json( $res->content ),
+ { count => 1,
+ query => {
+ limit => 50,
+ offset => 0,
+ plain_query => { id => [1] },
+ plain_query_str => "(id='1')",
+ query => [ "id", 1 ],
+ query_obj => "(id='1')",
+ sort_by => "t1.id DESC, t1.name ASC",
+ sort_order => [ { id => "DESC" }, { name => "ASC" } ],
+ },
+ results => [ { id => 1, name => "blue" } ],
+ },
+ "multi-sort content"
+ );
+
+}
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/My/DB.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/My/DB.pm 2012-11-05 03:13:42 UTC (rev 14385)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/My/DB.pm 2012-11-05 04:45:47 UTC (rev 14386)
@@ -3,6 +3,7 @@
use warnings;
use base qw( Rose::DB );
use Carp;
+use Rose::DBx::TestDB;
# create a temp db
my $db = Rose::DBx::TestDB->new;
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/Foo.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/Foo.pm 2012-11-05 03:13:42 UTC (rev 14385)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/Foo.pm 2012-11-05 04:45:47 UTC (rev 14386)
@@ -23,7 +23,7 @@
my $thing = $c->model('Foo')->new_object( id => 1 );
- for my $m (qw( create read update delete)) {
+ for my $m (qw( create read update delete )) {
croak unless $thing->can($m);
}
Added: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/REST/Foo.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/REST/Foo.pm (rev 0)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/REST/Foo.pm 2012-11-05 04:45:47 UTC (rev 14386)
@@ -0,0 +1,21 @@
+package MyApp::Controller::REST::Foo;
+use Moose;
+use namespace::autoclean;
+
+# we might not have this module installed
+BEGIN {
+ eval {
+ extends 'CatalystX::CRUD::Controller::REST';
+ __PACKAGE__->config(
+ model_name => 'Foo',
+ primary_key => 'id',
+ page_size => 50,
+ default => 'application/json',
+ );
+ };
+ if ($@) {
+ warn "CatalystX::CRUD::Controller::REST not available";
+ }
+}
+
+1;
More information about the Catalyst-commits
mailing list