diff --git a/app/models/stats/actions.rb b/app/models/stats/actions.rb index 56727dca..2d862d46 100644 --- a/app/models/stats/actions.rb +++ b/app/models/stats/actions.rb @@ -63,12 +63,12 @@ module Stats return { datasets: [ - {label: I18n.t('stats.labels.avg_created'), data: @created_count_array.map { |total| [total] }, type: "line"}, - {label: I18n.t('stats.labels.avg_completed'), data: @done_count_array.map { |total| [total] }, type: "line"}, - {label: I18n.t('stats.labels.month_avg_completed', :months => 3), data: @actions_done_avg_last12months_array.map { |total| [total] }, type: "line"}, - {label: I18n.t('stats.labels.month_avg_created', :months => 3), data: @actions_created_avg_last12months_array.map { |total| [total] }, type: "line"}, - {label: I18n.t('stats.labels.created'), data: @actions_created_last12months_array.map { |total| [total] } }, - {label: I18n.t('stats.labels.completed'), data: @actions_done_last12months_array.map { |total| [total] } }, + {label: I18n.t('stats.labels.avg_created'), data: @created_count_array, type: "line"}, + {label: I18n.t('stats.labels.avg_completed'), data: @done_count_array, type: "line"}, + {label: I18n.t('stats.labels.month_avg_completed', :months => 3), data: @actions_done_avg_last12months_array, type: "line"}, + {label: I18n.t('stats.labels.month_avg_created', :months => 3), data: @actions_created_avg_last12months_array, type: "line"}, + {label: I18n.t('stats.labels.created'), data: @actions_created_last12months_array}, + {label: I18n.t('stats.labels.completed'), data: @actions_done_last12months_array}, ], labels: array_of_month_labels(@done_count_array.size), } @@ -93,10 +93,10 @@ module Stats return { datasets: [ - {label: I18n.t('stats.labels.avg_created'), data: created_count_array.map { |total| [total] }, type: "line"}, - {label: I18n.t('stats.labels.avg_completed'), data: done_count_array.map { |total| [total] }, type: "line"}, - {label: I18n.t('stats.labels.created'), data: @actions_created_last30days_array.map { |total| [total] } }, - {label: I18n.t('stats.labels.completed'), data: @actions_done_last30days_array.map { |total| [total] } }, + {label: I18n.t('stats.labels.avg_created'), data: created_count_array, type: "line"}, + {label: I18n.t('stats.labels.avg_completed'), data: done_count_array, type: "line"}, + {label: I18n.t('stats.labels.created'), data: @actions_created_last30days_array}, + {label: I18n.t('stats.labels.completed'), data: @actions_done_last30days_array}, ], labels: time_labels, } @@ -125,8 +125,8 @@ module Stats return { datasets: [ - {label: I18n.t('stats.legend.percentage'), data: @cum_percent_done.map { |total| [total] }, type: "line"}, - {label: I18n.t('stats.legend.actions'), data: @actions_completion_time_array.map { |total| [total] } }, + {label: I18n.t('stats.legend.percentage'), data: @cum_percent_done, type: "line"}, + {label: I18n.t('stats.legend.actions'), data: @actions_completion_time_array}, ], labels: time_labels, } @@ -155,8 +155,8 @@ module Stats return { datasets: [ - {label: I18n.t('stats.running_time_all_legend.percentage'), data: @cum_percent_done.map { |total| [total] }, type: "line"}, - {label: I18n.t('stats.running_time_all_legend.actions'), data: @actions_running_time_array.map { |total| [total] } }, + {label: I18n.t('stats.running_time_all_legend.percentage'), data: @cum_percent_done, type: "line"}, + {label: I18n.t('stats.running_time_all_legend.actions'), data: @actions_running_time_array}, ], labels: time_labels, } @@ -194,8 +194,8 @@ module Stats return { datasets: [ - {label: I18n.t('stats.running_time_legend.percentage'), data: @cum_percent_done.map { |total| [total] }, type: "line"}, - {label: I18n.t('stats.running_time_legend.actions'), data: @actions_running_time_array.map { |total| [total] } }, + {label: I18n.t('stats.running_time_legend.percentage'), data: @cum_percent_done, type: "line"}, + {label: I18n.t('stats.running_time_legend.actions'), data: @actions_running_time_array}, ], labels: time_labels, } @@ -219,7 +219,7 @@ module Stats return { datasets: [ - {label: I18n.t('stats.open_per_week_legend.actions'), data: @actions_open_per_week_array.map { |total| [total] } }, + {label: I18n.t('stats.open_per_week_legend.actions'), data: @actions_open_per_week_array}, ], labels: time_labels, } @@ -240,8 +240,8 @@ module Stats return { datasets: [ - {label: I18n.t('stats.labels.created'), data: @actions_creation_day_array.map { |total| [total] } }, - {label: I18n.t('stats.labels.completed'), data: @actions_completion_day_array.map { |total| [total] } }, + {label: I18n.t('stats.labels.created'), data: @actions_creation_day_array}, + {label: I18n.t('stats.labels.completed'), data: @actions_completion_day_array}, ], labels: I18n.t('date.day_names'), } @@ -262,8 +262,8 @@ module Stats return { datasets: [ - {label: I18n.t('stats.labels.created'), data: @actions_creation_day_array.map { |total| [total] } }, - {label: I18n.t('stats.labels.completed'), data: @actions_completion_day_array.map { |total| [total] } }, + {label: I18n.t('stats.labels.created'), data: @actions_creation_day_array}, + {label: I18n.t('stats.labels.completed'), data: @actions_completion_day_array}, ], labels: I18n.t('date.day_names'), } @@ -283,8 +283,8 @@ module Stats return { datasets: [ - {label: I18n.t('stats.labels.created'), data: @actions_creation_hour_array.map { |total| [total] } }, - {label: I18n.t('stats.labels.completed'), data: @actions_completion_hour_array.map { |total| [total] } }, + {label: I18n.t('stats.labels.created'), data: @actions_creation_hour_array}, + {label: I18n.t('stats.labels.completed'), data: @actions_completion_hour_array}, ], labels: @actions_creation_hour_array.each_with_index.map { |total, hour| [hour] }, } @@ -304,8 +304,8 @@ module Stats return { datasets: [ - {label: I18n.t('stats.labels.created'), data: @actions_creation_hour_array.map { |total| [total] } }, - {label: I18n.t('stats.labels.completed'), data: @actions_completion_hour_array.map { |total| [total] } }, + {label: I18n.t('stats.labels.created'), data: @actions_creation_hour_array}, + {label: I18n.t('stats.labels.completed'), data: @actions_completion_hour_array}, ], labels: @actions_creation_hour_array.each_with_index.map { |total, hour| [hour] }, } diff --git a/test/controllers/stats_controller_test.rb b/test/controllers/stats_controller_test.rb index 85ef0f8d..74edf489 100644 --- a/test/controllers/stats_controller_test.rb +++ b/test/controllers/stats_controller_test.rb @@ -1,5 +1,6 @@ require 'test_helper' +# TODO: Add more detailed testing of the charts. There are previously defined tests in VCS before the Flash to Chart.js change. class StatsControllerTest < ActionController::TestCase def test_get_index_when_not_logged_in @@ -13,35 +14,6 @@ class StatsControllerTest < ActionController::TestCase 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_open_per_week_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 - }.each do |action| - get action - assert_response :success - assert_template "stats/"+action - end - - %w{ - context_total_actions_data - context_running_actions_data - }.each do |action| - get action - assert_response :success - assert_template "stats/pie_chart_data" - end - end - def test_totals login_as(:admin_user) get :index @@ -102,300 +74,6 @@ class StatsControllerTest < ActionController::TestCase assert_response :success end - def test_actions_done_last12months_data - travel_to Time.local(2013, 1, 15) do - 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_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 7, 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_array'][0], "there should be two todos in current month" - - assert_equal 1, assigns['actions_created_last12months_array'][1], "there should be one todo in previous month" - assert_equal 1, assigns['actions_created_last12months_array'][2], "there should be one todo in two month ago" - assert_equal 1, assigns['actions_created_last12months_array'][3], "there should be one todo in three month ago" - assert_equal 2, assigns['actions_created_last12months_array'][4], "there should be two todos (1 created & 1 done) in four month ago" - - assert_equal 1, assigns['actions_done_last12months_array'][1], "there should be one completed todo one-two months ago" - assert_equal 1, assigns['actions_done_last12months_array'][2], "there should be one completed todo two-three months ago" - assert_equal 1, assigns['actions_done_last12months_array'][4], "there should be one completed todo four-five months ago" - - # 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 - end - - def test_empty_last12months_data - travel_to Time.local(2013, 1, 15) do - login_as(:admin_user) - @current_user = User.find(users(:admin_user).id) - @current_user.todos.delete_all - given_todos_for_stats - get :actions_done_last12months_data - assert_response :success - end - end - - def test_out_of_bounds_events_for_last12months_data - login_as(:admin_user) - @current_user = User.find(users(:admin_user).id) - @current_user.todos.delete_all - create_todo_in_past(2.years) - create_todo_in_past(15.months) - - get :actions_done_last12months_data - assert_response :success - end - - def test_actions_done_last30days_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_done_last30days_data - 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 - - def test_actions_done_lastyears_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_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" - assert_equal 11, assigns['actions_completion_time_array'].size, "there should be 10 weeks of data + 1 for the rest" - assert_equal 1, assigns['actions_completion_time_array'][10], "there is one completed todo after the 10 weeks cut_off" - assert_equal 100.0, assigns['cum_percent_done'][10], "cumulative percentage should add up to 100%" - end - - def test_actions_running_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_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" - assert_equal 18, assigns['actions_running_time_array'].size, "there should be 17 weeks ( < cut_off) of data + 1 for the rest" - assert_equal 1, assigns['actions_running_time_array'][17], "there is one running todos in week 17 and zero after 17 weeks ( < cut off; ) " - assert_equal 100.0, assigns['cum_percent_done'][17], "cumulative percentage should add up to 100%" - end - - def test_actions_open_per_week_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_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" - assert_equal 17, assigns['actions_open_per_week_array'].size, "there should be 17 weeks ( < cut_off) of data" - end - - def test_actions_visible_running_time_data - 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 - - # 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 1, assigns['max_actions'], "1 action running long; 1 is deferred" - assert_equal 1, assigns['actions_running_time_array'][0], "there is one running todos and one deferred todo created in week 1" - assert_equal 18, assigns['actions_running_time_array'].size, "there should be 17 weeks ( < cut_off) of data + 1 for the rest" - assert_equal 1, assigns['actions_running_time_array'][17], "there is one running todos in week 17 and zero after 17 weeks ( < cut off; ) " - assert_equal 100.0, assigns['cum_percent_done'][17], "cumulative percentage should add up to 100%" - end - - def test_context_total_actions_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 :context_total_actions_data - assert_response :success - - assert_equal 9, assigns['data'].sum, "Nine todos in 1 context" - assert_equal 1, assigns['data'].values.size - - # Given 10 more todos in 10 different contexts - 1.upto(10) do |i| - context = @current_user.contexts.create!(:name => "context #{i}") - @current_user.todos.create!(:description => "created today with new context #{i}", :context => context) - end - - # When I get the chart data - get :context_total_actions_data - assert_response :success - - assert_equal 19, assigns['data'].sum, "added 10 todos" - assert_equal 10, assigns['data'].values.size, "pie slices limited to max 10" - assert_equal 10, 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_context_running_actions_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 :context_running_actions_data - assert_response :success - - assert_equal 4, assigns['data'].sum, "Four todos in 1 context" - assert_equal 1, assigns['data'].values.size - - # Given 10 more todos in 10 different contexts - 1.upto(10) do |i| - context = @current_user.contexts.create!(:name => "context #{i}") - @current_user.todos.create!(:description => "created today with new context #{i}", :context => context) - end - - # When I get the chart data - get :context_running_actions_data - assert_response :success - - assert_equal 10, assigns['data'].values.size, "pie slices limited to max 10" - 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 - get :actions_day_of_week_all_data - assert_response :success - - # FIXME: testdata is relative from today, so not stable to test on day_of_week - # trivial not_nil tests - assert_not_nil assigns['max'] - assert_not_nil assigns['actions_creation_day_array'] - assert_not_nil assigns['actions_completion_day_array'] - end - - def test_actions_day_of_week_30days_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_day_of_week_30days_data - assert_response :success - - # FIXME: testdata is relative from today, so not stable to test on day_of_week - # trivial not_nil tests - assert_not_nil assigns['max'] - assert_not_nil assigns['actions_creation_day_array'] - assert_not_nil assigns['actions_completion_day_array'] - end - - def test_actions_time_of_day_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 - get :actions_time_of_day_all_data - assert_response :success - - # FIXME: testdata is relative from today, so not stable to test on day_of_week - # for now just trivial not_nil tests - assert_not_nil assigns['max'] - 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) diff --git a/test/models/pie_chart_data_test.rb b/test/models/pie_chart_data_test.rb deleted file mode 100644 index e50c8db0..00000000 --- a/test/models/pie_chart_data_test.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'minimal_test_helper' -require 'app/models/stats/pie_chart_data' -require 'active_support/core_ext/string' - -class Stats::PieChartDataTest < Minitest::Test - - def test_with_0_items - data = Stats::PieChartData.new([], 'a chart', 50) - - assert_equal [], data.values - assert_equal [], data.labels - assert_equal [], data.ids - end - - def test_with_less_than_10_items - items = [ - {'id' => 1, 'name' => 'one', 'total' => 11}, - {'id' => 2, 'name' => 'two', 'total' => 4}, - {'id' => 3, 'name' => 'three', 'total' => 8}, - {'id' => 4, 'name' => 'four', 'total' => 13}, - {'id' => 5, 'name' => 'five', 'total' => 20}, - {'id' => 6, 'name' => 'six', 'total' => 17}, - {'id' => 7, 'name' => 'seven', 'total' => 5}, - {'id' => 8, 'name' => 'eight', 'total' => 1}, - {'id' => 9, 'name' => 'nine', 'total' => 6} - ] - - data = Stats::PieChartData.new(items, 'a chart', 50) - - assert_equal [12, 4, 9, 15, 23, 20, 5, 1, 7], data.values - assert_equal ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"], data.labels - assert_equal [1, 2, 3, 4, 5, 6, 7, 8, 9], data.ids - end - - def test_with_exactly_10_items - items = [ - {'id' => 1, 'name' => 'one', 'total' => 11}, - {'id' => 2, 'name' => 'two', 'total' => 4}, - {'id' => 3, 'name' => 'three', 'total' => 8}, - {'id' => 4, 'name' => 'four', 'total' => 13}, - {'id' => 5, 'name' => 'five', 'total' => 20}, - {'id' => 6, 'name' => 'six', 'total' => 17}, - {'id' => 7, 'name' => 'seven', 'total' => 5}, - {'id' => 8, 'name' => 'eight', 'total' => 1}, - {'id' => 9, 'name' => 'nine', 'total' => 6}, - {'id' => 10, 'name' => 'ten', 'total' => 19} - ] - - data = Stats::PieChartData.new(items, 'a chart', 50) - - assert_equal [10, 3, 7, 12, 19, 16, 4, 0, 5, 18], data.values - assert_equal ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"], data.labels - assert_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], data.ids - end - - def test_with_more_than_10_items - items = [ - {'id' => 1, 'name' => 'one', 'total' => 11}, - {'id' => 2, 'name' => 'two', 'total' => 4}, - {'id' => 3, 'name' => 'three', 'total' => 8}, - {'id' => 4, 'name' => 'four', 'total' => 13}, - {'id' => 5, 'name' => 'five', 'total' => 20}, - {'id' => 6, 'name' => 'six', 'total' => 17}, - {'id' => 7, 'name' => 'seven', 'total' => 5}, - {'id' => 8, 'name' => 'eight', 'total' => 1}, - {'id' => 9, 'name' => 'nine', 'total' => 6}, - {'id' => 10, 'name' => 'ten', 'total' => 19}, - {'id' => 11, 'name' => 'eleven', 'total' => 14} - ] - - data = Stats::PieChartData.new(items, 'a chart', 50) - - assert_equal [9, 3, 6, 11, 16, 14, 4, 0, 5, 27], data.values - assert_equal ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "(others)"], data.labels - assert_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, -1], data.ids - end - -end