6 my $base = 'http://localhost';
11 use Test::More tests => 17;
12 use Test::WWW::Mechanize::Catalyst 'RestTest';
13 use HTTP::Request::Common;
16 my $json = JSON->new->utf8;
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok( my $schema = DBICTest->init_schema(), 'got schema' );
21 my $artist_list_url = "$base/api/rpc/artist/list";
22 my $cd_list_url = "$base/api/rpc/cd/list";
24 foreach my $req_params ( { 'list_prefetch' => '["cds"]' },
25 { 'list_prefetch' => 'cds' } )
27 my $uri = URI->new($artist_list_url);
28 $uri->query_form($req_params);
29 my $req = GET( $uri, 'Accept' => 'text/x-json' );
31 cmp_ok( $mech->status, '==', 200,
32 'search with simple prefetch request okay' );
34 $schema->resultset('Artist')
35 ->search( undef, { prefetch => ['cds'] } );
36 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
38 my $expected_response = { list => \@rows, success => 'true' };
39 my $response = $json->decode( $mech->content );
41 #use Data::Dumper; warn Dumper($response, $expected_response);
42 is_deeply( $expected_response, $response,
43 'correct data returned for search with simple prefetch specified as param'
47 foreach my $req_params (
48 { 'list_prefetch' => '{"cds":"tracks"}' },
49 { 'list_prefetch.cds' => 'tracks' }
52 my $uri = URI->new($artist_list_url);
53 $uri->query_form($req_params);
54 my $req = GET( $uri, 'Accept' => 'text/x-json' );
56 cmp_ok( $mech->status, '==', 200,
57 'search with multi-level prefetch request okay' );
59 $schema->resultset('Artist')
60 ->search( undef, { prefetch => { 'cds' => 'tracks' } } );
61 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
63 my $expected_response = { list => \@rows, success => 'true' };
64 my $response = $json->decode( $mech->content );
66 #use Data::Dumper; warn Dumper($response, $expected_response);
67 is_deeply( $expected_response, $response,
68 'correct data returned for search with multi-level prefetch specified as param'
72 foreach my $req_params ( { 'list_prefetch' => '["artist"]' },
73 { 'list_prefetch' => 'artist' } )
75 my $uri = URI->new($cd_list_url);
76 $uri->query_form($req_params);
77 my $req = GET( $uri, 'Accept' => 'text/x-json' );
79 cmp_ok( $mech->status, '==', 400, 'prefetch of artist not okay' );
81 my $expected_response = map {
83 } $schema->resultset('CD')->all;
84 my $response = $json->decode( $mech->content );
86 #use Data::Dumper; warn Dumper($response, $expected_response);
87 is( $response->{success}, 'false', 'correct message returned' );
89 $response->{messages}->[0],
90 qr/not an allowed prefetch in:/,
91 'correct message returned'
96 my $uri = URI->new($cd_list_url);
98 { 'list_prefetch' => 'tracks',
99 'list_ordered_by' => 'title',
104 my $req = GET( $uri, 'Accept' => 'text/x-json' );
105 $mech->request($req);
107 $mech->status, '==', 400,
108 'order_by on non-unique col with paging returns error'
112 my $response = $json->decode( $mech->content );
115 { messages => ['a database error has occured.'],
118 'error returned for order_by on non-existing col with paging'