add test for bulk create with database error
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / update.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;
0b8c7370 11use Test::More;
d2739840 12use Test::WWW::Mechanize::Catalyst 'RestTest';
13use HTTP::Request::Common;
0b0bf911 14use JSON;
15
16my $json = JSON->new->utf8;
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_url = "$base/api/rest/track/";
25my $track_update_url = $track_url . $track->id;
0b8c7370 26my $tracks_update_url = $track_url;
d2739840 27
28# test invalid track id caught
29{
23bb3784 30 diag 'DBIx::Class warns about a non-numeric id which is ok because we test for that too';
0b0bf911 31 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
32 my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
33 my $test_data = $json->encode( { title => 'value' } );
34 my $req = POST( $incorrect_url, Content => $test_data );
35 $req->content_type('text/x-json');
36 $mech->request($req);
37
38 cmp_ok( $mech->status, '==', 400,
39 'Attempt with invalid track id caught' );
40
41 my $response = $json->decode( $mech->content );
42 like(
43 $response->{messages}->[0],
44 qr/No object found for id/,
45 'correct message returned'
46 );
47
48 $track->discard_changes;
49 is_deeply(
50 { $track->get_columns },
51 \%original_cols,
52 'no update occurred'
53 );
54 }
d2739840 55}
56
57# validation when no params sent
58{
0b0bf911 59 my $test_data = $json->encode( { wrong_param => 'value' } );
60 my $req = POST( $track_update_url, Content => $test_data );
61 $req->content_type('text/x-json');
62 $mech->request($req);
63
64 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
65
66 my $response = $json->decode( $mech->content );
67 is_deeply( $response->{messages}, ['No valid keys passed'],
68 'correct message returned' );
69
70 $track->discard_changes;
71 is_deeply(
72 { $track->get_columns },
73 \%original_cols,
74 'no update occurred'
75 );
d2739840 76}
77
78{
0b0bf911 79 my $test_data = $json->encode( { title => undef } );
80 my $req = POST( $track_update_url, Content => $test_data );
81 $req->content_type('text/x-json');
82 $mech->request($req);
83 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
84
85 $track->discard_changes;
86 isnt( $track->title, $original_cols{title}, 'Title changed' );
87 is( $track->title, undef, 'Title changed to undef' );
d2739840 88}
89
90{
0b0bf911 91 my $test_data = $json->encode(
92 { title => 'monkey monkey', 'cd' => { year => 2009 } } );
93 my $req = POST( $track_update_url, Content => $test_data );
94 $req->content_type('text/x-json');
95 $mech->request($req);
d2739840 96
0b0bf911 97 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
d2739840 98
0b0bf911 99 $track->discard_changes;
100 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
101 is( $track->cd->year, 2009, 'related row updated' );
d2739840 102}
0b8c7370 103
7c8cb609 104# bulk_update existing objects
0b8c7370 105{
0b0bf911 106
107 # order to get a stable order of rows
108 my $tracks_rs =
109 $schema->resultset('Track')
110 ->search( undef, { order_by => 'trackid', rows => 3 } );
111 my $test_data = $json->encode(
112 { list => [
113 map +{ id => $_->id, title => 'Track ' . $_->id },
114 $tracks_rs->all
115 ]
116 }
117 );
118 my $req = PUT( $tracks_update_url, Content => $test_data );
119 $req->content_type('text/x-json');
120 $mech->request($req);
121 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
122
123 $tracks_rs->reset;
124 while ( my $track = $tracks_rs->next ) {
125 is( $track->title, 'Track ' . $track->id, 'Title changed' );
126 }
0b8c7370 127}
128
7c8cb609 129# bulk_update nonexisting objects
130{
0b0bf911 131
132 # order to get a stable order of rows
133 my $test_data = $json->encode(
134 { list => [
135 map +{ id => $_, title => 'Track ' . $_ },
136 ( 1000 .. 1002 )
137 ]
138 }
139 );
140 my $req = PUT( $tracks_update_url, Content => $test_data );
141 $req->content_type('text/x-json');
142 $mech->request($req);
143 cmp_ok( $mech->status, '==', 400,
144 'Attempt to update three nonexisting tracks fails' );
145 my $response = $json->decode( $mech->content );
146 is( $response->{success}, JSON::false,
147 'success property returns unquoted false' );
148 like(
149 $response->{messages}->[0],
150 qr/No object found for id/,
151 'correct message returned'
152 );
7c8cb609 153}
154
0b8c7370 155done_testing();