use JSON instead of JSON::Any to get rid of the CPAN Testers failures when only JSON...
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / list_json_search.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;
0b0bf911 16use JSON;
17
18my $json = JSON->new->utf8;
d2739840 19
20my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 21ok( my $schema = DBICTest->init_schema(), 'got schema' );
d2739840 22
23my $artist_list_url = "$base/api/rpc/artist/list";
0b0bf911 24my $base_rs =
25 $schema->resultset('Track')
26 ->search( {},
27 { select => [qw/me.title me.position/], order_by => 'position' } );
d2739840 28
29{
0b0bf911 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 );
d2739840 44}
45
46{
0b0bf911 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' );
d2739840 60}
61
62{
0b0bf911 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' );
d2739840 77}
78
79{
0b0bf911 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' );
d2739840 94}
95
96done_testing();