cleaned up delete tests
[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 /;
16use JSON::Any;
d2739840 17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
19ok(my $schema = DBICTest->init_schema(), 'got schema');
20
21my $track = $schema->resultset('Track')->first;
22my %original_cols = $track->get_columns;
23
a1f71064 24my $track_url = "$base/api/rest/track/";
25my $track_delete_url = $track_url . $track->id;
d2739840 26
27{
28 my $req = HTTP::Request->new( DELETE => $track_delete_url );
29 $req->content_type('text/x-json');
30 $mech->request($req);
31 cmp_ok( $mech->status, '==', 200, 'Attempt to delete track ok' );
32
33 my $deleted_track = $schema->resultset('Track')->find($track->id);
34 is($deleted_track, undef, 'track deleted');
35}
36
37{
38 my $req = HTTP::Request->new( DELETE => $track_delete_url );
39 $req->content_type('text/x-json');
40 $mech->request($req);
41 cmp_ok( $mech->status, '==', 400, 'Attempt to delete again caught' );
42}
a1f71064 43
44{
45 my $track_cnt = $schema->resultset('Track')->count;
35331c88 46 my $tracks_rs = $schema->resultset('Track')->search(undef, { select => ['trackid'], as => ['id'], rows => 3 });
a1f71064 47 $tracks_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
48 my $test_data = JSON::Any->Dump({ list => [$tracks_rs->all] });
49 my $req = DELETE( $track_url, Content => $test_data );
50 $req->content_type('text/x-json');
51 $mech->request($req);
52 cmp_ok( $mech->status, '==', 200, 'Attempt to delete three tracks ok' );
53
54 is($schema->resultset('Track')->count + 3, $track_cnt, 'Three tracks deleted');
55}
56
57done_testing();