diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index ca3b6e024..79189a0a9 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,2 +1,12 @@ class CustomersController < ApplicationController + + def index + customers = Customer.all + + render( + json: customers.as_json(only: [:id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count]), + status: :ok + ) + end + end diff --git a/app/models/customer.rb b/app/models/customer.rb index f7e41057f..bee6939d0 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,4 +1,6 @@ class Customer < ApplicationRecord has_many :movies + + validates :name, presence: true end diff --git a/db/migrate/20171106221254_add_movies_checked_out_count.rb b/db/migrate/20171106221254_add_movies_checked_out_count.rb new file mode 100644 index 000000000..1aacf2ac3 --- /dev/null +++ b/db/migrate/20171106221254_add_movies_checked_out_count.rb @@ -0,0 +1,5 @@ +class AddMoviesCheckedOutCount < ActiveRecord::Migration[5.1] + def change + add_column :customers, :movies_checked_out_count, :integer, :default => 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 00ae47220..2ea5a9c1d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20171106213616) do +ActiveRecord::Schema.define(version: 20171106221254) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -26,6 +26,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.float "account_credit" + t.integer "movies_checked_out_count", default: 0 end create_table "movies", force: :cascade do |t| diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index 5e123f6cd..7142455c4 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -1,7 +1,43 @@ require "test_helper" describe CustomersController do - # it "must be a real test" do - # flunk "Need real tests" - # end + describe "index" do + it "is a real working route" do + get customers_path + must_respond_with :success + end + + it "returns json" do + get customers_path + response.header['Content-Type'].must_include 'json' + end + + it "returns an Array" do + get customers_path + + body = JSON.parse(response.body) + body.must_be_kind_of Array + end + + it "returns all of the customers" do + get customers_path + + body = JSON.parse(response.body) + body.length.must_equal Customer.count+1 + end + + it "returns an empty array if there are no customers" do + Customer.destroy_all + + get customers_path + + must_respond_with :success + body = JSON.parse(response.body) + body.must_be_kind_of Array + + body.must_be :empty? + + end + end + end diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index d6e29f30e..94a3c46f5 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -3,7 +3,58 @@ describe Customer do - # at least one positive and one negative test case for each relation, validation, and custom function + describe "validations" do + it "wont save if name is not present" do + invalid_customer_data = { + phone: "(555)555-555" + } + + invalid_customer = Customer.new(invalid_customer_data) + invalid_customer.wont_be :valid? + invalid_customer.errors.messages.must_include :name + invalid_customer.errors.messages.values.first.must_include "can't be blank" + end + + it "will save if name is present" do + valid_customer_data = { + name: "test", + phone: "(555)555-555" + } + + start_count = Customer.count + + valid_customer = Customer.new(valid_customer_data) + valid_customer.must_be :valid? + valid_customer.save + + Customer.count.must_equal start_count+1 + end + + it "will default movies_checked_out_count to 0" do + valid_customer_data = { + name: "test", + phone: "(555)555-555" + } + + valid_customer = Customer.new(valid_customer_data) + valid_customer.save + + valid_customer.movies_checked_out_count.must_equal 0 + end + + it "will change movies_checked_out_count if provided" do + valid_customer_data = { + name: "test", + phone: "(555)555-555", + movies_checked_out_count: 3 + } + + valid_customer = Customer.new(valid_customer_data) + valid_customer.save + + valid_customer.movies_checked_out_count.must_equal 3 + end + end end