Commit | Line | Data |
d2739840 |
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::Any; |
17 | |
18 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
19 | ok(my $schema = DBICTest->init_schema(), 'got schema'); |
20 | |
21 | my $artist_list_url = "$base/api/rpc/artist/list"; |
22 | my $cd_list_url = "$base/api/rpc/cd/list"; |
23 | |
24 | foreach 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 | |
39 | foreach 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 | |
54 | foreach 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 | } |