2014-05-16 18:03:13 -04:00
require 'test_helper'
2014-01-27 16:42:54 +01:00
module RecurringTodos
class YearlyRecurringTodosBuilderTest < ActiveSupport :: TestCase
fixtures :users
def setup
@admin = users ( :admin_user )
end
def test_pattern_is_yearly
object = RecurringTodosBuilder . new ( @admin , { 'recurring_period' = > 'yearly' , 'yearly_selector' = > 'yearly_every_x_day' } )
assert object . builder . is_a? YearlyRecurringTodosBuilder
end
def test_filter_non_daily_attributes
attributes = {
'recurring_period' = > 'yearly' ,
2015-02-10 16:25:27 +01:00
'description' = > 'a recurring todo' , # generic
2014-01-27 16:42:54 +01:00
'yearly_selector' = > 'yearly_every_x_day' , # daily specific
'yearly_month_of_year' = > '1' , # mapped to evert_other2 because yearly_selector is yearly_every_x_day
'bla_bla' = > 'go away' # irrelevant for daily
}
result = RecurringTodosBuilder . new ( @admin , attributes ) . attributes
2014-02-07 22:55:52 +01:00
2014-02-09 21:48:52 +01:00
assert_nil result . get ( 'bla_bla' ) , " bla_bla should be filtered "
assert_nil result . get ( :bla_bla ) , " bla_bla should be filtered "
assert_equal '1' , result . get ( :every_other2 ) , " yearly attributes should be preserved "
2015-02-10 16:25:27 +01:00
assert_equal " a recurring todo " , result . get ( :description ) , " description should be preserved "
2014-01-27 16:42:54 +01:00
end
2014-02-07 22:55:52 +01:00
def test_valid_selector
2014-02-09 21:48:52 +01:00
attributes = Tracks :: AttributeHandler . new ( @admin , {
2014-02-07 22:55:52 +01:00
'recurring_period' = > 'yearly'
2014-02-09 21:48:52 +01:00
} )
2014-02-07 22:55:52 +01:00
# should not raise
%w{ yearly_every_x_day yearly_every_xth_day } . each do | selector |
2014-02-09 21:48:52 +01:00
attributes . set ( :yearly_selector , selector )
2014-02-07 22:55:52 +01:00
YearlyRecurringTodosBuilder . new ( @admin , attributes )
end
# should raise
2014-02-09 21:48:52 +01:00
attributes = Tracks :: AttributeHandler . new ( @admin , {
2014-02-07 22:55:52 +01:00
'recurring_period' = > 'yearly' ,
'yearly_selector' = > 'wrong value'
2014-02-09 21:48:52 +01:00
} )
2014-02-07 22:55:52 +01:00
# 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' ,
2015-02-10 16:25:27 +01:00
'description' = > 'a recurring todo' , # generic
2014-02-07 22:55:52 +01:00
'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
}
2014-02-09 21:48:52 +01:00
pattern = YearlyRecurringTodosBuilder . new ( @admin , Tracks :: AttributeHandler . new ( @admin , attributes ) )
2014-02-07 22:55:52 +01:00
2014-02-09 21:48:52 +01:00
assert_equal '5' , pattern . mapped_attributes . get ( :every_other1 ) , " every_other1 should be set to yearly_every_x_day "
assert_equal '1' , pattern . mapped_attributes . get ( :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 . get ( :every_other3 ) , " every_other3 should be set to yearly_every_xth_day "
assert_equal '3' , pattern . mapped_attributes . get ( :every_count ) , " every_count should be set to yearly_day_of_week "
2014-02-07 22:55:52 +01:00
attributes = {
'recurring_period' = > 'yearly' ,
2015-02-10 16:25:27 +01:00
'description' = > 'a recurring todo' , # generic
2014-02-07 22:55:52 +01:00
'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
}
2014-02-09 21:48:52 +01:00
pattern = YearlyRecurringTodosBuilder . new ( @admin , Tracks :: AttributeHandler . new ( @admin , attributes ) )
assert_equal '2' , pattern . mapped_attributes . get ( :every_other2 ) , " every_other2 should be set to yearly_month_of_year2 because selector is yearly_every_xth_day "
2014-02-07 22:55:52 +01:00
end
2014-01-27 16:42:54 +01:00
end
end