fixed test failure in generic.t with Moose > 2.02
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / list_json_search.t
1 use strict;
2 use warnings;
3
4 use lib 't/lib';
5
6 my $base = 'http://localhost';
7
8 use RestTest;
9 use DBICTest;
10 use URI;
11 use Test::More;
12 use Test::WWW::Mechanize::Catalyst 'RestTest';
13 use HTTP::Request::Common;
14 use JSON;
15
16 my $json = JSON->new->utf8;
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 =
23     $schema->resultset('Track')
24     ->search( {},
25     { select => [qw/me.title me.position/], order_by => 'position' } );
26
27 {
28     my $uri = URI->new($artist_list_url);
29     $uri->query_form( { 'search' => '{"gibberish}' } );
30     my $req = GET( $uri, 'Accept' => 'text/x-json' );
31     $mech->request($req);
32     cmp_ok( $mech->status, '==', 400,
33         'attempt with gibberish json not okay' );
34     my $response = $json->decode( $mech->content );
35     is( $response->{success}, 'false',
36         'correct data returned for gibberish in search' );
37     like(
38         $response->{messages}->[0],
39         qr/Attribute \(search\) does not pass the type constraint because/,
40         'correct data returned for gibberish in search'
41     );
42 }
43
44 {
45     my $uri = URI->new($artist_list_url);
46     $uri->query_form( { 'search' => '{"name":{"LIKE":"%waul%"}}' } );
47     my $req = GET( $uri, 'Accept' => 'text/x-json' );
48     $mech->request($req);
49     cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
50
51     my @expected_response = map {
52         { $_->get_columns }
53         } $schema->resultset('Artist')
54         ->search( { name => { LIKE => '%waul%' } } )->all;
55     my $response = $json->decode( $mech->content );
56     is_deeply( { list => \@expected_response, success => 'true' },
57         $response, 'correct data returned for complex query' );
58 }
59
60 {
61     my $uri = URI->new($artist_list_url);
62     $uri->query_form(
63         { 'search' => '{ "cds": { "title": "Spoonful of bees" }}' } );
64     my $req = GET( $uri, 'Accept' => 'text/x-json' );
65     $mech->request($req);
66     cmp_ok( $mech->status, '==', 200, 'attempt with related search okay' );
67     my @expected_response = map {
68         { $_->get_columns }
69         } $schema->resultset('Artist')
70         ->search( { 'cds.title' => 'Spoonful of bees' }, { join => 'cds' } )
71         ->all;
72     my $response = $json->decode( $mech->content );
73     is_deeply( { list => \@expected_response, success => 'true' },
74         $response, 'correct data returned for complex query' );
75 }
76
77 {
78     my $uri = URI->new($artist_list_url);
79     $uri->query_form( { 'search.name' => '{"LIKE":"%waul%"}' } );
80     my $req = GET( $uri, 'Accept' => 'text/x-json' );
81     $mech->request($req);
82     cmp_ok( $mech->status, '==', 200,
83         'attempt with mixed CGI::Expand + JSON search okay' );
84
85     my @expected_response = map {
86         { $_->get_columns }
87         } $schema->resultset('Artist')
88         ->search( { name => { LIKE => '%waul%' } } )->all;
89     my $response = $json->decode( $mech->content );
90     is_deeply( { list => \@expected_response, success => 'true' },
91         $response, 'correct data returned for complex query' );
92 }
93
94 done_testing();