use JSON instead of JSON::Any to get rid of the CPAN Testers failures when only JSON...
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / list_prefetch.t
1 use 5.6.0;
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7
8 my $base = 'http://localhost';
9
10 use RestTest;
11 use DBICTest;
12 use URI;
13 use Test::More tests => 17;
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
16 use JSON;
17
18 my $json = JSON->new->utf8;
19
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
22
23 my $artist_list_url = "$base/api/rpc/artist/list";
24 my $cd_list_url     = "$base/api/rpc/cd/list";
25
26 foreach my $req_params ( { 'list_prefetch' => '["cds"]' },
27     { 'list_prefetch' => 'cds' } )
28 {
29     my $uri = URI->new($artist_list_url);
30     $uri->query_form($req_params);
31     my $req = GET( $uri, 'Accept' => 'text/x-json' );
32     $mech->request($req);
33     cmp_ok( $mech->status, '==', 200,
34         'search with simple prefetch request okay' );
35     my $rs =
36         $schema->resultset('Artist')
37         ->search( undef, { prefetch => ['cds'] } );
38     $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
39     my @rows              = $rs->all;
40     my $expected_response = { list => \@rows, success => 'true' };
41     my $response          = $json->decode( $mech->content );
42
43     #use Data::Dumper; warn Dumper($response, $expected_response);
44     is_deeply( $expected_response, $response,
45         'correct data returned for search with simple prefetch specified as param'
46     );
47 }
48
49 foreach my $req_params (
50     { 'list_prefetch'     => '{"cds":"tracks"}' },
51     { 'list_prefetch.cds' => 'tracks' }
52     )
53 {
54     my $uri = URI->new($artist_list_url);
55     $uri->query_form($req_params);
56     my $req = GET( $uri, 'Accept' => 'text/x-json' );
57     $mech->request($req);
58     cmp_ok( $mech->status, '==', 200,
59         'search with multi-level prefetch request okay' );
60     my $rs =
61         $schema->resultset('Artist')
62         ->search( undef, { prefetch => { 'cds' => 'tracks' } } );
63     $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
64     my @rows              = $rs->all;
65     my $expected_response = { list => \@rows, success => 'true' };
66     my $response          = $json->decode( $mech->content );
67
68     #use Data::Dumper; warn Dumper($response, $expected_response);
69     is_deeply( $expected_response, $response,
70         'correct data returned for search with multi-level prefetch specified as param'
71     );
72 }
73
74 foreach my $req_params ( { 'list_prefetch' => '["artist"]' },
75     { 'list_prefetch' => 'artist' } )
76 {
77     my $uri = URI->new($cd_list_url);
78     $uri->query_form($req_params);
79     my $req = GET( $uri, 'Accept' => 'text/x-json' );
80     $mech->request($req);
81     cmp_ok( $mech->status, '==', 400, 'prefetch of artist not okay' );
82
83     my $expected_response = map {
84         { $_->get_columns }
85     } $schema->resultset('CD')->all;
86     my $response = $json->decode( $mech->content );
87
88     #use Data::Dumper; warn Dumper($response, $expected_response);
89     is( $response->{success}, 'false', 'correct message returned' );
90     like(
91         $response->{messages}->[0],
92         qr/not an allowed prefetch in:/,
93         'correct message returned'
94     );
95 }
96
97 {
98     my $uri = URI->new($cd_list_url);
99     $uri->query_form(
100         {   'list_prefetch'   => 'tracks',
101             'list_ordered_by' => 'title',
102             'list_count'      => 2,
103             'list_page'       => 1
104         }
105     );
106     my $req = GET( $uri, 'Accept' => 'text/x-json' );
107     $mech->request($req);
108     if (cmp_ok(
109             $mech->status, '==', 400,
110             'order_by on non-unique col with paging returns error'
111         )
112         )
113     {
114         my $response = $json->decode( $mech->content );
115         is_deeply(
116             $response,
117             {   messages => ['a database error has occured.'],
118                 success  => 'false'
119             },
120             'error returned for order_by on non-existing col with paging'
121         );
122     }
123 }