initial commit with working tests, docs, and conversion to dzil+podweaver
[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{
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
108done_testing();