109d1b20d685753fa7debd0adc58463fd6d2db90
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / delete.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 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
10
11 use RestTest;
12 use DBICTest;
13 use Test::More;
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
16
17 my $mech = Test::WWW::Mechanize::Catalyst->new;
18 ok(my $schema = DBICTest->init_schema(), 'got schema');
19
20 my $track = $schema->resultset('Track')->first;
21 my %original_cols = $track->get_columns;
22
23 my $track_delete_url = "$base/api/rpc/track/id/" . $track->id . "/delete";
24 my $tracks_delete_url = "$base/api/rpc/track/delete";
25
26 {
27   my $req = POST( $track_delete_url, {
28
29   });
30   $mech->request($req, $content_type);
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 = POST( $track_delete_url, {
39
40   });
41   $mech->request($req, $content_type);
42   cmp_ok( $mech->status, '==', 400, 'Attempt to delete again caught' );
43 }
44
45 {
46   my $track_cnt = $schema->resultset('Track')->count;
47   my $tracks_rs = $schema->resultset('Track')->search(undef, { select => ['trackid'], as => ['id'], rows => 3 });
48   $tracks_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
49   my $test_data = JSON::Any->Dump({ list => [$tracks_rs->all] });
50   my $req = POST( $tracks_delete_url, Content => $test_data );
51   $req->content_type('text/x-json');
52   $mech->request($req);
53   cmp_ok( $mech->status, '==', 200, 'Attempt to delete three tracks ok' );
54
55   is($schema->resultset('Track')->count + 3, $track_cnt, 'Three tracks deleted');
56 }
57
58 done_testing();