2014-05-16 18:03:13 -04:00
require 'test_helper'
2014-01-27 16:42:54 +01:00
module RecurringTodos
2015-02-10 16:25:27 +01:00
class AbstractRecurrencePatternTest < ActiveSupport :: TestCase
2014-01-27 16:42:54 +01:00
fixtures :users
def setup
2014-02-27 16:35:47 +01:00
super
2014-01-27 16:42:54 +01:00
@admin = users ( :admin_user )
2015-08-04 23:08:13 +02:00
end
2014-01-27 16:42:54 +01:00
2014-02-10 19:39:39 +01:00
def test_pattern_builds_from_existing_recurring_todo
rt = @admin . recurring_todos . first
pattern = rt . pattern
2015-02-10 16:25:27 +01:00
assert pattern . is_a? ( DailyRecurrencePattern ) , " recurring todo should have daily pattern "
2014-02-10 19:39:39 +01:00
end
def test_validation_on_due_date
attributes = {
'weekly_every_x_week' = > 1 ,
2014-03-05 09:37:34 +01:00
'weekly_return_monday' = > 'm' , # weekly specific
2014-02-10 19:39:39 +01:00
}
2014-03-05 09:37:34 +01:00
pattern = create_recurring_todo ( attributes )
2014-02-10 19:39:39 +01:00
assert ! pattern . valid? , " should fail because show_always and show_from_delta are not there "
attributes [ 'recurring_show_always' ] = false
2014-03-05 09:37:34 +01:00
pattern = create_recurring_todo ( attributes )
2014-02-10 19:39:39 +01:00
assert ! pattern . valid? , " should fail because show_from_delta is not there "
attributes [ :recurring_show_days_before ] = 5
2014-03-05 09:37:34 +01:00
pattern = create_recurring_todo ( attributes )
2014-02-10 19:39:39 +01:00
assert pattern . valid? , " should be valid: " + pattern . errors . full_messages . to_s
end
def test_validation_on_start_date
attributes = {
'weekly_every_x_week' = > 1 ,
2014-03-05 09:37:34 +01:00
'weekly_return_monday' = > 'm' , # weekly specific
2014-02-10 19:39:39 +01:00
'recurring_show_always' = > false ,
'recurring_show_days_before' = > 5 ,
2014-03-05 09:37:34 +01:00
'start_from' = > nil
2014-02-10 19:39:39 +01:00
}
2014-03-05 09:37:34 +01:00
pattern = create_recurring_todo ( attributes )
2014-02-10 19:39:39 +01:00
assert ! pattern . valid? , " should be not valid because start_from is empty "
attributes [ 'start_from' ] = Time . zone . now - 1 . week
2014-03-05 09:37:34 +01:00
pattern = create_recurring_todo ( attributes )
2014-02-10 19:39:39 +01:00
assert pattern . valid? , " should be valid: " + pattern . errors . full_messages . to_s
end
def test_validation_on_end_date
attributes = {
'weekly_return_monday' = > 'm' , # weekly specific
'ends_on' = > 'invalid_value' ,
'weekly_every_x_week' = > 1 ,
'recurring_show_always' = > false ,
'recurring_show_days_before' = > 5 ,
}
2014-03-05 09:37:34 +01:00
pattern = create_recurring_todo ( attributes )
2014-02-10 19:39:39 +01:00
assert ! pattern . valid?
attributes [ 'ends_on' ] = 'ends_on_end_date'
2014-03-05 09:37:34 +01:00
attributes [ 'end_date' ] = nil
pattern = create_recurring_todo ( attributes )
2014-02-10 19:39:39 +01:00
assert ! pattern . valid? , " should not be valid, because end_date is not supplied "
attributes [ 'end_date' ] = Time . zone . now + 1 . week
2014-03-05 09:37:34 +01:00
pattern = create_recurring_todo ( attributes )
2014-02-10 19:39:39 +01:00
assert pattern . valid? , " should be valid "
end
2015-02-10 15:49:13 +01:00
def test_validation_on_number_of_occurrences
2014-08-06 14:40:30 +02:00
attributes = {
'weekly_return_monday' = > 'm' , # weekly specific
'weekly_every_x_week' = > 1 ,
'recurring_show_always' = > false ,
'recurring_show_days_before' = > 5 ,
'ends_on' = > 'ends_on_number_of_times' ,
}
# pattern = create_recurring_todo(attributes)
2015-02-10 15:49:13 +01:00
# assert !pattern.valid?, "number_of_occurrences should be filled"
2014-08-06 14:40:30 +02:00
2015-02-10 15:49:13 +01:00
attributes [ 'number_of_occurrences' ] = 5
2014-08-06 14:40:30 +02:00
pattern = create_recurring_todo ( attributes )
assert pattern . valid? , " should be valid "
end
2014-02-27 16:35:47 +01:00
def test_end_date_on_recurring_todo
rt = recurring_todos ( :call_bill_gates_every_day )
assert_equal true , rt . continues_recurring? ( @in_three_days )
assert_equal true , rt . continues_recurring? ( @in_four_days )
rt . end_date = @in_four_days
rt . ends_on = 'ends_on_end_date'
assert_equal false , rt . continues_recurring? ( @in_four_days )
end
2014-03-05 09:37:34 +01:00
def test_continues_recurring
rt = recurring_todos ( :call_bill_gates_every_day )
assert rt . continues_recurring? ( Time . zone . now ) , " should not end "
rt . end_date = Time . zone . now - 1 . day
rt . ends_on = 'ends_on_end_date'
assert ! rt . continues_recurring? ( Time . zone . now ) , " should end because end_date is in the past "
rt . reload # reset
2015-02-10 15:49:13 +01:00
rt . number_of_occurrences = 2
rt . occurrences_count = 1
assert rt . continues_recurring? ( Time . zone . now ) , " should continue since there still may come occurrences "
2014-03-05 09:37:34 +01:00
2015-02-10 15:49:13 +01:00
rt . occurrences_count = 2
assert ! rt . continues_recurring? ( Time . zone . now ) , " should end since all occurrences are there "
2014-03-05 09:37:34 +01:00
end
2014-03-31 11:09:00 +02:00
def test_determine_start
2014-05-16 13:24:40 -04:00
travel_to Time . zone . local ( 2013 , 1 , 1 ) do
2014-03-31 11:09:00 +02:00
rt = create_recurring_todo
2015-08-04 23:08:13 +02:00
assert_equal Time . zone . parse ( " 2013-01-01 00:00:00 " ) , rt . send ( :determine_start , nil ) , " no previous date, use today "
2025-06-29 13:25:14 +03:00
assert_equal Time . zone . parse ( " 2013-01-01 00:00:00 " ) , rt . send ( :determine_start , nil , 1 . day ) . to_formatted_s ( :db ) , " no previous date, use today without offset "
assert_equal Time . zone . parse ( " 2013-01-02 00:00:00 " ) , rt . send ( :determine_start , Time . zone . now , 1 . day ) . to_formatted_s ( :db ) , " use previous date and offset "
2014-03-31 11:09:00 +02:00
end
end
def test_xth_day_of_month
rt = create_recurring_todo
# march 2014 has 5 saturdays, the last will return the 5th
2025-06-29 13:25:14 +03:00
assert_equal Time . zone . parse ( " 2014-03-01 00:00:00 " ) , rt . send ( :get_xth_day_of_month , 1 , 6 , 3 , 2014 ) . to_formatted_s ( :db )
assert_equal Time . zone . parse ( " 2014-03-22 00:00:00 " ) , rt . send ( :get_xth_day_of_month , 4 , 6 , 3 , 2014 ) . to_formatted_s ( :db )
assert_equal Time . zone . parse ( " 2014-03-29 00:00:00 " ) , rt . send ( :get_xth_day_of_month , 5 , 6 , 3 , 2014 ) . to_formatted_s ( :db )
2014-03-31 11:09:00 +02:00
# march 2014 has 4 fridays, the last will return the 4th
2025-06-29 13:25:14 +03:00
assert_equal Time . zone . parse ( " 2014-03-07 00:00:00 " ) , rt . send ( :get_xth_day_of_month , 1 , 5 , 3 , 2014 ) . to_formatted_s ( :db )
assert_equal Time . zone . parse ( " 2014-03-28 00:00:00 " ) , rt . send ( :get_xth_day_of_month , 4 , 5 , 3 , 2014 ) . to_formatted_s ( :db )
assert_equal Time . zone . parse ( " 2014-03-28 00:00:00 " ) , rt . send ( :get_xth_day_of_month , 5 , 5 , 3 , 2014 ) . to_formatted_s ( :db )
2014-03-31 11:09:00 +02:00
assert_raise ( RuntimeError , " should check on valid weekdays " ) { rt . send ( :get_xth_day_of_month , 5 , 9 , 3 , 2014 ) }
assert_raise ( RuntimeError , " should check on valid count x " ) { rt . send ( :get_xth_day_of_month , 6 , 5 , 3 , 2014 ) }
end
2014-02-10 19:39:39 +01:00
private
def create_pattern ( attributes )
builder = RecurringTodosBuilder . new ( @admin , attributes )
builder . build
builder . pattern
end
2014-03-31 11:09:00 +02:00
def create_recurring_todo ( attributes = { } )
2014-03-05 09:37:34 +01:00
create_pattern ( attributes . reverse_merge ( {
'recurring_period' = > 'weekly' ,
'recurring_target' = > 'due_date' ,
2015-02-10 16:25:27 +01:00
'description' = > 'a recurring todo' , # generic
2014-03-05 09:37:34 +01:00
'ends_on' = > 'ends_on_end_date' ,
'end_date' = > Time . zone . now + 1 . week ,
'context_id' = > @admin . contexts . first . id ,
'start_from' = > Time . zone . now - 1 . week ,
} ) )
end
2014-01-27 16:42:54 +01:00
end
2015-08-04 23:08:13 +02:00
end