initial commit with working tests, docs, and conversion to dzil+podweaver
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / 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;
13use Test::More;
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
24my $track_update_url = "$base/api/rpc/track/id/" . $track->id . "/update";
25my $any_track_update_url = "$base/api/rpc/any/track/id/" . $track->id . "/update";
26
27# test invalid track id caught
28{
29 foreach my $wrong_id ('sdsdsdsd', 3434234) {
30 my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
31 my $req = POST( $incorrect_url, {
32 title => 'value'
33 });
34
35 $mech->request($req, $content_type);
36 cmp_ok( $mech->status, '==', 400, 'Attempt with invalid track id caught' );
37
38 my $response = JSON::Any->Load( $mech->content);
39 like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
40
41 $track->discard_changes;
42 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
43 }
44}
45
46# validation when no params sent
47{
48 my $req = POST( $track_update_url, {
49 wrong_param => 'value'
50 });
51 $mech->request($req, $content_type);
52 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
53
54 my $response = JSON::Any->Load( $mech->content);
55 is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
56
57 $track->discard_changes;
58 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
59}
60
61{
62 my $req = POST( $track_update_url, {
63 wrong_param => 'value'
64 });
65 $mech->request($req, $content_type);
66 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
67
68 my $response = JSON::Any->Load( $mech->content);
69 is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
70
71 $track->discard_changes;
72 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
73}
74
75{
76 my $req = POST( $track_update_url, {
77 title => undef
78 });
79 $mech->request($req, $content_type);
80 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
81
82 $track->discard_changes;
83 isnt($track->title, $original_cols{title}, 'Title changed');
84 is($track->title, '', 'Title changed to undef');
85}
86
87{
88 my $req = POST( $track_update_url, {
89 title => 'monkey monkey'
90 });
91 $mech->request($req, $content_type);
92 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
93
94 $track->discard_changes;
95 is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
96}
97
98{
99 my $req = POST( $track_update_url, {
100 title => 'sheep sheep',
101 'cd.year' => '2009'
102 });
103 $mech->request($req, $content_type);
104 cmp_ok( $mech->status, '==', 200, 'Update with key with value and related key okay' );
105
106 $track->discard_changes;
107 is($track->title, 'sheep sheep', 'Title changed');
108 is($track->cd->year, '2009', 'Related field changed"');
109}
110
111{
112 my $req = POST( $any_track_update_url, {
113 title => 'baa'
114 });
115 $mech->request($req, $content_type);
116 cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
117
118 $track->discard_changes;
119 is($track->title, 'baa', 'Title changed');
120}
121
122{
123 my $req = POST( $any_track_update_url, {
124 position => '14'
125 });
126 $mech->request($req, $content_type);
127 cmp_ok( $mech->status, '==', 200, 'Position update okay' );
128
129 $track->discard_changes;
130 is($track->get_column('position'), '14', 'Position changed');
131}
132
133done_testing();