From 3cb18cd875f44b99bee8a813b7995ac7ac370aa4 Mon Sep 17 00:00:00 2001 From: Reinier Balt Date: Wed, 5 Mar 2014 09:37:34 +0100 Subject: [PATCH] move tests from recurring_todo_test to respective repeat_pattern_tests --- .../recurring_todos/monthly_repeat_pattern.rb | 38 +++++--- config/locales/en.yml | 1 + test/models/recurring_todo_test.rb | 89 ------------------- .../abstract_repeat_pattern_test.rb | 69 ++++++++------ .../monthly_repeat_pattern_test.rb | 31 +++++++ .../weekly_repeat_pattern_test.rb | 29 ++++++ .../yearly_repeat_pattern_test.rb | 36 ++++++++ 7 files changed, 163 insertions(+), 130 deletions(-) diff --git a/app/models/recurring_todos/monthly_repeat_pattern.rb b/app/models/recurring_todos/monthly_repeat_pattern.rb index ddb49c06..3fd6204f 100644 --- a/app/models/recurring_todos/monthly_repeat_pattern.rb +++ b/app/models/recurring_todos/monthly_repeat_pattern.rb @@ -44,20 +44,9 @@ module RecurringTodos def recurrence_pattern if recurrence_selector == 0 - 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", :n => every_xth_day) + " " + I18n.t('common.months') + on_day - else - I18n.t("todos.recurrence.pattern.every_month") + on_day - end + recurrence_pattern_for_specific_day 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) + recurrence_pattern_for_relative_day_in_month end end @@ -123,6 +112,29 @@ module RecurringTodos 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 \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 90e9970d..987ae205 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -624,6 +624,7 @@ en: third: third every_n: every %{n} every_n_days: every %{n} days + every_n_months: every %{n} months every_day: every day on_day_n: on day %{n} second: second diff --git a/test/models/recurring_todo_test.rb b/test/models/recurring_todo_test.rb index c9ea6703..7ac14610 100644 --- a/test/models/recurring_todo_test.rb +++ b/test/models/recurring_todo_test.rb @@ -58,95 +58,6 @@ class RecurringTodoTest < ActiveSupport::TestCase # weekly/monthly/yearly 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 # test handling of nil as previous # diff --git a/test/models/recurring_todos/abstract_repeat_pattern_test.rb b/test/models/recurring_todos/abstract_repeat_pattern_test.rb index 748b0e94..fbf8a78a 100644 --- a/test/models/recurring_todos/abstract_repeat_pattern_test.rb +++ b/test/models/recurring_todos/abstract_repeat_pattern_test.rb @@ -19,73 +19,57 @@ module RecurringTodos def test_validation_on_due_date 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_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" 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" 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 end def test_validation_on_start_date 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_return_monday' => 'm', # weekly specific 'recurring_show_always' => false, '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" 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 end def test_validation_on_end_date attributes = { - 'recurring_period' => 'weekly', - 'recurring_target' => 'due_date', - 'description' => 'a repeating todo', # generic 'weekly_return_monday' => 'm', # weekly specific 'ends_on' => 'invalid_value', - 'context_id' => @admin.contexts.first.id, - 'start_from' => Time.zone.now - 1.week, 'weekly_every_x_week' => 1, 'recurring_show_always' => false, 'recurring_show_days_before' => 5, } - pattern = create_pattern(attributes) + pattern = create_recurring_todo(attributes) assert !pattern.valid? 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" attributes['end_date']= Time.zone.now + 1.week - pattern = create_pattern(attributes) + pattern = create_recurring_todo(attributes) assert pattern.valid?, "should be valid" end @@ -99,6 +83,23 @@ module RecurringTodos assert_equal false, rt.continues_recurring?(@in_four_days) 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 def create_pattern(attributes) @@ -107,6 +108,18 @@ module RecurringTodos builder.pattern 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 \ No newline at end of file diff --git a/test/models/recurring_todos/monthly_repeat_pattern_test.rb b/test/models/recurring_todos/monthly_repeat_pattern_test.rb index b3608074..5603108f 100644 --- a/test/models/recurring_todos/monthly_repeat_pattern_test.rb +++ b/test/models/recurring_todos/monthly_repeat_pattern_test.rb @@ -6,6 +6,7 @@ module RecurringTodos fixtures :users def setup + super @admin = users(:admin_user) end @@ -112,6 +113,36 @@ module RecurringTodos assert_equal "every month on day 1", rt.recurrence_pattern 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 \ No newline at end of file diff --git a/test/models/recurring_todos/weekly_repeat_pattern_test.rb b/test/models/recurring_todos/weekly_repeat_pattern_test.rb index c1904af1..87f87397 100644 --- a/test/models/recurring_todos/weekly_repeat_pattern_test.rb +++ b/test/models/recurring_todos/weekly_repeat_pattern_test.rb @@ -6,6 +6,7 @@ module RecurringTodos fixtures :users def setup + super @admin = users(:admin_user) end @@ -48,6 +49,34 @@ module RecurringTodos assert_equal "weekly", rt.recurrence_pattern 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 \ No newline at end of file diff --git a/test/models/recurring_todos/yearly_repeat_pattern_test.rb b/test/models/recurring_todos/yearly_repeat_pattern_test.rb index a08f88d9..0514e655 100644 --- a/test/models/recurring_todos/yearly_repeat_pattern_test.rb +++ b/test/models/recurring_todos/yearly_repeat_pattern_test.rb @@ -6,6 +6,7 @@ module RecurringTodos fixtures :users def setup + super @admin = users(:admin_user) end @@ -74,6 +75,41 @@ module RecurringTodos assert_equal "every year on the third wednesday of June", rt.recurrence_pattern 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 \ No newline at end of file