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 / delete.t
CommitLineData
d2739840 1use 5.6.0;
2
3use strict;
4use warnings;
5
6use lib 't/lib';
7
8my $base = 'http://localhost';
9my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
10
11use RestTest;
12use DBICTest;
a1f71064 13use Test::More;
d2739840 14use Test::WWW::Mechanize::Catalyst 'RestTest';
a1f71064 15use HTTP::Request::Common qw/ DELETE /;
0b0bf911 16use JSON;
17
18my $json = JSON->new->utf8;
d2739840 19
20my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 21ok( my $schema = DBICTest->init_schema(), 'got schema' );
d2739840 22
0b0bf911 23my $track = $schema->resultset('Track')->first;
d2739840 24my %original_cols = $track->get_columns;
25
0b0bf911 26my $track_url = "$base/api/rest/track/";
a1f71064 27my $track_delete_url = $track_url . $track->id;
d2739840 28
29{
0b0bf911 30 my $req = HTTP::Request->new( DELETE => $track_delete_url );
31 $req->content_type('text/x-json');
32 $mech->request($req);
33 cmp_ok( $mech->status, '==', 200, 'Attempt to delete track ok' );
d2739840 34
0b0bf911 35 my $deleted_track = $schema->resultset('Track')->find( $track->id );
36 is( $deleted_track, undef, 'track deleted' );
d2739840 37}
38
39{
0b0bf911 40 my $req = HTTP::Request->new( DELETE => $track_delete_url );
41 $req->content_type('text/x-json');
42 $mech->request($req);
43 cmp_ok( $mech->status, '==', 400, 'Attempt to delete again caught' );
d2739840 44}
a1f71064 45
46{
0b0bf911 47 my $track_cnt = $schema->resultset('Track')->count;
48 my $tracks_rs =
49 $schema->resultset('Track')
50 ->search( undef, { select => ['trackid'], as => ['id'], rows => 3 } );
51 $tracks_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
52 my $test_data = $json->encode( { list => [ $tracks_rs->all ] } );
53 my $req = DELETE( $track_url, Content => $test_data );
54 $req->content_type('text/x-json');
55 $mech->request($req);
56 cmp_ok( $mech->status, '==', 200, 'Attempt to delete three tracks ok' );
57
58 is( $schema->resultset('Track')->count + 3,
59 $track_cnt, 'Three tracks deleted' );
a1f71064 60}
61
62done_testing();