Skip to content

Instantly share code, notes, and snippets.

@jaigouk
Created October 30, 2014 05:15
Show Gist options
  • Save jaigouk/c53906d2931b4e473399 to your computer and use it in GitHub Desktop.
Save jaigouk/c53906d2931b4e473399 to your computer and use it in GitHub Desktop.
example
require "test_helper"
describe Phase do
before do
# Timecop.travel computes an offset between what we currently think Time.now is (recall that we support nested traveling) and the time passed in.
# joe = User.find(1)
# joe.purchase_home()
# assert !joe.mortgage_due?
# # move ahead a month and assert that the mortgage is due
# Timecop.freeze(Time.now.to_date + 30) do
# assert joe.mortgage_due?
# end
t = Time.local(1990)
Timecop.travel(t)
User.all.delete
@client = User.new first_name: "Sally", last_name: "Park", email: "[email protected]", password: "testtest", password_confirmation: "testtest"
@client.save!
@user = User.new first_name: "jaigouk", last_name: "kim", email: "[email protected]", password: "testtest", password_confirmation: "testtest"
@user.save!
@params = {"service"=>"Website",
"user_id"=>"#{@client.id.to_s}", #buyer
"receiver_id"=>"#{@user.id.to_s}", #seller
"units"=>"3",
"budget"=>"< $500 ",
"deadline"=> (Time.now.to_date + 30).to_s,
"details"=>"what now?"}
@x_project = Project.create_when_buyer_requests_contact!(@params)
@phase = @x_project.phases.create(name: "phase1", expected_completion_date: ((Time.now.to_date + 2).to_s))
@phase.save!
end
after do
User.all.delete
end
it 'should have a default state with ready' do
@phase.current_state.must_equal 'not_started'
end
describe "#to_s" do
it 'returns name of phase' do
@phase.name = "Kick Off"
check = @phase.to_s
check.must_equal "Kick Off"
end
end
describe "#start!" do
it 'can be changed into started' do
@phase.expected_completion_date = (Time.now.to_date + 2)
@phase.start!
check = @phase.current_state
check.must_equal 'started'
end
it 'will not be changed to started once delivered' do
@phase.expected_completion_date = (Time.now.to_date + 2)
@phase.start!
@phase.deliver!
@phase.start!
check = @phase.current_state
check.wont_equal "started"
end
it 'will not be changed to started once approved' do
@phase.expected_completion_date = (Time.now.to_date + 2)
@phase.start!
@phase.deliver!
@phase.approve!
@phase.start!
check = @phase.current_state
check.wont_equal "started"
end
end
describe "#expect_date_valid?" do
it "returns true if expect date is within deadline" do
check = @phase.expect_date_valid?
check.must_equal true
end
it "returns false if expect date is later than deadline" do
phase2 = @x_project.phases.new(name: "phase2", expected_completion_date: (Time.now.to_date + 60))
check = phase2.expect_date_valid?
check.must_equal false
end
it "returns false if expect_date is not a week ago" do
week_ago = Time.now.to_date - 7
@phase.expected_completion_date = week_ago
check = @phase.expect_date_valid?
check.must_equal false
end
it "returns true if expect_date is today or later" do
today_or_later = Chronic.parse('next week')
@x_project.deadline = (Time.now.to_date + 30)
@phase.expected_completion_date = today_or_later
check = @phase.expect_date_valid?
check.must_equal true
end
end
describe "#started?" do
it 'returns true if phase is started' do
@phase.start!
check = @phase.started?
check.must_equal true
end
it 'returns false if phase is not started' do
check = @phase.started?
check.must_equal false
end
end
describe "#deliver!" do
it "changes current_state to delivered" do
@phase.start!
@phase.deliver!
check = @phase.current_state
check.must_equal "delivered"
end
it "saves delivered time" do
@phase.start!
@phase.deliver!
check = @phase.delivered_at
check.wont_equal nil
end
it 'will not deliver if current_state is ready' do
@phase.current_state == "not_started"
@phase.save
@phase.deliver!
check = (@phase.current_state == "delivered")
check.must_equal false
end
it 'will not deliver if current_state is started' do
@phase.deliver!
check = (@phase.current_state == "delivered")
check.must_equal false
end
end
describe "#delivered?" do
it "returns true if the milestone is delivered" do
@phase.start!
@phase.deliver!
check = @phase.delivered?
check.must_equal true
end
it "returns false if the milestone is not delivered" do
@phase.start!
check = @phase.delivered?
check.must_equal false
end
end
describe "#approve!" do
it "changes state to approved" do
@phase.start!
@phase.deliver!
@phase.approve!
check = @phase.current_state
check.must_equal "approved"
end
it "saves approved time" do
@phase.start!
@phase.deliver!
@phase.approve!
check = @phase.approved_at
check.wont_equal "approved"
end
it "will not approve if milestone is not started" do
check = @phase.approve!
check.wont_equal "approved"
end
it "will not approve if milestone is not delivered" do
@phase.start!
check = @phase.approve!
check.wont_equal "approved"
end
end
describe "#approved?" do
it "returns true if the milestone is approved" do
@phase.start!
@phase.deliver!
@phase.approve!
check = @phase.approved?
check.must_equal true
end
it "returns false with not_started state" do
check = @phase.approved?
check.must_equal false
end
it "returns false with started state" do
@phase.start!
check = @phase.approved?
check.must_equal false
end
it "returns false with delivered state" do
@phase.start!
@phase.deliver!
check = @phase.approved?
check.must_equal false
end
end
describe "#delayed_to_deliver?" do
it "returns false if it has not_started state" do
@phase.save!
check = @phase.delayed_to_deliver?
check.must_equal false
end
it "returns false if it has started state" do
@phase.start!
check = @phase.delayed_to_deliver?
check.must_equal false
end
it "returns false if it was delivered in time" do
@phase.expected_completion_date = Time.now.to_date + 9
@phase.start!
@phase.deliver!
check = @phase.delayed_to_deliver?
check.must_equal false
end
it "returns true if it was not delivered in time" do
@phase.expected_completion_date = Time.now.to_date + 7
@phase.start!
Timecop.freeze(Time.now.to_date + 9) do
@phase.deliver!
check = @phase.delayed_to_deliver?
check.must_equal true
end
end
it "returns false if it was delivered in time with approval" do
@phase.expected_completion_date = Time.now.to_date + 7
@phase.start!
Timecop.freeze(Time.now.to_date + 5) do
@phase.deliver!
@phase.approve!
check = @phase.delayed_to_deliver?
check.must_equal false
end
end
it "returns true if it was not delivered in time with approval" do
@phase.expected_completion_date = Time.now.to_date + 9
@phase.start!
Timecop.freeze(Time.now.to_date + 14) do
@phase.deliver!
@phase.approve!
check = @phase.delayed_to_deliver?
check.must_equal true
end
end
end
describe "#delayed_delivery_days" do
it "returns 0 if it was not started" do
check = @phase.delayed_delivery_days
check.must_equal 0
end
it "returns 0 if it was not delivered" do
@phase.start!
check = @phase.delayed_delivery_days
check.must_equal 0
end
it "returns 0 if it was not late for expected date with delivered state" do
@phase.expected_completion_date = Time.now.to_date + 1
@phase.start!
@phase.deliver!
check = @phase.delayed_delivery_days
check.must_equal 0
end
it "returns delayed dates if it was late for expected date with delivered state" do
@phase.expected_completion_date = Time.now
@phase.start!
Timecop.freeze(Time.now.to_date + 7) do
@phase.deliver!
check = @phase.delayed_delivery_days
check.must_equal 7
end
end
it "returns 0 if it was not late for expected date with approved state" do
@phase.expected_completion_date = Time.now.to_date + 7
@phase.start!
Timecop.freeze(Time.now.to_date + 7) do
@phase.deliver!
@phase.approve!
check = @phase.delayed_delivery_days
check.must_equal 0
end
end
it "returns delayed dates if it was late for expected date with approved state" do
@phase.expected_completion_date = Time.now
@phase.start!
Timecop.freeze(Time.now.to_date + 7) do
@phase.deliver!
@phase.approve!
check = @phase.delayed_delivery_days
check.must_equal 7
end
end
end
describe "#delayed_to_approve?" do
it "returns false with not_started state" do
check = @phase.delayed_to_approve?
check.must_equal false
end
it "returns false with started state" do
@phase.start!
check = @phase.delayed_to_approve?
check.must_equal false
end
it "returns false with delivered state within allowed time" do
@phase.start!
@phase.deliver!
Timecop.freeze(Time.now.to_date + 1) do
check = @phase.delayed_to_approve?
check.must_equal false
end
end
it "returns true with delivered state more than allowed time" do
@phase.start!
@phase.deliver!
Timecop.freeze(Time.now.to_date + 5) do
check = @phase.delayed_to_approve?
check.must_equal true
end
end
it "returns false with approved state within allowed time" do
@phase.start!
@phase.deliver!
Timecop.freeze(Time.now.to_date + 1) do
@phase.approve!
check = @phase.delayed_to_approve?
check.must_equal false
end
end
it "returns true with approved state more than allowed time" do
@phase.start!
@phase.deliver!
Timecop.freeze(Time.now.to_date + 7) do
@phase.approve!
check = @phase.delayed_to_approve?
check.must_equal true
end
end
end
describe "#delayed_approve_days" do
it "returns 0 with not_started state" do
check = @phase.delayed_approve_days
check.must_equal 0
end
it "returns 0 with started state" do
@phase.start!
check = @phase.delayed_approve_days
check.must_equal 0
end
it "returns 2 with delivered state with 3days after delivery" do
@phase.start!
@phase.deliver!
Timecop.freeze(Time.now.to_date + 3) do
check = @phase.delayed_approve_days
check.must_equal 2
end
end
it "returns 4 with approve state with 5days after delivery(allowed 2 days)" do
@phase.start!
@phase.deliver!
Timecop.freeze(Time.now.to_date + 5) do
@phase.approve!
check = @phase.delayed_approve_days
check.must_equal 4
end
end
it "returns 0 with approve state with same devlivery/approve date" do
@phase.start!
Timecop.freeze(Time.now.to_date + 4) do
@phase.deliver!
@phase.approve!
check = @phase.delayed_approve_days
check.must_equal 0
end
end
end # END #delayed_approve_dates
end # END MileStone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment