From 8ed20b35f2967a86806500a0519e052423ef3daa Mon Sep 17 00:00:00 2001 From: Don Cruse Date: Sun, 28 Jul 2013 19:00:51 -0500 Subject: [PATCH] Fixing bug #1442 This bug was caused when the chart encountered data outside of its visible range (i.e., the upper bound of the array size). A test was added that inserts some data from 2 years and 16 months in the past. The test failed on the old version, throwing exactly the error in bug report #1442. The fix is to check whether or not the value is within the array range before sending the "+=" operator. With this change, the test passes. --- app/controllers/stats_controller.rb | 2 +- test/controllers/stats_controller_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index d6be1be0..a2490acc 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -377,7 +377,7 @@ class StatsController < ApplicationController # the block should return an array of indexes each is added to the hash and summed def convert_to_array(records, upper_bound) a = Array.new(upper_bound, 0) - records.each { |r| (yield r).each { |i| a[i] += 1 } } + records.each { |r| (yield r).each { |i| a[i] += 1 if a[i] } } a end diff --git a/test/controllers/stats_controller_test.rb b/test/controllers/stats_controller_test.rb index aacef8be..20add95c 100644 --- a/test/controllers/stats_controller_test.rb +++ b/test/controllers/stats_controller_test.rb @@ -142,6 +142,28 @@ class StatsControllerTest < ActionController::TestCase end end + def test_empty_last12months_data + Timecop.travel(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)