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
CommitLineData
609916e5 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;
0b0bf911 16use JSON;
17
18my $json = JSON->new->utf8;
609916e5 19
20my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 21ok( my $schema = DBICTest->init_schema(), 'got schema' );
609916e5 22
23my $artist_view_url = "$base/api/rest/artist/";
24
25{
26 my $id = 1;
0b0bf911 27 my $req =
28 GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
609916e5 29 $mech->request($req);
30 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
0b0bf911 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 );
609916e5 39}
40
41{
42 my $id = 5;
0b0bf911 43 my $req =
44 GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
609916e5 45 $mech->request($req);
46 cmp_ok( $mech->status, '==', 400, 'open attempt not ok' );
0b0bf911 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 );
609916e5 55}
56
15754afc 57my $track_view_url = "$base/api/rest/track/";
58
59{
60 my $id = 9;
0b0bf911 61 my $req =
62 GET( $track_view_url . $id, undef, 'Accept' => 'application/json' );
15754afc 63 $mech->request($req);
64 cmp_ok( $mech->status, '==', 200, 'got track with datetime object okay' );
0b0bf911 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 );
15754afc 73}
74
609916e5 75done_testing();