move tests from recurring_todo_test to respective repeat_pattern_tests

This commit is contained in:
Reinier Balt 2014-03-05 09:37:34 +01:00
parent 0c153ef28c
commit 3cb18cd875
7 changed files with 163 additions and 130 deletions

View file

@ -44,20 +44,9 @@ module RecurringTodos
def recurrence_pattern def recurrence_pattern
if recurrence_selector == 0 if recurrence_selector == 0
on_day = " #{I18n.t('todos.recurrence.pattern.on_day_n', :n => every_x_day)}" recurrence_pattern_for_specific_day
if every_xth_day(0) > 1
I18n.t("todos.recurrence.pattern.every_n", :n => every_xth_day) + " " + I18n.t('common.months') + on_day
else else
I18n.t("todos.recurrence.pattern.every_month") + on_day recurrence_pattern_for_relative_day_in_month
end
else
n_months = if get(:every_other2) > 1
"#{get(:every_other2)} #{I18n.t('common.months')}"
else
I18n.t('common.month')
end
I18n.t('todos.recurrence.pattern.every_xth_day_of_every_n_months',
:x => xth(every_xth_day), :day => day_of_week_as_text(day_of_week), :n_months => n_months)
end end
end end
@ -123,6 +112,29 @@ module RecurringTodos
end end
end end
private
def recurrence_pattern_for_specific_day
on_day = " #{I18n.t('todos.recurrence.pattern.on_day_n', :n => every_x_day)}"
if every_xth_day(0) > 1
I18n.t("todos.recurrence.pattern.every_n_months", :n => every_xth_day) + on_day
else
I18n.t("todos.recurrence.pattern.every_month") + on_day
end
end
def recurrence_pattern_for_relative_day_in_month
n_months = if every_x_month2 > 1
"#{every_x_month2} #{I18n.t('common.months')}"
else
I18n.t('common.month')
end
I18n.t('todos.recurrence.pattern.every_xth_day_of_every_n_months',
x: xth(every_xth_day),
day: day_of_week_as_text(day_of_week),
n_months: n_months)
end
end end
end end

View file

@ -624,6 +624,7 @@ en:
third: third third: third
every_n: every %{n} every_n: every %{n}
every_n_days: every %{n} days every_n_days: every %{n} days
every_n_months: every %{n} months
every_day: every day every_day: every day
on_day_n: on day %{n} on_day_n: on day %{n}
second: second second: second

View file

@ -58,95 +58,6 @@ class RecurringTodoTest < ActiveSupport::TestCase
# weekly/monthly/yearly # weekly/monthly/yearly
end end
def test_weekly_pattern
assert_equal true, @weekly_every_day.continues_recurring?(nil)
due_date = @weekly_every_day.get_due_date(@sunday)
assert_equal @monday, due_date
# saturday is last day in week, so the next date should be sunday + n-1 weeks
# n-1 because sunday is already in the next week
@weekly_every_day.every_other1 = 3
due_date = @weekly_every_day.get_due_date(@saturday)
assert_equal @sunday + 2.weeks, due_date
# remove tuesday and wednesday
@weekly_every_day.every_day = 'sm tfs'
due_date = @weekly_every_day.get_due_date(@monday)
assert_equal @thursday, due_date
@weekly_every_day.every_other1 = 1
@weekly_every_day.every_day = ' tw '
due_date = @weekly_every_day.get_due_date(@tuesday)
assert_equal @wednesday, due_date
due_date = @weekly_every_day.get_due_date(@wednesday)
assert_equal @tuesday+1.week, due_date
@weekly_every_day.every_day = ' s'
due_date = @weekly_every_day.get_due_date(@sunday)
assert_equal @saturday+1.week, due_date
end
def test_monthly_pattern
due_date = @monthly_every_last_friday.get_due_date(@sunday)
assert_equal Time.zone.local(2008,6,27), due_date
friday_is_last_day_of_month = Time.zone.local(2008,10,31)
due_date = @monthly_every_last_friday.get_due_date(friday_is_last_day_of_month-1.day )
assert_equal friday_is_last_day_of_month , due_date
@monthly_every_third_friday = @monthly_every_last_friday
@monthly_every_third_friday.every_other3=3 #third
due_date = @monthly_every_last_friday.get_due_date(@sunday) # june 8th 2008
assert_equal Time.zone.local(2008, 6, 20), due_date
# set date past third friday of this month
due_date = @monthly_every_last_friday.get_due_date(Time.zone.local(2008,6,21)) # june 21th 2008
assert_equal Time.zone.local(2008, 8, 15), due_date # every 2 months, so aug
@monthly = @monthly_every_last_friday
@monthly.recurrence_selector=0
@monthly.every_other1 = 8 # every 8th day of the month
@monthly.every_other2 = 2 # every 2 months
due_date = @monthly.get_due_date(@saturday) # june 7th
assert_equal @sunday, due_date # june 8th
due_date = @monthly.get_due_date(@sunday) # june 8th
assert_equal Time.zone.local(2008,8,8), due_date # aug 8th
end
def test_yearly_pattern
# beginning of same year
due_date = @yearly.get_due_date(Time.zone.local(2008,2,10)) # feb 10th
assert_equal @sunday, due_date # june 8th
# same month, previous date
due_date = @yearly.get_due_date(@saturday) # june 7th
show_from_date = @yearly.get_show_from_date(@saturday) # june 7th
assert_equal @sunday, due_date # june 8th
assert_equal @sunday-5.days, show_from_date
# same month, day after
due_date = @yearly.get_due_date(@monday) # june 9th
assert_equal Time.zone.local(2009,6,8), due_date # june 8th next year
# very overdue
due_date = @yearly.get_due_date(@monday+5.months-2.days) # november 7
assert_equal Time.zone.local(2009,6,8), due_date # june 8th next year
@yearly.recurrence_selector = 1
@yearly.every_other3 = 2 # second
@yearly.every_count = 3 # wednesday
# beginning of same year
due_date = @yearly.get_due_date(Time.zone.local(2008,2,10)) # feb 10th
assert_equal Time.zone.local(2008,6,11), due_date # june 11th
# same month, before second wednesday
due_date = @yearly.get_due_date(@saturday) # june 7th
assert_equal Time.zone.local(2008,6,11), due_date # june 11th
# same month, after second wednesday
due_date = @yearly.get_due_date(Time.zone.local(2008,6,12)) # june 7th
assert_equal Time.zone.local(2009,6,10), due_date # june 10th
end
def test_next_todo_without_previous_todo def test_next_todo_without_previous_todo
# test handling of nil as previous # test handling of nil as previous
# #

View file

@ -19,73 +19,57 @@ module RecurringTodos
def test_validation_on_due_date def test_validation_on_due_date
attributes = { attributes = {
'recurring_period' => 'weekly',
'recurring_target' => 'due_date',
'description' => 'a repeating todo', # generic
'weekly_return_monday' => 'm', # weekly specific
'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,
'weekly_every_x_week' => 1, 'weekly_every_x_week' => 1,
'weekly_return_monday' => 'm', # weekly specific
} }
pattern = create_pattern(attributes) pattern = create_recurring_todo(attributes)
assert !pattern.valid?, "should fail because show_always and show_from_delta are not there" assert !pattern.valid?, "should fail because show_always and show_from_delta are not there"
attributes['recurring_show_always'] = false attributes['recurring_show_always'] = false
pattern = create_pattern(attributes) pattern = create_recurring_todo(attributes)
assert !pattern.valid?, "should fail because show_from_delta is not there" assert !pattern.valid?, "should fail because show_from_delta is not there"
attributes[:recurring_show_days_before] = 5 attributes[:recurring_show_days_before] = 5
pattern = create_pattern(attributes) pattern = create_recurring_todo(attributes)
assert pattern.valid?, "should be valid:" + pattern.errors.full_messages.to_s assert pattern.valid?, "should be valid:" + pattern.errors.full_messages.to_s
end end
def test_validation_on_start_date def test_validation_on_start_date
attributes = { attributes = {
'recurring_period' => 'weekly',
'recurring_target' => 'due_date',
'description' => 'a repeating todo', # generic
'weekly_return_monday' => 'm', # weekly specific
'ends_on' => 'ends_on_end_date',
'context_id' => @admin.contexts.first.id,
'end_date' => Time.zone.now + 1.week,
'weekly_every_x_week' => 1, 'weekly_every_x_week' => 1,
'weekly_return_monday' => 'm', # weekly specific
'recurring_show_always' => false, 'recurring_show_always' => false,
'recurring_show_days_before' => 5, 'recurring_show_days_before' => 5,
'start_from' => nil
} }
pattern = create_pattern(attributes) pattern = create_recurring_todo(attributes)
assert !pattern.valid?, "should be not valid because start_from is empty" assert !pattern.valid?, "should be not valid because start_from is empty"
attributes['start_from'] = Time.zone.now - 1.week attributes['start_from'] = Time.zone.now - 1.week
pattern = create_pattern(attributes) pattern = create_recurring_todo(attributes)
assert pattern.valid?, "should be valid: " + pattern.errors.full_messages.to_s assert pattern.valid?, "should be valid: " + pattern.errors.full_messages.to_s
end end
def test_validation_on_end_date def test_validation_on_end_date
attributes = { attributes = {
'recurring_period' => 'weekly',
'recurring_target' => 'due_date',
'description' => 'a repeating todo', # generic
'weekly_return_monday' => 'm', # weekly specific 'weekly_return_monday' => 'm', # weekly specific
'ends_on' => 'invalid_value', 'ends_on' => 'invalid_value',
'context_id' => @admin.contexts.first.id,
'start_from' => Time.zone.now - 1.week,
'weekly_every_x_week' => 1, 'weekly_every_x_week' => 1,
'recurring_show_always' => false, 'recurring_show_always' => false,
'recurring_show_days_before' => 5, 'recurring_show_days_before' => 5,
} }
pattern = create_pattern(attributes) pattern = create_recurring_todo(attributes)
assert !pattern.valid? assert !pattern.valid?
attributes['ends_on']='ends_on_end_date' attributes['ends_on']='ends_on_end_date'
pattern = create_pattern(attributes) attributes['end_date']=nil
pattern = create_recurring_todo(attributes)
assert !pattern.valid?, "should not be valid, because end_date is not supplied" assert !pattern.valid?, "should not be valid, because end_date is not supplied"
attributes['end_date']= Time.zone.now + 1.week attributes['end_date']= Time.zone.now + 1.week
pattern = create_pattern(attributes) pattern = create_recurring_todo(attributes)
assert pattern.valid?, "should be valid" assert pattern.valid?, "should be valid"
end end
@ -99,6 +83,23 @@ module RecurringTodos
assert_equal false, rt.continues_recurring?(@in_four_days) assert_equal false, rt.continues_recurring?(@in_four_days)
end end
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
rt.number_of_occurences = 2
rt.occurences_count = 1
assert rt.continues_recurring?(Time.zone.now), "should continue since there still may come occurences"
rt.occurences_count = 2
assert !rt.continues_recurring?(Time.zone.now), "should end since all occurences are there"
end
private private
def create_pattern(attributes) def create_pattern(attributes)
@ -107,6 +108,18 @@ module RecurringTodos
builder.pattern builder.pattern
end end
def create_recurring_todo(attributes)
create_pattern(attributes.reverse_merge({
'recurring_period' => 'weekly',
'recurring_target' => 'due_date',
'description' => 'a repeating todo', # generic
'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
end end
end end

View file

@ -6,6 +6,7 @@ module RecurringTodos
fixtures :users fixtures :users
def setup def setup
super
@admin = users(:admin_user) @admin = users(:admin_user)
end end
@ -112,6 +113,36 @@ module RecurringTodos
assert_equal "every month on day 1", rt.recurrence_pattern assert_equal "every month on day 1", rt.recurrence_pattern
end end
def test_monthly_pattern
@monthly_every_last_friday = recurring_todos(:check_with_bill_every_last_friday_of_month)
due_date = @monthly_every_last_friday.get_due_date(@sunday)
assert_equal Time.zone.local(2008,6,27), due_date
friday_is_last_day_of_month = Time.zone.local(2008,10,31)
due_date = @monthly_every_last_friday.get_due_date(friday_is_last_day_of_month-1.day )
assert_equal friday_is_last_day_of_month , due_date
@monthly_every_third_friday = @monthly_every_last_friday
@monthly_every_third_friday.every_other3=3 #third
due_date = @monthly_every_last_friday.get_due_date(@sunday) # june 8th 2008
assert_equal Time.zone.local(2008, 6, 20), due_date
# set date past third friday of this month
due_date = @monthly_every_last_friday.get_due_date(Time.zone.local(2008,6,21)) # june 21th 2008
assert_equal Time.zone.local(2008, 8, 15), due_date # every 2 months, so aug
@monthly = @monthly_every_last_friday
@monthly.recurrence_selector=0
@monthly.every_other1 = 8 # every 8th day of the month
@monthly.every_other2 = 2 # every 2 months
due_date = @monthly.get_due_date(@saturday) # june 7th
assert_equal @sunday, due_date # june 8th
due_date = @monthly.get_due_date(@sunday) # june 8th
assert_equal Time.zone.local(2008,8,8), due_date # aug 8th
end
end end
end end

View file

@ -6,6 +6,7 @@ module RecurringTodos
fixtures :users fixtures :users
def setup def setup
super
@admin = users(:admin_user) @admin = users(:admin_user)
end end
@ -48,6 +49,34 @@ module RecurringTodos
assert_equal "weekly", rt.recurrence_pattern assert_equal "weekly", rt.recurrence_pattern
end end
def test_weekly_pattern
rt = recurring_todos(:call_bill_gates_every_week)
due_date = rt.get_due_date(@sunday)
assert_equal @monday, due_date
# saturday is last day in week, so the next date should be sunday + n-1 weeks
# n-1 because sunday is already in the next week
rt.every_other1 = 3
due_date = rt.get_due_date(@saturday)
assert_equal @sunday + 2.weeks, due_date
# remove tuesday and wednesday
rt.every_day = 'sm tfs'
due_date = rt.get_due_date(@monday)
assert_equal @thursday, due_date
rt.every_other1 = 1
rt.every_day = ' tw '
due_date = rt.get_due_date(@tuesday)
assert_equal @wednesday, due_date
due_date = rt.get_due_date(@wednesday)
assert_equal @tuesday+1.week, due_date
rt.every_day = ' s'
due_date = rt.get_due_date(@sunday)
assert_equal @saturday+1.week, due_date
end
end end
end end

View file

@ -6,6 +6,7 @@ module RecurringTodos
fixtures :users fixtures :users
def setup def setup
super
@admin = users(:admin_user) @admin = users(:admin_user)
end end
@ -74,6 +75,41 @@ module RecurringTodos
assert_equal "every year on the third wednesday of June", rt.recurrence_pattern assert_equal "every year on the third wednesday of June", rt.recurrence_pattern
end end
def test_yearly_pattern
@yearly = recurring_todos(:birthday_reinier)
# beginning of same year
due_date = @yearly.get_due_date(Time.zone.local(2008,2,10)) # feb 10th
assert_equal @sunday, due_date # june 8th
# same month, previous date
due_date = @yearly.get_due_date(@saturday) # june 7th
show_from_date = @yearly.get_show_from_date(@saturday) # june 7th
assert_equal @sunday, due_date # june 8th
assert_equal @sunday-5.days, show_from_date
# same month, day after
due_date = @yearly.get_due_date(@monday) # june 9th
assert_equal Time.zone.local(2009,6,8), due_date # june 8th next year
# very overdue
due_date = @yearly.get_due_date(@monday+5.months-2.days) # november 7
assert_equal Time.zone.local(2009,6,8), due_date # june 8th next year
@yearly.recurrence_selector = 1
@yearly.every_other3 = 2 # second
@yearly.every_count = 3 # wednesday
# beginning of same year
due_date = @yearly.get_due_date(Time.zone.local(2008,2,10)) # feb 10th
assert_equal Time.zone.local(2008,6,11), due_date # june 11th
# same month, before second wednesday
due_date = @yearly.get_due_date(@saturday) # june 7th
assert_equal Time.zone.local(2008,6,11), due_date # june 11th
# same month, after second wednesday
due_date = @yearly.get_due_date(Time.zone.local(2008,6,12)) # june 7th
assert_equal Time.zone.local(2009,6,10), due_date # june 10th
end
end end
end end