8 my $base = 'http://localhost';
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok(my $schema = DBICTest->init_schema(), 'got schema');
21 my $artist_list_url = "$base/api/rest/artist";
22 my $filtered_artist_list_url = "$base/api/rest/bound_artist";
23 my $producer_list_url = "$base/api/rest/producer";
24 my $cd_list_url = "$base/api/rest/cd";
25 my $track_list_url = "$base/api/rest/track";
29 my $req = GET( $artist_list_url, {
31 }, 'Accept' => 'text/x-json' );
33 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
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' );
40 my $uri = URI->new( $artist_list_url );
41 $uri->query_form({ 'search.artistid' => 1 });
42 my $req = GET( $uri, 'Accept' => 'text/x-json' );
44 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
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' );
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' );
56 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
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' );
64 my $uri = URI->new( $producer_list_url );
65 my $req = GET( $uri, 'Accept' => 'text/x-json' );
67 cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
69 my @expected_response = map { { $_->get_columns } } $schema->resultset('Producer')->search({}, { select => ['name'] })->all;
70 my $response = JSON::Any->Load( $mech->content);
71 is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with list_returns specified' );
75 my $uri = URI->new( $artist_list_url );
76 $uri->query_form({ 'search.cds.title' => 'Forkful of bees' });
77 my $req = GET( $uri, 'Accept' => 'text/x-json' );
79 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
81 my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { join => 'cds' })->all;
82 my $response = JSON::Any->Load( $mech->content);
83 is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with select specified' );
87 my $uri = URI->new( $artist_list_url );
88 $uri->query_form({ 'search.cds.title' => 'Forkful of bees', 'list_returns.0.count' => '*', 'as.0' => 'count'});
89 my $req = GET( $uri, 'Accept' => 'text/x-json' );
91 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
93 my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { select => [ {count => '*'} ], as => [ 'count' ], join => 'cds' })->all;
94 my $response = JSON::Any->Load( $mech->content);
95 is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for count' );
99 my $uri = URI->new( $filtered_artist_list_url );
100 $uri->query_form({ 'search.artistid' => '2' });
101 my $req = GET( $uri, 'Accept' => 'text/x-json' );
102 $mech->request($req);
103 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
104 my $response = JSON::Any->Load( $mech->content);
105 my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'artistid' => '1' })->all;
106 is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with setup_list_method specified' );
110 my $uri = URI->new( $cd_list_url );
111 $uri->query_form({ 'search.tracks.position' => '1', 'search.artist.name' => 'Caterwauler McCrae' });
112 my $req = GET( $uri, 'Accept' => 'text/x-json' );
113 $mech->request($req);
114 cmp_ok( $mech->status, '==', 200, 'search multiple params request okay' );
115 my $response = JSON::Any->Load( $mech->content);
116 my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({
117 'artist.name' => 'Caterwauler McCrae',
118 'tracks.position' => 1,
120 join => [qw/ artist tracks /],
122 is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for multiple search params' );
125 # page specified in controller config (RT#56226)
127 my $uri = URI->new( $track_list_url );
129 my $req = GET( $uri, 'Accept' => 'text/x-json' );
130 $mech->request($req);
131 cmp_ok( $mech->status, '==', 200, 'get first page ok' );
132 my $response = JSON::Any->Load( $mech->content);
133 my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search(undef, {
136 is_deeply( $response, { list => \@expected_response, success => 'true', totalcount => 15 }, 'correct data returned for static configured paging' );
140 my $uri = URI->new( $artist_list_url );
141 $uri->query_form({ 'search.cds.track.title' => 'Suicidal' });
142 my $req = GET( $uri, 'Accept' => 'text/x-json' );
143 $mech->request($req);
144 cmp_ok( $mech->status, '==', 400, 'attempt with nonexisting relationship fails' );
145 my $response = JSON::Any->Load( $mech->content);
146 is_deeply( $response->{messages}, ['track is neither a relationship nor a column'], 'correct error message returned' );
150 my $uri = URI->new( $artist_list_url );
151 $uri->query_form({ 'search.cds.tracks.foo' => 'Bar' });
152 my $req = GET( $uri, 'Accept' => 'text/x-json' );
153 $mech->request($req);
154 cmp_ok( $mech->status, '==', 400, 'attempt with nonexisting column fails' );
155 my $response = JSON::Any->Load( $mech->content);
156 is_deeply( $response->{messages}, ['a database error has occured.'], 'correct error message returned' );
160 my $uri = URI->new( $artist_list_url );
161 $uri->query_form({ 'search.cds.tracks.title.like' => 'Boring%' });
162 my $req = GET( $uri, 'Accept' => 'text/x-json' );
163 $mech->request($req);
164 cmp_ok( $mech->status, '==', 200, 'attempt with sql function ok' );
165 my $response = JSON::Any->Load( $mech->content);
166 my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({
167 'tracks.title' => { 'like' => 'Boring%' },
169 join => { cds => 'tracks' },
171 is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for search with sql function' );