6 my $base = 'http://localhost';
12 use Test::WWW::Mechanize::Catalyst 'RestTest';
13 use HTTP::Request::Common;
16 my $json = JSON->new->utf8;
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok( my $schema = DBICTest->init_schema(), 'got schema' );
21 my $artist_list_url = "$base/api/rpc/artist/list";
23 $schema->resultset('Track')
25 { select => [qw/me.title me.position/], order_by => 'position' } );
28 my $uri = URI->new($artist_list_url);
29 $uri->query_form( { 'search' => '{"gibberish}' } );
30 my $req = GET( $uri, 'Accept' => 'text/x-json' );
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' );
38 $response->{messages}->[0],
39 qr/Attribute \(search\) does not pass the type constraint because/,
40 'correct data returned for gibberish in search'
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' );
49 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
51 my @expected_response = map {
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' );
61 my $uri = URI->new($artist_list_url);
63 { 'search' => '{ "cds": { "title": "Spoonful of bees" }}' } );
64 my $req = GET( $uri, 'Accept' => 'text/x-json' );
66 cmp_ok( $mech->status, '==', 200, 'attempt with related search okay' );
67 my @expected_response = map {
69 } $schema->resultset('Artist')
70 ->search( { 'cds.title' => 'Spoonful of bees' }, { join => 'cds' } )
72 my $response = $json->decode( $mech->content );
73 is_deeply( { list => \@expected_response, success => 'true' },
74 $response, 'correct data returned for complex query' );
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' );
82 cmp_ok( $mech->status, '==', 200,
83 'attempt with mixed CGI::Expand + JSON search okay' );
85 my @expected_response = map {
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' );