use JSON instead of JSON::Any to get rid of the CPAN Testers failures when only JSON...
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / 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/rest/artist/";
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 my $track_view_url = "$base/api/rest/track/";
58
59 {
60     my $id = 9;
61     my $req =
62         GET( $track_view_url . $id, undef, 'Accept' => 'application/json' );
63     $mech->request($req);
64     cmp_ok( $mech->status, '==', 200, 'got track with datetime object okay' );
65     my %expected_response =
66         $schema->resultset('Track')->find($id)->get_columns;
67     my $response = $json->decode( $mech->content );
68     is_deeply(
69         $response,
70         { data => \%expected_response, success => 'true' },
71         'correct data returned for track with datetime'
72     );
73 }
74
75 done_testing();