Commit | Line | Data |
d2739840 |
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 | { |
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 | |
107 | done_testing(); |