From 9075c53e3befe517d50facfeedd2e441e8af88ae Mon Sep 17 00:00:00 2001 From: Bennett Rahn Date: Mon, 6 Nov 2017 14:11:34 -0800 Subject: [PATCH 1/3] add index, and tests --- app/controllers/movies_controller.rb | 5 ++++- db/schema.rb | 12 +++++++++--- test/controllers/movies_controller_test.rb | 11 ++++++++++- test/fixtures/customers.yml | 2 +- test/models/customer_test.rb | 6 +----- test/models/movie_test.rb | 5 +---- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 863982c2a..7837f281c 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -3,7 +3,10 @@ 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 diff --git a/db/schema.rb b/db/schema.rb index 40b43857d..00ae47220 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index f2d06c62e..214ea55b1 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -17,7 +17,16 @@ 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 end diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index f1002930e..ef01a8091 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -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 # diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index cbe618fa0..d6e29f30e 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -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 diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 34d1d30a5..8798308e5 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -1,9 +1,6 @@ require "test_helper" describe Movie do - let(:movie) { Movie.new } - it "must be valid" do - value(movie).must_be :valid? - end + end From 5b442726761095784577e6b11b11877cd786cc33 Mon Sep 17 00:00:00 2001 From: Bennett Rahn Date: Mon, 6 Nov 2017 14:31:41 -0800 Subject: [PATCH 2/3] code and tests for show and create --- app/controllers/movies_controller.rb | 37 ++++++++++++++++++++++ test/controllers/movies_controller_test.rb | 37 ++++++++++++++++++++++ test/fixtures/movies.yml | 16 +++++++--- 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 7837f281c..143f7887c 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -9,5 +9,42 @@ def index ) 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: pet.errors.messages }, + status: :bad_request + ) + end + end + +private + def movie_params + params.require(:movie).permit(:title, :overview, :release_date, :inventory) + end + end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 214ea55b1..cfb2d0031 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -29,4 +29,41 @@ 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 + end end diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index dc3ee79b5..5e737617f 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -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 From 0e97815e9fbd5a9b20e08f0ae373be3237cc5cf3 Mon Sep 17 00:00:00 2001 From: Bennett Rahn Date: Mon, 6 Nov 2017 14:53:49 -0800 Subject: [PATCH 3/3] add and test validations --- app/controllers/movies_controller.rb | 2 +- app/models/movie.rb | 3 +++ test/controllers/movies_controller_test.rb | 11 ++++++++ test/models/movie_test.rb | 30 ++++++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 143f7887c..5f2f79848 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -35,7 +35,7 @@ def create ) else render( - json: { errors: pet.errors.messages }, + json: { errors: movie.errors.messages }, status: :bad_request ) end diff --git a/app/models/movie.rb b/app/models/movie.rb index dc614df15..182143100 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,2 +1,5 @@ class Movie < ApplicationRecord + + validates :title, presence: true + validates :inventory, numericality: { greater_than: 0 } end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index cfb2d0031..bbf6beda6 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -65,5 +65,16 @@ }.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 diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 8798308e5..52cae63c3 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -1,6 +1,36 @@ require "test_helper" describe Movie do + 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 "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