8 my $base = 'http://localhost';
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
18 my $json = JSON->new->utf8;
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
23 my $artist_list_url = "$base/api/rpc/artist/list";
25 $schema->resultset('Track')
27 { select => [qw/me.title me.position/], order_by => 'position' } );
30 my $uri = URI->new($artist_list_url);
31 $uri->query_form( { 'search' => '{"gibberish}' } );
32 my $req = GET( $uri, 'Accept' => 'text/x-json' );
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' );
40 $response->{messages}->[0],
41 qr/Attribute \(search\) does not pass the type constraint because/,
42 'correct data returned for gibberish in search'
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' );
51 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
53 my @expected_response = map {
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' );
63 my $uri = URI->new($artist_list_url);
65 { 'search' => '{ "cds": { "title": "Spoonful of bees" }}' } );
66 my $req = GET( $uri, 'Accept' => 'text/x-json' );
68 cmp_ok( $mech->status, '==', 200, 'attempt with related search okay' );
69 my @expected_response = map {
71 } $schema->resultset('Artist')
72 ->search( { 'cds.title' => 'Spoonful of bees' }, { join => 'cds' } )
74 my $response = $json->decode( $mech->content );
75 is_deeply( { list => \@expected_response, success => 'true' },
76 $response, 'correct data returned for complex query' );
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' );
84 cmp_ok( $mech->status, '==', 200,
85 'attempt with mixed CGI::Expand + JSON search okay' );
87 my @expected_response = map {
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' );