c7683f88577292c2d2bc53f761b6d0ec942b55e5
[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
25 # test open request
26 {
27   my $req = GET( $artist_list_url, {
28
29   }, 'Accept' => 'text/x-json' );
30   $mech->request($req);
31   cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
32   my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->all;
33   my $response = JSON::Any->Load( $mech->content);
34   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct message returned' );
35 }
36
37 {
38   my $uri = URI->new( $artist_list_url );
39   $uri->query_form({ 'search.artistid' => 1 });
40   my $req = GET( $uri, 'Accept' => 'text/x-json' );
41   $mech->request($req);
42   cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
43
44   my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ artistid => 1 })->all;
45   my $response = JSON::Any->Load( $mech->content);
46   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned' );
47 }
48
49 {
50   my $uri = URI->new( $artist_list_url );
51   $uri->query_form({ 'search.name.LIKE' => '%waul%' });
52   my $req = GET( $uri, 'Accept' => 'text/x-json' );
53   $mech->request($req);
54   cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
55
56   my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }})->all;
57   my $response = JSON::Any->Load( $mech->content);
58   is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for complex query' );
59 }
60
61 {
62     $DB::single = 1;
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 done_testing();