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