9a90cb9b7dfdfb9ac2867b48a17ff25feeb892ef
[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;
17
18 my $json = JSON->new->utf8;
19
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
22
23 my $artist_list_url = "$base/api/rpc/artist/list";
24 my $base_rs =
25     $schema->resultset('Track')
26     ->search( {},
27     { select => [qw/me.title me.position/], order_by => 'position' } );
28
29 {
30     my $uri = URI->new($artist_list_url);
31     $uri->query_form( { 'search' => '{"gibberish}' } );
32     my $req = GET( $uri, 'Accept' => 'text/x-json' );
33     $mech->request($req);
34     cmp_ok( $mech->status, '==', 400,
35         'attempt with gibberish json not okay' );
36     my $response = $json->decode( $mech->content );
37     is( $response->{success}, 'false',
38         'correct data returned for gibberish in search' );
39     like(
40         $response->{messages}->[0],
41         qr/Attribute \(search\) does not pass the type constraint because/,
42         'correct data returned for gibberish in search'
43     );
44 }
45
46 {
47     my $uri = URI->new($artist_list_url);
48     $uri->query_form( { 'search' => '{"name":{"LIKE":"%waul%"}}' } );
49     my $req = GET( $uri, 'Accept' => 'text/x-json' );
50     $mech->request($req);
51     cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
52
53     my @expected_response = map {
54         { $_->get_columns }
55         } $schema->resultset('Artist')
56         ->search( { name => { LIKE => '%waul%' } } )->all;
57     my $response = $json->decode( $mech->content );
58     is_deeply( { list => \@expected_response, success => 'true' },
59         $response, 'correct data returned for complex query' );
60 }
61
62 {
63     my $uri = URI->new($artist_list_url);
64     $uri->query_form(
65         { 'search' => '{ "cds": { "title": "Spoonful of bees" }}' } );
66     my $req = GET( $uri, 'Accept' => 'text/x-json' );
67     $mech->request($req);
68     cmp_ok( $mech->status, '==', 200, 'attempt with related search okay' );
69     my @expected_response = map {
70         { $_->get_columns }
71         } $schema->resultset('Artist')
72         ->search( { 'cds.title' => 'Spoonful of bees' }, { join => 'cds' } )
73         ->all;
74     my $response = $json->decode( $mech->content );
75     is_deeply( { list => \@expected_response, success => 'true' },
76         $response, 'correct data returned for complex query' );
77 }
78
79 {
80     my $uri = URI->new($artist_list_url);
81     $uri->query_form( { 'search.name' => '{"LIKE":"%waul%"}' } );
82     my $req = GET( $uri, 'Accept' => 'text/x-json' );
83     $mech->request($req);
84     cmp_ok( $mech->status, '==', 200,
85         'attempt with mixed CGI::Expand + JSON search okay' );
86
87     my @expected_response = map {
88         { $_->get_columns }
89         } $schema->resultset('Artist')
90         ->search( { name => { LIKE => '%waul%' } } )->all;
91     my $response = $json->decode( $mech->content );
92     is_deeply( { list => \@expected_response, success => 'true' },
93         $response, 'correct data returned for complex query' );
94 }
95
96 done_testing();