Merge pull request #1903 from lrbalt/fix-test-errors

Fix failing tests for non-UTC timezones
This commit is contained in:
Matt Rogers 2015-08-07 19:50:58 -04:00
commit 1c0a70df75
27 changed files with 205 additions and 198 deletions

View file

@ -184,7 +184,7 @@ class RecurringTodosControllerTest < ActionController::TestCase
# mark as active
xhr :post, :toggle_check, :id=>1, :_source_view=>""
recurring_todo_1 = RecurringTodo.find(1) # reload seems to not work
assert recurring_todo_1.active?, "recurring todo should be active but is #{recurring_todo_1.aasm.current_state}"
@ -203,7 +203,7 @@ class RecurringTodosControllerTest < ActionController::TestCase
# change due date in four days from now and show from 10 days before, i.e. 6
# days ago
target_date = Time.now.utc + 4.days
target_date = Time.zone.now + 4.days
@yearly.every_other1 = target_date.day
@yearly.every_other2 = target_date.month
@yearly.show_from_delta = 10
@ -260,7 +260,7 @@ class RecurringTodosControllerTest < ActionController::TestCase
"recurring_show_days_before"=>"0",
"recurring_target"=>"due_date",
"recurring_show_always" => "1",
"start_from"=>"1/10/2012",
"start_from"=>"1/10/2012",
"weekly_every_x_week"=>"1",
"weekly_return_monday"=>"w",
"yearly_day_of_week"=>"0",
@ -419,9 +419,9 @@ class RecurringTodosControllerTest < ActionController::TestCase
login_as(:admin_user)
rt = recurring_todos(:call_bill_gates_every_day)
put :update,
"recurring_todo" => {
"description" => "changed",
put :update,
"recurring_todo" => {
"description" => "changed",
"daily_selector" => "daily_every_x_day",
"daily_every_x_days" => "2",
"ends_on" => "no_end_date",
@ -433,7 +433,7 @@ class RecurringTodosControllerTest < ActionController::TestCase
"recurring_todo_edit_start_from" => "2/1/2013",
"end_date" => nil,
"ends_on" => "no_end_date",
"id" => "#{rt.id}",
"id" => "#{rt.id}",
"context_name" => "library",
format: :js

View file

@ -45,11 +45,14 @@ class StatsControllerTest < ActionController::TestCase
def test_totals
login_as(:admin_user)
get :index
assert_response :success
totals = assigns['stats'].totals
assert_equal 4, totals.tags
assert_equal 2, totals.unique_tags
assert_equal 2.week.ago.utc.at_midnight, totals.first_action_at.utc.at_midnight
Time.zone = users(:admin_user).prefs.time_zone # calculations are done in users timezone
assert_equal 2.weeks.ago.at_midnight, totals.first_action_at.at_midnight
end
def test_downdrill
@ -128,15 +131,15 @@ class StatsControllerTest < ActionController::TestCase
# And they should be averaged over three months
assert_equal 2/3.0, assigns['actions_done_avg_last12months_array'][1], "fourth month should be excluded"
assert_equal 2/3.0, assigns['actions_done_avg_last12months_array'][2], "fourth month should be included"
assert_equal (3)/3.0, assigns['actions_created_avg_last12months_array'][1], "one every month"
assert_equal (4)/3.0, assigns['actions_created_avg_last12months_array'][2], "two in fourth month"
# And the current month should be interpolated
fraction = Time.zone.now.day.to_f / Time.zone.now.end_of_month.day.to_f
assert_equal (2*(1/fraction)+2)/3.0, assigns['interpolated_actions_created_this_month'], "two this month and one in the last two months"
assert_equal (2)/3.0, assigns['interpolated_actions_done_this_month'], "none this month and one two the last two months"
# And totals should be calculated
assert_equal 2, assigns['max'], "max of created or completed todos in one month"
end
@ -168,7 +171,7 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
@ -176,7 +179,7 @@ class StatsControllerTest < ActionController::TestCase
assert_response :success
# only tests relevant differences with actions_done_last_12months_data
assert_equal 31, assigns['actions_done_last30days_array'].size, "30 complete days plus 1 for the current day"
assert_equal 2, assigns['max'], "two actions created on one day is max"
end
@ -185,31 +188,31 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
get :actions_done_lastyears_data
assert_response :success
# only tests difference with actions_done_last_12months_data
# And the last two months are corrected
assert_equal 2/3.0, assigns['actions_done_avg_last_months_array'][23]
assert_equal 2/3.0, assigns['actions_done_avg_last_months_array'][24]
end
def test_actions_completion_time_data
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
get :actions_completion_time_data
assert_response :success
# do not test stuff already implicitly tested in other tests
assert_equal 104, assigns['max_weeks'], "two years is 104 weeks (for completed_at)"
assert_equal 3, assigns['max_actions'], "3 completed within one week"
@ -222,13 +225,13 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
get :actions_running_time_data
assert_response :success
# do not test stuff already implicitly tested in other tests
assert_equal 17, assigns['max_weeks'], "there are actions in the first 17 weeks of this year"
assert_equal 2, assigns['max_actions'], "2 actions running long together"
@ -241,13 +244,13 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
get :actions_open_per_week_data
assert_response :success
# do not test stuff already implicitly tested in other tests
assert_equal 17, assigns['max_weeks'], "there are actions in the first 17 weeks of this year"
assert_equal 4, assigns['max_actions'], "4 actions running together"
@ -258,12 +261,12 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# Given todo1 is deferred (i.e. not visible)
@todo_today1.show_from = Time.zone.now + 1.week
@todo_today1.save
# When I get the chart data
get :actions_visible_running_time_data
assert_response :success
@ -281,7 +284,7 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
@ -311,7 +314,7 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
@ -335,12 +338,12 @@ class StatsControllerTest < ActionController::TestCase
assert_equal 14, assigns['data'].values[9], "pie slices limited to max 10; last pie contains sum of rest (in percentage)"
assert_equal "(others)", assigns['data'].labels[9], "pie slices limited to max 10; last slice contains label for others"
end
def test_actions_day_of_week_all_data
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
@ -358,7 +361,7 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
@ -376,7 +379,7 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
@ -389,12 +392,12 @@ class StatsControllerTest < ActionController::TestCase
assert_not_nil assigns['actions_creation_hour_array']
assert_not_nil assigns['actions_completion_hour_array']
end
def test_show_selected_actions_from_chart_avrt
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
@ -409,7 +412,7 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
@ -424,7 +427,7 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
@ -439,7 +442,7 @@ class StatsControllerTest < ActionController::TestCase
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
@ -451,7 +454,7 @@ class StatsControllerTest < ActionController::TestCase
end
private
def given_todos_for_stats
# Given two todos created today
@todo_today1 = @current_user.todos.create!(:description => "created today1", :context => contexts(:office))
@ -491,7 +494,7 @@ class StatsControllerTest < ActionController::TestCase
def difference_in_days(date1, date2)
return ((date1.at_midnight-date2.at_midnight)/(60*60*24)).to_i
end
# assumes date1 > date2
def difference_in_weeks(date1, date2)
return difference_in_days(date1, date2) / 7

View file

@ -120,7 +120,7 @@ class TodosControllerTest < ActionController::TestCase
assert_response :success
assert_equal 3, @tagged
end
def test_find_tagged_with_terms_separated_with_dot
login_as :admin_user
create_todo(description: "test dotted tag", tag_list: "first.last, second")
@ -408,7 +408,7 @@ class TodosControllerTest < ActionController::TestCase
#######
# defer
#######
#######
def test_update_clearing_show_from_makes_todo_active
t = Todo.find(1)
@ -437,7 +437,7 @@ class TodosControllerTest < ActionController::TestCase
# given a todo in the tickler that should be activated
travel_to 2.weeks.ago do
create_todo(
description: "tickler",
description: "tickler",
show_from: 1.week.from_now.
in_time_zone(users(:admin_user).prefs.time_zone).
strftime("#{users(:admin_user).prefs.date_format}"))
@ -715,17 +715,25 @@ class TodosControllerTest < ActionController::TestCase
end
def test_toggle_check_on_rec_todo_show_from_today
# warning: the Time.zone set in site.yml will be overwritten by
# :admin_user.prefs.time_zone in ApplicationController. This messes with
# the calculation. So set time_zone to admin_user's time_zone setting
Time.zone = users(:admin_user).prefs.time_zone
travel_to Time.zone.local(2014, 1, 15) do
today = Time.zone.now.at_midnight
login_as(:admin_user)
# link todo_1 and recurring_todo_1
recurring_todo_1 = RecurringTodo.find(1)
todo_1 = Todo.where(:recurring_todo_id => 1).first
today = Time.zone.now.at_midnight
todo_1.due = today
assert todo_1.save
# change recurrence pattern to monthly and set show_from to today
# change recurrence pattern to monthly on a specific
# day (recurrence_selector=0) and set show_from
# (every_other2=1) to today
recurring_todo_1.target = 'show_from_date'
recurring_todo_1.recurring_period = 'monthly'
recurring_todo_1.recurrence_selector = 0
@ -746,15 +754,13 @@ class TodosControllerTest < ActionController::TestCase
assert_not_equal todo_1.id, new_todo.id, "check that the new todo is not the same as todo_1"
assert !new_todo.show_from.nil?, "check that the new_todo is in the tickler to show next month"
# do not use today here. It somehow gets messed up with the timezone calculation.
next_month = (Time.zone.now + 1.month).at_midnight
assert_equal next_month.utc.to_date.to_s(:db), new_todo.show_from.utc.to_date.to_s(:db)
assert_equal today + 1.month, new_todo.show_from
end
end
def test_check_for_next_todo
login_as :admin_user
Time.zone = users(:admin_user).prefs.time_zone
tomorrow = Time.zone.now + 1.day
@ -791,6 +797,7 @@ class TodosControllerTest < ActionController::TestCase
def test_check_for_next_todo_monthly
login_as :admin_user
Time.zone = users(:admin_user).prefs.time_zone
tomorrow = Time.zone.now + 1.day
@ -1024,13 +1031,13 @@ class TodosControllerTest < ActionController::TestCase
private
def create_todo(params={})
defaults = { source_view: 'todo',
context_name: "library", project_name: "Build a working time machine",
defaults = { source_view: 'todo',
context_name: "library", project_name: "Build a working time machine",
notes: "note", description: "a new todo", due: nil, tag_list: "a,b,c"}
params=params.reverse_merge(defaults)
put :create, _source_view: params[:_source_view],
put :create, _source_view: params[:_source_view],
context_name: params[:context_name], project_name: params[:project_name], tag_list: params[:tag_list],
todo: {notes: params[:notes], description: params[:description], due: params[:due], show_from: params[:show_from]}
end