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; |
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 $producer_list_url = "$base/api/rpc/producer/list"; |
23 | my $track_list_url = "$base/api/rpc/track/list"; |
24 | my $cd_list_url = "$base/api/rpc/cd/list"; |
25 | |
26 | # test open request |
27 | { |
28 | my $req = GET( $artist_list_url, { |
29 | |
30 | }, 'Accept' => 'text/x-json' ); |
31 | $mech->request($req); |
32 | cmp_ok( $mech->status, '==', 200, 'open attempt okay' ); |
33 | |
34 | my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->all; |
35 | my $response = JSON::Any->Load( $mech->content); |
36 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct message returned' ); |
37 | } |
38 | |
39 | { |
40 | my $uri = URI->new( $artist_list_url ); |
41 | $uri->query_form({ 'search.artistid' => 1 }); |
42 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
43 | $mech->request($req); |
44 | cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' ); |
45 | |
46 | my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ artistid => 1 })->all; |
47 | my $response = JSON::Any->Load( $mech->content); |
48 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned' ); |
49 | } |
50 | |
51 | { |
52 | my $uri = URI->new( $artist_list_url ); |
53 | $uri->query_form({ 'search.name.LIKE' => '%waul%' }); |
54 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
55 | $mech->request($req); |
56 | cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' ); |
57 | |
58 | my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }})->all; |
59 | my $response = JSON::Any->Load( $mech->content); |
60 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for complex query' ); |
61 | } |
62 | |
63 | { |
64 | my $uri = URI->new( $artist_list_url ); |
65 | $uri->query_form({ 'search.name.LIKE' => '%waul%', 'list_returns.0.count' => '*', 'as.0' => 'count'}); |
66 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
67 | $mech->request($req); |
68 | cmp_ok( $mech->status, '==', 200, 'attempt with basic count' ); |
69 | |
70 | my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }}, { select => [ {count => '*'} ], as => ['count'] } )->all; |
71 | my $response = JSON::Any->Load( $mech->content); |
72 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for count' ); |
73 | } |
74 | |
75 | { |
76 | my $uri = URI->new( $producer_list_url ); |
77 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
78 | $mech->request($req); |
79 | cmp_ok( $mech->status, '==', 200, 'open producer request okay' ); |
80 | |
81 | my @expected_response = map { { $_->get_columns } } $schema->resultset('Producer')->search({}, { select => ['name'] })->all; |
82 | my $response = JSON::Any->Load( $mech->content); |
83 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with list_returns specified' ); |
84 | } |
85 | |
86 | |
87 | { |
88 | my $uri = URI->new( $artist_list_url ); |
89 | $uri->query_form({ 'search.cds.title' => 'Forkful of bees' }); |
90 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
91 | $mech->request($req); |
92 | cmp_ok( $mech->status, '==', 200, 'search related request okay' ); |
93 | |
94 | my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { join => 'cds' })->all; |
95 | my $response = JSON::Any->Load( $mech->content); |
96 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with list_returns specified' ); |
97 | } |
98 | |
99 | { |
100 | my $uri = URI->new( $track_list_url ); |
101 | $uri->query_form({ 'list_ordered_by' => 'position' }); |
102 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
103 | $mech->request($req); |
104 | cmp_ok( $mech->status, '==', 200, 'search related request okay' ); |
105 | |
106 | my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position' })->all; |
107 | my $response = JSON::Any->Load( $mech->content); |
108 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with everything specified in class' ); |
109 | } |
110 | |
111 | { |
112 | my $uri = URI->new( $track_list_url ); |
113 | $uri->query_form({ 'list_ordered_by' => 'cd', 'list_returns' => 'cd', 'list_grouped_by' => 'cd' }); |
114 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
115 | $mech->request($req); |
116 | cmp_ok( $mech->status, '==', 200, 'search related request okay' ); |
117 | |
118 | my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'cd', order_by => 'cd ASC', select => 'cd' })->all; |
119 | my $response = JSON::Any->Load( $mech->content); |
120 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned when everything overridden in query' ); |
121 | } |
122 | |
123 | { |
124 | my $uri = URI->new( $track_list_url ); |
125 | $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 2 }); |
126 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
127 | $mech->request($req); |
128 | cmp_ok( $mech->status, '==', 200, 'list count request okay' ); |
129 | |
130 | my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position', rows => 2 })->all; |
131 | my $response = JSON::Any->Load( $mech->content); |
132 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned' ); |
133 | } |
134 | |
135 | { |
136 | my $uri = URI->new( $track_list_url ); |
137 | $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 2, 'list_page' => 'fgdg' }); |
138 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
139 | $mech->request($req); |
140 | cmp_ok( $mech->status, '==', 400, 'non numeric list_page request not okay' ); |
141 | my $response = JSON::Any->Load( $mech->content); |
142 | is($response->{success}, 'false', 'correct data returned'); |
25448916 |
143 | like($response->{messages}->[0], qr/Attribute \(page\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value fgdg/, 'correct data returned'); |
d2739840 |
144 | } |
145 | |
146 | { |
147 | my $uri = URI->new( $track_list_url ); |
148 | $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 'sdsdf', 'list_page' => 2 }); |
149 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
150 | $mech->request($req); |
151 | cmp_ok( $mech->status, '==', 400, 'non numeric list_count request not okay' ); |
152 | my $response = JSON::Any->Load( $mech->content); |
153 | is($response->{success}, 'false', 'correct data returned'); |
25448916 |
154 | like($response->{messages}->[0], qr/Attribute \(count\) does not pass the type constraint because: Validation failed for 'Int' (failed )?with value sdsdf/, 'correct data returned'); |
d2739840 |
155 | |
156 | } |
157 | |
158 | { |
159 | my $uri = URI->new( $track_list_url ); |
160 | $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 2, 'list_page' => 2 }); |
161 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
162 | $mech->request($req); |
163 | cmp_ok( $mech->status, '==', 200, 'list count with page request okay' ); |
164 | |
165 | my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position', rows => 2, page => 2 })->all; |
166 | my $response = JSON::Any->Load( $mech->content); |
167 | is_deeply( $response, { list => \@expected_response, success => 'true', totalcount => 3 }, 'correct data returned' ); |
168 | } |
169 | |
170 | { |
171 | my $uri = URI->new( $track_list_url ); |
172 | $uri->query_form({ 'list_ordered_by' => 'cd', 'list_page' => 2 }); |
173 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
174 | $mech->request($req); |
175 | cmp_ok( $mech->status, '==', 400, 'list page without count returns error' ); |
176 | my $response = JSON::Any->Load( $mech->content); |
177 | like( $response->{messages}->[0], qr/a database error has occured/, 'correct data returned' ); |
178 | } |
179 | |
180 | { |
181 | my $uri = URI->new( $cd_list_url ); |
182 | $uri->query_form({ 'search.artist.name' => 'Caterwauler McCrae' }); |
183 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
184 | $mech->request($req); |
185 | if (cmp_ok( $mech->status, '==', 200, 'search on rel with same name column request okay' )) { |
186 | my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({'me.artist' => 1})->all; |
187 | my $response = JSON::Any->Load( $mech->content); |
188 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for search on rel with same name column' ); |
189 | } |
190 | } |
191 | |
192 | { |
193 | my $uri = URI->new( $cd_list_url ); |
194 | $uri->query_form({ 'search.artist' => 1 }); |
195 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
196 | $mech->request($req); |
197 | cmp_ok( $mech->status, '==', 200, 'search on column with same name rel request okay' ); |
198 | |
199 | my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({'me.artist' => 1})->all; |
200 | my $response = JSON::Any->Load( $mech->content); |
201 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for search on column with same name rel' ); |
202 | } |
203 | |
204 | { |
205 | my $uri = URI->new( $cd_list_url ); |
206 | $uri->query_form({ 'search.title' => 'Spoonful of bees', 'search.tracks.position' => 1 }); |
207 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
208 | $mech->request($req); |
209 | if (cmp_ok( $mech->status, '==', 200, 'search on col which exists for me and related table okay' )) { |
210 | my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({'me.title' => 'Spoonful of bees', 'tracks.position' => 1}, { join => 'tracks' })->all; |
211 | my $response = JSON::Any->Load( $mech->content); |
212 | is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for search on col which exists for me and related table' ); |
213 | } |
214 | } |
215 | |
216 | { |
217 | my $uri = URI->new( $cd_list_url ); |
218 | $uri->query_form({ 'list_ordered_by' => 'invalid_column' }); |
219 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
220 | $mech->request($req); |
221 | if (cmp_ok( $mech->status, '==', 400, 'order_by on non-existing col returns error' )) { |
222 | my $response = JSON::Any->Load( $mech->content); |
223 | is_deeply( $response, { messages => ['a database error has occured.'], success => 'false' }, |
224 | 'error returned for order_by on non-existing col' ); |
225 | } |
226 | } |
227 | |
228 | { |
229 | my $uri = URI->new( $cd_list_url ); |
230 | $uri->query_form({ 'list_ordered_by' => 'invalid_column', 'list_count' => 2, 'list_page' => 1 }); |
231 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
232 | $mech->request($req); |
233 | if (cmp_ok( $mech->status, '==', 400, 'order_by on invalid col with paging returns error' )) { |
234 | my $response = JSON::Any->Load( $mech->content); |
235 | is_deeply( $response, { messages => ['a database error has occured.'], success => 'false' }, |
236 | 'error returned for order_by on non-existing col with paging' ); |
237 | } |
238 | } |
239 | |
240 | done_testing(); |