use JSON instead of JSON::Any to get rid of the CPAN Testers failures when only JSON...
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / list.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;
14use Test::WWW::Mechanize::Catalyst 'RestTest';
15use HTTP::Request::Common;
0b0bf911 16use JSON;
17
18my $json = JSON->new->utf8;
d2739840 19
20my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 21ok( my $schema = DBICTest->init_schema(), 'got schema' );
d2739840 22
0b0bf911 23my $artist_list_url = "$base/api/rest/artist";
d2739840 24my $filtered_artist_list_url = "$base/api/rest/bound_artist";
0b0bf911 25my $producer_list_url = "$base/api/rest/producer";
26my $cd_list_url = "$base/api/rest/cd";
27my $track_list_url = "$base/api/rest/track";
d2739840 28
29# test open request
30{
0b0bf911 31 my $req = GET(
32 $artist_list_url,
33 {
34
35 },
36 'Accept' => 'text/x-json'
37 );
38 $mech->request($req);
39 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
40 my @expected_response = map {
41 { $_->get_columns }
42 } $schema->resultset('Artist')->all;
43 my $response = $json->decode( $mech->content );
44 is_deeply(
45 $response,
46 { list => \@expected_response, success => 'true' },
47 'correct message returned'
48 );
d2739840 49}
50
51{
0b0bf911 52 my $uri = URI->new($artist_list_url);
53 $uri->query_form( { 'search.artistid' => 1 } );
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 {
59 { $_->get_columns }
60 } $schema->resultset('Artist')->search( { artistid => 1 } )->all;
61 my $response = $json->decode( $mech->content );
62 is_deeply(
63 $response,
64 { list => \@expected_response, success => 'true' },
65 'correct data returned'
66 );
d2739840 67}
68
69{
0b0bf911 70 my $uri = URI->new($artist_list_url);
71 $uri->query_form( { 'search.name.LIKE' => '%waul%' } );
72 my $req = GET( $uri, 'Accept' => 'text/x-json' );
73 $mech->request($req);
74 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
75
76 my @expected_response = map {
77 { $_->get_columns }
78 } $schema->resultset('Artist')
79 ->search( { name => { LIKE => '%waul%' } } )->all;
80 my $response = $json->decode( $mech->content );
81 is_deeply(
82 $response,
83 { list => \@expected_response, success => 'true' },
84 'correct data returned for complex query'
85 );
d2739840 86}
87
88{
0b0bf911 89 my $uri = URI->new($producer_list_url);
90 my $req = GET( $uri, 'Accept' => 'text/x-json' );
91 $mech->request($req);
92 cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
93
94 my @expected_response = map {
95 { $_->get_columns }
96 } $schema->resultset('Producer')->search( {}, { select => ['name'] } )
97 ->all;
98 my $response = $json->decode( $mech->content );
99 is_deeply(
100 $response,
101 { list => \@expected_response, success => 'true' },
102 'correct data returned for class with list_returns specified'
103 );
d2739840 104}
105
106{
0b0bf911 107 my $uri = URI->new($artist_list_url);
108 $uri->query_form( { 'search.cds.title' => 'Forkful of bees' } );
109 my $req = GET( $uri, 'Accept' => 'text/x-json' );
110 $mech->request($req);
111 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
112
113 my @expected_response = map {
114 { $_->get_columns }
115 } $schema->resultset('Artist')
116 ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } )
117 ->all;
118 my $response = $json->decode( $mech->content );
119 is_deeply(
120 $response,
121 { list => \@expected_response, success => 'true' },
122 'correct data returned for class with select specified'
123 );
d2739840 124}
125
126{
0b0bf911 127 my $uri = URI->new($artist_list_url);
128 $uri->query_form(
129 { 'search.cds.title' => 'Forkful of bees',
130 'list_returns.0.count' => '*',
131 'as.0' => 'count'
132 }
133 );
134 my $req = GET( $uri, 'Accept' => 'text/x-json' );
135 $mech->request($req);
136 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
137
138 my @expected_response = map {
139 { $_->get_columns }
140 } $schema->resultset('Artist')
141 ->search( { 'cds.title' => 'Forkful of bees' },
142 { select => [ { count => '*' } ], as => ['count'], join => 'cds' } )
143 ->all;
144 my $response = $json->decode( $mech->content );
145 is_deeply(
146 $response,
147 { list => \@expected_response, success => 'true' },
148 'correct data returned for count'
149 );
d2739840 150}
151
152{
0b0bf911 153 my $uri = URI->new($filtered_artist_list_url);
154 $uri->query_form( { 'search.artistid' => '2' } );
155 my $req = GET( $uri, 'Accept' => 'text/x-json' );
156 $mech->request($req);
157 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
158 my $response = $json->decode( $mech->content );
159 my @expected_response = map {
160 { $_->get_columns }
161 } $schema->resultset('Artist')->search( { 'artistid' => '1' } )->all;
162 is_deeply(
163 $response,
164 { list => \@expected_response, success => 'true' },
165 'correct data returned for class with setup_list_method specified'
166 );
d2739840 167}
168
02b625cd 169{
0b0bf911 170 my $uri = URI->new($cd_list_url);
171 $uri->query_form(
172 { 'search.tracks.position' => '1',
173 'search.artist.name' => 'Caterwauler McCrae'
174 }
175 );
176 my $req = GET( $uri, 'Accept' => 'text/x-json' );
177 $mech->request($req);
178 cmp_ok( $mech->status, '==', 200, 'search multiple params request okay' );
179 my $response = $json->decode( $mech->content );
180 my @expected_response = map {
181 { $_->get_columns }
182 } $schema->resultset('CD')->search(
183 { 'artist.name' => 'Caterwauler McCrae',
184 'tracks.position' => 1,
185 },
186 { join => [qw/ artist tracks /], }
187 )->all;
188 is_deeply(
189 $response,
190 { list => \@expected_response, success => 'true' },
191 'correct data returned for multiple search params'
192 );
02b625cd 193}
194
4a805f62 195# page specified in controller config (RT#56226)
196{
0b0bf911 197 my $uri = URI->new($track_list_url);
4a805f62 198 $uri->query_form();
199 my $req = GET( $uri, 'Accept' => 'text/x-json' );
200 $mech->request($req);
201 cmp_ok( $mech->status, '==', 200, 'get first page ok' );
0b0bf911 202 my $response = $json->decode( $mech->content );
203 my @expected_response = map {
204 { $_->get_columns }
205 } $schema->resultset('Track')->search( undef, { page => 1, } )->all;
206 is_deeply(
207 $response,
208 { list => \@expected_response, success => 'true', totalcount => 15 },
209 'correct data returned for static configured paging'
210 );
4a805f62 211}
212
11ba2ccc 213{
0b0bf911 214 my $uri = URI->new($artist_list_url);
215 $uri->query_form( { 'search.cds.track.title' => 'Suicidal' } );
11ba2ccc 216 my $req = GET( $uri, 'Accept' => 'text/x-json' );
217 $mech->request($req);
0b0bf911 218 cmp_ok( $mech->status, '==', 400,
219 'attempt with nonexisting relationship fails' );
220 my $response = $json->decode( $mech->content );
221 is_deeply(
222 $response->{messages},
223 ['track is neither a relationship nor a column'],
224 'correct error message returned'
225 );
11ba2ccc 226}
227
228{
0b0bf911 229 my $uri = URI->new($artist_list_url);
230 $uri->query_form( { 'search.cds.tracks.foo' => 'Bar' } );
11ba2ccc 231 my $req = GET( $uri, 'Accept' => 'text/x-json' );
232 $mech->request($req);
0b0bf911 233 cmp_ok( $mech->status, '==', 400,
234 'attempt with nonexisting column fails' );
235 my $response = $json->decode( $mech->content );
236 is_deeply(
237 $response->{messages},
238 ['a database error has occured.'],
239 'correct error message returned'
240 );
11ba2ccc 241}
242
ff26acf7 243{
0b0bf911 244 my $uri = URI->new($artist_list_url);
245 $uri->query_form( { 'search.cds.tracks.title.like' => 'Boring%' } );
ff26acf7 246 my $req = GET( $uri, 'Accept' => 'text/x-json' );
247 $mech->request($req);
248 cmp_ok( $mech->status, '==', 200, 'attempt with sql function ok' );
0b0bf911 249 my $response = $json->decode( $mech->content );
250 my @expected_response = map {
251 { $_->get_columns }
252 } $schema->resultset('Artist')
253 ->search( { 'tracks.title' => { 'like' => 'Boring%' }, },
254 { join => { cds => 'tracks' }, } )->all;
255 is_deeply(
256 $response,
257 { list => \@expected_response, success => 'true' },
258 'correct data returned for search with sql function'
259 );
ff26acf7 260}
261
d2739840 262done_testing();