mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-16 15:20:13 +01:00
add tests for done_todos
This commit is contained in:
parent
23d92411d3
commit
11bc4294a8
2 changed files with 92 additions and 11 deletions
|
|
@ -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
|
||||
|
|
|
|||
64
test/models/todos/done_todos_test.rb
Normal file
64
test/models/todos/done_todos_test.rb
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue