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