add tests for done_todos

This commit is contained in:
Reinier Balt 2013-09-25 15:08:25 +02:00
parent 23d92411d3
commit 11bc4294a8
2 changed files with 92 additions and 11 deletions

View file

@ -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

View file

@ -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