add some more tests

This commit is contained in:
Reinier Balt 2014-02-10 19:39:39 +01:00
parent b23338eaa2
commit 5de96d7eda
8 changed files with 281 additions and 65 deletions

View file

@ -0,0 +1,97 @@
require_relative '../test_helper'
class AttributeHandlerTest < ActiveSupport::TestCase
fixtures :users
def test_setting_attributes
h = Tracks::AttributeHandler.new(nil, {})
h.set('test', '123')
h['other']='one'
assert_equal '123', h.attributes[:test], ":test should be added"
assert_nil h.attributes['test'], "string should be converted to symbol"
assert_equal 'one', h[:other], ":other should be added as symbol using []="
assert_nil h.attributes[:new]
h.set_if_nil(:new, 'value')
assert_equal 'value', h.attributes[:new], "value should be set for new key"
h.set_if_nil(:new, 'other')
assert_equal 'value', h.attributes[:new], "value should not be set for existing key"
h.attributes[:empty] = nil
h.set_if_nil(:empty, "test")
assert_equal "test", h.attributes[:empty], "nil value should be overwritten"
end
def test_getting_attributes
h = Tracks::AttributeHandler.new(nil, { :get => "me"} )
assert h.key?(:get), "attributehandler should have key :get"
assert h.key?('get'), "attributehandler should have key :get"
assert_equal "me", h.attributes[:get], "attributehandler should have key :get"
assert_equal "me", h.get('get'), "key should be converted to symbol"
assert_equal "me", h[:get], "AttributeHandler should act like hash"
end
def test_removing_attributes
h = Tracks::AttributeHandler.new(nil, { :i_am => "here"} )
assert h.key?(:i_am)
h.except(:i_am)
assert h.key?(:i_am), "AttributeHandler should be immutable"
h2 = h.except("i_am")
assert !h2.key?(:i_am), "key as symbol should be removed"
end
def test_project_specified_by_name
h = Tracks::AttributeHandler.new(nil, { } )
assert !h.project_specified_by_name?, "project is not specified by id or by name"
h[:project_id]=4
assert !h.project_specified_by_name?, "project is specified by id, not by name"
h = h.except(:project_id)
h[:project_name] = "A project"
assert h.project_specified_by_name?, "project is specified by name"
h[:project_name] = "None"
assert !h.project_specified_by_name?, "None is special token to specify nil-project"
end
def test_context_specified_by_name
h = Tracks::AttributeHandler.new(nil, { } )
assert !h.context_specified_by_name?, "context is not specified by id or by name"
h["context_id"] = 4
assert !h.context_specified_by_name?, "context is specified by id, not by name"
h = h.except(:context_id)
h[:context_name] = "A context"
assert h.context_specified_by_name?, "context is specified by name"
end
def test_parse_collection
admin = users(:admin_user)
project = admin.projects.first
h = Tracks::AttributeHandler.new(admin, { "project_id" => project.id } )
parsed_project, new_project_created = h.parse_collection(:project, admin.projects)
assert !new_project_created, "should find existing project"
assert_equal project.id, parsed_project.id, "it should find the project"
h = Tracks::AttributeHandler.new(admin, { "project_name" => project.name } )
parsed_project, new_project_created = h.parse_collection(:project, admin.projects)
assert !new_project_created, "should find existing project"
assert_equal project.id, parsed_project.id, "it should find the project"
h = Tracks::AttributeHandler.new(admin, { "project_name" => "new project" } )
parsed_project, new_project_created = h.parse_collection(:project, admin.projects)
assert new_project_created, "should detect that no project exist with that name"
assert_equal "new project", parsed_project.name, "it should return a new project"
assert !parsed_project.persisted?, "new project should not be persisted (yet)"
end
end

View file

@ -46,7 +46,7 @@ module RecurringTodos
}
builder = RecurringTodosBuilder.new(@admin, attributes)
assert_equal "tag, this, that", builder.attributes.get(:tag_list)
assert_equal "tag, this, that", builder.attributes[:tag_list]
# given attributes without tag_list
attributes = {
@ -55,7 +55,7 @@ module RecurringTodos
}
builder = RecurringTodosBuilder.new(@admin, attributes)
assert_equal "", builder.attributes.get(:tag_list)
assert_equal "", builder.attributes[:tag_list]
# given attributes with nil tag_list
attributes = {
@ -65,7 +65,7 @@ module RecurringTodos
}
builder = RecurringTodosBuilder.new(@admin, attributes)
assert_equal "", builder.attributes.get(:tag_list)
assert_equal "", builder.attributes[:tag_list]
# given attributes with empty tag_list ==> should be stripped
attributes = {
@ -75,7 +75,7 @@ module RecurringTodos
}
builder = RecurringTodosBuilder.new(@admin, attributes)
assert_equal "", builder.attributes.get(:tag_list)
assert_equal "", builder.attributes[:tag_list]
end
def test_tags_should_be_saved_on_create_and_update
@ -137,27 +137,27 @@ module RecurringTodos
def test_map_removes_mapped_key
attributes = Tracks::AttributeHandler.new(@admin, { :source => "value"})
arp = WeeklyRecurringTodosBuilder.new(@admin, attributes)
attributes = arp.map(attributes, :target, :source)
a_builder = WeeklyRecurringTodosBuilder.new(@admin, attributes)
attributes = a_builder.map(attributes, :target, :source)
assert_equal "value", attributes.get(:target)
assert_nil attributes.get(:source)
assert_equal "value", attributes[:target]
assert_nil attributes[:source]
assert !attributes.key?(:source)
end
def test_get_selector_removes_selector_from_hash
attributes = Tracks::AttributeHandler.new(@admin, { :selector => "weekly" })
arp = WeeklyRecurringTodosBuilder.new(@admin, attributes)
a_builder = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert "weekly", arp.get_selector(:selector)
assert !arp.attributes.key?(:selector)
assert "weekly", a_builder.get_selector(:selector)
assert !a_builder.attributes.key?(:selector)
end
def test_get_selector_raises_exception_when_missing_selector
attributes = Tracks::AttributeHandler.new(@admin, { })
arp = WeeklyRecurringTodosBuilder.new(@admin, attributes)
a_builder = WeeklyRecurringTodosBuilder.new(@admin, attributes)
assert_raise(Exception, "should raise exception when recurrence selector is missing"){ arp.get_selector(:selector) }
assert_raise(Exception, "should raise exception when recurrence selector is missing"){ a_builder.get_selector(:selector) }
end
end

View file

@ -9,6 +9,93 @@ module RecurringTodos
@admin = users(:admin_user)
end
def test_pattern_builds_from_existing_recurring_todo
rt = @admin.recurring_todos.first
pattern = rt.pattern
assert pattern.is_a?(DailyRepeatPattern), "recurring todo should have daily pattern"
end
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,
}
pattern = create_pattern(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)
assert !pattern.valid?, "should fail because show_from_delta is not there"
attributes[:recurring_show_days_before] = 5
pattern = create_pattern(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,
'recurring_show_always' => false,
'recurring_show_days_before' => 5,
}
pattern = create_pattern(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)
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)
assert !pattern.valid?
attributes['ends_on']='ends_on_end_date'
pattern = create_pattern(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)
assert pattern.valid?, "should be valid"
end
private
def create_pattern(attributes)
builder = RecurringTodosBuilder.new(@admin, attributes)
builder.build
builder.pattern
end
end
end

View file

@ -10,7 +10,11 @@ module RecurringTodos
end
def test_create_builder_needs_selector
assert_raise(Exception){ builder = RecurringTodosBuilder.new(@admin, {}) }
assert_raise(RuntimeError){ builder = RecurringTodosBuilder.new(@admin, {}) }
end
def test_create_builder_needs_valid_selector
assert_raise(RuntimeError){ builder = RecurringTodosBuilder.new(@admin, { 'recurring_period' => 'wrong_value'}) }
end
def test_create_builder_uses_selector
@ -35,8 +39,8 @@ module RecurringTodos
'end_date' => '05/05/05'
})
assert builder.attributes.get(:start_from).is_a?(ActiveSupport::TimeWithZone), "Dates should be parsed to ActiveSupport::TimeWithZone class"
assert builder.attributes.get(:end_date).is_a?(ActiveSupport::TimeWithZone), "Dates should be parsed to ActiveSupport::TimeWithZone class"
assert builder.attributes[:start_from].is_a?(ActiveSupport::TimeWithZone), "Dates should be parsed to ActiveSupport::TimeWithZone class"
assert builder.attributes[:end_date ].is_a?(ActiveSupport::TimeWithZone), "Dates should be parsed to ActiveSupport::TimeWithZone class"
end
def test_exisisting_project_is_used