Skip to content
Merged
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
42 changes: 41 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,47 @@ class MoviesController < ApplicationController
def index
movies = Movie.all

render json: movies.as_json
render(
json: movies.as_json(only: [:id, :title, :release_date]),
status: :ok
)
end

def show
movie = Movie.find_by(id: params[:id])

if movie
render(
json: movie.as_json(only: [:title, :overview, :release_date, :inventory]),
status: :ok
)
else
render(
json: { "not found" => true },
status: :not_found
)
end
end

def create
movie = Movie.new(movie_params)

if movie.save
render(
json: { id: movie.id },
status: :ok
)
else
render(
json: { errors: movie.errors.messages },
status: :bad_request
)
end
end

private
def movie_params
params.require(:movie).permit(:title, :overview, :release_date, :inventory)
end


Expand Down
3 changes: 3 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class Movie < ApplicationRecord

validates :title, presence: true
validates :inventory, numericality: { greater_than: 0 }
end
12 changes: 9 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
#
# It's strongly recommended that you check this file into your version control system.


ActiveRecord::Schema.define(version: 20171106213051) do
ActiveRecord::Schema.define(version: 20171106213616) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"


create_table "customers", force: :cascade do |t|
t.string "name"
t.string "registered_at"
Expand All @@ -28,7 +26,15 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "account_credit"
end

create_table "movies", force: :cascade do |t|
t.string "title"
t.string "overview"
t.string "release_date"
t.integer "inventory"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
59 changes: 58 additions & 1 deletion test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,64 @@
must_respond_with :success

body = JSON.parse(response.body)
body.must_be empty?
body.must_be :empty?
end

it "returns the correct information" do
get movies_path
body = JSON.parse(response.body)

body.each do |movie|
movie.keys.sort.must_equal ["id", "release_date", "title"]
end
end
end

describe "show" do
it "returns a hash of information for one movie" do
get movie_path(movies(:jaws).id)
must_respond_with :success

body = JSON.parse(response.body)
body.must_be_kind_of Hash
body["title"].must_equal movies(:jaws).title
end

it "returns not_found when movie doesn't exist" do
get movie_path(Movie.last.id + 1)
must_respond_with :not_found

body = JSON.parse(response.body)
body.must_equal "not found" => true
end
end

describe "create" do
let(:movie_data) {
{
title: "Pirates of the Caribbean",
overview: "great movie with pirates",
release_date: "2001-05-09",
inventory: 3
}
}

it "creates a movie" do
proc {
post movies_path, params: {movie: movie_data}
}.must_change 'Movie.count', 1
must_respond_with :success
end

it "responds with bad_request if invalid data" do
movie_data[:inventory] = nil
proc {
post movies_path, params: {movie: movie_data}
}.wont_change 'Movie.count'
must_respond_with :bad_request

body = JSON.parse(response.body)
body.must_equal "errors" => {"inventory" => ["is not a number"]}
end
end
end
2 changes: 1 addition & 1 deletion test/fixtures/customers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ one:
city: Hillsboro
state: OR,
postal_code: 24309
phone": (322) 510-8695
phone: (322) 510-8695
account_credit: 13.15
# column: value
#
Expand Down
16 changes: 11 additions & 5 deletions test/fixtures/movies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
psycho:
title: "Psycho"
overview: "When larcenous real estate clerk Marion Crane goes on the lam with a wad of cash and hopes of starting a new life, she ends up at the notorious Bates Motel, where manager Norman Bates cares for his housebound mother. The place seems quirky, but fine… until Marion decides to take a shower."
release_date: "1960-06-16"
inventory: 8

jaws:
title: "The Exorcist"
overview: "12-year-old Regan MacNeil begins to adapt an explicit new personality as strange events befall the local area of Georgetown. Her mother becomes torn between science and superstition in a desperate bid to save her daughter, and ultimately turns to her last hope: Father Damien Karras, a troubled priest who is struggling with his own faith."
release_date: "1973-12-26"
inventory: 7
6 changes: 1 addition & 5 deletions test/models/customer_test.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
require "test_helper"

describe Customer do
let(:customer) { Customer.new }

it "must be valid" do
value(customer).must_be :valid?
end

# at least one positive and one negative test case for each relation, validation, and custom function


end
33 changes: 30 additions & 3 deletions test/models/movie_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
require "test_helper"

describe Movie do
let(:movie) { Movie.new }
describe "validations" do
before do
@movie_params = {
overview: "this is an overview",
release_date: "some date"
}
end
it "works when the right things are there" do
movie = movies(:psycho)
movie.must_be :valid?
end

it "must be valid" do
value(movie).must_be :valid?
it "wont save if there is no name" do
movie = Movie.new(@movie_params)
movie.wont_be :valid?
movie.errors.messages.must_include :title
end

it "wont save if there is no inventory" do
movie = Movie.new(@movie_params)
movie.wont_be :valid?
movie.errors.messages.must_include :inventory
end

it "must have an inventory greater_than 0" do
@movie_params[:inventory] = -12
movie = Movie.new(@movie_params)
movie.wont_be :valid?
movie.errors.messages.must_include :inventory
end
end

end