X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Controller-DBIC-API.git;a=blobdiff_plain;f=t%2Frpc%2Fupdate.t;fp=t%2Frpc%2Fupdate.t;h=a8777591203134e9269e036364e7390fc5d632bf;hp=c0fcc7720f93222742744661a945c17b558d87dc;hb=88b67dcdf3e0e7eec62209595a8a90f142da308d;hpb=167f91619e02db8320b61f428a1e09706e300bb4 diff --git a/t/rpc/update.t b/t/rpc/update.t index c0fcc77..a877759 100644 --- a/t/rpc/update.t +++ b/t/rpc/update.t @@ -26,6 +26,8 @@ my $any_track_update_url = "$base/api/rpc/any/track/id/" . $track->id . "/update"; my $tracks_update_url = "$base/api/rpc/track/update"; +my $artist_update_url = "$base/api/rpc/artist/id/1/update"; + # test invalid track id caught { diag 'DBIx::Class warns about a non-numeric id which is ok because we test for that too'; @@ -124,6 +126,79 @@ my $tracks_update_url = "$base/api/rpc/track/update"; } { + my $test_data = $json->encode( + { name => 'Caterwauler B. McCrae', + cds => [ + { + cdid => 1, + title => 'All the bees are gone', + year => 3030, + }, + { + cdid => 2, + title => 'All the crops are gone', + year => 3031 + } + ] + } + ); + + my $req = POST( $artist_update_url, Content => $test_data ); + $req->content_type('text/x-json'); + $mech->request($req); + + cmp_ok( $mech->status, '==', 200, 'Multi-row update returned 200 OK' ); + + my $artist = $schema->resultset('Artist')->search({ artistid => 1 }); + ok ($artist->next->name eq "Caterwauler B. McCrae", "mutliple related row parent record update"); + + # make sure the old cds don't exist, it's possible we just inserted the new rows instead of updating them + my $old_cds = $artist->search_related('cds', { title => ['Spoonful of bees', 'Forkful of bees'] } )->count; + ok ($old_cds == 0, 'update performed update and not create on related rows'); + + my @cds = $artist->search_related('cds', { year => ['3030', '3031'] }, { order_by => 'year' })->all; + ok (@cds == 2, 'update modified proper number of related rows'); + ok ($cds[0]->title eq 'All the bees are gone', 'update modified column on related row'); + ok ($cds[1]->title eq 'All the crops are gone', 'update modified column on second related row'); +} + +# update related rows using only unique_constraint on CD vs. primary key +# update the year on constraint match +{ + my $test_data = $json->encode( + { name => 'Caterwauler McCrae', + cds => [ + { + artist => 1, + title => 'All the bees are gone', + year => 3032, + }, + { + artist => 1, + title => 'All the crops are gone', + year => 3032 + } + ] + } + ); + + my $req = POST( $artist_update_url, Content => $test_data ); + $req->content_type('text/x-json'); + $mech->request($req); + + cmp_ok( $mech->status, '==', 200, 'Multi-row unique constraint update returned 200 OK' ); + + my $artist = $schema->resultset('Artist')->search({ artistid => 1 }); + ok ($artist->next->name eq "Caterwauler McCrae", "multi-row unique constraint related row parent record updated"); + + my $old_cds = $artist->search_related('cds', { year => ['3030', '3031'] }, { order_by => 'year' })->count; + ok ( $old_cds == 0, 'multi-row update with unique constraint updated year' ); + + my $cds = $artist->search_related('cds', { 'year' => 3032 } )->count; + ok ( $cds == 2, 'multi-row update with unique constraint okay' ); +} + +{ my $req = POST( $any_track_update_url, { title => 'baa' } ); $mech->request( $req, $content_type ); cmp_ok( $mech->status, '==', 200, 'Stash update okay' ); @@ -141,6 +216,7 @@ my $tracks_update_url = "$base/api/rpc/track/update"; is( $track->get_column('position'), '14', 'Position changed' ); } + # bulk_update existing objects {