cleaned up delete tests
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / list_prefetch.t
CommitLineData
d2739840 1use 5.6.0;
2
3use strict;
4use warnings;
5
6use lib 't/lib';
7
8my $base = 'http://localhost';
9
10use RestTest;
11use DBICTest;
12use URI;
13use Test::More tests => 17;
14use Test::WWW::Mechanize::Catalyst 'RestTest';
15use HTTP::Request::Common;
16use JSON::Any;
17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
19ok(my $schema = DBICTest->init_schema(), 'got schema');
20
21my $artist_list_url = "$base/api/rpc/artist/list";
22my $cd_list_url = "$base/api/rpc/cd/list";
23
24foreach my $req_params ({ 'list_prefetch' => '["cds"]' }, { 'list_prefetch' => 'cds' }) {
25 my $uri = URI->new( $artist_list_url );
26 $uri->query_form($req_params);
27 my $req = GET( $uri, 'Accept' => 'text/x-json' );
28 $mech->request($req);
29 cmp_ok( $mech->status, '==', 200, 'search with simple prefetch request okay' );
30 my $rs = $schema->resultset('Artist')->search(undef, { prefetch => ['cds'] });
31 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
32 my @rows = $rs->all;
33 my $expected_response = { list => \@rows, success => 'true' };
34 my $response = JSON::Any->Load( $mech->content);
35 #use Data::Dumper; warn Dumper($response, $expected_response);
36 is_deeply( $expected_response, $response, 'correct data returned for search with simple prefetch specified as param' );
37}
38
39foreach my $req_params ({ 'list_prefetch' => '{"cds":"tracks"}' }, { 'list_prefetch.cds' => 'tracks' }) {
40 my $uri = URI->new( $artist_list_url );
41 $uri->query_form($req_params);
42 my $req = GET( $uri, 'Accept' => 'text/x-json' );
43 $mech->request($req);
44 cmp_ok( $mech->status, '==', 200, 'search with multi-level prefetch request okay' );
45 my $rs = $schema->resultset('Artist')->search(undef, { prefetch => {'cds' => 'tracks'} });
46 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
47 my @rows = $rs->all;
48 my $expected_response = { list => \@rows, success => 'true' };
49 my $response = JSON::Any->Load( $mech->content);
50 #use Data::Dumper; warn Dumper($response, $expected_response);
51 is_deeply( $expected_response, $response, 'correct data returned for search with multi-level prefetch specified as param' );
52}
53
54foreach my $req_params ({ 'list_prefetch' => '["artist"]' }, { 'list_prefetch' => 'artist' }) {
55 my $uri = URI->new( $cd_list_url );
56 $uri->query_form($req_params);
57 my $req = GET( $uri, 'Accept' => 'text/x-json' );
58 $mech->request($req);
59 cmp_ok( $mech->status, '==', 400, 'prefetch of artist not okay' );
60
61 my $expected_response = map { { $_->get_columns } } $schema->resultset('CD')->all;
62 my $response = JSON::Any->Load( $mech->content);
63 #use Data::Dumper; warn Dumper($response, $expected_response);
64 is($response->{success}, 'false', 'correct message returned' );
65 like($response->{messages}->[0], qr/not an allowed prefetch in:/, 'correct message returned' );
66}
67
68{
69 my $uri = URI->new( $cd_list_url );
70 $uri->query_form({ 'list_prefetch' => 'tracks', 'list_ordered_by' => 'title', 'list_count' => 2, 'list_page' => 1 });
71 my $req = GET( $uri, 'Accept' => 'text/x-json' );
72 $mech->request($req);
73 if (cmp_ok( $mech->status, '==', 400, 'order_by on non-unique col with paging returns error' )) {
74 my $response = JSON::Any->Load( $mech->content);
75 is_deeply( $response, { messages => ['a database error has occured.'], success => 'false' },
76 'error returned for order_by on non-existing col with paging' );
77 }
78}