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
10 changes: 10 additions & 0 deletions app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Customer < ApplicationRecord
has_many :movies

validates :name, presence: true

end
5 changes: 5 additions & 0 deletions db/migrate/20171106221254_add_movies_checked_out_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMoviesCheckedOutCount < ActiveRecord::Migration[5.1]
def change
add_column :customers, :movies_checked_out_count, :integer, :default => 0
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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|
Expand Down
42 changes: 39 additions & 3 deletions test/controllers/customers_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
53 changes: 52 additions & 1 deletion test/models/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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