move mapping of form attributes to model attributes from pattern to builder to make

pattern independent of form
This commit is contained in:
Reinier Balt 2014-02-07 22:55:52 +01:00
parent 1f36c27af8
commit 776a046465
22 changed files with 463 additions and 446 deletions

View file

@ -9,13 +9,21 @@ class RecurringTodosControllerTest < ActionController::TestCase
def test_destroy_recurring_todo
login_as(:admin_user)
rc = RecurringTodo.find(1)
todo = rc.todos.first
xhr :post, :destroy, :id => 1, :_source_view => 'todo'
begin
rc = RecurringTodo.find(1)
rescue
rc = nil
end
assert_nil rc
assert_nil rc, "rc should be deleted"
assert_nil todo.reload.recurring_todo_id, "todo should be unlinked from deleted recurring_todo"
end
def test_new_recurring_todo
@ -362,7 +370,6 @@ class RecurringTodosControllerTest < ActionController::TestCase
def test_update_recurring_todo
login_as(:admin_user)
rt = recurring_todos(:call_bill_gates_every_day)
current_descr = rt.description
put :update,
"recurring_todo" => {

View file

@ -5,6 +5,16 @@ module RecurringTodos
class AbstractRecurringTodosBuilderTest < ActiveSupport::TestCase
fixtures :users
class TestRepeatPattern < AbstractRepeatPattern
def selector_key
'test'
end
def valid_selector?(selector)
true
end
end
def setup
@admin = users(:admin_user)
end
@ -23,7 +33,7 @@ module RecurringTodos
}
assert_raise(Exception, "should have exception since we are using abstract builder") do
builder = AbstractRecurringTodosBuilder.new(@admin, attributes)
builder = AbstractRecurringTodosBuilder.new(@admin, attributes, DailyRepeatPattern)
end
end
@ -101,7 +111,6 @@ module RecurringTodos
assert_equal "bar, foo", rt.tag_list
end
def test_saved_should_raise_exception_on_validation_errors
attributes = {
'recurring_period' => "daily",
@ -125,6 +134,32 @@ module RecurringTodos
assert_raise(Exception, "should have exception since there is no saved recurring todo"){ builder.saved_recurring_todo }
end
def test_map_removes_mapped_key
attributes = { :source => "value"}
arp = WeeklyRecurringTodosBuilder.new(@admin, attributes)
attributes = arp.map(attributes, :target, :source)
assert_equal "value", attributes[:target]
assert_nil attributes[:source]
assert !attributes.key?(:source)
end
def test_get_selector_removes_selector_from_hash
attributes = { :selector => "weekly" }
arp = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert "weekly", arp.get_selector(:selector)
assert !arp.attributes.key?(:selector)
end
def test_get_selector_raises_exception_when_missing_selector
attributes = { }
arp = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert_raise(Exception, "should raise exception when recurrence selector is missing"){ arp.get_selector(:selector) }
end
end
end

View file

@ -5,42 +5,10 @@ module RecurringTodos
class AbstractRepeatPatternTest < ActiveSupport::TestCase
fixtures :users
class TestRepeatPattern < AbstractRepeatPattern
def valid_selector?(selector)
true
end
end
def setup
@admin = users(:admin_user)
end
def test_map_removes_mapped_key
attributes = { :source => "value"}
arp = AbstractRepeatPattern.new(@admin, attributes)
attributes = arp.map(attributes, :target, :source)
assert_equal "value", attributes[:target]
assert_nil attributes[:source]
assert !attributes.key?(:source)
end
def test_get_selector_removes_selector_from_hash
attributes = { :selector => "weekly" }
arp = TestRepeatPattern.new(@admin, attributes)
assert "weekly", arp.get_selector(:selector)
assert !arp.attributes.key?(:selector)
end
def test_get_selector_raises_exception_when_missing_selector
attributes = { }
arp = TestRepeatPattern.new(@admin, attributes)
assert_raise(Exception, "should raise exception when recurrence selector is missing"){ arp.get_selector(:selector) }
end
end
end

View file

@ -30,6 +30,50 @@ module RecurringTodos
assert_equal "a repeating todo", result[:description], "description should be preserved"
end
def test_valid_selector
attributes = {
'recurring_period' => 'daily'
}
# should not raise
%w{daily_every_x_day daily_every_work_day}.each do |selector|
attributes['daily_selector'] = selector
DailyRecurringTodosBuilder.new(@admin, attributes)
end
# should raise
attributes = {
'recurring_period' => 'daily',
'daily_selector' => 'wrong value'
}
# should raise
assert_raise(Exception, "should have exception since daily_selector has wrong value"){ DailyRecurringTodosBuilder.new(@admin, attributes) }
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'daily',
'description' => 'a repeating todo', # generic
'daily_selector' => 'daily_every_x_day', # daily specific --> mapped to only_work_days=false
'daily_every_x_days' => '5' # mapped to every_other1
}
pattern = DailyRecurringTodosBuilder.new(@admin, attributes)
assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to daily_every_x_days"
assert_equal false, pattern.mapped_attributes[:only_work_days], "only_work_days should be set to false for daily_every_x_day"
attributes = {
'recurring_period' => 'daily',
'description' => 'a repeating todo', # generic
'daily_selector' => 'daily_every_work_day', # daily specific --> mapped to only_work_days=true
}
pattern = DailyRecurringTodosBuilder.new(@admin, attributes)
assert_equal true, pattern.mapped_attributes[:only_work_days]
end
end
end

View file

@ -9,50 +9,6 @@ module RecurringTodos
@admin = users(:admin_user)
end
def test_valid_selector
attributes = {
'recurring_period' => 'daily'
}
# should not raise
%w{daily_every_x_day daily_every_work_day}.each do |selector|
attributes['daily_selector'] = selector
DailyRepeatPattern.new(@admin, attributes)
end
# should raise
attributes = {
'recurring_period' => 'daily',
'daily_selector' => 'wrong value'
}
# should raise
assert_raise(Exception, "should have exception since daily_selector has wrong value"){ DailyRepeatPattern.new(@admin, attributes) }
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'daily',
'description' => 'a repeating todo', # generic
'daily_selector' => 'daily_every_x_day', # daily specific --> mapped to only_work_days=false
'daily_every_x_days' => '5' # mapped to every_other1
}
pattern = DailyRepeatPattern.new(@admin, attributes)
assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to daily_every_x_days"
assert_equal false, pattern.mapped_attributes[:only_work_days], "only_work_days should be set to false for daily_every_x_day"
attributes = {
'recurring_period' => 'daily',
'description' => 'a repeating todo', # generic
'daily_selector' => 'daily_every_work_day', # daily specific --> mapped to only_work_days=true
}
pattern = DailyRepeatPattern.new(@admin, attributes)
assert_equal true, pattern.mapped_attributes[:only_work_days]
end
end
end

View file

@ -30,6 +30,66 @@ module RecurringTodos
assert_equal 5, result[:every_other1], "should be preserved"
end
def test_valid_selector
attributes = {
'recurring_period' => 'monthly'
}
# should not raise
%w{monthly_every_x_day monthly_every_xth_day}.each do |selector|
attributes['monthly_selector'] = selector
MonthlyRecurringTodosBuilder.new(@admin, attributes)
end
# should raise
attributes = {
'recurring_period' => 'monthly',
'monthly_selector' => 'wrong value'
}
# should raise
assert_raise(Exception, "should have exception since monthly_selector has wrong value"){ MonthlyRecurringTodosBuilder.new(@admin, attributes) }
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'monthly',
'description' => 'a repeating todo', # generic
'monthly_selector' => 'monthly_every_x_day', # monthly specific
'monthly_every_x_day' => '5', # mapped to :every_other1
'monthly_every_xth_day' => '7', # mapped to :every_other3
'monthly_day_of_week' => 3, # mapped to :every_count
'monthly_every_x_month' => '10', # mapped to :every_other2
'monthly_every_x_month2' => '20' # not mapped
}
builder = MonthlyRecurringTodosBuilder.new(@admin, attributes)
assert_equal 0, builder.mapped_attributes[:recurrence_selector], "selector should be 0 for monthly_every_x_day"
assert_equal '5', builder.mapped_attributes[:every_other1], "every_other1 should be set to monthly_every_x_days"
assert_equal '10', builder.mapped_attributes[:every_other2], "every_other2 should be set to monthly_every_x_month when selector is monthly_every_x_day (=0)"
assert_equal '7', builder.mapped_attributes[:every_other3], "every_other3 should be set to monthly_every_xth_day"
assert_equal 3, builder.mapped_attributes[:every_count], "every_count should be set to monthly_day_of_week"
builder.build
assert builder.pattern.every_x_day?, "every_x_day? should say true for selector monthly_every_x_day"
attributes = {
'recurring_period' => 'monthly',
'description' => 'a repeating todo', # generic
'monthly_selector' => 'monthly_every_xth_day', # monthly specific
'monthly_every_x_day' => '5', # mapped to :every_other1
'monthly_every_x_month' => '10', # not mapped
'monthly_every_x_month2' => '20' # mapped to :every_other2
}
builder = MonthlyRecurringTodosBuilder.new(@admin, attributes)
assert_equal 1, builder.mapped_attributes[:recurrence_selector], "selector should be 1 for monthly_every_xth_day"
assert_equal '20', builder.mapped_attributes[:every_other2], "every_other2 should be set to monthly_every_x_month2 when selector is monthly_every_xth_day (=0)"
builder.build
assert builder.pattern.every_xth_day?, "every_xth_day? should say true for selector monthly_every_xth_day"
end
end
end

View file

@ -8,67 +8,7 @@ module RecurringTodos
def setup
@admin = users(:admin_user)
end
def test_valid_selector
attributes = {
'recurring_period' => 'monthly'
}
# should not raise
%w{monthly_every_x_day monthly_every_xth_day}.each do |selector|
attributes['monthly_selector'] = selector
MonthlyRepeatPattern.new(@admin, attributes)
end
# should raise
attributes = {
'recurring_period' => 'monthly',
'monthly_selector' => 'wrong value'
}
# should raise
assert_raise(Exception, "should have exception since monthly_selector has wrong value"){ MonthlyRepeatPattern.new(@admin, attributes) }
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'monthly',
'description' => 'a repeating todo', # generic
'monthly_selector' => 'monthly_every_x_day', # monthly specific
'monthly_every_x_day' => '5', # mapped to :every_other1
'monthly_every_xth_day' => '7', # mapped to :every_other3
'monthly_day_of_week' => 3, # mapped to :every_count
'monthly_every_x_month' => '10', # mapped to :every_other2
'monthly_every_x_month2' => '20' # not mapped
}
pattern = MonthlyRepeatPattern.new(@admin, attributes)
assert_equal 0, pattern.mapped_attributes[:recurrence_selector], "selector should be 0 for monthly_every_x_day"
assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to monthly_every_x_days"
assert_equal '10', pattern.mapped_attributes[:every_other2], "every_other2 should be set to monthly_every_x_month when selector is monthly_every_x_day (=0)"
assert_equal '7', pattern.mapped_attributes[:every_other3], "every_other3 should be set to monthly_every_xth_day"
assert_equal 3, pattern.mapped_attributes[:every_count], "every_count should be set to monthly_day_of_week"
pattern.build_recurring_todo
assert pattern.every_x_day?, "every_x_day? should say true for selector monthly_every_x_day"
attributes = {
'recurring_period' => 'monthly',
'description' => 'a repeating todo', # generic
'monthly_selector' => 'monthly_every_xth_day', # monthly specific
'monthly_every_x_day' => '5', # mapped to :every_other1
'monthly_every_x_month' => '10', # not mapped
'monthly_every_x_month2' => '20' # mapped to :every_other2
}
pattern = MonthlyRepeatPattern.new(@admin, attributes)
assert_equal 1, pattern.mapped_attributes[:recurrence_selector], "selector should be 1 for monthly_every_xth_day"
assert_equal '20', pattern.mapped_attributes[:every_other2], "every_other2 should be set to monthly_every_x_month2 when selector is monthly_every_xth_day (=0)"
pattern.build_recurring_todo
assert pattern.every_xth_day?, "every_xth_day? should say true for selector monthly_every_xth_day"
end
end
end

View file

@ -43,6 +43,45 @@ module RecurringTodos
assert w.attributes_to_filter.include?('weekly_return_monday'), "attributes_to_filter should return generated weekly_return_xyz"
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'weekly',
'description' => 'a repeating todo', # generic
'weekly_every_x_week' => '5', # mapped to every_other1
'weekly_return_monday' => 'm'
}
pattern = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to weekly_every_x_week"
assert_equal ' m ', pattern.mapped_attributes[:every_day], "weekly_return_<weekday> should be mapped to :every_day in format 'smtwtfs'"
end
def test_map_day
attributes = {
'recurring_period' => 'weekly',
'description' => 'a repeating todo', # generic
'weekly_every_x_week' => '5' # mapped to every_other1
}
pattern = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert_equal ' ', pattern.mapped_attributes[:every_day], "all days should be empty in :every_day"
# add all days
{ sunday: 's', monday: 'm', tuesday: 't', wednesday: 'w', thursday: 't', friday: 'f', saturday: 's' }.each do |day, short|
attributes["weekly_return_#{day}"] = short
end
pattern = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert_equal 'smtwtfs', pattern.mapped_attributes[:every_day], "all days should be filled in :every_day"
# remove wednesday
attributes = attributes.except('weekly_return_wednesday')
pattern = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert_equal 'smt tfs', pattern.mapped_attributes[:every_day], "only wednesday should be empty in :every_day"
end
end
end

View file

@ -8,45 +8,7 @@ module RecurringTodos
def setup
@admin = users(:admin_user)
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'weekly',
'description' => 'a repeating todo', # generic
'weekly_every_x_week' => '5', # mapped to every_other1
'weekly_return_monday' => 'm'
}
pattern = WeeklyRepeatPattern.new(@admin, attributes)
assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to weekly_every_x_week"
assert_equal ' m ', pattern.mapped_attributes[:every_day], "weekly_return_<weekday> should be mapped to :every_day in format 'smtwtfs'"
end
def test_map_day
attributes = {
'recurring_period' => 'weekly',
'description' => 'a repeating todo', # generic
'weekly_every_x_week' => '5' # mapped to every_other1
}
pattern = WeeklyRepeatPattern.new(@admin, attributes)
assert_equal ' ', pattern.mapped_attributes[:every_day], "all days should be empty in :every_day"
# add all days
{ sunday: 's', monday: 'm', tuesday: 't', wednesday: 'w', thursday: 't', friday: 'f', saturday: 's' }.each do |day, short|
attributes["weekly_return_#{day}"] = short
end
pattern = WeeklyRepeatPattern.new(@admin, attributes)
assert_equal 'smtwtfs', pattern.mapped_attributes[:every_day], "all days should be filled in :every_day"
# remove wednesday
attributes = attributes.except('weekly_return_wednesday')
pattern = WeeklyRepeatPattern.new(@admin, attributes)
assert_equal 'smt tfs', pattern.mapped_attributes[:every_day], "only wednesday should be empty in :every_day"
end
end
end

View file

@ -24,13 +24,65 @@ module RecurringTodos
}
result = RecurringTodosBuilder.new(@admin, attributes).attributes
assert_nil result['bla_bla'], "bla_bla should be filtered"
assert_nil result[:bla_bla], "bla_bla should be filtered"
assert_equal '1', result[:every_other2], "yearly attributes should be preserved"
assert_equal "a repeating todo", result[:description], "description should be preserved"
end
def test_valid_selector
attributes = {
'recurring_period' => 'yearly'
}
# should not raise
%w{yearly_every_x_day yearly_every_xth_day}.each do |selector|
attributes['yearly_selector'] = selector
YearlyRecurringTodosBuilder.new(@admin, attributes)
end
# should raise
attributes = {
'recurring_period' => 'yearly',
'yearly_selector' => 'wrong value'
}
# should raise
assert_raise(Exception, "should have exception since yearly_selector has wrong value"){ YearlyRecurringTodosBuilder.new(@admin, attributes) }
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'yearly',
'description' => 'a repeating todo', # generic
'yearly_selector' => 'yearly_every_x_day', # yearly specific
'yearly_every_x_day' => '5', # mapped to every_other1
'yearly_every_xth_day' => '7', # mapped to every_other3
'yearly_day_of_week' => '3', # mapped to every_count
'yearly_month_of_year' => '1', # mapped to evert_other2 because yearly_selector is yearly_every_x_day
'yearly_month_of_year2' => '2' # ignored because yearly_selector is yearly_every_x_day
}
pattern = YearlyRecurringTodosBuilder.new(@admin, attributes)
assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to yearly_every_x_day"
assert_equal '1', pattern.mapped_attributes[:every_other2], "every_other2 should be set to yearly_month_of_year because selector is yearly_every_x_day"
assert_equal '7', pattern.mapped_attributes[:every_other3], "every_other3 should be set to yearly_every_xth_day"
assert_equal '3', pattern.mapped_attributes[:every_count], "every_count should be set to yearly_day_of_week"
attributes = {
'recurring_period' => 'yearly',
'description' => 'a repeating todo', # generic
'yearly_selector' => 'yearly_every_xth_day', # daily specific --> mapped to only_work_days=false
'yearly_month_of_year' => '1', # ignored because yearly_selector is yearly_every_xth_day
'yearly_month_of_year2' => '2' # mapped to evert_other2 because yearly_selector is yearly_every_xth_day
}
pattern = YearlyRecurringTodosBuilder.new(@admin, attributes)
assert_equal '2', pattern.mapped_attributes[:every_other2], "every_other2 should be set to yearly_month_of_year2 because selector is yearly_every_xth_day"
end
end
end

View file

@ -9,58 +9,6 @@ module RecurringTodos
@admin = users(:admin_user)
end
def test_valid_selector
attributes = {
'recurring_period' => 'yearly'
}
# should not raise
%w{yearly_every_x_day yearly_every_xth_day}.each do |selector|
attributes['yearly_selector'] = selector
YearlyRepeatPattern.new(@admin, attributes)
end
# should raise
attributes = {
'recurring_period' => 'yearly',
'yearly_selector' => 'wrong value'
}
# should raise
assert_raise(Exception, "should have exception since yearly_selector has wrong value"){ YearlyRepeatPattern.new(@admin, attributes) }
end
def test_mapping_of_attributes
attributes = {
'recurring_period' => 'yearly',
'description' => 'a repeating todo', # generic
'yearly_selector' => 'yearly_every_x_day', # yearly specific
'yearly_every_x_day' => '5', # mapped to every_other1
'yearly_every_xth_day' => '7', # mapped to every_other3
'yearly_day_of_week' => '3', # mapped to every_count
'yearly_month_of_year' => '1', # mapped to evert_other2 because yearly_selector is yearly_every_x_day
'yearly_month_of_year2' => '2' # ignored because yearly_selector is yearly_every_x_day
}
pattern = YearlyRepeatPattern.new(@admin, attributes)
assert_equal '5', pattern.mapped_attributes[:every_other1], "every_other1 should be set to yearly_every_x_day"
assert_equal '1', pattern.mapped_attributes[:every_other2], "every_other2 should be set to yearly_month_of_year because selector is yearly_every_x_day"
assert_equal '7', pattern.mapped_attributes[:every_other3], "every_other3 should be set to yearly_every_xth_day"
assert_equal '3', pattern.mapped_attributes[:every_count], "every_count should be set to yearly_day_of_week"
attributes = {
'recurring_period' => 'yearly',
'description' => 'a repeating todo', # generic
'yearly_selector' => 'yearly_every_xth_day', # daily specific --> mapped to only_work_days=false
'yearly_month_of_year' => '1', # ignored because yearly_selector is yearly_every_xth_day
'yearly_month_of_year2' => '2' # mapped to evert_other2 because yearly_selector is yearly_every_xth_day
}
pattern = YearlyRepeatPattern.new(@admin, attributes)
assert_equal '2', pattern.mapped_attributes[:every_other2], "every_other2 should be set to yearly_month_of_year2 because selector is yearly_every_xth_day"
end
end
end