Skip to content

Commit

Permalink
Fixed 'end of week' spec in tasks using Timecop.
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyken committed Feb 8, 2014
1 parent 9d9aeb8 commit 66b097f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ group :test do
gem 'factory_girl_rails'
gem 'zeus' unless ENV["CI"]
gem 'coveralls', :require => false
gem 'timecop'
end

group :heroku do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ GEM
rack (>= 1.0.0)
thor (0.18.1)
tilt (1.4.1)
timecop (0.7.1)
timers (1.1.0)
tins (1.0.0)
treetop (1.4.15)
Expand Down Expand Up @@ -359,6 +360,7 @@ DEPENDENCIES
therubyracer
thin
thor
timecop
turbo-sprockets-rails3
uglifier (>= 1.0.3)
unicorn
Expand Down
73 changes: 38 additions & 35 deletions spec/models/polymorphic/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# background_info :string(255)
#

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'spec_helper'

describe Task do

Expand All @@ -38,19 +38,19 @@
task.should be_valid
task.errors.should be_empty
end

[ nil, Time.now.utc_offset + 3600 ].each do |offset|
before do
adjust_timezone(offset)
end

it "should create a task with due date selected from dropdown within #{offset ? 'different' : 'current'} timezone" do
task = FactoryGirl.create(:task, :due_at => Time.now.end_of_week, :bucket => "due_this_week")
task.errors.should be_empty
task.bucket.should == "due_this_week"
task.due_at.should == Time.zone.now.end_of_week
end

it "should create a task with due date selected from the calendar within #{offset ? 'different' : 'current'} timezone" do
task = FactoryGirl.create(:task, :bucket => "specific_time", :calendar => "2020-03-20")
task.errors.should be_empty
Expand All @@ -59,22 +59,22 @@
end
end
end

describe "Task/Update" do
it "should update task name" do
task = FactoryGirl.create(:task, :name => "Hello")
task.update_attributes({ :name => "World"})
task.errors.should be_empty
task.name.should == "World"
end

it "should update task category" do
task = FactoryGirl.create(:task, :category => "call")
task.update_attributes({ :category => "email" })
task.errors.should be_empty
task.category.should == "email"
end

it "should reassign the task to another person" do
him = FactoryGirl.create(:user)
her = FactoryGirl.create(:user)
Expand All @@ -84,7 +84,7 @@
task.assigned_to.should == her.id
task.assignee.should == her
end

it "should reassign the task from another person to myself" do
him = FactoryGirl.create(:user)
task = FactoryGirl.create(:task, :assigned_to => him.id)
Expand All @@ -93,20 +93,20 @@
task.assigned_to.should == nil
task.assignee.should == nil
end

[ nil, Time.now.utc_offset + 3600 ].each do |offset|
before do
adjust_timezone(offset)
end

it "should update due date based on selected bucket within #{offset ? 'different' : 'current'} timezone" do
task = FactoryGirl.create(:task, :due_at => Time.now.midnight.tomorrow, :bucket => "due_tomorrow")
task.update_attributes( { :bucket => "due_this_week" } )
task.errors.should be_empty
task.bucket.should == "due_this_week"
task.due_at.should == Time.zone.now.end_of_week
end

it "should update due date if specific calendar date selected within #{offset ? 'different' : 'current'} timezone" do
task = FactoryGirl.create(:task, :due_at => Time.now.midnight.tomorrow, :bucket => "due_tomorrow")
task.update_attributes( { :bucket => "specific_time", :calendar => "2020-03-20" } )
Expand All @@ -115,9 +115,9 @@
task.due_at.to_i.should == Time.parse("2020-03-20").to_i
end
end

end

describe "Task/Complete" do
it "should comlete a task that is overdue" do
task = FactoryGirl.create(:task, :due_at => 2.days.ago, :bucket => "overdue")
Expand All @@ -126,15 +126,15 @@
task.completed_at.should_not == nil
task.completor.should == current_user
end

it "should complete a task due sometime in the future" do
task = FactoryGirl.create(:task, :due_at => Time.now.midnight.tomorrow, :bucket => "due_tomorrow")
task.update_attributes(:completed_at => Time.now, :completed_by => current_user.id)
task.errors.should be_empty
task.completed_at.should_not == nil
task.completor.should == current_user
end

it "should complete a task that is due on specific date in the future" do
task = FactoryGirl.create(:task, :calendar => "10/10/2022 12:00 AM", :bucket => "specific_time")
task.calendar = nil # Calendar is not saved in the database; we need it only to set the :due_at.
Expand All @@ -143,7 +143,7 @@
task.completed_at.should_not == nil
task.completor.should == current_user
end

it "should complete a task that is due on specific date in the past" do
task = FactoryGirl.create(:task, :calendar => "10/10/1992 12:00 AM", :bucket => "specific_time")
task.calendar = nil # Calendar is not saved in the database; we need it only to set the :due_at.
Expand All @@ -152,7 +152,7 @@
task.completed_at.should_not == nil
task.completor.should == current_user
end

it "completion should preserve original due date" do
due_at = Time.now - 42.days
task = FactoryGirl.create(:task, :due_at => due_at, :bucket => "specific_time",
Expand All @@ -162,72 +162,73 @@
task.due_at.should == due_at.utc.strftime('%Y-%m-%d %H:%M')
end
end

# named_scope :my, lambda { |user| { :conditions => [ "(user_id = ? AND assigned_to IS NULL) OR assigned_to = ?", user.id, user.id ], :include => :assignee } }
describe "task.my?" do
it "should match a task created by the user" do
task = FactoryGirl.create(:task, :user => current_user, :assignee => nil)
task.my?(current_user).should == true
end

it "should match a task assigned to the user" do
task = FactoryGirl.create(:task, :user => FactoryGirl.create(:user), :assignee => current_user)
task.my?(current_user).should == true
end

it "should Not match a task not created by the user" do
task = FactoryGirl.create(:task, :user => FactoryGirl.create(:user))
task.my?(current_user).should == false
end

it "should Not match a task created by the user but assigned to somebody else" do
task = FactoryGirl.create(:task, :user => current_user, :assignee => FactoryGirl.create(:user))
task.my?(current_user).should == false
end
end

# named_scope :assigned_by, lambda { |user| { :conditions => [ "user_id = ? AND assigned_to IS NOT NULL AND assigned_to != ?", user.id, user.id ], :include => :assignee } }
describe "task.assigned_by?" do
it "should match a task assigned by the user to somebody else" do
task = FactoryGirl.create(:task, :user => current_user, :assignee => FactoryGirl.create(:user))
task.assigned_by?(current_user).should == true
end

it "should Not match a task not created by the user" do
task = FactoryGirl.create(:task, :user => FactoryGirl.create(:user))
task.assigned_by?(current_user).should == false
end

it "should Not match a task not assigned to anybody" do
task = FactoryGirl.create(:task, :assignee => nil)
task.assigned_by?(current_user).should == false
end

it "should Not match a task assigned to the user" do
task = FactoryGirl.create(:task, :assignee => current_user)
task.assigned_by?(current_user).should == false
end
end

# named_scope :tracked_by, lambda { |user| { :conditions => [ "user_id = ? OR assigned_to = ?", user.id, user.id ], :include => :assignee } }
describe "task.tracked_by?" do
it "should match a task created by the user" do
task = FactoryGirl.create(:task, :user => current_user)
task.tracked_by?(current_user).should == true
end

it "should match a task assigned to the user" do
task = FactoryGirl.create(:task, :assignee => current_user)
task.tracked_by?(current_user).should == true
end

it "should Not match a task that is neither created nor assigned to the user" do
task = FactoryGirl.create(:task, :user => FactoryGirl.create(:user), :assignee => FactoryGirl.create(:user))
task.tracked_by?(current_user).should == false
end
end

describe "task.computed_bucket" do

context "when overdue" do
subject { described_class.new(:due_at => 1.days.ago, :bucket => "specific_time") }

Expand All @@ -253,10 +254,12 @@
end

context "when due this week" do
subject { described_class.new(:due_at => Time.zone.now.end_of_week, :bucket => "specific_time") }

it "returns a sensible value" do
subject.computed_bucket.should == "due_this_week"
# Freeze time so tests will pass when run at the end of the week!!
Timecop.freeze(Time.local(2014, 1, 1, 16, 14)) do
task = described_class.new(:due_at => Time.zone.now.end_of_week, :bucket => "specific_time")
expect(task.computed_bucket).to eql("due_this_week")
end
end
end

Expand Down Expand Up @@ -302,7 +305,7 @@
end
end
end

describe "Exportable" do
describe "unassigned tasks" do
before do
Expand All @@ -314,7 +317,7 @@
let(:exported) { Task.all }
end
end

describe "assigned tasks" do
before do
Task.delete_all
Expand All @@ -325,7 +328,7 @@
let(:exported) { Task.all }
end
end

describe "completed tasks" do
before do
Task.delete_all
Expand All @@ -337,7 +340,7 @@
end
end
end

describe "#parse_calendar_date" do

it "should parse the date" do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'acts_as_fu'
require 'factory_girl_rails'
require 'ffaker'
require 'timecop'

require 'coveralls'
Coveralls.wear!
Expand Down

0 comments on commit 66b097f

Please sign in to comment.