Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fire - Tram and Pauline #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class RentalsController < ApplicationController
def check_out
rental = Rental.new(video: @video, customer: @customer, due_date: params[:due_date])

if rental.video.available_inventory < 1
render status: :bad_request, json: { errors: { video: ["No copies available"] } }
return
end

if rental.save
render status: :ok, json: {}
else
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,23 @@ def show
)
end

def create
video = Video.new(video_params)

if video.save
render json: video.as_json, status: :created
return
else
render json: {errors: video.errors.messages}, status: :bad_request
end
end

private

def video_params
params.permit(:title, :release_date, :overview, :image_url, :external_id, :inventory)
end

def require_video
@video = Video.find_by(title: params[:title])
unless @video
Expand Down
1 change: 1 addition & 0 deletions app/models/rental.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Rental < ApplicationRecord
# validates :video, uniqueness: { scope: :customer }
validates :due_date, presence: true
validate :due_date_in_future, on: :create
validates_associated :customer, :video

after_initialize :set_checkout_date
after_initialize :set_returned
Expand Down
2 changes: 2 additions & 0 deletions app/models/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Video < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals

validates :external_id, uniqueness: true
validates :inventory, numericality: {only_integer: true, greater_than: 0}
def available_inventory
self.inventory - self.rentals.where(returned: false).length
end
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

resources :customers, only: [:index]

resources :videos, only: [:index, :show], param: :title
resources :videos, only: [:index, :show, :create], param: :title

post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out"
post "/rentals/:title/return", to: "rentals#check_in", as: "check_in"
Expand Down
16 changes: 16 additions & 0 deletions test/controllers/rentals_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ class RentalsControllerTest < ActionDispatch::IntegrationTest
expect(data).must_include "errors"
expect(data["errors"]).must_include "due_date"
end

it "requires sufficient inventory" do
videos(:one)['inventory'] = 1
videos(:one).save # ensure new inventory number is saved to invalidate this test
customer = customers(:two)
post check_out_path(title: videos(:one).title), params: {
customer_id: customer.id,
due_date: Date.today + 1
}

must_respond_with :bad_request
data = JSON.parse @response.body
expect(data).must_include "errors"
expect(data["errors"]).must_include "video"
expect(data["errors"]["video"]).must_include "No copies available"
end
end

describe "check-in" do
Expand Down
40 changes: 40 additions & 0 deletions test/controllers/videos_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,44 @@ class VideosControllerTest < ActionDispatch::IntegrationTest
expect(data["errors"]).must_include "title"
end
end

describe "create" do
before do
# Arrange
@video_hash = {
title: "the l3go movie",
overview: "it IS getting a trequel, right?",
release_date: "2022-04-30",
inventory: 10,
external_id: 10,
image_url: "https://www.themoviedb.org/t/p/w1280/lbctonEnewCYZ4FYoTZhs8cidAl.jpg"
}
end
it "can create a valid video" do
# Assert
expect {
post videos_path, params: @video_hash
}.must_change "Video.count", 1

must_respond_with :created
end

it "will respond with bad request and errors for an invalid movie" do
# Arrange
@video_hash[:external_id] = 7 # match external id of fixture

# Assert
expect {
post videos_path, params: @video_hash
}.wont_change "Video.count"
body = JSON.parse(response.body)

expect(body.keys).must_include "errors"
expect(body["errors"].keys).must_include "external_id"
expect(body["errors"]["external_id"]).must_include "has already been taken"

must_respond_with :bad_request
end

end
end
2 changes: 2 additions & 0 deletions test/fixtures/videos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ one:
overview: MyText
release_date: 2017-01-11
inventory: 4
external_id: 7

two:
title: MuchFilm
overview: MyText
release_date: 2017-01-11
inventory: 7
external_id: 8
9 changes: 9 additions & 0 deletions test/models/rental_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ class RentalTest < ActiveSupport::TestCase
expect(@rental).must_respond_to :video
end

it "has a video with sufficient inventory" do
videos(:one)['inventory'] = 0
rental_data['video'] = videos(:one)
bad_rental = Rental.new(rental_data)
expect(bad_rental.valid?).must_equal false
expect(bad_rental.errors.messages).must_include :video
expect(bad_rental.errors.messages[:video]).must_include "is invalid"
end

it "Cannot be created without a video" do
data = rental_data.clone
data.delete :video
Expand Down
35 changes: 34 additions & 1 deletion test/models/video_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class VideoTest < ActiveSupport::TestCase
"title": "Hidden Figures",
"overview": "Some text",
"release_date": "1960-06-16",
"inventory": 8
"inventory": 8,
"external_id": 9
}
}

Expand Down Expand Up @@ -78,4 +79,36 @@ class VideoTest < ActiveSupport::TestCase
expect(after_ai).must_equal before_ai + 1
end
end

describe "model validations" do
it "cannot add a movie with a matching external id (aka twice from MovieDB)" do
video_data["external_id"] = 7 # match fixture external_id

invalid_video = Video.create(video_data)

expect(invalid_video.valid?).must_equal false
expect(invalid_video.errors[:external_id]).must_include "has already been taken"

end

it "must have a total_inventory greater than 0" do
videos.each do |video|
video.inventory = 0
expect(video.valid?).must_equal false
expect(video.errors[:inventory]).must_include "must be greater than 0"
end
end

it "must have numerical, integer values for total/available inventory" do
error_message_hash = {"NaN" => "is not a number", 1.5 => "must be an integer"}
# ^ allows us to test for invalid number and integer input in one neat test :)
error_message_hash.each do |error, message|
videos(:one)[:inventory] = error

expect(videos(:one).valid?).must_equal false

expect(videos(:one).errors[:inventory]).must_include message
end
end
end
end