From 14f95e12524fe6d0c9d755fac2cfe597bbf25d41 Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Mon, 6 Nov 2017 13:52:15 -0800 Subject: [PATCH 1/2] merging --- db/schema.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 2611543b3..120f7ccd7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,22 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 20171106213051) 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" + t.string "address" + t.string "city" + t.string "state" + t.string "postal_code" + t.string "phone" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.float "account_credit" + end + end From 0e09124690a74c4835590e7300f625b1a5471d03 Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Mon, 6 Nov 2017 15:30:39 -0800 Subject: [PATCH 2/2] add customer tests and movies_checked_out column --- app/controllers/customers_controller.rb | 10 +++ app/models/customer.rb | 2 + ...1106221254_add_movies_checked_out_count.rb | 5 ++ db/schema.rb | 3 +- test/controllers/customers_controller_test.rb | 42 ++++++++++- test/fixtures/customers.yml | 2 +- test/models/customer_test.rb | 70 +++++++++++++++++-- 7 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20171106221254_add_movies_checked_out_count.rb 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/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..3420eed99 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -1,13 +1,73 @@ require "test_helper" describe Customer do - let(:customer) { Customer.new } - it "must be valid" do - value(customer).must_be :valid? + # at least one positive and one negative test case for each relation, validation, and custom function + + describe "relations" do + + it "must respond to movies" do + customers = Customer.all + + customers.each do |customer| + customer.must_respond_to :movies + end + end + end - # 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