From 36648e1fa370d2e6214ab6f42ac0251a624b4202 Mon Sep 17 00:00:00 2001 From: Tamira Vojnar Date: Tue, 7 Nov 2017 15:04:39 -0800 Subject: [PATCH 1/3] wrote positive case test for checkin --- app/controllers/rentals_controller.rb | 5 +++- config/routes.rb | 3 ++- test/controllers/rentals_controller_test.rb | 27 ++++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 7b7fa25ba..fee966367 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -12,7 +12,10 @@ def create json: {error: rental.errors.messages}, status: :bad_request ) end # if/else - end + end # create + + def update + end # update private def rental_data diff --git a/config/routes.rb b/config/routes.rb index c8e3581b1..be900cde6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ resources :movies, only: [:index, :show, :create] resources :customers, only: [:index] - resources :rentals, only: [:create] + resources :rentals, only: [:create, :update] + # patch '/rentals/:id', to: "rentals#checkin", as: 'rental_checkin' end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 0e0ba0394..f2446ae98 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -71,4 +71,29 @@ body.keys.sort.must_equal keys end end # checkout -end + + describe "checkin" do + let(:r) {Rental.new(customer_id: c.id,movie_id: m.id)} + + it "will check in a movie" do + # arrange + start_count = Rental.count + availible = m.available_inventory + num_movies = c.movies_checked_out_count + + r = post rentals_path, params: {rental: rental_data} + + Rental.count.must_equal start_count + 1 + + r_id = Rental.last.id + + # Act + patch rental_path(r_id) + + # Assert + Rental.count.must_equal start_count + m.available_inventory.must_equal availible + c.movies_checked_out_count.must_equal num_movies + end # checkin a movie + end # checkin +end # renals From d7d4510ca7acbe1ccb35e79346c09c901f4efb34 Mon Sep 17 00:00:00 2001 From: Tamira Vojnar Date: Tue, 7 Nov 2017 15:15:37 -0800 Subject: [PATCH 2/3] positive test for checking in a movie passes --- app/controllers/rentals_controller.rb | 14 +++++++++++++- test/controllers/rentals_controller_test.rb | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index fee966367..d5b446c17 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -15,7 +15,19 @@ def create end # create def update - end # update + rental = Rental.find_by(id: params[:id]) + rental.checked_out = false + if rental.save + binding.pry + render( + json: {"checked_in" => true}, status: :ok + ) + else + render( + json: {error: rental.errors.messages}, status: :bad_request + ) + end # if/else + end # update private def rental_data diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index f2446ae98..8f2523f69 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -84,6 +84,8 @@ r = post rentals_path, params: {rental: rental_data} Rental.count.must_equal start_count + 1 + m.available_inventory.must_equal availible - 1 + c.movies_checked_out_count.must_equal num_movies + 1 r_id = Rental.last.id @@ -91,7 +93,7 @@ patch rental_path(r_id) # Assert - Rental.count.must_equal start_count + Rental.count.must_equal start_count + 1 m.available_inventory.must_equal availible c.movies_checked_out_count.must_equal num_movies end # checkin a movie From c870ccae1a72e1b86d6708e94d6275d187fdc844 Mon Sep 17 00:00:00 2001 From: Tamira Vojnar Date: Tue, 7 Nov 2017 15:27:04 -0800 Subject: [PATCH 3/3] added test and functionality to handle when the rental being updated does not exist --- app/controllers/rentals_controller.rb | 21 +++++++++++------- test/controllers/rentals_controller_test.rb | 24 +++++++++++++++++++-- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index d5b446c17..c94b15a5e 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -16,17 +16,22 @@ def create def update rental = Rental.find_by(id: params[:id]) - rental.checked_out = false - if rental.save - binding.pry - render( - json: {"checked_in" => true}, status: :ok - ) + if rental + rental.checked_out = false + if rental.save + render( + json: {"checked_in" => true}, status: :ok + ) + else + render( + json: {error: rental.errors.messages}, status: :bad_request + ) + end # if/else else render( - json: {error: rental.errors.messages}, status: :bad_request + json: {"no rental found": true}, status: :not_found ) - end # if/else + end # if rental end # update private diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 8f2523f69..1caa9a9c9 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -75,7 +75,7 @@ describe "checkin" do let(:r) {Rental.new(customer_id: c.id,movie_id: m.id)} - it "will check in a movie" do + it "will check in a movie if the movie exists" do # arrange start_count = Rental.count availible = m.available_inventory @@ -85,7 +85,7 @@ Rental.count.must_equal start_count + 1 m.available_inventory.must_equal availible - 1 - c.movies_checked_out_count.must_equal num_movies + 1 + c.movies_checked_out_count.must_equal num_movies + 1 r_id = Rental.last.id @@ -97,5 +97,25 @@ m.available_inventory.must_equal availible c.movies_checked_out_count.must_equal num_movies end # checkin a movie + + it "will return not_found if rental does not exist" do + # arrange + start_count = Rental.count + + r = post rentals_path, params: {rental: rental_data} + + Rental.count.must_equal start_count + 1 + + r_id = Rental.last.id + 1 + + # Act + patch rental_path(r_id) + + # Assert + must_respond_with :not_found + + body = JSON.parse(response.body) + body.must_equal "no rental found" => true + end # not_found end # checkin end # renals