c84f2e4e2db482d6d8a1bdb55760176d1b24b09f
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / item.t
1 use 5.6.0;
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7
8 my $base = 'http://localhost';
9
10 use RestTest;
11 use DBICTest;
12 use URI;
13 use Test::More;
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
16 use JSON;
17
18 my $json = JSON->new->utf8;
19
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
22
23 my $artist_view_url = "$base/api/rpc/artist/id/";
24
25 {
26     my $id = 1;
27     my $req =
28         GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
29     $mech->request($req);
30     cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
31     my %expected_response =
32         $schema->resultset('Artist')->find($id)->get_columns;
33     my $response = $json->decode( $mech->content );
34     is_deeply(
35         $response,
36         { data => \%expected_response, success => 'true' },
37         'correct data returned'
38     );
39 }
40
41 {
42     my $id = 5;
43     my $req =
44         GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
45     $mech->request($req);
46     cmp_ok( $mech->status, '==', 400, 'open attempt not ok' );
47     my $response = $json->decode( $mech->content );
48     is( $response->{success}, 'false',
49         'not existing object fetch failed ok' );
50     like(
51         $response->{messages}->[0],
52         qr/^No object found for id/,
53         'error message for not existing object fetch ok'
54     );
55 }
56
57 done_testing();