add basis tests for patterns and remove doubles from recurring_todo_test.rb

This commit is contained in:
Reinier Balt 2014-02-23 15:13:53 +01:00
parent a7807a4b66
commit cbdbb792a5
9 changed files with 212 additions and 111 deletions

View file

@ -51,6 +51,10 @@ module RecurringTodos
@pattern.attributes
end
def errors
@recurring_todo.try(:errors)
end
def attributes_to_filter
raise Exception.new, "attributes_to_filter should be overridden"
end

View file

@ -52,7 +52,7 @@ module RecurringTodos
case recurrence_selector
when 0 # 'monthly_every_x_day'
validate_not_blank(every_x_month, "Every other nth month may not be empty for recurrence setting")
when 1 # 'every_xth_day'
when 1 # 'monthly_every_xth_day'
validate_not_blank(every_x_month2, "Every other nth month may not be empty for recurrence setting")
validate_not_blank(day_of_week, "The day of the month may not be empty for recurrence setting")
else

View file

@ -51,6 +51,10 @@ module RecurringTodos
@builder.pattern
end
def errors
@builder.errors
end
private
def valid_selector?(selector)

View file

@ -92,7 +92,7 @@ call_bill_gates_every_week:
end_date: ~
number_of_occurences: ~
target: due_date
show_from_delta: ~
show_from_delta: 5
recurring_period: weekly
recurrence_selector: ~
show_always: false

View file

@ -320,109 +320,4 @@ class RecurringTodoTest < ActiveSupport::TestCase
assert_equal true, @every_day.continues_recurring?(@in_three_days)
assert_equal 0, @every_day.occurences_count
end
def test_invalid_recurring_period_will_not_save
@every_day.recurring_period = 'invalid'
assert !@every_day.valid?
@every_month.recurrence_selector = 99
assert_raise(Exception){ @every_month.valid? }
@yearly.recurrence_selector = 99
assert_raise(RuntimeError){ @yearly.valid? }
end
def test_every_n_the_day_must_be_filled
@every_day.every_other1 = nil
assert !@every_day.valid?
end
def test_every_n_week_must_be_filled
@every_week.every_other1 = nil
assert !@every_week.valid?
end
def test_every_n_month_must_be_filled
@every_month.every_other1 = nil
@every_month.every_other2 = nil
assert !@every_month.valid?
@every_month.recurrence_selector = 0
assert !@every_month.valid?
end
def test_set_every_n_days_from_form_input
builder = RecurringTodos::RecurringTodosBuilder.new(users(:admin_user), {
:description => "Task every 2 days",
:context_id => Context.first.id,
:recurring_target => "show_from_date",
:start_from => "01/01/01",
:ends_on => "no_end_date",
:recurring_period => "daily",
:daily_selector => "daily_every_x_day",
:daily_every_x_days => 2,
})
builder.save
todo = builder.saved_recurring_todo
assert todo.valid?, todo.errors.full_messages
assert_equal 2, todo.every_other1
end
def test_set_every_n_weeks_from_form_input
builder = RecurringTodos::RecurringTodosBuilder.new(users(:admin_user), {
:description => "Task every 3 weeks",
:context_id => Context.first.id,
:recurring_target => "show_from_date",
:start_from => "01/01/01",
:ends_on => "no_end_date",
:recurring_period => "weekly",
:weekly_every_x_week => 3,
:weekly_return_monday => "m",
})
builder.save
todo = builder.saved_recurring_todo
assert todo.valid?, todo.errors.full_messages
assert_equal 3, todo.every_other1
assert todo.pattern.on_monday
end
def test_set_every_n_months_from_form_input
builder = RecurringTodos::RecurringTodosBuilder.new(users(:admin_user), {
:description => "Task every 4 months",
:context_id => Context.first.id,
:recurring_target => "show_from_date",
:start_from => "01/01/01",
:ends_on => "no_end_date",
:recurring_period => "monthly",
:monthly_selector => "monthly_every_x_day",
:monthly_every_x_day => 1,
:monthly_every_x_month => 4,
})
builder.save
todo = builder.saved_recurring_todo
assert todo.valid?, todo.errors.full_messages
assert_equal 4, todo.every_other2
end
def test_set_yearly_from_form_input
builder = RecurringTodos::RecurringTodosBuilder.new(users(:admin_user), {
:description => "Task every year in May",
:context_id => Context.first.id,
:recurring_target => "show_from_date",
:start_from => "01/01/01",
:ends_on => "no_end_date",
:recurring_period => "yearly",
:yearly_selector => "yearly_every_x_day",
:yearly_every_x_day => 15,
:yearly_month_of_year => 5,
})
builder.save
todo = builder.saved_recurring_todo
assert todo.valid?, todo.errors.full_messages
assert_equal 5, todo.every_other2
end
end
end

View file

@ -7,7 +7,30 @@ module RecurringTodos
def setup
@admin = users(:admin_user)
end
end
def test_daily_attributes
rt = @admin.recurring_todos.first
assert_equal rt.every_other1, rt.pattern.every_x_days
assert_equal rt.only_work_days, rt.pattern.only_work_days?
end
def test_validate
rt = @admin.recurring_todos.first
assert rt.valid?, "rt should be valid at start"
rt.every_other1 = nil
rt.only_work_days = false
assert !rt.valid?, "every_x_days should not be empty then only_work_days==false"
rt.only_work_days = true
assert rt.valid?, "every_x_days may have any value for only_work_days==true"
rt.only_work_days = false
rt.every_other1 = 2
assert rt.valid?, "should be valid again"
end
end

View file

@ -7,8 +7,97 @@ module RecurringTodos
def setup
@admin = users(:admin_user)
end
end
def test_attribute_mapping
builder = RecurringTodosBuilder.new(@admin, {
'recurring_period' => 'monthly',
'description' => 'a repeating todo', # generic
'recurring_period' => 'monthly',
'recurring_target' => 'show_from_date',
'ends_on' => 'ends_on_end_date',
'end_date' => Time.zone.now + 1.week,
'start_from' => Time.zone.now,
'context_name' => @admin.contexts.first.name,
'monthly_selector' => 'monthly_every_x_day',
'monthly_every_xth_day' => 1,
'monthly_day_of_week' => 2,
'monthly_every_x_month' => 3
})
assert builder.save, "should save: #{builder.errors.full_messages}"
rt = builder.saved_recurring_todo
assert builder.pattern.is_a?(MonthlyRepeatPattern), "should be monthly pattern, but is #{builder.pattern.class}"
assert builder.pattern.every_x_day?, "should be true for monthly_every_x_day"
assert 1, rt.recurrence_selector
assert_equal 1, builder.pattern.every_xth_day, "pattern should map every_other2 to every_xth_day from monthly_every_xth_day"
assert_equal 1, rt.every_other3
assert_equal 2, builder.pattern.day_of_week, "pattern should map every_count to day_of_week from monthly_day_of_week"
assert_equal 2, rt.every_count
end
def test_every_x_month
builder = RecurringTodosBuilder.new(@admin, {
'recurring_period' => 'monthly',
'description' => 'a repeating todo', # generic
'recurring_period' => 'monthly',
'recurring_target' => 'show_from_date',
'ends_on' => 'ends_on_end_date',
'end_date' => Time.zone.now + 1.week,
'start_from' => Time.zone.now,
'context_name' => @admin.contexts.first.name,
'monthly_selector' => 'monthly_every_x_day',
'monthly_every_x_month' => 3,
'monthly_every_x_month2' => 2
})
assert builder.save, "should save: #{builder.errors.full_messages}"
rt = builder.saved_recurring_todo
assert_equal 3, builder.pattern.every_x_month
assert_equal 3, rt.every_other2
builder = RecurringTodosBuilder.new(@admin, {
'recurring_period' => 'monthly',
'description' => 'a repeating todo', # generic
'recurring_period' => 'monthly',
'recurring_target' => 'show_from_date',
'ends_on' => 'ends_on_end_date',
'end_date' => Time.zone.now + 1.week,
'start_from' => Time.zone.now,
'context_name' => @admin.contexts.first.name,
'monthly_selector' => 'monthly_every_xth_day',
'monthly_every_x_month' => 3,
'monthly_every_x_month2' => 2,
'monthly_day_of_week' => 7
})
assert builder.save, "should save: #{builder.errors.full_messages}"
rt = builder.saved_recurring_todo
assert_equal 2, builder.pattern.every_x_month2
assert_equal 2, rt.every_other2
end
def test_validations
rt = @admin.recurring_todos.where(recurring_period: 'monthly').first
assert rt.valid?, "should be valid at start: #{rt.errors.full_messages}"
rt.recurrence_selector = 0 # 'monthly_every_x_day'
rt.every_other2 = nil
assert !rt.valid?, "should not be valid since every_x_month is empty"
rt.recurrence_selector = 1 # 'monthly_every_xth_day'
rt.every_other2 = nil
assert !rt.valid?, "should not be valid since every_xth_month is empty"
rt.every_count = nil
assert !rt.valid?, "should not be valid since day_of_week is empty"
end
end
end

View file

@ -8,6 +8,37 @@ module RecurringTodos
def setup
@admin = users(:admin_user)
end
def test_every_x_week
rt = @admin.recurring_todos.where(recurring_period: 'weekly').first
assert_equal rt.every_other1, rt.pattern.every_x_week
end
def test_on_xday
rt = @admin.recurring_todos.where(recurring_period: 'weekly').first
assert rt.valid?, "should be valid at start: id= #{rt.id} --> #{rt.errors.full_messages}"
rt.every_day = 'smtwtfs'
%w{monday tuesday wednesday thursday friday saturday sunday}.each do |day|
assert rt.pattern.send("on_#{day}"), "on_#{day} should return true"
end
rt.every_day = 'smt tfs' # no wednesday
assert !rt.pattern.on_wednesday, "wednesday should be false"
end
def test_validations
rt = @admin.recurring_todos.where(recurring_period: 'weekly').first
assert rt.valid?, "should be valid at start: #{rt.errors.full_messages}"
rt.every_other1 = nil
assert !rt.valid?, "missing evert_x_week should not be valid"
rt.every_other1 = 1
rt.every_day = ' '
assert !rt.valid?, "missing selected days in every_day"
end
end

View file

@ -9,6 +9,61 @@ module RecurringTodos
@admin = users(:admin_user)
end
def test_attribute_mapping
builder = RecurringTodosBuilder.new(@admin, {
'recurring_period' => 'yearly',
'description' => 'a repeating todo', # generic
'recurring_target' => 'show_from_date',
'ends_on' => 'ends_on_end_date',
'end_date' => Time.zone.now + 1.week,
'start_from' => Time.zone.now,
'context_name' => @admin.contexts.first.name,
'yearly_selector' => 'yearly_every_x_day',
'yearly_every_x_day' => 5,
'yearly_every_xth_day' => 6,
'yearly_month_of_year' => 7,
'yearly_month_of_year2' => 8,
'yearly_day_of_week' => 9
})
assert builder.save, "should save: #{builder.errors.full_messages}"
rt = builder.saved_recurring_todo
assert builder.pattern.is_a?(YearlyRepeatPattern), "should be monthly pattern, but is #{builder.pattern.class}"
assert_equal rt.recurrence_selector, builder.pattern.recurrence_selector
assert_equal rt.every_other2, builder.pattern.month_of_year
assert_equal rt.every_other1, builder.pattern.every_x_day
assert_equal rt.every_other3, builder.pattern.every_xth_day
assert_equal rt.every_count, builder.pattern.day_of_week
assert_equal Time.zone.now.month, builder.pattern.month_of_year2, "uses default for moy2, which is current month"
rt.recurrence_selector = 1 # 'yearly_every_xth_day'
assert_equal rt.every_other2, rt.pattern.month_of_year2, "uses every_other2 for moy2 when yearly_every_xth_day"
end
def test_validations
rt = @admin.recurring_todos.where(recurring_period: 'yearly').first
assert rt.valid?, "should be valid at start: #{rt.errors.full_messages}"
rt.recurrence_selector = 0 # 'yearly_every_x_day'
rt.every_other1 = nil
assert !rt.valid?, "should not be valid since every_x_day is empty"
rt.every_other1 = 1
rt.every_other2 = nil
assert !rt.valid?, "should not be valid since month_of_year is empty"
rt.recurrence_selector = 1 # 'yearly_every_xth_day'
rt.every_other2 = nil
assert !rt.valid?, "should not be valid since month_of_year2 is empty"
rt.every_other2 = 1
rt.every_other3 = nil
assert !rt.valid?, "should not be valid since every_xth_day is empty"
rt.every_other3 = 1
rt.every_count = nil
assert !rt.valid?, "should not be valid since day_of_week is empty"
end
end
end