removed comments in synopsis as suggested by castaway
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / update.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;
0b8c7370 13use Test::More;
d2739840 14use Test::WWW::Mechanize::Catalyst 'RestTest';
15use HTTP::Request::Common;
16use JSON::Any;
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
0b8c7370 24my $track_url = "$base/api/rest/track/";
25my $track_update_url = $track_url . $track->id;
26my $tracks_update_url = $track_url;
d2739840 27
28# test invalid track id caught
29{
30 foreach my $wrong_id ('sdsdsdsd', 3434234) {
31 my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
32 my $test_data = JSON::Any->Dump({ title => 'value' });
33 my $req = POST( $incorrect_url, Content => $test_data );
34 $req->content_type('text/x-json');
35 $mech->request($req);
36
37 cmp_ok( $mech->status, '==', 400, 'Attempt with invalid track id caught' );
38
39 my $response = JSON::Any->Load( $mech->content);
40 like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
41
42 $track->discard_changes;
43 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
44 }
45}
46
47# validation when no params sent
48{
49 my $test_data = JSON::Any->Dump({ wrong_param => 'value' });
50 my $req = POST( $track_update_url, Content => $test_data );
51 $req->content_type('text/x-json');
52 $mech->request($req);
53
54 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
55
56 my $response = JSON::Any->Load( $mech->content);
57 is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
58
59 $track->discard_changes;
60 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
61}
62
63{
64 my $test_data = JSON::Any->Dump({ title => undef });
65 my $req = POST( $track_update_url, Content => $test_data );
66 $req->content_type('text/x-json');
67 $mech->request($req);
68 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
69
70 $track->discard_changes;
71 isnt($track->title, $original_cols{title}, 'Title changed');
72 is($track->title, undef, 'Title changed to undef');
73}
74
75{
533075c7 76 my $test_data = JSON::Any->Dump({ title => 'monkey monkey', 'cd' => { year => 2009 } });
d2739840 77 my $req = POST( $track_update_url, Content => $test_data );
78 $req->content_type('text/x-json');
79 $mech->request($req);
80
81 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
82
83 $track->discard_changes;
84 is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
9f9ffe56 85 is($track->cd->year, 2009, 'related row updated');
d2739840 86}
0b8c7370 87
7c8cb609 88# bulk_update existing objects
0b8c7370 89{
90 # order to get a stable order of rows
91 my $tracks_rs = $schema->resultset('Track')->search(undef, { order_by => 'trackid', rows => 3 });
92 my $test_data = JSON::Any->Dump({ list => [map +{ id => $_->id, title => 'Track ' . $_->id }, $tracks_rs->all] });
93 my $req = PUT( $tracks_update_url, Content => $test_data );
94 $req->content_type('text/x-json');
95 $mech->request($req);
96 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
97
98 $tracks_rs->reset;
99 while (my $track = $tracks_rs->next) {
100 is($track->title, 'Track ' . $track->id, 'Title changed');
101 }
102}
103
7c8cb609 104# bulk_update nonexisting objects
105{
106 # order to get a stable order of rows
107 my $test_data = JSON::Any->Dump({ list => [map +{ id => $_, title => 'Track ' . $_ }, (1000..1002)] });
108 my $req = PUT( $tracks_update_url, Content => $test_data );
109 $req->content_type('text/x-json');
110 $mech->request($req);
111 cmp_ok( $mech->status, '==', 400, 'Attempt to update three nonexisting tracks fails' );
112 my $response = JSON::Any->Load( $mech->content);
113 is( $response->{success}, JSON::Any::false, 'success property returns false' );
114 like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
115}
116
0b8c7370 117done_testing();