2014-05-16 18:03:13 -04:00
|
|
|
require 'test_helper'
|
2007-09-13 03:21:37 +00:00
|
|
|
|
2019-05-20 01:03:28 +03:00
|
|
|
# TODO: Add more detailed testing of the charts. There are previously defined tests in VCS before the Flash to Chart.js change.
|
2009-06-02 21:22:50 +02:00
|
|
|
class StatsControllerTest < ActionController::TestCase
|
2007-09-13 03:21:37 +00:00
|
|
|
|
2008-02-13 14:09:00 +00:00
|
|
|
def test_get_index_when_not_logged_in
|
|
|
|
|
get :index
|
2012-04-24 20:47:07 +02:00
|
|
|
assert_redirected_to login_url
|
2008-02-13 14:09:00 +00:00
|
|
|
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_totals
|
|
|
|
|
login_as(:admin_user)
|
|
|
|
|
get :index
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2008-02-13 14:09:00 +00:00
|
|
|
assert_response :success
|
2013-03-05 07:48:08 -07:00
|
|
|
totals = assigns['stats'].totals
|
|
|
|
|
assert_equal 4, totals.tags
|
|
|
|
|
assert_equal 2, totals.unique_tags
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2017-06-19 09:24:10 -05:00
|
|
|
longest_running_projects = assigns['stats'].projects.runtime
|
|
|
|
|
assert_equal longest_running_projects, users(:admin_user).projects.order('created_at').reverse
|
|
|
|
|
|
2015-08-04 23:08:13 +02:00
|
|
|
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
|
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
|
2012-06-27 21:40:12 +02:00
|
|
|
# this will fail 500
|
|
|
|
|
#
|
|
|
|
|
# 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
|
2018-11-03 11:00:27 -05:00
|
|
|
get :show_selected_actions_from_chart, params: { :id => 'avrt', :index => 0 }
|
2008-03-02 20:33:04 +00:00
|
|
|
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
|
2018-11-03 11:00:27 -05:00
|
|
|
get :show_selected_actions_from_chart, params: { :id => 'avrt_end', :index => 0 }
|
2008-03-02 20:33:04 +00:00
|
|
|
assert_response :success
|
|
|
|
|
assert_template "stats/show_selection_from_chart"
|
|
|
|
|
|
|
|
|
|
# get week 0-1 for actions running
|
2018-11-03 11:00:27 -05:00
|
|
|
get :show_selected_actions_from_chart, params: { :id => 'art', :index => 0 }
|
2008-03-02 20:33:04 +00:00
|
|
|
assert_response :success
|
|
|
|
|
assert_template "stats/show_selection_from_chart"
|
|
|
|
|
|
|
|
|
|
# get week 0 and further for actions running
|
2018-11-03 11:00:27 -05:00
|
|
|
get :show_selected_actions_from_chart, params: { :id => 'art_end', :index => 0 }
|
2008-03-02 20:33:04 +00:00
|
|
|
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
|
|
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
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
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
given_todos_for_stats
|
|
|
|
|
|
|
|
|
|
# When I get the chart data
|
2018-11-03 11:00:27 -05:00
|
|
|
get :show_selected_actions_from_chart, params: {:id => "avrt", :index => 1}
|
2012-01-03 22:08:50 +01:00
|
|
|
assert_response :success
|
|
|
|
|
|
|
|
|
|
assert_equal false, assigns['further'] # not at end
|
|
|
|
|
assert_equal 0, assigns['count']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_show_selected_actions_from_chart_avrt_end
|
|
|
|
|
login_as(:admin_user)
|
|
|
|
|
@current_user = User.find(users(:admin_user).id)
|
|
|
|
|
@current_user.todos.delete_all
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
given_todos_for_stats
|
|
|
|
|
|
|
|
|
|
# When I get the chart data
|
2018-11-03 11:00:27 -05:00
|
|
|
get :show_selected_actions_from_chart, params: {:id => "avrt_end", :index => 1}
|
2012-01-03 22:08:50 +01:00
|
|
|
assert_response :success
|
|
|
|
|
|
|
|
|
|
assert assigns['further'] # at end
|
|
|
|
|
assert_equal 2, assigns['count']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_show_selected_actions_from_chart_art
|
|
|
|
|
login_as(:admin_user)
|
|
|
|
|
@current_user = User.find(users(:admin_user).id)
|
|
|
|
|
@current_user.todos.delete_all
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
given_todos_for_stats
|
|
|
|
|
|
|
|
|
|
# When I get the chart data
|
2018-11-03 11:00:27 -05:00
|
|
|
get :show_selected_actions_from_chart, params: {:id => "art", :index => 1}
|
2012-01-03 22:08:50 +01:00
|
|
|
assert_response :success
|
|
|
|
|
|
|
|
|
|
assert_equal false, assigns['further'] # not at end
|
|
|
|
|
assert_equal 0, assigns['count']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_show_selected_actions_from_chart_art_end
|
|
|
|
|
login_as(:admin_user)
|
|
|
|
|
@current_user = User.find(users(:admin_user).id)
|
|
|
|
|
@current_user.todos.delete_all
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
given_todos_for_stats
|
|
|
|
|
|
|
|
|
|
# When I get the chart data
|
2018-11-03 11:00:27 -05:00
|
|
|
get :show_selected_actions_from_chart, params: {:id => "art_end", :index => 1}
|
2012-01-03 22:08:50 +01:00
|
|
|
assert_response :success
|
|
|
|
|
|
|
|
|
|
assert assigns['further'] # at end
|
|
|
|
|
assert_equal 2, assigns['count']
|
|
|
|
|
end
|
|
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
private
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
def given_todos_for_stats
|
|
|
|
|
# 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
|
2011-12-09 17:17:42 +01:00
|
|
|
@todo_month1 = create_completed_todo_in_past(1.month+1.weeks+1.day, 1.month+1.day)
|
2011-11-28 23:38:57 +01:00
|
|
|
# And a todo created two months ago
|
2011-12-09 17:17:42 +01:00
|
|
|
@todo_month2 = create_completed_todo_in_past(2.months+2.days, 2.months+1.day)
|
2011-11-28 23:38:57 +01:00
|
|
|
# And a todo created three months ago
|
|
|
|
|
@todo_month3 = create_todo_in_past(3.months+1.day)
|
|
|
|
|
# And a todo created four months ago
|
|
|
|
|
@todo_month4 = create_todo_in_past(4.months+1.day)
|
|
|
|
|
# And a todo created four months ago
|
2011-12-09 17:17:42 +01:00
|
|
|
@todo_month5 = create_completed_todo_in_past(4.months+2.days, 4.months+1.day)
|
2011-11-28 23:38:57 +01:00
|
|
|
# And a todo created over a year ago
|
2011-12-09 17:17:42 +01:00
|
|
|
@todo_year1 = create_completed_todo_in_past(2.years+2.days, 2.years+1.day)
|
|
|
|
|
@todo_year2 = create_completed_todo_in_past(2.years+3.months, 2.years+1.day)
|
2011-11-25 14:32:07 +01:00
|
|
|
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
|
|
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# assumes date1 > date2
|
|
|
|
|
def difference_in_days(date1, date2)
|
|
|
|
|
return ((date1.at_midnight-date2.at_midnight)/(60*60*24)).to_i
|
|
|
|
|
end
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# assumes date1 > date2
|
|
|
|
|
def difference_in_weeks(date1, date2)
|
|
|
|
|
return difference_in_days(date1, date2) / 7
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# assumes date1 > date2
|
|
|
|
|
def difference_in_months(date1, date2)
|
|
|
|
|
diff = (date1.year - date2.year)*12 + (date1.month - date2.month)
|
|
|
|
|
return diff-1 if date1.day - date2.day < 0 # correct for incomplete months
|
|
|
|
|
return diff
|
|
|
|
|
end
|
|
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
end
|