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