ab2aa4ec47471064df7dea0daf3bd70f2e7e4a2b
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / list.t
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/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";
26
27 # test open request
28 {
29   my $req = GET( $artist_list_url, {
30
31   }, 'Accept' => 'text/x-json' );
32   $mech->request($req);
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' );
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( $producer_list_url );
65   my $req = GET( $uri, 'Accept' => 'text/x-json' );
66   $mech->request($req);
67   cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
68
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' );
72 }
73
74 {
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' );
78   $mech->request($req);
79   cmp_ok( $mech->status, '==', 200, 'search related request okay' );
80
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' );
84 }
85
86 {
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' );
90   $mech->request($req);
91   cmp_ok( $mech->status, '==', 200, 'search related request okay' );
92
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' );
96 }
97
98 {
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' );
107 }
108
109 {
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,
119       }, {
120           join => [qw/ artist tracks /],
121       })->all;
122   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for multiple search params' );
123 }
124
125 # page specified in controller config (RT#56226)
126 {
127     my $uri = URI->new( $track_list_url );
128     $uri->query_form();
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, {
134             page => 1,
135         })->all;
136     is_deeply( $response, { list => \@expected_response, success => 'true', totalcount => 15 }, 'correct data returned for static configured paging' );
137 }
138
139 {
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\n"], 'correct error message returned' );
147 }
148
149 {
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' );
157 }
158
159 done_testing();