2011-10-09 20:30:48 +02:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2007-09-13 03:21:37 +00:00
|
|
|
require 'stats_controller'
|
|
|
|
|
|
|
|
# Re-raise errors caught by the controller.
|
|
|
|
class StatsController; def rescue_action(e) raise e end; end
|
|
|
|
|
2009-06-02 21:22:50 +02:00
|
|
|
class StatsControllerTest < ActionController::TestCase
|
2009-01-23 14:15:02 -05:00
|
|
|
fixtures :users, :preferences, :projects, :contexts, :todos, :recurring_todos, :recurring_todos, :tags, :taggings
|
2011-09-13 07:11:33 +02:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
def setup
|
|
|
|
@controller = StatsController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
end
|
|
|
|
|
2008-02-13 14:09:00 +00:00
|
|
|
def test_get_index_when_not_logged_in
|
|
|
|
get :index
|
|
|
|
assert_redirected_to :controller => 'login', :action => 'login'
|
|
|
|
end
|
2011-09-13 07:11:33 +02:00
|
|
|
|
2008-02-13 14:09:00 +00:00
|
|
|
def test_get_index
|
|
|
|
login_as(:admin_user)
|
|
|
|
get :index
|
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get_charts
|
|
|
|
login_as(:admin_user)
|
|
|
|
%w{ actions_done_last30days_data
|
|
|
|
actions_done_last12months_data
|
|
|
|
actions_completion_time_data
|
|
|
|
actions_visible_running_time_data
|
|
|
|
actions_running_time_data
|
|
|
|
actions_day_of_week_all_data
|
|
|
|
actions_day_of_week_30days_data
|
|
|
|
actions_time_of_day_all_data
|
|
|
|
actions_time_of_day_30days_data
|
|
|
|
context_total_actions_data
|
|
|
|
context_running_actions_data
|
|
|
|
}.each do |action|
|
|
|
|
get action
|
|
|
|
assert_response :success
|
|
|
|
assert_template "stats/"+action
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_totals
|
|
|
|
login_as(:admin_user)
|
|
|
|
get :index
|
|
|
|
assert_response :success
|
2009-01-08 10:18:03 +01:00
|
|
|
assert_equal 4, assigns['tags_count']
|
|
|
|
assert_equal 2, assigns['unique_tags_count']
|
2011-09-13 07:11:33 +02:00
|
|
|
assert_equal 2.week.ago.utc.at_midnight, assigns['first_action'].created_at.utc.at_midnight
|
2008-02-13 14:09:00 +00:00
|
|
|
end
|
2011-09-13 07:11:33 +02:00
|
|
|
|
2008-03-02 20:33:04 +00:00
|
|
|
def test_downdrill
|
|
|
|
login_as(:admin_user)
|
2011-09-13 07:11:33 +02:00
|
|
|
|
2008-03-02 20:33:04 +00:00
|
|
|
# drill down without parameters
|
|
|
|
get :show_selected_actions_from_chart
|
|
|
|
assert_response :not_found
|
|
|
|
assert_template nil
|
2011-09-13 07:11:33 +02:00
|
|
|
|
2008-03-02 20:33:04 +00:00
|
|
|
# get week 0-1 for actions visible running
|
|
|
|
get :show_selected_actions_from_chart, :id => 'avrt', :index => 0
|
|
|
|
assert_response :success
|
|
|
|
assert_template "stats/show_selection_from_chart"
|
|
|
|
|
2011-09-13 07:11:33 +02:00
|
|
|
# get week 0 and further for actions visible running
|
2008-03-02 20:33:04 +00:00
|
|
|
get :show_selected_actions_from_chart, :id => 'avrt_end', :index => 0
|
|
|
|
assert_response :success
|
|
|
|
assert_template "stats/show_selection_from_chart"
|
|
|
|
|
|
|
|
# get week 0-1 for actions running
|
|
|
|
get :show_selected_actions_from_chart, :id => 'art', :index => 0
|
|
|
|
assert_response :success
|
|
|
|
assert_template "stats/show_selection_from_chart"
|
|
|
|
|
|
|
|
# get week 0 and further for actions running
|
|
|
|
get :show_selected_actions_from_chart, :id => 'art_end', :index => 0
|
|
|
|
assert_response :success
|
|
|
|
assert_template "stats/show_selection_from_chart"
|
|
|
|
end
|
2009-01-15 22:13:22 +01:00
|
|
|
|
|
|
|
def test_stats_render_when_tasks_have_no_taggings
|
|
|
|
login_as(:admin_user)
|
|
|
|
|
|
|
|
# using the default fixtures, todos have tags
|
|
|
|
get :index
|
|
|
|
assert_response :success
|
|
|
|
|
|
|
|
# clear taggings table and render again
|
2009-01-17 14:43:06 +01:00
|
|
|
Tagging.delete_all
|
2009-01-15 22:13:22 +01:00
|
|
|
get :index
|
|
|
|
assert_response :success
|
2011-11-25 14:32:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_actions_done_last12months_data
|
|
|
|
login_as(:admin_user)
|
|
|
|
@current_user = User.find(users(:admin_user).id)
|
|
|
|
@current_user.todos.delete_all
|
|
|
|
|
|
|
|
# Given two todos created today
|
|
|
|
todo_today1 = @current_user.todos.create!(:description => "created today1", :context => contexts(:office))
|
|
|
|
todo_today2 = @current_user.todos.create!(:description => "created today2", :context => contexts(:office))
|
|
|
|
# And a todo created a month ago
|
|
|
|
todo_month1 = create_todo_in_past(1.month+1.day)
|
|
|
|
# And a todo created two months ago
|
|
|
|
todo_month2 = create_completed_todo_in_past(2.months+1.day, 2.months+2.days)
|
|
|
|
# And a todo created three months ago
|
|
|
|
todo_month3 = create_todo_in_past(3.months+1.day)
|
|
|
|
# And a todo created over a year ago
|
|
|
|
todo_year = create_todo_in_past(2.years+1.day)
|
|
|
|
|
|
|
|
# When I get the chart data
|
|
|
|
get :actions_done_last12months_data
|
|
|
|
assert_response :success
|
|
|
|
|
|
|
|
# Then the todos for the chart should be retrieved
|
|
|
|
assert_not_nil assigns['actions_done_last12months']
|
|
|
|
assert_not_nil assigns['actions_created_last12months']
|
|
|
|
assert_equal 5, assigns['actions_created_last12months'].count, "very old todo should not be retrieved"
|
|
|
|
|
|
|
|
# And they should be totalled in a hash
|
|
|
|
assert_equal 2, assigns['actions_created_last12months_hash'][0], "there should be two todos in current month"
|
|
|
|
assert_equal 1, assigns['actions_created_last12months_hash'][1], "there should be one todo in previous month"
|
|
|
|
assert_equal 1, assigns['actions_created_last12months_hash'][2], "there should be one todo in two month ago"
|
|
|
|
assert_equal 1, assigns['actions_created_last12months_hash'][3], "there should be one todo in three month ago"
|
2009-01-15 22:13:22 +01:00
|
|
|
|
2011-11-25 14:32:07 +01:00
|
|
|
assert_equal 1, assigns['actions_done_last12months_hash'][2], "there should be one completed todo in two month ago"
|
|
|
|
|
|
|
|
# And they should be averaged
|
|
|
|
# And the current month should be interpolated
|
|
|
|
# And totals should be calculated
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_todo_in_past(creation_time_in_past)
|
|
|
|
todo = @current_user.todos.create!(:description => "created #{creation_time_in_past} ago", :context => contexts(:office))
|
|
|
|
todo.created_at = Time.zone.now - creation_time_in_past
|
|
|
|
todo.save!
|
|
|
|
return todo
|
2009-01-15 22:13:22 +01:00
|
|
|
end
|
2011-11-25 14:32:07 +01:00
|
|
|
|
|
|
|
def create_completed_todo_in_past(creation_time_in_past, completed_time_in_past)
|
|
|
|
todo = @current_user.todos.create!(:description => "created #{creation_time_in_past} ago", :context => contexts(:office))
|
|
|
|
todo.complete!
|
|
|
|
todo.completed_at = Time.zone.now - completed_time_in_past
|
|
|
|
todo.created_at = Time.zone.now - creation_time_in_past
|
|
|
|
todo.save!
|
|
|
|
return todo
|
|
|
|
end
|
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
end
|