use JSON instead of JSON::Any to get rid of the CPAN Testers failures when only JSON...
Alexander Hartmaier [Wed, 16 Feb 2011 15:47:57 +0000 (16:47 +0100)]
18 files changed:
Changes
dist.ini
lib/Catalyst/Controller/DBIC/API.pm
lib/Catalyst/Controller/DBIC/API/StaticArguments.pm
t/rest/create.t
t/rest/delete.t
t/rest/item.t
t/rest/list.t
t/rest/update.t
t/rpc/create.t
t/rpc/delete.t
t/rpc/item.t
t/rpc/list.t
t/rpc/list_json_search.t
t/rpc/list_prefetch.t
t/rpc/list_search_allows.t
t/rpc/setup_dbic_args.t
t/rpc/update.t

diff --git a/Changes b/Changes
index bd6b1c7..e87bc3f 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,9 @@ Revision history for Catalyst-Controller-DBIC-API: {{ $dist->version }}
 
 {{ $NEXT }}
 
+- Use JSON instead of JSON::Any to get rid of the CPAN Testers failures
+  when only JSON::Syck is available
+
 2.002004  2011-01-13 19:22:20 Europe/Vienna
 
 - Renamed Visitor to Validator::Visitor to conform with Data::DPath::Validator
index d13b174..f672cba 100644 (file)
--- a/dist.ini
+++ b/dist.ini
@@ -39,7 +39,7 @@ Catalyst::Controller::ActionRole = 0
 Catalyst::ActionRole::MatchRequestMethod = 0
 Catalyst::Action::Serialize = 0.83
 CGI::Expand = 2.02
-JSON::Any = 1.19
+JSON = 2.50
 Data::DPath::Validator = 0.093411
 Catalyst::Model::DBIC::Schema = 0.20
 MooseX::Types::Structured = 0.20
index 2a72e28..537bec2 100644 (file)
@@ -6,7 +6,7 @@ BEGIN { extends 'Catalyst::Controller::ActionRole'; }
 
 use CGI::Expand ();
 use DBIx::Class::ResultClass::HashRefInflator;
-use JSON::Any;
+use JSON;
 use Test::Deep::NoTest('eq_deeply');
 use MooseX::Types::Moose(':all');
 use Moose::Util;
@@ -15,6 +15,17 @@ use Try::Tiny;
 use Catalyst::Controller::DBIC::API::Request;
 use namespace::autoclean;
 
+has '_json' => (
+    is => 'ro',
+    isa => 'JSON',
+    lazy_build => 1,
+);
+
+sub _build__json {
+    # no ->utf8 here because the request params get decoded by Catalyst
+    return JSON->new;
+}
+
 with 'Catalyst::Controller::DBIC::API::StoredResultSource',
      'Catalyst::Controller::DBIC::API::StaticArguments',
      'Catalyst::Controller::DBIC::API::RequestArguments' => { static => 1 };
@@ -106,7 +117,7 @@ sub setup :Chained('specify.in.subclass.config') :CaptureArgs(0) :PathPart('spec
 
  :Chained('setup') :CaptureArgs(0) :PathPart('') :ActionClass('Deserialize')
 
-deserialize absorbs the request data and transforms it into useful bits by using CGI::Expand->expand_hash and a smattering of JSON::Any->from_json for a handful of arguments. Current only the following arguments are capable of being expressed as JSON:
+deserialize absorbs the request data and transforms it into useful bits by using CGI::Expand->expand_hash and a smattering of JSON->decode for a handful of arguments. Current only the following arguments are capable of being expressed as JSON:
 
     search_arg
     count_arg
@@ -142,9 +153,12 @@ sub deserialize :Chained('setup') :CaptureArgs(0) :PathPart('') :ActionClass('De
             {
                 for my $key ( keys %{$req_params->{$param}} )
                 {
+                    # copy the value because JSON::XS will alter it
+                    # even if decoding failed
+                    my $value = $req_params->{$param}->{$key};
                     try
                     {
-                        my $deserialized = JSON::Any->from_json($req_params->{$param}->{$key});
+                        my $deserialized = $self->_json->decode($value);
                         $req_params->{$param}->{$key} = $deserialized;
                     }
                     catch
@@ -158,7 +172,8 @@ sub deserialize :Chained('setup') :CaptureArgs(0) :PathPart('') :ActionClass('De
             {
                 try
                 {
-                    my $deserialized = JSON::Any->from_json($req_params->{$param});
+                    my $value = $req_params->{$param};
+                    my $deserialized = $self->_json->decode($value);
                     $req_params->{$param} = $deserialized;
                 }
                 catch
@@ -639,7 +654,7 @@ sub validate_object
             }
 
             # check for multiple values
-            if (ref($value) && !(reftype($value) eq reftype(JSON::Any::true)))
+            if (ref($value) && !(reftype($value) eq reftype(JSON::true)))
             {
                 require Data::Dumper;
                 die "Multiple values for '${key}': ${\Data::Dumper::Dumper($value)}";
@@ -734,7 +749,7 @@ sub update_object_from_params
     foreach my $key (keys %$params)
     {
         my $value = $params->{$key};
-        if (ref($value) && !(reftype($value) eq reftype(JSON::Any::true)))
+        if (ref($value) && !(reftype($value) eq reftype(JSON::true)))
         {
             $self->update_object_relation($c, $object, delete $params->{$key}, $key);
         }
@@ -766,7 +781,7 @@ sub update_object_relation
     if ($row) {
         foreach my $key (keys %$related_params) {
             my $value = $related_params->{$key};
-            if (ref($value) && !(reftype($value) eq reftype(JSON::Any::true)))
+            if (ref($value) && !(reftype($value) eq reftype(JSON::true)))
             {
                 $self->update_object_relation($c, $row, delete $related_params->{$key}, $key);
             }
@@ -800,7 +815,7 @@ sub insert_object_from_params
 
     my %rels;
     while (my ($k, $v) = each %{ $params }) {
-        if (ref($v) && !(reftype($v) eq reftype(JSON::Any::true))) {
+        if (ref($v) && !(reftype($v) eq reftype(JSON::true))) {
             $rels{$k} = $v;
         }
         else {
@@ -858,13 +873,13 @@ sub end :Private
     # Check for errors caught elsewhere
     if ( $c->res->status and $c->res->status != 200 ) {
         $default_status = $c->res->status;
-        $c->stash->{$self->stash_key}->{success} = $self->use_json_boolean ? JSON::Any::false : 'false';
+        $c->stash->{$self->stash_key}->{success} = $self->use_json_boolean ? JSON::false : 'false';
     } elsif ($self->get_errors($c)) {
         $c->stash->{$self->stash_key}->{messages} = $self->get_errors($c);
-        $c->stash->{$self->stash_key}->{success} = $self->use_json_boolean ? JSON::Any::false : 'false';
+        $c->stash->{$self->stash_key}->{success} = $self->use_json_boolean ? JSON::false : 'false';
         $default_status = 400;
     } else {
-        $c->stash->{$self->stash_key}->{success} = $self->use_json_boolean ? JSON::Any::true : 'true';
+        $c->stash->{$self->stash_key}->{success} = $self->use_json_boolean ? JSON::true : 'true';
         $default_status = 200;
     }
 
@@ -996,7 +1011,7 @@ By default, the response data is serialized into $c->stash->{$self->stash_key}->
 
 =head3 use_json_boolean
 
-By default, the response success status is set to a string value of "true" or "false". If this attribute is true, JSON::Any's true() and false() will be used instead. Note, this does not effect other internal processing of boolean values.
+By default, the response success status is set to a string value of "true" or "false". If this attribute is true, JSON's true() and false() will be used instead. Note, this does not effect other internal processing of boolean values.
 
 =head3 count_arg, page_arg, select_arg, search_arg, grouped_by_arg, ordered_by_arg, prefetch_arg, as_arg, total_entries_arg
 
@@ -1126,7 +1141,7 @@ If more extensive customization is required, it is recommened to peer into the r
 
 It should be noted that version 1.004 and above makes a rapid depature from the status quo. The internals were revamped to use more modern tools such as Moose and its role system to refactor functionality out into self-contained roles.
 
-To this end, internally, this module now understands JSON boolean values (as represented by JSON::Any) and will Do The Right Thing in handling those values. This means you can have ColumnInflators installed that can covert between JSON::Any booleans and whatever your database wants for boolean values.
+To this end, internally, this module now understands JSON boolean values (as represented by the JSON module) and will Do The Right Thing in handling those values. This means you can have ColumnInflators installed that can covert between JSON booleans and whatever your database wants for boolean values.
 
 Validation for various *_allows or *_exposes is now accomplished via Data::DPath::Validator with a lightly simplified, via subclass, Data::DPath::Validator::Visitor. The rough jist of the process goes as follows: Arguments provided to those attributes are fed into the Validator and Data::DPaths are generated. Then, incoming requests are validated against these paths generated. The validator is set in "loose" mode meaning only one path is required to match. For more information, please see L<Data::DPath::Validator> and more specifically L<Catalyst::Controller::DBIC::API::Validator>.
 
index 280e92a..e2c2c90 100644 (file)
@@ -157,7 +157,7 @@ has 'total_entries_arg' => ( is => 'ro', isa => Str, default => 'totalcount' );
 
 =attribute_public use_json_boolean is: ro, isa: Bool, default: 0
 
-use_json_boolean controls whether JSON::Any boolean types are used in the success parameter of the response or if raw strings are used
+use_json_boolean controls whether JSON boolean types are used in the success parameter of the response or if raw strings are used
 
 =cut
 
index 58a6ea8..4a96e81 100644 (file)
@@ -12,79 +12,99 @@ use DBICTest;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
-my $artist_create_url = "$base/api/rest/artist";
+my $artist_create_url   = "$base/api/rest/artist";
 my $producer_create_url = "$base/api/rest/producer";
 
 # test validation when no params sent
 {
-       my $test_data = JSON::Any->Dump({ wrong_param => 'value' });
-       my $req = PUT( $artist_create_url );
-       $req->content_type('text/x-json');
-       $req->content_length(
-                                                do { use bytes; length( $test_data ) }
-                                                );
-       $req->content( $test_data );
-       $mech->request($req);
-    
-       cmp_ok( $mech->status, '==', 400, 'attempt without required params caught' );
-       my $response = JSON::Any->Load( $mech->content);
-       like($response->{messages}->[0], qr/No value supplied for name and no default/, 'correct message returned' );
+    my $test_data = $json->encode( { wrong_param => 'value' } );
+    my $req = PUT($artist_create_url);
+    $req->content_type('text/x-json');
+    $req->content_length(
+        do { use bytes; length($test_data) }
+    );
+    $req->content($test_data);
+    $mech->request($req);
+
+    cmp_ok( $mech->status, '==', 400,
+        'attempt without required params caught' );
+    my $response = $json->decode( $mech->content );
+    like(
+        $response->{messages}->[0],
+        qr/No value supplied for name and no default/,
+        'correct message returned'
+    );
 }
 
 # test default value used if default value exists
 {
-       my $test_data = JSON::Any->Dump({});
-       my $req = PUT( $producer_create_url );
-       $req->content_type('text/x-json');
-       $req->content_length(
-                                                do { use bytes; length( $test_data ) }
-                                                );
-       $req->content( $test_data );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'default value used when not supplied' );
-       ok($schema->resultset('Producer')->find({ name => 'fred' }), 'record created with default name');
+    my $test_data = $json->encode( {} );
+    my $req = PUT($producer_create_url);
+    $req->content_type('text/x-json');
+    $req->content_length(
+        do { use bytes; length($test_data) }
+    );
+    $req->content($test_data);
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200,
+        'default value used when not supplied' );
+    ok( $schema->resultset('Producer')->find( { name => 'fred' } ),
+        'record created with default name' );
 }
 
 # test create works as expected when passing required value
 {
-       my $test_data = JSON::Any->Dump({ name => 'king luke' });
-       my $req = PUT( $producer_create_url );
-       $req->content_type('text/x-json');
-       $req->content_length(
-                                                do { use bytes; length( $test_data ) }
-                                                );
-       $req->content( $test_data );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
-       my $new_obj = $schema->resultset('Producer')->find({ name => 'king luke' });
-       ok($new_obj, 'record created with specified name');
+    my $test_data = $json->encode( { name => 'king luke' } );
+    my $req = PUT($producer_create_url);
+    $req->content_type('text/x-json');
+    $req->content_length(
+        do { use bytes; length($test_data) }
+    );
+    $req->content($test_data);
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
+    my $new_obj =
+        $schema->resultset('Producer')->find( { name => 'king luke' } );
+    ok( $new_obj, 'record created with specified name' );
 
-       my $response = JSON::Any->Load( $mech->content);
-       is_deeply( $response->{list}, { $new_obj->get_columns }, 'json for new producer returned' );
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response->{list},
+        { $new_obj->get_columns },
+        'json for new producer returned'
+    );
 }
 
 # test bulk create
 {
-       my $test_data = JSON::Any->Dump({ list => [{ name => 'king nperez' }, { name => 'queen perla'}] });
-       my $req = PUT( $producer_create_url );
-       $req->content_type('text/x-json');
-       $req->content_length(
-                                                do { use bytes; length( $test_data ) }
-                                                );
-       $req->content( $test_data );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
-       my $rs = $schema->resultset('Producer')->search([ { name => 'king nperez' }, { name => 'queen perla' } ]);
-       ok($rs, 'record created with specified name');
+    my $test_data = $json->encode(
+        { list => [ { name => 'king nperez' }, { name => 'queen perla' } ] }
+    );
+    my $req = PUT($producer_create_url);
+    $req->content_type('text/x-json');
+    $req->content_length(
+        do { use bytes; length($test_data) }
+    );
+    $req->content($test_data);
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
+    my $rs =
+        $schema->resultset('Producer')
+        ->search( [ { name => 'king nperez' }, { name => 'queen perla' } ] );
+    ok( $rs, 'record created with specified name' );
 
-       my $response = JSON::Any->Load( $mech->content);
-    my $expected = [ map { my %foo = $_->get_inflated_columns; \%foo; } $rs->all ];
-       is_deeply( $response->{list}, $expected, 'json for bulk create returned' );
+    my $response = $json->decode( $mech->content );
+    my $expected =
+        [ map { my %foo = $_->get_inflated_columns; \%foo; } $rs->all ];
+    is_deeply( $response->{list}, $expected,
+        'json for bulk create returned' );
 }
 
 done_testing();
index 17e1baa..3034558 100644 (file)
@@ -13,45 +13,50 @@ use DBICTest;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common qw/ DELETE /;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
-my $track = $schema->resultset('Track')->first;
+my $track         = $schema->resultset('Track')->first;
 my %original_cols = $track->get_columns;
 
-my $track_url = "$base/api/rest/track/";
+my $track_url        = "$base/api/rest/track/";
 my $track_delete_url = $track_url . $track->id;
 
 {
-  my $req = HTTP::Request->new( DELETE => $track_delete_url );
-  $req->content_type('text/x-json');
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'Attempt to delete track ok' );
+    my $req = HTTP::Request->new( DELETE => $track_delete_url );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'Attempt to delete track ok' );
 
-  my $deleted_track = $schema->resultset('Track')->find($track->id);
-  is($deleted_track, undef, 'track deleted');
+    my $deleted_track = $schema->resultset('Track')->find( $track->id );
+    is( $deleted_track, undef, 'track deleted' );
 }
 
 {
-  my $req = HTTP::Request->new( DELETE => $track_delete_url );
-  $req->content_type('text/x-json');
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 400, 'Attempt to delete again caught' );
+    my $req = HTTP::Request->new( DELETE => $track_delete_url );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400, 'Attempt to delete again caught' );
 }
 
 {
-  my $track_cnt = $schema->resultset('Track')->count;
-  my $tracks_rs = $schema->resultset('Track')->search(undef, { select => ['trackid'], as => ['id'], rows => 3 });
-  $tracks_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
-  my $test_data = JSON::Any->Dump({ list => [$tracks_rs->all] });
-  my $req = DELETE( $track_url, Content => $test_data );
-  $req->content_type('text/x-json');
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'Attempt to delete three tracks ok' );
-
-  is($schema->resultset('Track')->count + 3, $track_cnt, 'Three tracks deleted');
+    my $track_cnt = $schema->resultset('Track')->count;
+    my $tracks_rs =
+        $schema->resultset('Track')
+        ->search( undef, { select => ['trackid'], as => ['id'], rows => 3 } );
+    $tracks_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
+    my $test_data = $json->encode( { list => [ $tracks_rs->all ] } );
+    my $req = DELETE( $track_url, Content => $test_data );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'Attempt to delete three tracks ok' );
+
+    is( $schema->resultset('Track')->count + 3,
+        $track_cnt, 'Three tracks deleted' );
 }
 
 done_testing();
index 29d2f1e..ff9a51b 100644 (file)
@@ -13,43 +13,63 @@ use URI;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
 my $artist_view_url = "$base/api/rest/artist/";
 
 {
     my $id = 1;
-    my $req = GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
+    my $req =
+        GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
     $mech->request($req);
     cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
-    my %expected_response = $schema->resultset('Artist')->find($id)->get_columns;
-    my $response = JSON::Any->Load( $mech->content);
-    is_deeply( $response, { data => \%expected_response, success => 'true' }, 'correct data returned' );
+    my %expected_response =
+        $schema->resultset('Artist')->find($id)->get_columns;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { data => \%expected_response, success => 'true' },
+        'correct data returned'
+    );
 }
 
 {
     my $id = 5;
-    my $req = GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
+    my $req =
+        GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
     $mech->request($req);
     cmp_ok( $mech->status, '==', 400, 'open attempt not ok' );
-    my $response = JSON::Any->Load( $mech->content);
-    is($response->{success}, 'false', 'not existing object fetch failed ok');
-    like($response->{messages}->[0], qr/^No object found for id/, 'error message for not existing object fetch ok');
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, 'false',
+        'not existing object fetch failed ok' );
+    like(
+        $response->{messages}->[0],
+        qr/^No object found for id/,
+        'error message for not existing object fetch ok'
+    );
 }
 
 my $track_view_url = "$base/api/rest/track/";
 
 {
     my $id = 9;
-    my $req = GET( $track_view_url . $id, undef, 'Accept' => 'application/json' );
+    my $req =
+        GET( $track_view_url . $id, undef, 'Accept' => 'application/json' );
     $mech->request($req);
     cmp_ok( $mech->status, '==', 200, 'got track with datetime object okay' );
-    my %expected_response = $schema->resultset('Track')->find($id)->get_columns;
-    my $response = JSON::Any->Load( $mech->content);
-    is_deeply( $response, { data => \%expected_response, success => 'true' }, 'correct data returned for track with datetime' );
+    my %expected_response =
+        $schema->resultset('Track')->find($id)->get_columns;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { data => \%expected_response, success => 'true' },
+        'correct data returned for track with datetime'
+    );
 }
 
 done_testing();
index c1aa7b2..acb8872 100644 (file)
@@ -13,162 +13,250 @@ use URI;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
-my $artist_list_url = "$base/api/rest/artist";
+my $artist_list_url          = "$base/api/rest/artist";
 my $filtered_artist_list_url = "$base/api/rest/bound_artist";
-my $producer_list_url = "$base/api/rest/producer";
-my $cd_list_url = "$base/api/rest/cd";
-my $track_list_url = "$base/api/rest/track";
+my $producer_list_url        = "$base/api/rest/producer";
+my $cd_list_url              = "$base/api/rest/cd";
+my $track_list_url           = "$base/api/rest/track";
 
 # 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::Any->Load( $mech->content);
-  is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct message returned' );
+    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->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        '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::Any->Load( $mech->content);
-  is_deeply( $response, { list => \@expected_response, success => 'true' }, '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->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        '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::Any->Load( $mech->content);
-  is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for complex query' );
+    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->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        '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::Any->Load( $mech->content);
-  is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with list_returns specified' );
+    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->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for class with list_returns specified'
+    );
 }
 
 {
-  my $uri = URI->new( $artist_list_url );
-  $uri->query_form({ 'search.cds.title' => 'Forkful of bees' });
-  my $req = GET( $uri, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'search related request okay' );
-
-  my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { join => 'cds' })->all;
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with select specified' );
+    my $uri = URI->new($artist_list_url);
+    $uri->query_form( { 'search.cds.title' => 'Forkful of bees' } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('Artist')
+        ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } )
+        ->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for class with select specified'
+    );
 }
 
 {
-  my $uri = URI->new( $artist_list_url );
-  $uri->query_form({ 'search.cds.title' => 'Forkful of bees', 'list_returns.0.count' => '*', 'as.0' => 'count'});      
-  my $req = GET( $uri, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'search related request okay' );
-
-  my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { select => [ {count => '*'} ], as => [ 'count' ], join => 'cds' })->all;
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for count' );
+    my $uri = URI->new($artist_list_url);
+    $uri->query_form(
+        {   'search.cds.title'     => 'Forkful of bees',
+            'list_returns.0.count' => '*',
+            'as.0'                 => 'count'
+        }
+    );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('Artist')
+        ->search( { 'cds.title' => 'Forkful of bees' },
+        { select => [ { count => '*' } ], as => ['count'], join => 'cds' } )
+        ->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for count'
+    );
 }
 
 {
-  my $uri = URI->new( $filtered_artist_list_url );
-  $uri->query_form({ 'search.artistid' => '2' });      
-  my $req = GET( $uri, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'search related request okay' );
-  my $response = JSON::Any->Load( $mech->content);
-  my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'artistid' => '1' })->all;
-  is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with setup_list_method specified' );
+    my $uri = URI->new($filtered_artist_list_url);
+    $uri->query_form( { 'search.artistid' => '2' } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+    my $response          = $json->decode( $mech->content );
+    my @expected_response = map {
+        { $_->get_columns }
+    } $schema->resultset('Artist')->search( { 'artistid' => '1' } )->all;
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for class with setup_list_method specified'
+    );
 }
 
 {
-  my $uri = URI->new( $cd_list_url );
-  $uri->query_form({ 'search.tracks.position' => '1', 'search.artist.name' => 'Caterwauler McCrae' });
-  my $req = GET( $uri, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'search multiple params request okay' );
-  my $response = JSON::Any->Load( $mech->content);
-  my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({
-          'artist.name'     => 'Caterwauler McCrae',
-          'tracks.position' => 1,
-      }, {
-          join => [qw/ artist tracks /],
-      })->all;
-  is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for multiple search params' );
+    my $uri = URI->new($cd_list_url);
+    $uri->query_form(
+        {   'search.tracks.position' => '1',
+            'search.artist.name'     => 'Caterwauler McCrae'
+        }
+    );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'search multiple params request okay' );
+    my $response          = $json->decode( $mech->content );
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('CD')->search(
+        {   'artist.name'     => 'Caterwauler McCrae',
+            'tracks.position' => 1,
+        },
+        { join => [qw/ artist tracks /], }
+        )->all;
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for multiple search params'
+    );
 }
 
 # page specified in controller config (RT#56226)
 {
-    my $uri = URI->new( $track_list_url );
+    my $uri = URI->new($track_list_url);
     $uri->query_form();
     my $req = GET( $uri, 'Accept' => 'text/x-json' );
     $mech->request($req);
     cmp_ok( $mech->status, '==', 200, 'get first page ok' );
-    my $response = JSON::Any->Load( $mech->content);
-    my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search(undef, {
-            page => 1,
-        })->all;
-    is_deeply( $response, { list => \@expected_response, success => 'true', totalcount => 15 }, 'correct data returned for static configured paging' );
+    my $response          = $json->decode( $mech->content );
+    my @expected_response = map {
+        { $_->get_columns }
+    } $schema->resultset('Track')->search( undef, { page => 1, } )->all;
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true', totalcount => 15 },
+        'correct data returned for static configured paging'
+    );
 }
 
 {
-    my $uri = URI->new( $artist_list_url );
-    $uri->query_form({ 'search.cds.track.title' => 'Suicidal' });
+    my $uri = URI->new($artist_list_url);
+    $uri->query_form( { 'search.cds.track.title' => 'Suicidal' } );
     my $req = GET( $uri, 'Accept' => 'text/x-json' );
     $mech->request($req);
-    cmp_ok( $mech->status, '==', 400, 'attempt with nonexisting relationship fails' );
-    my $response = JSON::Any->Load( $mech->content);
-    is_deeply( $response->{messages}, ['track is neither a relationship nor a column'], 'correct error message returned' );
+    cmp_ok( $mech->status, '==', 400,
+        'attempt with nonexisting relationship fails' );
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response->{messages},
+        ['track is neither a relationship nor a column'],
+        'correct error message returned'
+    );
 }
 
 {
-    my $uri = URI->new( $artist_list_url );
-    $uri->query_form({ 'search.cds.tracks.foo' => 'Bar' });
+    my $uri = URI->new($artist_list_url);
+    $uri->query_form( { 'search.cds.tracks.foo' => 'Bar' } );
     my $req = GET( $uri, 'Accept' => 'text/x-json' );
     $mech->request($req);
-    cmp_ok( $mech->status, '==', 400, 'attempt with nonexisting column fails' );
-    my $response = JSON::Any->Load( $mech->content);
-    is_deeply( $response->{messages}, ['a database error has occured.'], 'correct error message returned' );
+    cmp_ok( $mech->status, '==', 400,
+        'attempt with nonexisting column fails' );
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response->{messages},
+        ['a database error has occured.'],
+        'correct error message returned'
+    );
 }
 
 {
-    my $uri = URI->new( $artist_list_url );
-    $uri->query_form({ 'search.cds.tracks.title.like' => 'Boring%' });
+    my $uri = URI->new($artist_list_url);
+    $uri->query_form( { 'search.cds.tracks.title.like' => 'Boring%' } );
     my $req = GET( $uri, 'Accept' => 'text/x-json' );
     $mech->request($req);
     cmp_ok( $mech->status, '==', 200, 'attempt with sql function ok' );
-    my $response = JSON::Any->Load( $mech->content);
-    my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({
-            'tracks.title' => { 'like' => 'Boring%' },
-        }, {
-            join => { cds => 'tracks' },
-        })->all;
-    is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for search with sql function' );
+    my $response          = $json->decode( $mech->content );
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('Artist')
+        ->search( { 'tracks.title' => { 'like' => 'Boring%' }, },
+        { join => { cds => 'tracks' }, } )->all;
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for search with sql function'
+    );
 }
 
 done_testing();
index 3aa7e49..9a6d439 100644 (file)
@@ -13,105 +13,144 @@ use DBICTest;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
-my $track = $schema->resultset('Track')->first;
+my $track         = $schema->resultset('Track')->first;
 my %original_cols = $track->get_columns;
 
-my $track_url = "$base/api/rest/track/";
-my $track_update_url = $track_url . $track->id;
+my $track_url         = "$base/api/rest/track/";
+my $track_update_url  = $track_url . $track->id;
 my $tracks_update_url = $track_url;
 
 # test invalid track id caught
 {
-       foreach my $wrong_id ('sdsdsdsd', 3434234) {
-               my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
-               my $test_data = JSON::Any->Dump({ title => 'value' });
-               my $req = POST( $incorrect_url, Content => $test_data );
-               $req->content_type('text/x-json');
-               $mech->request($req);
-
-               cmp_ok( $mech->status, '==', 400, 'Attempt with invalid track id caught' );
-               
-               my $response = JSON::Any->Load( $mech->content);
-               like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
-               
-               $track->discard_changes;
-               is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
-       }
+    foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
+        my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
+        my $test_data     = $json->encode( { title => 'value' } );
+        my $req           = POST( $incorrect_url, Content => $test_data );
+        $req->content_type('text/x-json');
+        $mech->request($req);
+
+        cmp_ok( $mech->status, '==', 400,
+            'Attempt with invalid track id caught' );
+
+        my $response = $json->decode( $mech->content );
+        like(
+            $response->{messages}->[0],
+            qr/No object found for id/,
+            'correct message returned'
+        );
+
+        $track->discard_changes;
+        is_deeply(
+            { $track->get_columns },
+            \%original_cols,
+            'no update occurred'
+        );
+    }
 }
 
 # validation when no params sent
 {
-       my $test_data = JSON::Any->Dump({ wrong_param => 'value' });
-       my $req = POST( $track_update_url, Content => $test_data );
-       $req->content_type('text/x-json');
-       $mech->request($req);
-
-       cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
-
-       my $response = JSON::Any->Load( $mech->content);
-       is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
-
-       $track->discard_changes;
-       is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
+    my $test_data = $json->encode( { wrong_param => 'value' } );
+    my $req = POST( $track_update_url, Content => $test_data );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+
+    cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
+
+    my $response = $json->decode( $mech->content );
+    is_deeply( $response->{messages}, ['No valid keys passed'],
+        'correct message returned' );
+
+    $track->discard_changes;
+    is_deeply(
+        { $track->get_columns },
+        \%original_cols,
+        'no update occurred'
+    );
 }
 
 {
-       my $test_data = JSON::Any->Dump({ title => undef });
-       my $req = POST( $track_update_url, Content => $test_data );
-       $req->content_type('text/x-json');
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
-
-       $track->discard_changes;
-       isnt($track->title, $original_cols{title}, 'Title changed');
-       is($track->title, undef, 'Title changed to undef');
+    my $test_data = $json->encode( { title => undef } );
+    my $req = POST( $track_update_url, Content => $test_data );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
+
+    $track->discard_changes;
+    isnt( $track->title, $original_cols{title}, 'Title changed' );
+    is( $track->title, undef, 'Title changed to undef' );
 }
 
 {
-       my $test_data = JSON::Any->Dump({ title => 'monkey monkey', 'cd' => { year => 2009 } });
-       my $req = POST( $track_update_url, Content => $test_data );
-       $req->content_type('text/x-json');
-       $mech->request($req);
+    my $test_data = $json->encode(
+        { title => 'monkey monkey', 'cd' => { year => 2009 } } );
+    my $req = POST( $track_update_url, Content => $test_data );
+    $req->content_type('text/x-json');
+    $mech->request($req);
 
-       cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
+    cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
 
-       $track->discard_changes;
-       is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
-       is($track->cd->year, 2009, 'related row updated');
+    $track->discard_changes;
+    is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
+    is( $track->cd->year, 2009, 'related row updated' );
 }
 
 # bulk_update existing objects
 {
-  # order to get a stable order of rows
-  my $tracks_rs = $schema->resultset('Track')->search(undef, { order_by => 'trackid', rows => 3 });
-  my $test_data = JSON::Any->Dump({ list => [map +{ id => $_->id, title => 'Track ' . $_->id }, $tracks_rs->all] });
-  my $req = PUT( $tracks_update_url, Content => $test_data );
-  $req->content_type('text/x-json');
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
-
-  $tracks_rs->reset;
-  while (my $track = $tracks_rs->next) {
-    is($track->title, 'Track ' . $track->id, 'Title changed');
-  }
+
+    # order to get a stable order of rows
+    my $tracks_rs =
+        $schema->resultset('Track')
+        ->search( undef, { order_by => 'trackid', rows => 3 } );
+    my $test_data = $json->encode(
+        {   list => [
+                map +{ id => $_->id, title => 'Track ' . $_->id },
+                $tracks_rs->all
+            ]
+        }
+    );
+    my $req = PUT( $tracks_update_url, Content => $test_data );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
+
+    $tracks_rs->reset;
+    while ( my $track = $tracks_rs->next ) {
+        is( $track->title, 'Track ' . $track->id, 'Title changed' );
+    }
 }
 
 # bulk_update nonexisting objects
 {
-  # order to get a stable order of rows
-  my $test_data = JSON::Any->Dump({ list => [map +{ id => $_, title => 'Track ' . $_ }, (1000..1002)] });
-  my $req = PUT( $tracks_update_url, Content => $test_data );
-  $req->content_type('text/x-json');
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 400, 'Attempt to update three nonexisting tracks fails' );
-  my $response = JSON::Any->Load( $mech->content);
-  is( $response->{success}, JSON::Any::false, 'success property returns unquoted false' );
-  like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
+
+    # order to get a stable order of rows
+    my $test_data = $json->encode(
+        {   list => [
+                map +{ id => $_, title => 'Track ' . $_ },
+                ( 1000 .. 1002 )
+            ]
+        }
+    );
+    my $req = PUT( $tracks_update_url, Content => $test_data );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400,
+        'Attempt to update three nonexisting tracks fails' );
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, JSON::false,
+        'success property returns unquoted false' );
+    like(
+        $response->{messages}->[0],
+        qr/No object found for id/,
+        'correct message returned'
+    );
 }
 
 done_testing();
index f49a5b6..d3b249d 100644 (file)
@@ -13,76 +13,108 @@ use DBICTest;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
-my $artist_create_url = "$base/api/rpc/artist/create";
+my $artist_create_url     = "$base/api/rpc/artist/create";
 my $any_artist_create_url = "$base/api/rpc/any/artist/create";
-my $producer_create_url = "$base/api/rpc/producer/create";
+my $producer_create_url   = "$base/api/rpc/producer/create";
 
 # test validation when no params sent
 {
-  my $req = POST( $artist_create_url, {
-         wrong_param => 'value'
-  }, 'Accept' => 'text/json' );
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 400, 'attempt without required params caught' );
-  my $response = JSON::Any->Load( $mech->content);
-  like( $response->{messages}->[0], qr/No value supplied for name and no default/, 'correct message returned' );
+    my $req = POST(
+        $artist_create_url,
+        { wrong_param => 'value' },
+        'Accept' => 'text/json'
+    );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 400,
+        'attempt without required params caught' );
+    my $response = $json->decode( $mech->content );
+    like(
+        $response->{messages}->[0],
+        qr/No value supplied for name and no default/,
+        'correct message returned'
+    );
 }
 
 # test default value used if default value exists
 {
-  my $req = POST( $producer_create_url, {
-
-  }, 'Accept' => 'text/json' );
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 200, 'default value used when not supplied' );
-  ok($schema->resultset('Producer')->find({ name => 'fred' }), 'record created with default name');
+    my $req = POST(
+        $producer_create_url,
+        {
+
+        },
+        'Accept' => 'text/json'
+    );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 200,
+        'default value used when not supplied' );
+    ok( $schema->resultset('Producer')->find( { name => 'fred' } ),
+        'record created with default name' );
 }
 
 # test create works as expected when passing required value
 {
-  my $req = POST( $producer_create_url, {
-         name => 'king luke'
-  }, 'Accept' => 'text/json' );
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 200, 'param value used when supplied' );
-
-  my $new_obj = $schema->resultset('Producer')->find({ name => 'king luke' });
-  ok($new_obj, 'record created with specified name');
-
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply( $response->{list}, { $new_obj->get_columns }, 'json for new producer returned' );
+    my $req = POST(
+        $producer_create_url,
+        { name => 'king luke' },
+        'Accept' => 'text/json'
+    );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 200, 'param value used when supplied' );
+
+    my $new_obj =
+        $schema->resultset('Producer')->find( { name => 'king luke' } );
+    ok( $new_obj, 'record created with specified name' );
+
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response->{list},
+        { $new_obj->get_columns },
+        'json for new producer returned'
+    );
 }
 
 # test stash config handling
 {
-  my $req = POST( $any_artist_create_url, {
-         name => 'queen monkey'
-  }, 'Accept' => 'text/json' );
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 200, 'stashed config okay' );
-
-  my $new_obj = $schema->resultset('Artist')->find({ name => 'queen monkey' });
-  ok($new_obj, 'record created with specified name');
-
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply( $response, { success => 'true' }, 'json for new artist returned' );
+    my $req = POST(
+        $any_artist_create_url,
+        { name => 'queen monkey' },
+        'Accept' => 'text/json'
+    );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 200, 'stashed config okay' );
+
+    my $new_obj =
+        $schema->resultset('Artist')->find( { name => 'queen monkey' } );
+    ok( $new_obj, 'record created with specified name' );
+
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { success => 'true' },
+        'json for new artist returned'
+    );
 }
 
 # test create returns an error as expected when passing invalid value
 {
-  my $long_string = '-' x 1024;
-
-  my $req = POST( $producer_create_url, {
-         producerid => $long_string,
-      name       => $long_string,
-  }, 'Accept' => 'text/json' );
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 400, 'invalid param value produces error' );
+    my $long_string = '-' x 1024;
+
+    my $req = POST(
+        $producer_create_url,
+        {   producerid => $long_string,
+            name       => $long_string,
+        },
+        'Accept' => 'text/json'
+    );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 400, 'invalid param value produces error' );
 }
 
 done_testing();
index 109d1b2..ffd649a 100644 (file)
@@ -13,46 +13,53 @@ use DBICTest;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
-my $track = $schema->resultset('Track')->first;
+my $track         = $schema->resultset('Track')->first;
 my %original_cols = $track->get_columns;
 
-my $track_delete_url = "$base/api/rpc/track/id/" . $track->id . "/delete";
+my $track_delete_url  = "$base/api/rpc/track/id/" . $track->id . "/delete";
 my $tracks_delete_url = "$base/api/rpc/track/delete";
 
 {
-  my $req = POST( $track_delete_url, {
-
-  });
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 200, 'Attempt to delete track ok' );
+    my $req = POST( $track_delete_url, {} );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 200, 'Attempt to delete track ok' );
 
-  my $deleted_track = $schema->resultset('Track')->find($track->id);
-  is($deleted_track, undef, 'track deleted');
+    my $deleted_track = $schema->resultset('Track')->find( $track->id );
+    is( $deleted_track, undef, 'track deleted' );
 }
 
 {
-  my $req = POST( $track_delete_url, {
+    my $req = POST(
+        $track_delete_url,
+        {
 
-  });
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 400, 'Attempt to delete again caught' );
+        }
+    );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 400, 'Attempt to delete again caught' );
 }
 
 {
-  my $track_cnt = $schema->resultset('Track')->count;
-  my $tracks_rs = $schema->resultset('Track')->search(undef, { select => ['trackid'], as => ['id'], rows => 3 });
-  $tracks_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
-  my $test_data = JSON::Any->Dump({ list => [$tracks_rs->all] });
-  my $req = POST( $tracks_delete_url, Content => $test_data );
-  $req->content_type('text/x-json');
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'Attempt to delete three tracks ok' );
-
-  is($schema->resultset('Track')->count + 3, $track_cnt, 'Three tracks deleted');
+    my $track_cnt = $schema->resultset('Track')->count;
+    my $tracks_rs =
+        $schema->resultset('Track')
+        ->search( undef, { select => ['trackid'], as => ['id'], rows => 3 } );
+    $tracks_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
+    my $test_data = $json->encode( { list => [ $tracks_rs->all ] } );
+    my $req = POST( $tracks_delete_url, Content => $test_data );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'Attempt to delete three tracks ok' );
+
+    is( $schema->resultset('Track')->count + 3,
+        $track_cnt, 'Three tracks deleted' );
 }
 
 done_testing();
index 538c379..c84f2e4 100644 (file)
@@ -13,31 +13,45 @@ use URI;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
 my $artist_view_url = "$base/api/rpc/artist/id/";
 
 {
     my $id = 1;
-    my $req = GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
+    my $req =
+        GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
     $mech->request($req);
     cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
-    my %expected_response = $schema->resultset('Artist')->find($id)->get_columns;
-    my $response = JSON::Any->Load( $mech->content);
-    is_deeply( $response, { data => \%expected_response, success => 'true' }, 'correct data returned' );
+    my %expected_response =
+        $schema->resultset('Artist')->find($id)->get_columns;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { data => \%expected_response, success => 'true' },
+        'correct data returned'
+    );
 }
 
 {
     my $id = 5;
-    my $req = GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
+    my $req =
+        GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
     $mech->request($req);
     cmp_ok( $mech->status, '==', 400, 'open attempt not ok' );
-    my $response = JSON::Any->Load( $mech->content);
-    is($response->{success}, 'false', 'not existing object fetch failed ok');
-    like($response->{messages}->[0], qr/^No object found for id/, 'error message for not existing object fetch ok');
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, 'false',
+        'not existing object fetch failed ok' );
+    like(
+        $response->{messages}->[0],
+        qr/^No object found for id/,
+        'error message for not existing object fetch ok'
+    );
 }
 
 done_testing();
index d71bbc5..6cb9794 100644 (file)
@@ -13,228 +13,419 @@ use URI;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
-my $artist_list_url = "$base/api/rpc/artist/list";
+my $artist_list_url   = "$base/api/rpc/artist/list";
 my $producer_list_url = "$base/api/rpc/producer/list";
-my $track_list_url = "$base/api/rpc/track/list";
-my $cd_list_url = "$base/api/rpc/cd/list";
+my $track_list_url    = "$base/api/rpc/track/list";
+my $cd_list_url       = "$base/api/rpc/cd/list";
 
 # 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::Any->Load( $mech->content);
-       is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct message returned' );
+    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->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        '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::Any->Load( $mech->content);
-       is_deeply( $response, { list => \@expected_response, success => 'true' }, '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->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        '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::Any->Load( $mech->content);
-       is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for complex query' );
+    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->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for complex query'
+    );
 }
 
 {
-       my $uri = URI->new( $artist_list_url );
-       $uri->query_form({ 'search.name.LIKE' => '%waul%', 'list_returns.0.count' => '*', 'as.0' => 'count'});
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'attempt with basic count' );
-
-       my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }}, { select => [ {count => '*'} ], as => ['count'] } )->all;
-       my $response = JSON::Any->Load( $mech->content);
-       is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for count' );
+    my $uri = URI->new($artist_list_url);
+    $uri->query_form(
+        {   'search.name.LIKE'     => '%waul%',
+            'list_returns.0.count' => '*',
+            'as.0'                 => 'count'
+        }
+    );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'attempt with basic count' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('Artist')
+        ->search( { name => { LIKE => '%waul%' } },
+        { select => [ { count => '*' } ], as => ['count'] } )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for count'
+    );
 }
 
 {
-       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::Any->Load( $mech->content);
-       is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with list_returns specified' );
+    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->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for class with list_returns specified'
+    );
 }
 
-
 {
-       my $uri = URI->new( $artist_list_url );
-       $uri->query_form({ 'search.cds.title' => 'Forkful of bees' });  
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'search related request okay' );
-
-       my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { join => 'cds' })->all;
-       my $response = JSON::Any->Load( $mech->content);
-       is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with list_returns specified' );
+    my $uri = URI->new($artist_list_url);
+    $uri->query_form( { 'search.cds.title' => 'Forkful of bees' } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('Artist')
+        ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } )
+        ->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for class with list_returns specified'
+    );
 }
 
 {
-       my $uri = URI->new( $track_list_url );
-       $uri->query_form({ 'list_ordered_by' => 'position' });  
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'search related request okay' );
-
-       my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position' })->all;
-       my $response = JSON::Any->Load( $mech->content);
-       is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with everything specified in class' );
+    my $uri = URI->new($track_list_url);
+    $uri->query_form( { 'list_ordered_by' => 'position' } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('Track')->search(
+        {},
+        {   group_by => 'position',
+            order_by => 'position ASC',
+            select   => 'position'
+        }
+        )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for class with everything specified in class'
+    );
 }
 
 {
-       my $uri = URI->new( $track_list_url );
-       $uri->query_form({ 'list_ordered_by' => 'cd', 'list_returns' => 'cd', 'list_grouped_by' => 'cd' });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'search related request okay' );
-
-       my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'cd', order_by => 'cd ASC', select => 'cd' })->all;
-       my $response = JSON::Any->Load( $mech->content);
-       is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned when everything overridden in query' );
+    my $uri = URI->new($track_list_url);
+    $uri->query_form(
+        {   'list_ordered_by' => 'cd',
+            'list_returns'    => 'cd',
+            'list_grouped_by' => 'cd'
+        }
+    );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('Track')
+        ->search( {},
+        { group_by => 'cd', order_by => 'cd ASC', select => 'cd' } )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned when everything overridden in query'
+    );
 }
 
 {
-       my $uri = URI->new( $track_list_url );
-       $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 2 });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'list count request okay' );
-
-       my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position', rows => 2 })->all;
-       my $response = JSON::Any->Load( $mech->content);
-       is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned' );
+    my $uri = URI->new($track_list_url);
+    $uri->query_form( { 'list_ordered_by' => 'cd', 'list_count' => 2 } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'list count request okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('Track')->search(
+        {},
+        {   group_by => 'position',
+            order_by => 'position ASC',
+            select   => 'position',
+            rows     => 2
+        }
+        )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned'
+    );
 }
 
 {
-       my $uri = URI->new( $track_list_url );
-       $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 2, 'list_page' => 'fgdg' });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 400, 'non numeric list_page request not okay' );
-       my $response = JSON::Any->Load( $mech->content);
-       is($response->{success}, 'false', 'correct data returned');
-    like($response->{messages}->[0], qr/Attribute \(page\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value fgdg/, 'correct data returned');
+    my $uri = URI->new($track_list_url);
+    $uri->query_form(
+        {   'list_ordered_by' => 'cd',
+            'list_count'      => 2,
+            'list_page'       => 'fgdg'
+        }
+    );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400,
+        'non numeric list_page request not okay' );
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, 'false', 'correct data returned' );
+    like(
+        $response->{messages}->[0],
+        qr/Attribute \(page\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value fgdg/,
+        'correct data returned'
+    );
 }
 
 {
-       my $uri = URI->new( $track_list_url );
-       $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 'sdsdf', 'list_page' => 2 });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 400, 'non numeric list_count request not okay' );
-       my $response = JSON::Any->Load( $mech->content);
-       is($response->{success}, 'false', 'correct data returned');
-    like($response->{messages}->[0], qr/Attribute \(count\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value sdsdf/, 'correct data returned');
-    
+    my $uri = URI->new($track_list_url);
+    $uri->query_form(
+        {   'list_ordered_by' => 'cd',
+            'list_count'      => 'sdsdf',
+            'list_page'       => 2
+        }
+    );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400,
+        'non numeric list_count request not okay' );
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, 'false', 'correct data returned' );
+    like(
+        $response->{messages}->[0],
+        qr/Attribute \(count\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value sdsdf/,
+        'correct data returned'
+    );
+
 }
 
 {
-       my $uri = URI->new( $track_list_url );
-       $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 2, 'list_page' => 2 });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'list count with page request okay' );
-
-       my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position', rows => 2, page => 2 })->all;
-       my $response = JSON::Any->Load( $mech->content);
-    is_deeply( $response, { list => \@expected_response, success => 'true', totalcount => 3 }, 'correct data returned' );
+    my $uri = URI->new($track_list_url);
+    $uri->query_form(
+        { 'list_ordered_by' => 'cd', 'list_count' => 2, 'list_page' => 2 } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'list count with page request okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('Track')->search(
+        {},
+        {   group_by => 'position',
+            order_by => 'position ASC',
+            select   => 'position',
+            rows     => 2,
+            page     => 2
+        }
+        )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true', totalcount => 3 },
+        'correct data returned'
+    );
 }
 
 {
-       my $uri = URI->new( $track_list_url );
-       $uri->query_form({ 'list_ordered_by' => 'cd', 'list_page' => 2 });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 400, 'list page without count returns error' );
-       my $response = JSON::Any->Load( $mech->content);
-       like( $response->{messages}->[0], qr/a database error has occured/, 'correct data returned' );
+    my $uri = URI->new($track_list_url);
+    $uri->query_form( { 'list_ordered_by' => 'cd', 'list_page' => 2 } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400,
+        'list page without count returns error' );
+    my $response = $json->decode( $mech->content );
+    like(
+        $response->{messages}->[0],
+        qr/a database error has occured/,
+        'correct data returned'
+    );
 }
 
 {
-       my $uri = URI->new( $cd_list_url );
-       $uri->query_form({ 'search.artist.name' => 'Caterwauler McCrae' });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       if (cmp_ok( $mech->status, '==', 200, 'search on rel with same name column request okay' )) {
-               my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({'me.artist' => 1})->all;
-               my $response = JSON::Any->Load( $mech->content);
-               is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for search on rel with same name column' );
-       }
+    my $uri = URI->new($cd_list_url);
+    $uri->query_form( { 'search.artist.name' => 'Caterwauler McCrae' } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    if (cmp_ok(
+            $mech->status, '==', 200,
+            'search on rel with same name column request okay'
+        )
+        )
+    {
+        my @expected_response = map {
+            { $_->get_columns }
+        } $schema->resultset('CD')->search( { 'me.artist' => 1 } )->all;
+        my $response = $json->decode( $mech->content );
+        is_deeply(
+            $response,
+            { list => \@expected_response, success => 'true' },
+            'correct data returned for search on rel with same name column'
+        );
+    }
 }
 
 {
-       my $uri = URI->new( $cd_list_url );
-       $uri->query_form({ 'search.artist' => 1 });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'search on column with same name rel request okay' );
-
-       my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({'me.artist' => 1})->all;
-       my $response = JSON::Any->Load( $mech->content);
-       is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for search on column with same name rel' );
+    my $uri = URI->new($cd_list_url);
+    $uri->query_form( { 'search.artist' => 1 } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200,
+        'search on column with same name rel request okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+    } $schema->resultset('CD')->search( { 'me.artist' => 1 } )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct data returned for search on column with same name rel'
+    );
 }
 
 {
-       my $uri = URI->new( $cd_list_url );
-       $uri->query_form({ 'search.title' => 'Spoonful of bees', 'search.tracks.position' => 1 });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       if (cmp_ok( $mech->status, '==', 200, 'search on col which exists for me and related table okay' )) {
-               my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({'me.title' => 'Spoonful of bees', 'tracks.position' => 1}, { join => 'tracks' })->all;
-               my $response = JSON::Any->Load( $mech->content);
-               is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for search on col which exists for me and related table' );
-       }
+    my $uri = URI->new($cd_list_url);
+    $uri->query_form(
+        {   'search.title'           => 'Spoonful of bees',
+            'search.tracks.position' => 1
+        }
+    );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    if (cmp_ok(
+            $mech->status, '==', 200,
+            'search on col which exists for me and related table okay'
+        )
+        )
+    {
+        my @expected_response = map {
+            { $_->get_columns }
+            } $schema->resultset('CD')
+            ->search(
+            { 'me.title' => 'Spoonful of bees', 'tracks.position' => 1 },
+            { join       => 'tracks' } )->all;
+        my $response = $json->decode( $mech->content );
+        is_deeply(
+            $response,
+            { list => \@expected_response, success => 'true' },
+            'correct data returned for search on col which exists for me and related table'
+        );
+    }
 }
 
 {
-       my $uri = URI->new( $cd_list_url );
-       $uri->query_form({ 'list_ordered_by' => 'invalid_column' });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       if (cmp_ok( $mech->status, '==', 400, 'order_by on non-existing col returns error' )) {
-               my $response = JSON::Any->Load( $mech->content);
-               is_deeply( $response, { messages => ['a database error has occured.'], success => 'false' },
-            'error returned for order_by on non-existing col' );
-       }
+    my $uri = URI->new($cd_list_url);
+    $uri->query_form( { 'list_ordered_by' => 'invalid_column' } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    if (cmp_ok(
+            $mech->status, '==', 400,
+            'order_by on non-existing col returns error'
+        )
+        )
+    {
+        my $response = $json->decode( $mech->content );
+        is_deeply(
+            $response,
+            {   messages => ['a database error has occured.'],
+                success  => 'false'
+            },
+            'error returned for order_by on non-existing col'
+        );
+    }
 }
 
 {
-       my $uri = URI->new( $cd_list_url );
-       $uri->query_form({ 'list_ordered_by' => 'invalid_column', 'list_count' => 2, 'list_page' => 1 });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       if (cmp_ok( $mech->status, '==', 400, 'order_by on invalid col with paging returns error' )) {
-               my $response = JSON::Any->Load( $mech->content);
-               is_deeply( $response, { messages => ['a database error has occured.'], success => 'false' },
-            'error returned for order_by on non-existing col with paging' );
-       }
+    my $uri = URI->new($cd_list_url);
+    $uri->query_form(
+        {   'list_ordered_by' => 'invalid_column',
+            'list_count'      => 2,
+            'list_page'       => 1
+        }
+    );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    if (cmp_ok(
+            $mech->status, '==', 400,
+            'order_by on invalid col with paging returns error'
+        )
+        )
+    {
+        my $response = $json->decode( $mech->content );
+        is_deeply(
+            $response,
+            {   messages => ['a database error has occured.'],
+                success  => 'false'
+            },
+            'error returned for order_by on non-existing col with paging'
+        );
+    }
 }
 
 done_testing();
index 089f5b7..9a90cb9 100644 (file)
@@ -13,58 +13,84 @@ use URI;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+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 $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::Any->Load( $mech->content);
-    is($response->{success}, 'false', 'correct data returned for gibberish in search' );
-       like($response->{messages}->[0], qr/Attribute \(search\) does not pass the type constraint because/, 'correct data returned for gibberish in search' );
+    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->decode( $mech->content );
+    is( $response->{success}, 'false',
+        'correct data returned for gibberish in search' );
+    like(
+        $response->{messages}->[0],
+        qr/Attribute \(search\) does not pass the type constraint because/,
+        '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::Any->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' => '{"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->decode( $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::Any->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->decode( $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.name' => '{"LIKE":"%waul%"}' });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       cmp_ok( $mech->status, '==', 200, 'attempt with mixed CGI::Expand + JSON search okay' );
-       
-       my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }})->all;
-       my $response = JSON::Any->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.name' => '{"LIKE":"%waul%"}' } );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200,
+        'attempt with mixed CGI::Expand + JSON search okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+        } $schema->resultset('Artist')
+        ->search( { name => { LIKE => '%waul%' } } )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply( { list => \@expected_response, success => 'true' },
+        $response, 'correct data returned for complex query' );
 }
 
 done_testing();
index bb82da7..11f7a89 100644 (file)
@@ -13,66 +13,111 @@ use URI;
 use Test::More tests => 17;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+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";
+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->decode( $mech->content );
 
-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::Any->Load( $mech->content);
-       #use Data::Dumper; warn Dumper($response, $expected_response);
-       is_deeply( $expected_response, $response, 'correct data returned for search with simple prefetch specified as param' );
+    #use Data::Dumper; warn Dumper($response, $expected_response);
+    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::Any->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'     => '{"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->decode( $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' );
+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->decode( $mech->content );
 
-       my $expected_response = map { { $_->get_columns } } $schema->resultset('CD')->all;
-       my $response = JSON::Any->Load( $mech->content);
-       #use Data::Dumper; warn Dumper($response, $expected_response);
-    is($response->{success}, 'false', 'correct message returned' );
-       like($response->{messages}->[0], qr/not an allowed prefetch in:/, 'correct message returned' );
+    #use Data::Dumper; warn Dumper($response, $expected_response);
+    is( $response->{success}, 'false', 'correct message returned' );
+    like(
+        $response->{messages}->[0],
+        qr/not an allowed prefetch in:/,
+        'correct message returned'
+    );
 }
 
 {
-       my $uri = URI->new( $cd_list_url );
-       $uri->query_form({ 'list_prefetch' => 'tracks', 'list_ordered_by' => 'title', 'list_count' => 2, 'list_page' => 1 });
-       my $req = GET( $uri, 'Accept' => 'text/x-json' );
-       $mech->request($req);
-       if (cmp_ok( $mech->status, '==', 400, 'order_by on non-unique col with paging returns error' )) {
-               my $response = JSON::Any->Load( $mech->content);
-               is_deeply( $response, { messages => ['a database error has occured.'], success => 'false' },
-            'error returned for order_by on non-existing col with paging' );
-       }
+    my $uri = URI->new($cd_list_url);
+    $uri->query_form(
+        {   'list_prefetch'   => 'tracks',
+            'list_ordered_by' => 'title',
+            'list_count'      => 2,
+            'list_page'       => 1
+        }
+    );
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    if (cmp_ok(
+            $mech->status, '==', 400,
+            'order_by on non-unique col with paging returns error'
+        )
+        )
+    {
+        my $response = $json->decode( $mech->content );
+        is_deeply(
+            $response,
+            {   messages => ['a database error has occured.'],
+                success  => 'false'
+            },
+            'error returned for order_by on non-existing col with paging'
+        );
+    }
 }
index c623fab..28bbdaf 100644 (file)
@@ -13,120 +13,220 @@ use URI;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
 my $track_list_url = "$base/api/rpc/track_exposed/list";
-my $base_rs = $schema->resultset('Track')->search({}, { select => [qw/me.title me.position/], order_by => 'position' });
+my $base_rs =
+    $schema->resultset('Track')
+    ->search( {},
+    { select => [qw/me.title me.position/], order_by => 'position' } );
 
 # test open request
 {
-  my $req = GET( $track_list_url, {
-               
-  }, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
-
-  my @expected_response = map { { $_->get_columns } } $base_rs->all;
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct message returned' );
+    my $req = GET(
+        $track_list_url,
+        {
+
+        },
+        'Accept' => 'text/x-json'
+    );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+    } $base_rs->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct message returned'
+    );
 }
 
 {
-  my $uri = URI->new( $track_list_url );
-  $uri->query_form({ 'search.position' => 1 });
-  my $req = GET( $uri, {
-               
-  }, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'search on position okay' );
-  my @expected_response = map { { $_->get_columns } } $base_rs->search({ position => 1 })->all;
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct message returned' );
+    my $uri = URI->new($track_list_url);
+    $uri->query_form( { 'search.position' => 1 } );
+    my $req = GET(
+        $uri,
+        {
+
+        },
+        'Accept' => 'text/x-json'
+    );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'search on position okay' );
+    my @expected_response = map {
+        { $_->get_columns }
+    } $base_rs->search( { position => 1 } )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct message returned'
+    );
 }
 
 {
-  my $uri = URI->new( $track_list_url );
-  $uri->query_form({ 'search.title' => 'Stripy' });
-  my $req = GET( $uri, {
-               
-  }, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 400, 'search on title not okay' );
-
-  my @expected_response = map { { $_->get_columns } } $base_rs->search({ position => 1 })->all;
-  my $response = JSON::Any->Load( $mech->content);
-  is($response->{success}, 'false', 'correct message returned');
-  like($response->{messages}->[0], qr/is not an allowed search term/, 'correct message returned');
+    my $uri = URI->new($track_list_url);
+    $uri->query_form( { 'search.title' => 'Stripy' } );
+    my $req = GET(
+        $uri,
+        {
+
+        },
+        'Accept' => 'text/x-json'
+    );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400, 'search on title not okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+    } $base_rs->search( { position => 1 } )->all;
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, 'false', 'correct message returned' );
+    like(
+        $response->{messages}->[0],
+        qr/is not an allowed search term/,
+        'correct message returned'
+    );
 }
 
 {
-  my $uri = URI->new( $track_list_url );
-  $uri->query_form({ 'search.title' => 'Stripy' });
-  my $req = GET( $uri, {
-               
-  }, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 400, 'search on title not okay' );
-
-  my $expected_response = map { { $_->get_columns } } $base_rs->search({ position => 1 })->all;
-  my $response = JSON::Any->Load( $mech->content);
-  is($response->{success}, 'false', 'correct message returned');
-  like($response->{messages}->[0], qr/is not an allowed search term/, 'correct message returned');
+    my $uri = URI->new($track_list_url);
+    $uri->query_form( { 'search.title' => 'Stripy' } );
+    my $req = GET(
+        $uri,
+        {
+
+        },
+        'Accept' => 'text/x-json'
+    );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400, 'search on title not okay' );
+
+    my $expected_response = map {
+        { $_->get_columns }
+    } $base_rs->search( { position => 1 } )->all;
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, 'false', 'correct message returned' );
+    like(
+        $response->{messages}->[0],
+        qr/is not an allowed search term/,
+        'correct message returned'
+    );
 }
 
 {
-  my $uri = URI->new( $track_list_url );
-  $uri->query_form({ 'search.cd.artist' => '1' });
-  my $req = GET( $uri, {
-               
-  }, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 400, 'search on various cd fields not okay' );
-  my $response = JSON::Any->Load( $mech->content);
-  is($response->{success}, 'false', 'correct message returned');
-  like($response->{messages}->[0], qr/is not an allowed search term/, 'correct message returned');
+    my $uri = URI->new($track_list_url);
+    $uri->query_form( { 'search.cd.artist' => '1' } );
+    my $req = GET(
+        $uri,
+        {
+
+        },
+        'Accept' => 'text/x-json'
+    );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400,
+        'search on various cd fields not okay' );
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, 'false', 'correct message returned' );
+    like(
+        $response->{messages}->[0],
+        qr/is not an allowed search term/,
+        'correct message returned'
+    );
 }
 
 {
-  my $uri = URI->new( $track_list_url );
-  $uri->query_form({ 'search.cd.title' => 'Spoonful of bees', 'search.cd.year' => '1999' });
-  my $req = GET( $uri, {
-               
-  }, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'search on various cd fields okay' );
-  my @expected_response = map { { $_->get_columns } } $base_rs->search({ 'cd.year' => '1999', 'cd.title' => 'Spoonful of bees' }, { join => 'cd' })->all;
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply($response, { list => \@expected_response, success => 'true' }, 'correct message returned' );
+    my $uri = URI->new($track_list_url);
+    $uri->query_form(
+        {   'search.cd.title' => 'Spoonful of bees',
+            'search.cd.year'  => '1999'
+        }
+    );
+    my $req = GET(
+        $uri,
+        {
+
+        },
+        'Accept' => 'text/x-json'
+    );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'search on various cd fields okay' );
+    my @expected_response = map {
+        { $_->get_columns }
+        } $base_rs->search(
+        { 'cd.year' => '1999', 'cd.title' => 'Spoonful of bees' },
+        { join      => 'cd' } )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct message returned'
+    );
 }
 
 {
-  my $uri = URI->new( $track_list_url );
-  $uri->query_form({ 'search.cd.title' => 'Spoonful of bees', 'search.cd.pretend' => '1999' });
-  my $req = GET( $uri, {
-               
-  }, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'search with custom col okay' );
-  my @expected_response = map { { $_->get_columns } } $base_rs->search({ 'cd.year' => '1999', 'cd.title' => 'Spoonful of bees' }, { join => 'cd' })->all;
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply($response, { list => \@expected_response, success => 'true' }, 'correct message returned' );
+    my $uri = URI->new($track_list_url);
+    $uri->query_form(
+        {   'search.cd.title'   => 'Spoonful of bees',
+            'search.cd.pretend' => '1999'
+        }
+    );
+    my $req = GET(
+        $uri,
+        {
+
+        },
+        'Accept' => 'text/x-json'
+    );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'search with custom col okay' );
+    my @expected_response = map {
+        { $_->get_columns }
+        } $base_rs->search(
+        { 'cd.year' => '1999', 'cd.title' => 'Spoonful of bees' },
+        { join      => 'cd' } )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct message returned'
+    );
 }
 
 {
-  my $uri = URI->new( $track_list_url );
-  $uri->query_form({ 'search.cd.artist.name' => 'Random Boy Band' });
-  my $req = GET( $uri, {
-               
-  }, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'search on artist field okay due to wildcard' );
-  my @expected_response = map { { $_->get_columns } } $base_rs->search({ 'artist.name' => 'Random Boy Band' }, { join => { cd => 'artist' } })->all;
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply($response, { list => \@expected_response, success => 'true' }, 'correct message returned' );
+    my $uri = URI->new($track_list_url);
+    $uri->query_form( { 'search.cd.artist.name' => 'Random Boy Band' } );
+    my $req = GET(
+        $uri,
+        {
+
+        },
+        'Accept' => 'text/x-json'
+    );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200,
+        'search on artist field okay due to wildcard' );
+    my @expected_response = map {
+        { $_->get_columns }
+        } $base_rs->search(
+        { 'artist.name' => 'Random Boy Band' },
+        { join          => { cd => 'artist' } }
+        )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+        { list => \@expected_response, success => 'true' },
+        'correct message returned'
+    );
 }
 
 done_testing();
index faa28dd..a1615e3 100644 (file)
@@ -13,25 +13,37 @@ use URI;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
 my $track_list_url = "$base/api/rpc/track_setup_dbic_args/list";
-my $base_rs = $schema->resultset('Track')->search({}, { select => [qw/me.title me.position/], order_by => 'position' });
+my $base_rs =
+    $schema->resultset('Track')
+    ->search( {},
+    { select => [qw/me.title me.position/], order_by => 'position' } );
 
 # test open request
 {
-  my $req = GET( $track_list_url, {
-               
-  }, 'Accept' => 'text/x-json' );
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
-
-  my @expected_response = map { { $_->get_columns } } $base_rs->search({ position => { '!=' => '1' } })->all;
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct message returned' );
+    my $req = GET(
+        $track_list_url,
+        {
+
+        },
+        'Accept' => 'text/x-json'
+    );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
+
+    my @expected_response = map {
+        { $_->get_columns }
+    } $base_rs->search( { position => { '!=' => '1' } } )->all;
+    my $response = $json->decode( $mech->content );
+    is_deeply( { list => \@expected_response, success => 'true' },
+        $response, 'correct message returned' );
 }
 
 done_testing();
index 37bdbe9..29dbf9d 100644 (file)
@@ -13,151 +13,184 @@ use DBICTest;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON::Any;
+use JSON;
+
+my $json = JSON->new->utf8;
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
-ok(my $schema = DBICTest->init_schema(), 'got schema');
+ok( my $schema = DBICTest->init_schema(), 'got schema' );
 
-my $track = $schema->resultset('Track')->first;
+my $track         = $schema->resultset('Track')->first;
 my %original_cols = $track->get_columns;
 
 my $track_update_url = "$base/api/rpc/track/id/" . $track->id . "/update";
-my $any_track_update_url = "$base/api/rpc/any/track/id/" . $track->id . "/update";
+my $any_track_update_url =
+    "$base/api/rpc/any/track/id/" . $track->id . "/update";
 my $tracks_update_url = "$base/api/rpc/track/update";
 
 # test invalid track id caught
 {
-               foreach my $wrong_id ('sdsdsdsd', 3434234) {
-                       my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
-                       my $req = POST( $incorrect_url, {
-                       title => 'value'
-               });
-
-               $mech->request($req, $content_type);
-               cmp_ok( $mech->status, '==', 400, 'Attempt with invalid track id caught' );
-               
-               my $response = JSON::Any->Load( $mech->content);
-               like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
-               
-               $track->discard_changes;
-               is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
-       }
+    foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
+        my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
+        my $req = POST( $incorrect_url, { title => 'value' } );
+
+        $mech->request( $req, $content_type );
+        cmp_ok( $mech->status, '==', 400,
+            'Attempt with invalid track id caught' );
+
+        my $response = $json->decode( $mech->content );
+        like(
+            $response->{messages}->[0],
+            qr/No object found for id/,
+            'correct message returned'
+        );
+
+        $track->discard_changes;
+        is_deeply(
+            { $track->get_columns },
+            \%original_cols,
+            'no update occurred'
+        );
+    }
 }
 
 # validation when no params sent
 {
-  my $req = POST( $track_update_url, {
-         wrong_param => 'value'
-  });
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
-
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
-
-  $track->discard_changes;
-  is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
+    my $req = POST( $track_update_url, { wrong_param => 'value' } );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
+
+    my $response = $json->decode( $mech->content );
+    is_deeply( $response->{messages}, ['No valid keys passed'],
+        'correct message returned' );
+
+    $track->discard_changes;
+    is_deeply(
+        { $track->get_columns },
+        \%original_cols,
+        'no update occurred'
+    );
 }
 
 {
-  my $req = POST( $track_update_url, {
-         wrong_param => 'value'
-  });
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
-
-  my $response = JSON::Any->Load( $mech->content);
-  is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
-
-  $track->discard_changes;
-  is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
+    my $req = POST( $track_update_url, { wrong_param => 'value' } );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
+
+    my $response = $json->decode( $mech->content );
+    is_deeply( $response->{messages}, ['No valid keys passed'],
+        'correct message returned' );
+
+    $track->discard_changes;
+    is_deeply(
+        { $track->get_columns },
+        \%original_cols,
+        'no update occurred'
+    );
 }
 
 {
-  my $req = POST( $track_update_url, {
-         title => undef
-  });
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
-
-  $track->discard_changes;
-  isnt($track->title, $original_cols{title}, 'Title changed');
-  is($track->title, '', 'Title changed to undef');
+    my $req = POST( $track_update_url, { title => undef } );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
+
+    $track->discard_changes;
+    isnt( $track->title, $original_cols{title}, 'Title changed' );
+    is( $track->title, '', 'Title changed to undef' );
 }
 
 {
-  my $req = POST( $track_update_url, {
-         title => 'monkey monkey'
-  });
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
-
-  $track->discard_changes;
-  is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
+    my $req = POST( $track_update_url, { title => 'monkey monkey' } );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
+
+    $track->discard_changes;
+    is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
 }
 
 {
-  my $req = POST( $track_update_url, {
-         title => 'sheep sheep',
-         'cd.year' => '2009'
-  });
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 200, 'Update with key with value and related key okay' );
-
-  $track->discard_changes;
-  is($track->title, 'sheep sheep', 'Title changed');
-  is($track->cd->year, '2009', 'Related field changed"');
+    my $req = POST(
+        $track_update_url,
+        {   title     => 'sheep sheep',
+            'cd.year' => '2009'
+        }
+    );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 200,
+        'Update with key with value and related key okay' );
+
+    $track->discard_changes;
+    is( $track->title,    'sheep sheep', 'Title changed' );
+    is( $track->cd->year, '2009',        'Related field changed"' );
 }
 
 {
-  my $req = POST( $any_track_update_url, {
-         title => 'baa'
-  });
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
-
-  $track->discard_changes;
-  is($track->title, 'baa', 'Title changed');
+    my $req = POST( $any_track_update_url, { title => 'baa' } );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
+
+    $track->discard_changes;
+    is( $track->title, 'baa', 'Title changed' );
 }
 
 {
-  my $req = POST( $any_track_update_url, {
-         position => '14'
-  });
-  $mech->request($req, $content_type);
-  cmp_ok( $mech->status, '==', 200, 'Position update okay' );
-
-  $track->discard_changes;
-  is($track->get_column('position'), '14', 'Position changed');
+    my $req = POST( $any_track_update_url, { position => '14' } );
+    $mech->request( $req, $content_type );
+    cmp_ok( $mech->status, '==', 200, 'Position update okay' );
+
+    $track->discard_changes;
+    is( $track->get_column('position'), '14', 'Position changed' );
 }
 
 # bulk_update existing objects
 {
-  # order to get a stable order of rows
-  my $tracks_rs = $schema->resultset('Track')->search(undef, { order_by => 'trackid', rows => 3 });
-  my $test_data = JSON::Any->Dump({ list => [map +{ id => $_->id, title => 'Track ' . $_->id }, $tracks_rs->all] });
-  my $req = POST( $tracks_update_url, Content => $test_data );
-  $req->content_type('text/x-json');
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
-
-  $tracks_rs->reset;
-  while (my $track = $tracks_rs->next) {
-    is($track->title, 'Track ' . $track->id, 'Title changed');
-  }
+
+    # order to get a stable order of rows
+    my $tracks_rs =
+        $schema->resultset('Track')
+        ->search( undef, { order_by => 'trackid', rows => 3 } );
+    my $test_data = $json->encode(
+        {   list => [
+                map +{ id => $_->id, title => 'Track ' . $_->id },
+                $tracks_rs->all
+            ]
+        }
+    );
+    my $req = POST( $tracks_update_url, Content => $test_data );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
+
+    $tracks_rs->reset;
+    while ( my $track = $tracks_rs->next ) {
+        is( $track->title, 'Track ' . $track->id, 'Title changed' );
+    }
 }
 
 # bulk_update nonexisting objects
 {
-  # order to get a stable order of rows
-  my $test_data = JSON::Any->Dump({ list => [map +{ id => $_, title => 'Track ' . $_ }, (1000..1002)] });
-  my $req = POST( $tracks_update_url, Content => $test_data );
-  $req->content_type('text/x-json');
-  $mech->request($req);
-  cmp_ok( $mech->status, '==', 400, 'Attempt to update three nonexisting tracks fails' );
-  my $response = JSON::Any->Load( $mech->content);
-  is( $response->{success}, 'false', 'success property returns quoted false' );
-  like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
+
+    # order to get a stable order of rows
+    my $test_data = $json->encode(
+        {   list => [
+                map +{ id => $_, title => 'Track ' . $_ },
+                ( 1000 .. 1002 )
+            ]
+        }
+    );
+    my $req = POST( $tracks_update_url, Content => $test_data );
+    $req->content_type('text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400,
+        'Attempt to update three nonexisting tracks fails' );
+    my $response = $json->decode( $mech->content );
+    is( $response->{success}, 'false',
+        'success property returns quoted false' );
+    like(
+        $response->{messages}->[0],
+        qr/No object found for id/,
+        'correct message returned'
+    );
 }
 
 done_testing();