From 11bc4294a839196e3ec57339f9b4d92132a0b5c5 Mon Sep 17 00:00:00 2001 From: Reinier Balt Date: Wed, 25 Sep 2013 15:08:25 +0200 Subject: [PATCH] add tests for done_todos --- lib/tracks/done_todos.rb | 39 ++++++++++++----- test/models/todos/done_todos_test.rb | 64 ++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 test/models/todos/done_todos_test.rb diff --git a/lib/tracks/done_todos.rb b/lib/tracks/done_todos.rb index 89cb1e3c..179f6661 100644 --- a/lib/tracks/done_todos.rb +++ b/lib/tracks/done_todos.rb @@ -1,32 +1,32 @@ class DoneTodos + + def self.done_todos_for_container(user) completed_todos = user.todos.completed return done_today(completed_todos), done_rest_of_week(completed_todos), done_rest_of_month(completed_todos) end def self.done_today(todos, includes = {:include => Todo::DEFAULT_INCLUDES}) - start_of_this_day = Time.zone.now.beginning_of_day # TODO: refactor to remove outer hash from includes param - todos.completed_after(start_of_this_day).includes(includes[:include]) + todos.completed_after(beginning_of_day).includes(includes[:include]) end def self.done_rest_of_week(todos, includes = {:include => Todo::DEFAULT_INCLUDES}) - done_between(todos, includes, Time.zone.now.beginning_of_day, Time.zone.now.beginning_of_week) + done_between(todos, includes, beginning_of_day, beginning_of_week) end def self.done_rest_of_month(todos, includes = {:include => Todo::DEFAULT_INCLUDES}) - done_between(todos, includes, Time.zone.now.beginning_of_week, Time.zone.now.beginning_of_month) + done_between(todos, includes, beginning_of_week, beginning_of_month) end def self.completed_period(date) - return nil if date.nil? + return nil if date.nil? - period = nil - period = "rest_of_month" if date > Time.zone.now.beginning_of_month - period = "rest_of_week" if date > Time.zone.now.beginning_of_week - period = "today" if date > Time.zone.now.beginning_of_day - - return period + return "today" if date >= end_of_day # treat todos with completed_at in future as done today (happens in tests) + return "today" if date.between?(beginning_of_day, end_of_day) + return "rest_of_week" if date >= beginning_of_week + return "rest_of_month" if date >= beginning_of_month + return nil end def self.remaining_in_container(user, period) @@ -41,4 +41,21 @@ class DoneTodos # TODO: refactor to remove outer hash from includes param todos.completed_before(start_date).completed_after(end_date).includes(includes[:include]) end + + def self.beginning_of_day + Time.zone.now.beginning_of_day + end + + def self.end_of_day + Time.zone.now.end_of_day + end + + def self.beginning_of_week + Time.zone.now.beginning_of_week + end + + def self.beginning_of_month + Time.zone.now.beginning_of_month + end + end diff --git a/test/models/todos/done_todos_test.rb b/test/models/todos/done_todos_test.rb new file mode 100644 index 00000000..b3b066e4 --- /dev/null +++ b/test/models/todos/done_todos_test.rb @@ -0,0 +1,64 @@ +require_relative '../../test_helper' + +module Todos + class DoneTodosTest < ActiveSupport::TestCase + + def test_completed_period + Timecop.travel(2013,1,23,12,00,00) do # wednesday at 12:00; + assert_equal "today", DoneTodos.completed_period(Time.zone.local(2013,1,23,9,00)) # today at 9:00 + assert_equal "rest_of_week", DoneTodos.completed_period(Time.zone.local(2013,1,21)) # monday this week + assert_equal "rest_of_month", DoneTodos.completed_period(Time.zone.local(2013,1,8)) # tuestday in first week of jan + + assert_nil DoneTodos.completed_period(nil) + assert_nil DoneTodos.completed_period(Time.zone.local(2012,12,1)) # older than this month + + assert_equal "today", DoneTodos.completed_period(Time.zone.local(2013,12,1)) # date in future -> act as if today + end + end + + def test_done_today + todos = users(:admin_user).todos + assert 0, DoneTodos.done_today(todos, {}).count + + t = users(:admin_user).todos.active.first + t.complete! + + assert 0, DoneTodos.done_today(todos.reload, {}).count + end + + def test_done_rest_of_week + todos = users(:admin_user).todos + + # When I mark a todo complete on jan 1 + Timecop.travel(2013,1,1,0,0) do + t = users(:admin_user).todos.active.first + t.complete! + end + + # Then I should be in rest_of_week on jan 2 + Timecop.travel(2013,1,2,0,0) do + assert 0, DoneTodos.done_today(todos.reload, {}).count + assert 1, DoneTodos.done_rest_of_week(todos.reload, {}).count + end + end + + def test_done_rest_of_month + todos = users(:admin_user).todos + + # When I mark a todo complete on jan 1 + Timecop.travel(2013,1,1,0,0) do + t = users(:admin_user).todos.active.first + t.complete! + end + + # Then I should be in rest_of_month on jan 21 + Timecop.travel(2013,1,21,0,0) do + assert 0, DoneTodos.done_today(todos.reload, {}).count + assert 0, DoneTodos.done_rest_of_week(todos.reload, {}).count + assert 1, DoneTodos.done_rest_of_month(todos.reload, {}).count + end + end + + + end +end