Removed tests' reliance on deprecated JSON behaviour of overloading eq operator
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / item.t
CommitLineData
609916e5 1use strict;
2use warnings;
3
4use lib 't/lib';
5
6my $base = 'http://localhost';
7
8use RestTest;
9use DBICTest;
10use URI;
11use Test::More;
12use Test::WWW::Mechanize::Catalyst 'RestTest';
13use HTTP::Request::Common;
0b0bf911 14use JSON;
15
16my $json = JSON->new->utf8;
609916e5 17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 19ok( my $schema = DBICTest->init_schema(), 'got schema' );
609916e5 20
21my $artist_view_url = "$base/api/rest/artist/";
22
23{
24 my $id = 1;
0b0bf911 25 my $req =
7dccc5de 26 GET( $artist_view_url . $id, 'Accept' => 'application/json' );
609916e5 27 $mech->request($req);
28 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
0b0bf911 29 my %expected_response =
30 $schema->resultset('Artist')->find($id)->get_columns;
31 my $response = $json->decode( $mech->content );
c2a3a0b3 32 #artist does not have use_json_boolean => 1, so true values are stringified to 'true'
0b0bf911 33 is_deeply(
34 $response,
35 { data => \%expected_response, success => 'true' },
36 'correct data returned'
37 );
609916e5 38}
39
40{
41 my $id = 5;
0b0bf911 42 my $req =
7dccc5de 43 GET( $artist_view_url . $id, 'Accept' => 'application/json' );
609916e5 44 $mech->request($req);
45 cmp_ok( $mech->status, '==', 400, 'open attempt not ok' );
0b0bf911 46 my $response = $json->decode( $mech->content );
47 is( $response->{success}, 'false',
48 'not existing object fetch failed ok' );
49 like(
50 $response->{messages}->[0],
51 qr/^No object found for id/,
52 'error message for not existing object fetch ok'
53 );
609916e5 54}
55
15754afc 56my $track_view_url = "$base/api/rest/track/";
57
58{
59 my $id = 9;
0b0bf911 60 my $req =
7dccc5de 61 GET( $track_view_url . $id, 'Accept' => 'application/json' );
15754afc 62 $mech->request($req);
63 cmp_ok( $mech->status, '==', 200, 'got track with datetime object okay' );
0b0bf911 64 my %expected_response =
65 $schema->resultset('Track')->find($id)->get_columns;
66 my $response = $json->decode( $mech->content );
67 is_deeply(
68 $response,
c2a3a0b3 69 { data => \%expected_response, success => JSON::true },
0b0bf911 70 'correct data returned for track with datetime'
71 );
15754afc 72}
73
68e02291 74{
75 my $req =
7dccc5de 76 GET( $artist_view_url . 'action_with_error', 'Accept' => 'application/json' );
68e02291 77 $mech->request($req);
78 cmp_ok( $mech->status, '==', 404, 'action returned error 404' );
79 my $response = $json->decode( $mech->content );
80 is_deeply(
81 $response,
82 { success => 'false' },
83 'correct data returned'
84 );
85}
86
609916e5 87done_testing();