Ports & Sockets - Jillianne & Shirley#24
Conversation
movie model testing
Jrcheckintestingcontinued
Video StoreWhat We're Looking For
|
| def index | ||
| customers = Customer.all | ||
| render status: :ok, json: customers.as_json(only: [:name, :phone, :postal_code, :registered_at, :id], methods: [:movies_checked_out_count]) | ||
| end |
There was a problem hiding this comment.
I would probably split line 4 across multiple lines.
|
|
||
| if params[:release_date] | ||
| movie.release_date = Date.parse(params[:release_date]).strftime('%Y-%m-%d') | ||
| end |
There was a problem hiding this comment.
Since release_date is a column of type date, postgres should handle this parsing automatically.
| def checkout | ||
| rental = Rental.new(rental_params) | ||
| rental.checkout_date = Date.today | ||
| rental.due_date = Date.today + 1.week |
There was a problem hiding this comment.
This controller action is doing a ton of work! Would it be possible to extract this to a model method, maybe something like Rental.check_out(customer_id, movie_id)? This would make testing easier too.
| movie = Movie.find_by(id: rental_params[:movie_id]) | ||
|
|
||
| unless movie | ||
| render json: { ok: false, errors: rental.errors.messages }, |
There was a problem hiding this comment.
Since Rental has a belongs_to relationship with Movie, this check should be made automatically.
Also, on line 12 you haven't called rental.save or rental.valid? yet, so rental.errors will be empty.
| if rental | ||
| if rental.update(checkin_date: Date.today) | ||
| movie = Movie.find(rental.movie_id) | ||
| movie.increase_inventory |
There was a problem hiding this comment.
Again, it would probably make sense to move this into a model method.
| def movies_checked_out_count | ||
| checkouts = [] | ||
|
|
||
| self.rentals.each do |rental| |
There was a problem hiding this comment.
I love that you've made this a method, instead of storing it in the database - that means there's no way to accidentally forget to update it.
You could simplify this method a bit as:
def movies_checked_out_count
return self.rentals.where(checkin_date: nil).length
end|
|
||
| def available_inventory | ||
| return self.inventory | ||
| end |
There was a problem hiding this comment.
I would probably have chosen to calculate this on the fly instead of storing it, similar to movies_checked_out_count
| it "returns JSON with bogus data" do | ||
| get movie_path(-1) | ||
| expect(response.header["Content-Type"]).must_include "json" | ||
| end |
There was a problem hiding this comment.
Here you might also check the structure of the JSON itself - you would expect it to contain at least:
{
ok: false
errors: // ... something ...
}| # failure case, responds with 404 | ||
| it 'responds with 404 for movie not found' do | ||
| id = -1 | ||
| get movie_path(id) |
There was a problem hiding this comment.
You switch back and forth between success and failure cases in this describe block - if you were to organize it differently, it might be easier to read.
Video Store API
Congratulations! You're submitting your assignment!
If you didn't get to the functionality the question is asking about, reply with what you would have done if you had completed it.
Comprehension Questions
POST /rentals/check-inendpoint? What does the time complexity depend on? Explain your reasoning.