This fixes failing tests when the timezone is different than utc

There were several problems:
* Time.now returns the systems time, not the users time
* fixtures do not translate dates from timezone to utc, but stores the
  date verbatim
* calling a controller will set the timezone to the preference of the
  current_user. So it could be changed while you do not realize this. I
  fixed the failing test, but problems could be elsewhere
This commit is contained in:
Reinier Balt 2015-08-04 23:08:13 +02:00
parent 0b44fe3f08
commit e58379e81f
27 changed files with 221 additions and 214 deletions

View file

@ -98,7 +98,7 @@ class ProjectTest < ActiveSupport::TestCase
assert_equal :completed, @timemachine.aasm.current_state
assert @timemachine.completed?
assert_not_nil @timemachine.completed_at, "completed_at not expected to be nil"
assert_in_delta Time.now, @timemachine.completed_at, 1
assert_in_delta Time.zone.now, @timemachine.completed_at, 1
end
def test_delete_project_deletes_todos_within_it
@ -254,5 +254,4 @@ class ProjectTest < ActiveSupport::TestCase
p.reload
assert_equal 4, p.running_time
end
end

View file

@ -11,7 +11,7 @@ class RecurringTodoTest < ActiveSupport::TestCase
@every_month = @monthly_every_last_friday
@yearly = recurring_todos(:birthday_reinier)
@today = Time.now.utc
@today = Time.zone.now
@tomorrow = @today + 1.day
@in_three_days = @today + 3.days
@in_four_days = @in_three_days + 1.day # need a day after start_from
@ -63,19 +63,19 @@ class RecurringTodoTest < ActiveSupport::TestCase
#
# start_from is way_back
due_date1 = @yearly.get_due_date(nil)
due_date2 = @yearly.get_due_date(Time.now.utc + 1.day)
due_date2 = @yearly.get_due_date(Time.zone.now + 1.day)
assert_equal due_date1, due_date2
# start_from is in the future
@yearly.start_from = Time.now.utc + 1.week
@yearly.start_from = Time.zone.now + 1.week
due_date1 = @yearly.get_due_date(nil)
due_date2 = @yearly.get_due_date(Time.now.utc + 1.day)
due_date2 = @yearly.get_due_date(Time.zone.now + 1.day)
assert_equal due_date1, due_date2
# start_from is nil
@yearly.start_from = nil
due_date1 = @yearly.get_due_date(nil)
due_date2 = @yearly.get_due_date(Time.now.utc + 1.day)
due_date2 = @yearly.get_due_date(Time.zone.now + 1.day)
assert_equal due_date1, due_date2
end
@ -117,7 +117,7 @@ class RecurringTodoTest < ActiveSupport::TestCase
assert_equal Time.zone.local(2021,6,8), @yearly.get_due_date(Time.zone.local(2019,6,1)) # also next year
assert_equal Time.zone.local(2021,6,8), @yearly.get_due_date(Time.zone.local(2020,6,15)) # also next year
this_year = Time.now.utc.year
this_year = Time.zone.now.utc.year
@yearly.start_from = Time.zone.local(this_year+1,6,12)
due_date = @yearly.get_due_date(nil)
assert_equal due_date.year, this_year+2
@ -168,4 +168,4 @@ class RecurringTodoTest < ActiveSupport::TestCase
assert_equal true, @every_day.continues_recurring?(@in_three_days)
assert_equal 0, @every_day.occurrences_count
end
end
end

View file

@ -8,7 +8,7 @@ module RecurringTodos
def setup
super
@admin = users(:admin_user)
end
end
def test_pattern_builds_from_existing_recurring_todo
rt = @admin.recurring_todos.first
@ -121,9 +121,9 @@ module RecurringTodos
def test_determine_start
travel_to Time.zone.local(2013,1,1) do
rt = create_recurring_todo
assert_equal "2013-01-01 00:00:00", rt.send(:determine_start, nil).to_s(:db), "no previous date, use today"
assert_equal "2013-01-01 00:00:00", rt.send(:determine_start, nil, 1.day).to_s(:db), "no previous date, use today without offset"
assert_equal "2013-01-02 00:00:00", rt.send(:determine_start, Time.zone.now, 1.day).to_s(:db), "use previous date and offset"
assert_equal Time.zone.parse("2013-01-01 00:00:00"), rt.send(:determine_start, nil), "no previous date, use today"
assert_equal Time.zone.parse("2013-01-01 00:00:00"), rt.send(:determine_start, nil, 1.day).to_s(:db), "no previous date, use today without offset"
assert_equal Time.zone.parse("2013-01-02 00:00:00"), rt.send(:determine_start, Time.zone.now, 1.day).to_s(:db), "use previous date and offset"
end
end
@ -131,14 +131,14 @@ module RecurringTodos
rt = create_recurring_todo
# march 2014 has 5 saturdays, the last will return the 5th
assert_equal "2014-03-01 00:00:00", rt.send(:get_xth_day_of_month, 1, 6, 3, 2014).to_s(:db)
assert_equal "2014-03-22 00:00:00", rt.send(:get_xth_day_of_month, 4, 6, 3, 2014).to_s(:db)
assert_equal "2014-03-29 00:00:00", rt.send(:get_xth_day_of_month, 5, 6, 3, 2014).to_s(:db)
assert_equal Time.zone.parse("2014-03-01 00:00:00"), rt.send(:get_xth_day_of_month, 1, 6, 3, 2014).to_s(:db)
assert_equal Time.zone.parse("2014-03-22 00:00:00"), rt.send(:get_xth_day_of_month, 4, 6, 3, 2014).to_s(:db)
assert_equal Time.zone.parse("2014-03-29 00:00:00"), rt.send(:get_xth_day_of_month, 5, 6, 3, 2014).to_s(:db)
# march 2014 has 4 fridays, the last will return the 4th
assert_equal "2014-03-07 00:00:00", rt.send(:get_xth_day_of_month, 1, 5, 3, 2014).to_s(:db)
assert_equal "2014-03-28 00:00:00", rt.send(:get_xth_day_of_month, 4, 5, 3, 2014).to_s(:db)
assert_equal "2014-03-28 00:00:00", rt.send(:get_xth_day_of_month, 5, 5, 3, 2014).to_s(:db)
assert_equal Time.zone.parse("2014-03-07 00:00:00"), rt.send(:get_xth_day_of_month, 1, 5, 3, 2014).to_s(:db)
assert_equal Time.zone.parse("2014-03-28 00:00:00"), rt.send(:get_xth_day_of_month, 4, 5, 3, 2014).to_s(:db)
assert_equal Time.zone.parse("2014-03-28 00:00:00"), rt.send(:get_xth_day_of_month, 5, 5, 3, 2014).to_s(:db)
assert_raise(RuntimeError, "should check on valid weekdays"){ rt.send(:get_xth_day_of_month, 5, 9, 3, 2014) }
assert_raise(RuntimeError, "should check on valid count x"){ rt.send(:get_xth_day_of_month, 6, 5, 3, 2014) }
@ -166,4 +166,4 @@ module RecurringTodos
end
end
end

View file

@ -16,14 +16,14 @@ class TodoCreateParamsHelperTest < ActiveSupport::TestCase
end
def test_show_from_accessor
expected_date = Time.now
expected_date = Time.zone.now
params = ActionController::Parameters.new({ 'todo' => { 'show_from' => expected_date}})
params_helper = Todos::TodoCreateParamsHelper.new(params, users(:admin_user))
assert_equal(expected_date, params_helper.show_from)
end
def test_due_accessor
expected_date = Time.now
expected_date = Time.zone.now
params = ActionController::Parameters.new({ 'todo' => { 'due' => expected_date}})
params_helper = Todos::TodoCreateParamsHelper.new(params, users(:admin_user))
assert_equal(expected_date, params_helper.due)
@ -119,5 +119,4 @@ class TodoCreateParamsHelperTest < ActiveSupport::TestCase
params_helper = Todos::TodoCreateParamsHelper.new(params, users(:admin_user))
assert_equal false, params_helper.context_specified_by_name?
end
end

View file

@ -22,8 +22,8 @@ class TodoTest < ActiveSupport::TestCase
assert_equal "Call Bill Gates to find out how much he makes per day", @not_completed1.description
assert_nil @not_completed1.notes
assert @not_completed1.completed? == false
assert_equal 1.week.ago.beginning_of_day.strftime("%Y-%m-%d %H:%M"), @not_completed1.created_at.beginning_of_day.strftime("%Y-%m-%d %H:%M")
assert_equal 2.week.from_now.beginning_of_day.strftime("%Y-%m-%d"), @not_completed1.due.strftime("%Y-%m-%d")
assert_equal 1.week.ago.utc.beginning_of_day, @not_completed1.created_at.utc
assert_equal 2.week.from_now.utc.beginning_of_day, @not_completed1.due.utc
assert_nil @not_completed1.completed_at
assert_equal 1, @not_completed1.user_id
end
@ -72,26 +72,26 @@ class TodoTest < ActiveSupport::TestCase
def test_validate_show_from_must_be_a_date_in_the_future
t = @not_completed2
t.show_from = 1.week.ago
assert !t.save, "todo should not be saved without validation errors"
assert_equal 1, t.errors.count
assert_equal "must be a date in the future", t.errors[:show_from][0]
end
def test_validate_circular_dependencies
@completed.activate!
@not_completed3=@completed
# 2 -> 1
@not_completed1.add_predecessor(@not_completed2)
assert @not_completed1.save!
assert_equal 1, @not_completed2.successors.count
# 3 -> 2 -> 1
@not_completed2.add_predecessor(@not_completed3)
assert @not_completed2.save!
assert_equal 1, @not_completed3.successors.count
# 1 -> 3 -> 2 -> 1 == circle
assert_raises ActiveRecord::RecordInvalid do
@not_completed3.add_predecessor(@not_completed1)
@ -131,7 +131,7 @@ class TodoTest < ActiveSupport::TestCase
t.toggle_completion!
assert_equal :active, t.aasm.current_state
end
def test_toggle_completion_with_show_from_in_future
t = @not_completed1
t.show_from= 1.week.from_now
@ -140,12 +140,12 @@ class TodoTest < ActiveSupport::TestCase
t.toggle_completion!
assert_equal :completed, t.aasm.current_state
end
def test_toggle_completion_with_show_from_in_past
t = @not_completed1
t.update_attribute(:show_from, 1.week.ago)
assert_equal :active, t.aasm.current_state
assert t.toggle_completion!, "shoud be able to mark active todo complete even if show_from is set in the past"
assert_equal :completed, t.aasm.current_state
end
@ -219,7 +219,7 @@ class TodoTest < ActiveSupport::TestCase
# And I update the state of the todo from its project
new_todo.update_state_from_project
# Then the todo should be hidden
assert new_todo.hidden?
assert new_todo.hidden?
end
def test_initial_state_defaults_to_active
@ -280,7 +280,7 @@ class TodoTest < ActiveSupport::TestCase
assert todo.pending?, "todo with predecessor should be blocked"
# cannot activate if part of hidden project
assert_raise(AASM::InvalidTransition) { todo.activate! }
assert_raise(AASM::InvalidTransition) { todo.activate! }
todo.remove_predecessor(todo2)
assert todo.reload.hidden?, "todo should be put back in hidden state"
@ -337,7 +337,7 @@ class TodoTest < ActiveSupport::TestCase
@not_completed1.add_predecessor(@not_completed2)
@not_completed1.save_predecessors
# blocking is not done automagically
@not_completed1.block!
@not_completed1.block!
assert @not_completed1.uncompleted_predecessors?
assert @not_completed1.pending?, "a todo with predecessors should be pending"
@ -358,7 +358,7 @@ class TodoTest < ActiveSupport::TestCase
@not_completed1.add_predecessor_list("#{@not_completed2.id}, #{@not_completed3.id}")
@not_completed1.save_predecessors
# blocking is not done automagically
@not_completed1.block!
@not_completed1.block!
# Then @completed1 should have predecessors and should be blocked
assert @not_completed1.uncompleted_predecessors?
@ -526,18 +526,18 @@ class TodoTest < ActiveSupport::TestCase
assert !older_created_todos.include?(todo_now)
assert !recent_created_todos.include?(todo_old)
end
def test_notes_are_rendered_on_save
user = @completed.user
todo = user.todos.create(:description => "test", :context => @completed.context)
assert_nil todo.notes
assert_nil todo.rendered_notes
todo.notes = "*test*"
todo.save!
todo.reload
assert_equal "*test*", todo.notes
assert_equal "<p><strong>test</strong></p>", todo.rendered_notes
end

View file

@ -75,7 +75,7 @@ class UserTest < ActiveSupport::TestCase
assert_equal "is too long (maximum is 80 characters)", u.errors[:login][0]
end
end
def test_validate_correct_length_login
assert_difference 'User.count' do
create_user :login => generate_random_string(6)
@ -206,14 +206,14 @@ class UserTest < ActiveSupport::TestCase
def test_find_and_activate_deferred_todos_that_are_ready
assert_equal 1, @admin_user.deferred_todos.count
@admin_user.deferred_todos[0].show_from = Time.now.utc - 5.seconds
@admin_user.deferred_todos[0].show_from = Time.zone.now - 5.seconds
@admin_user.deferred_todos[0].save(:validate => false)
@admin_user.deferred_todos.reload
@admin_user.deferred_todos.find_and_activate_ready
@admin_user.deferred_todos.reload
assert_equal 0, @admin_user.deferred_todos.count
end
def test_sort_active_projects_alphabetically
u = users(:admin_user)
u.projects.alphabetize(:state => "active")
@ -221,7 +221,7 @@ class UserTest < ActiveSupport::TestCase
assert_equal 2, projects(:gardenclean).position
assert_equal 3, projects(:moremoney).position
end
def test_sort_active_projects_alphabetically_case_insensitive
u = users(:admin_user)
projects(:timemachine).name = projects(:timemachine).name.downcase
@ -264,22 +264,22 @@ class UserTest < ActiveSupport::TestCase
users(:other_user).update_attributes(:password => 'new password', :password_confirmation => 'new password')
assert_equal users(:other_user), User.authenticate('jane', 'new password')
end
def test_should_not_rehash_password
users(:other_user).update_attributes(:login => 'jane2')
assert_equal users(:other_user), User.authenticate('jane2', 'sesame')
end
def test_should_authenticate_user
assert_equal users(:other_user), User.authenticate('jane', 'sesame')
end
def test_should_set_remember_token
users(:other_user).remember_me
assert_not_nil users(:other_user).remember_token
assert_not_nil users(:other_user).remember_token_expires_at
end
def test_should_unset_remember_token
users(:other_user).remember_me
assert_not_nil users(:other_user).remember_token
@ -319,7 +319,7 @@ class UserTest < ActiveSupport::TestCase
u.projects.actionize
assert_equal "3,2,1", u.projects.reload.map(&:id).join(",")
assert_equal "3,2,1", u.projects.reload.map(&:id).join(",")
end
def test_remember_token
@ -389,12 +389,12 @@ class UserTest < ActiveSupport::TestCase
assert_equal expect_notes_count, Note.count, "expected #{nr_of_notes} notes to be gone"
assert_equal expect_deps_count, Dependency.count, "expected #{nr_of_deps} dependencies to be gone"
end
protected
def create_user(options = {})
options[:password_confirmation] = options[:password] unless options.has_key?(:password_confirmation) || !options.has_key?(:password)
User.create({ :login => 'quire', :password => 'quire', :password_confirmation => 'quire' }.merge(options))
end
end