more robust json boolean testing
[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;
16use JSON::Any;
17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
19ok(my $schema = DBICTest->init_schema(), 'got schema');
20
21my $artist_list_url = "$base/api/rest/artist";
22my $filtered_artist_list_url = "$base/api/rest/bound_artist";
23my $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{
d2739840 62 my $uri = URI->new( $producer_list_url );
63 my $req = GET( $uri, 'Accept' => 'text/x-json' );
64 $mech->request($req);
65 cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
66
67 my @expected_response = map { { $_->get_columns } } $schema->resultset('Producer')->search({}, { select => ['name'] })->all;
68 my $response = JSON::Any->Load( $mech->content);
69 is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with list_returns specified' );
70}
71
72{
73 my $uri = URI->new( $artist_list_url );
74 $uri->query_form({ 'search.cds.title' => 'Forkful of bees' });
75 my $req = GET( $uri, 'Accept' => 'text/x-json' );
76 $mech->request($req);
77 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
78
79 my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { join => 'cds' })->all;
80 my $response = JSON::Any->Load( $mech->content);
81 is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with select specified' );
82}
83
84{
85 my $uri = URI->new( $artist_list_url );
86 $uri->query_form({ 'search.cds.title' => 'Forkful of bees', 'list_returns.0.count' => '*', 'as.0' => 'count'});
87 my $req = GET( $uri, 'Accept' => 'text/x-json' );
88 $mech->request($req);
89 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
90
91 my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { select => [ {count => '*'} ], as => [ 'count' ], join => 'cds' })->all;
92 my $response = JSON::Any->Load( $mech->content);
93 is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for count' );
94}
95
96{
97 my $uri = URI->new( $filtered_artist_list_url );
98 $uri->query_form({ 'search.artistid' => '2' });
99 my $req = GET( $uri, 'Accept' => 'text/x-json' );
100 $mech->request($req);
101 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
102 my $response = JSON::Any->Load( $mech->content);
103 my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'artistid' => '1' })->all;
104 is_deeply( $response, { list => \@expected_response, success => 'true' }, 'correct data returned for class with setup_list_method specified' );
105}
106
107done_testing();