added REST and RPC update_bulk tests
[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
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   my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->all;
34   my $response = JSON::Any->Load( $mech->content);
35   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct message returned' );
36 }
37
38 {
39   my $uri = URI->new( $artist_list_url );
40   $uri->query_form({ 'search.artistid' => 1 });
41   my $req = GET( $uri, 'Accept' => 'text/x-json' );
42   $mech->request($req);
43   cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
44
45   my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ artistid => 1 })->all;
46   my $response = JSON::Any->Load( $mech->content);
47   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned' );
48 }
49
50 {
51   my $uri = URI->new( $artist_list_url );
52   $uri->query_form({ 'search.name.LIKE' => '%waul%' });
53   my $req = GET( $uri, 'Accept' => 'text/x-json' );
54   $mech->request($req);
55   cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
56
57   my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }})->all;
58   my $response = JSON::Any->Load( $mech->content);
59   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for complex query' );
60 }
61
62 {
63   my $uri = URI->new( $producer_list_url );
64   my $req = GET( $uri, 'Accept' => 'text/x-json' );
65   $mech->request($req);
66   cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
67
68   my @expected_response = map { { $_->get_columns } } $schema->resultset('Producer')->search({}, { select => ['name'] })->all;
69   my $response = JSON::Any->Load( $mech->content);
70   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with list_returns specified' );
71 }
72
73 {
74   my $uri = URI->new( $artist_list_url );
75   $uri->query_form({ 'search.cds.title' => 'Forkful of bees' });        
76   my $req = GET( $uri, 'Accept' => 'text/x-json' );
77   $mech->request($req);
78   cmp_ok( $mech->status, '==', 200, 'search related request okay' );
79
80   my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { join => 'cds' })->all;
81   my $response = JSON::Any->Load( $mech->content);
82   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with select specified' );
83 }
84
85 {
86   my $uri = URI->new( $artist_list_url );
87   $uri->query_form({ 'search.cds.title' => 'Forkful of bees', 'list_returns.0.count' => '*', 'as.0' => 'count'});       
88   my $req = GET( $uri, 'Accept' => 'text/x-json' );
89   $mech->request($req);
90   cmp_ok( $mech->status, '==', 200, 'search related request okay' );
91
92   my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { select => [ {count => '*'} ], as => [ 'count' ], join => 'cds' })->all;
93   my $response = JSON::Any->Load( $mech->content);
94   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for count' );
95 }
96
97 {
98   my $uri = URI->new( $filtered_artist_list_url );
99   $uri->query_form({ 'search.artistid' => '2' });       
100   my $req = GET( $uri, 'Accept' => 'text/x-json' );
101   $mech->request($req);
102   cmp_ok( $mech->status, '==', 200, 'search related request okay' );
103   my $response = JSON::Any->Load( $mech->content);
104   my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'artistid' => '1' })->all;
105   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with setup_list_method specified' );
106 }
107
108 {
109   my $uri = URI->new( $cd_list_url );
110   $uri->query_form({ 'search.tracks.position' => '1', 'search.artist.name' => 'Caterwauler McCrae' });
111   my $req = GET( $uri, 'Accept' => 'text/x-json' );
112   $mech->request($req);
113   cmp_ok( $mech->status, '==', 200, 'search multiple params request okay' );
114   my $response = JSON::Any->Load( $mech->content);
115   my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({
116           'artist.name'     => 'Caterwauler McCrae',
117           'tracks.position' => 1,
118       }, {
119           join => [qw/ artist tracks /],
120       })->all;
121   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for multiple search params' );
122 }
123
124 done_testing();