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