8 my $base = 'http://localhost';
13 use Test::More tests => 17;
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
18 my $json = JSON->new->utf8;
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
23 my $artist_list_url = "$base/api/rpc/artist/list";
24 my $cd_list_url = "$base/api/rpc/cd/list";
26 foreach my $req_params ( { 'list_prefetch' => '["cds"]' },
27 { 'list_prefetch' => 'cds' } )
29 my $uri = URI->new($artist_list_url);
30 $uri->query_form($req_params);
31 my $req = GET( $uri, 'Accept' => 'text/x-json' );
33 cmp_ok( $mech->status, '==', 200,
34 'search with simple prefetch request okay' );
36 $schema->resultset('Artist')
37 ->search( undef, { prefetch => ['cds'] } );
38 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
40 my $expected_response = { list => \@rows, success => 'true' };
41 my $response = $json->decode( $mech->content );
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'
49 foreach my $req_params (
50 { 'list_prefetch' => '{"cds":"tracks"}' },
51 { 'list_prefetch.cds' => 'tracks' }
54 my $uri = URI->new($artist_list_url);
55 $uri->query_form($req_params);
56 my $req = GET( $uri, 'Accept' => 'text/x-json' );
58 cmp_ok( $mech->status, '==', 200,
59 'search with multi-level prefetch request okay' );
61 $schema->resultset('Artist')
62 ->search( undef, { prefetch => { 'cds' => 'tracks' } } );
63 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
65 my $expected_response = { list => \@rows, success => 'true' };
66 my $response = $json->decode( $mech->content );
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'
74 foreach my $req_params ( { 'list_prefetch' => '["artist"]' },
75 { 'list_prefetch' => 'artist' } )
77 my $uri = URI->new($cd_list_url);
78 $uri->query_form($req_params);
79 my $req = GET( $uri, 'Accept' => 'text/x-json' );
81 cmp_ok( $mech->status, '==', 400, 'prefetch of artist not okay' );
83 my $expected_response = map {
85 } $schema->resultset('CD')->all;
86 my $response = $json->decode( $mech->content );
88 #use Data::Dumper; warn Dumper($response, $expected_response);
89 is( $response->{success}, 'false', 'correct message returned' );
91 $response->{messages}->[0],
92 qr/not an allowed prefetch in:/,
93 'correct message returned'
98 my $uri = URI->new($cd_list_url);
100 { 'list_prefetch' => 'tracks',
101 'list_ordered_by' => 'title',
106 my $req = GET( $uri, 'Accept' => 'text/x-json' );
107 $mech->request($req);
109 $mech->status, '==', 400,
110 'order_by on non-unique col with paging returns error'
114 my $response = $json->decode( $mech->content );
117 { messages => ['a database error has occured.'],
120 'error returned for order_by on non-existing col with paging'