initial commit with working tests, docs, and conversion to dzil+podweaver
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / list_json_search.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/rpc/artist/list";
22 my $base_rs = $schema->resultset('Track')->search({}, { select => [qw/me.title me.position/], order_by => 'position' });
23
24 {
25         my $uri = URI->new( $artist_list_url );
26         $uri->query_form({ 'search' => '{"gibberish}' });
27         my $req = GET( $uri, 'Accept' => 'text/x-json' );
28         $mech->request($req);
29         cmp_ok( $mech->status, '==', 400, 'attempt with gibberish json not okay' );
30         my $response = JSON::Any->Load( $mech->content);
31     is($response->{success}, 'false', 'correct data returned for gibberish in search' );
32         like($response->{messages}->[0], qr/Attribute \(search\) does not pass the type constraint because/, 'correct data returned for gibberish in search' );
33 }
34
35 {
36         my $uri = URI->new( $artist_list_url );
37         $uri->query_form({ 'search' => '{"name":{"LIKE":"%waul%"}}' });
38         my $req = GET( $uri, 'Accept' => 'text/x-json' );
39         $mech->request($req);
40         cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
41         
42         my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }})->all;
43         my $response = JSON::Any->Load( $mech->content);
44         is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for complex query' );
45 }
46
47 {
48         my $uri = URI->new( $artist_list_url );
49         $uri->query_form({ 'search' => '{ "cds": { "title": "Spoonful of bees" }}' });
50         my $req = GET( $uri, 'Accept' => 'text/x-json' );
51         $mech->request($req);
52         cmp_ok( $mech->status, '==', 200, 'attempt with related search okay' );
53         my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Spoonful of bees' }, { join => 'cds' })->all;
54         my $response = JSON::Any->Load( $mech->content);
55         is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for complex query' );
56 }
57
58 {
59         my $uri = URI->new( $artist_list_url );
60         $uri->query_form({ 'search.name' => '{"LIKE":"%waul%"}' });
61         my $req = GET( $uri, 'Accept' => 'text/x-json' );
62         $mech->request($req);
63         cmp_ok( $mech->status, '==', 200, 'attempt with mixed CGI::Expand + JSON search okay' );
64         
65         my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }})->all;
66         my $response = JSON::Any->Load( $mech->content);
67         is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for complex query' );
68 }
69
70 done_testing();