mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-16 23:30:12 +01:00
add some more tests
This commit is contained in:
parent
b23338eaa2
commit
5de96d7eda
8 changed files with 281 additions and 65 deletions
|
|
@ -21,31 +21,16 @@ module RecurringTodos
|
||||||
# build does not add tags. For tags, the recurring todos needs to be saved
|
# build does not add tags. For tags, the recurring todos needs to be saved
|
||||||
def build
|
def build
|
||||||
@recurring_todo = @pattern.build_recurring_todo(@mapped_attributes)
|
@recurring_todo = @pattern.build_recurring_todo(@mapped_attributes)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(recurring_todo)
|
def update(recurring_todo)
|
||||||
@recurring_todo = @pattern.update_recurring_todo(recurring_todo, @mapped_attributes)
|
@recurring_todo = @pattern.update_recurring_todo(recurring_todo, @mapped_attributes)
|
||||||
@saved = @recurring_todo.save
|
save_recurring_todo
|
||||||
@recurring_todo.tag_with(@filterred_attributes.get(:tag_list)) if @saved && @filterred_attributes.get(:tag_list).present?
|
|
||||||
@recurring_todo.reload
|
|
||||||
|
|
||||||
return @saved
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def save
|
def save
|
||||||
build
|
build
|
||||||
@saved = @recurring_todo.save
|
save_recurring_todo
|
||||||
@recurring_todo.tag_with(@filterred_attributes.get(:tag_list)) if @saved && @filterred_attributes.get(:tag_list).present?
|
|
||||||
return @saved
|
|
||||||
end
|
|
||||||
|
|
||||||
def save_collection(collection, collection_id)
|
|
||||||
# save object (project or context) and add its id to @mapped_attributes and remove the object from the attributes
|
|
||||||
object = @mapped_attributes.get(collection)
|
|
||||||
object.save
|
|
||||||
@mapped_attributes.set(collection_id, object.id)
|
|
||||||
@mapped_attributes.except(collection)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_project
|
def save_project
|
||||||
|
|
@ -71,29 +56,31 @@ module RecurringTodos
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_attributes(attributes)
|
def filter_attributes(attributes)
|
||||||
|
# get pattern independend attributes
|
||||||
filterred_attributes = filter_generic_attributes(attributes)
|
filterred_attributes = filter_generic_attributes(attributes)
|
||||||
attributes_to_filter.each{|key| filterred_attributes.set(key, attributes.get(key)) if attributes.key?(key)}
|
# append pattern specific attributes
|
||||||
|
attributes_to_filter.each{|key| filterred_attributes[key]= attributes[key] if attributes.key?(key)}
|
||||||
|
|
||||||
filterred_attributes
|
filterred_attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_generic_attributes(attributes)
|
def filter_generic_attributes(attributes)
|
||||||
return Tracks::AttributeHandler.new(@user, {
|
return Tracks::AttributeHandler.new(@user, {
|
||||||
recurring_period: attributes.get(:recurring_period),
|
recurring_period: attributes[:recurring_period],
|
||||||
description: attributes.get(:description),
|
description: attributes[:description],
|
||||||
notes: attributes.get(:notes),
|
notes: attributes[:notes],
|
||||||
tag_list: tag_list_or_empty_string(attributes),
|
tag_list: tag_list_or_empty_string(attributes),
|
||||||
start_from: attributes.get(:start_from),
|
start_from: attributes[:start_from],
|
||||||
end_date: attributes.get(:end_date),
|
end_date: attributes[:end_date],
|
||||||
ends_on: attributes.get(:ends_on),
|
ends_on: attributes[:ends_on],
|
||||||
show_always: attributes.get(:show_always),
|
target: attributes[:target],
|
||||||
target: attributes.get(:target),
|
project: attributes[:project],
|
||||||
project: attributes.get(:project),
|
context: attributes[:context],
|
||||||
context: attributes.get(:context),
|
project_id: attributes[:project_id],
|
||||||
project_id: attributes.get(:project_id),
|
context_id: attributes[:context_id],
|
||||||
context_id: attributes.get(:context_id),
|
target: attributes[:recurring_target],
|
||||||
target: attributes.get(:recurring_target),
|
show_from_delta: attributes[:recurring_show_days_before],
|
||||||
show_from_delta: attributes.get(:recurring_show_days_before),
|
show_always: attributes[:recurring_show_always]
|
||||||
show_always: attributes.get(:recurring_show_always)
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -103,8 +90,9 @@ module RecurringTodos
|
||||||
end
|
end
|
||||||
|
|
||||||
# helper method to be used in mapped_attributes in subclasses
|
# helper method to be used in mapped_attributes in subclasses
|
||||||
|
# changes name of key from source_key to key
|
||||||
def map(mapping, key, source_key)
|
def map(mapping, key, source_key)
|
||||||
mapping.set(key, mapping.get(source_key))
|
mapping[key] = mapping[source_key]
|
||||||
mapping.except(source_key)
|
mapping.except(source_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -117,7 +105,7 @@ module RecurringTodos
|
||||||
return nil if key.nil?
|
return nil if key.nil?
|
||||||
|
|
||||||
raise Exception.new, "recurrence selector pattern (#{key}) not given" unless @attributes.selector_key_present?(key)
|
raise Exception.new, "recurrence selector pattern (#{key}) not given" unless @attributes.selector_key_present?(key)
|
||||||
selector = @attributes.get(key)
|
selector = @attributes[key]
|
||||||
|
|
||||||
raise Exception.new, "unknown recurrence selector pattern: '#{selector}'" unless valid_selector?(selector)
|
raise Exception.new, "unknown recurrence selector pattern: '#{selector}'" unless valid_selector?(selector)
|
||||||
|
|
||||||
|
|
@ -131,9 +119,28 @@ module RecurringTodos
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def save_recurring_todo
|
||||||
|
@saved = @recurring_todo.save
|
||||||
|
save_tags if @saved
|
||||||
|
return @saved
|
||||||
|
end
|
||||||
|
|
||||||
|
def save_tags
|
||||||
|
@recurring_todo.tag_with(@filterred_attributes[:tag_list]) if @filterred_attributes[:tag_list].present?
|
||||||
|
@recurring_todo.reload
|
||||||
|
end
|
||||||
|
|
||||||
|
def save_collection(collection, collection_id)
|
||||||
|
# save object (project or context) and add its id to @mapped_attributes and remove the object from the attributes
|
||||||
|
object = @mapped_attributes[collection]
|
||||||
|
object.save
|
||||||
|
@mapped_attributes[collection_id] = object.id
|
||||||
|
@mapped_attributes.except(collection)
|
||||||
|
end
|
||||||
|
|
||||||
def tag_list_or_empty_string(attributes)
|
def tag_list_or_empty_string(attributes)
|
||||||
# avoid nil
|
# avoid nil
|
||||||
attributes.get(:tag_list).blank? ? "" : attributes.get(:tag_list).strip
|
attributes[:tag_list].blank? ? "" : attributes[:tag_list].strip
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,10 @@ module RecurringTodos
|
||||||
@attributes = Tracks::AttributeHandler.new(@user, recurring_todo.attributes)
|
@attributes = Tracks::AttributeHandler.new(@user, recurring_todo.attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def valid?
|
||||||
|
@recurring_todo.valid?
|
||||||
|
end
|
||||||
|
|
||||||
def validate_not_blank(object, msg)
|
def validate_not_blank(object, msg)
|
||||||
errors[:base] << msg if object.blank?
|
errors[:base] << msg if object.blank?
|
||||||
end
|
end
|
||||||
|
|
@ -80,7 +84,7 @@ module RecurringTodos
|
||||||
validate_not_nil(show_always, "Please select when to show the action")
|
validate_not_nil(show_always, "Please select when to show the action")
|
||||||
validate_not_blank(show_from_delta, "Please fill in the number of days to show the todo before the due date") unless show_always
|
validate_not_blank(show_from_delta, "Please fill in the number of days to show the todo before the due date") unless show_always
|
||||||
else
|
else
|
||||||
raise Exception.new, "unexpected value of recurrence target selector '#{target}'"
|
errors[:base] << "Unexpected value of recurrence target selector '#{target}'"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -89,7 +93,7 @@ module RecurringTodos
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(attribute)
|
def get(attribute)
|
||||||
@attributes.get attribute
|
@attributes[attribute]
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -12,15 +12,12 @@ module RecurringTodos
|
||||||
parse_project
|
parse_project
|
||||||
parse_context
|
parse_context
|
||||||
|
|
||||||
@builder = create_builder(@attributes.get(:recurring_period))
|
@builder = create_builder(@attributes[:recurring_period])
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_builder(selector)
|
def create_builder(selector)
|
||||||
if %w{daily weekly monthly yearly}.include?(selector)
|
raise "Unknown recurrence selector in :recurring_period (#{selector})" unless valid_selector? selector
|
||||||
return eval("RecurringTodos::#{selector.capitalize}RecurringTodosBuilder.new(@user, @attributes)")
|
eval("RecurringTodos::#{selector.capitalize}RecurringTodosBuilder.new(@user, @attributes)")
|
||||||
else
|
|
||||||
raise Exception.new("Unknown recurrence selector in :recurring_period (#{selector})")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def build
|
def build
|
||||||
|
|
@ -50,18 +47,26 @@ module RecurringTodos
|
||||||
@builder.attributes
|
@builder.attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def pattern
|
||||||
|
@builder.pattern
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def valid_selector?(selector)
|
||||||
|
%w{daily weekly monthly yearly}.include?(selector)
|
||||||
|
end
|
||||||
|
|
||||||
def parse_dates
|
def parse_dates
|
||||||
%w{end_date start_from}.each {|date| @attributes.parse_date date }
|
%w{end_date start_from}.each {|date| @attributes.parse_date date }
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_project
|
def parse_project
|
||||||
@project, @new_project_created = @attributes.parse_collection(:project, @user.projects, @attributes.project_name)
|
@project, @new_project_created = @attributes.parse_collection(:project, @user.projects)
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_context
|
def parse_context
|
||||||
@context, @new_context_created = @attributes.parse_collection(:context, @user.contexts, @attributes.context_name)
|
@context, @new_context_created = @attributes.parse_collection(:context, @user.contexts)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,10 @@ module Tracks
|
||||||
@attributes[attribute.to_sym]
|
@attributes[attribute.to_sym]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def [](attribute)
|
||||||
|
get attribute
|
||||||
|
end
|
||||||
|
|
||||||
def set(key, value)
|
def set(key, value)
|
||||||
@attributes[key.to_sym] = value
|
@attributes[key.to_sym] = value
|
||||||
end
|
end
|
||||||
|
|
@ -21,6 +25,10 @@ module Tracks
|
||||||
@attributes[key.to_sym] ||= value
|
@attributes[key.to_sym] ||= value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def []=(attribute, value)
|
||||||
|
set attribute, value
|
||||||
|
end
|
||||||
|
|
||||||
def except(key)
|
def except(key)
|
||||||
AttributeHandler.new(@user, @attributes.except(key.to_sym))
|
AttributeHandler.new(@user, @attributes.except(key.to_sym))
|
||||||
end
|
end
|
||||||
|
|
@ -30,19 +38,19 @@ module Tracks
|
||||||
end
|
end
|
||||||
|
|
||||||
def selector_key_present?(key)
|
def selector_key_present?(key)
|
||||||
@attributes.key?(key.to_sym)
|
key?(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_date(date)
|
def parse_date(date)
|
||||||
set(date, @user.prefs.parse_date(get(date)))
|
set(date, @user.prefs.parse_date(get(date)))
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_collection(object_type, relation, name)
|
def parse_collection(object_type, relation)
|
||||||
object = nil
|
object = nil
|
||||||
new_object_created = false
|
new_object_created = false
|
||||||
|
|
||||||
if specified_by_name?(object_type)
|
if specified_by_name?(object_type)
|
||||||
object, new_object_created = find_or_create_by_name(relation, name)
|
object, new_object_created = find_or_create_by_name(relation, object_name(object_type))
|
||||||
# put id of object in @attributes, i.e. set :project_id to project.id
|
# put id of object in @attributes, i.e. set :project_id to project.id
|
||||||
@attributes[object_type.to_s + "_id"] = object.id unless new_object_created
|
@attributes[object_type.to_s + "_id"] = object.id unless new_object_created
|
||||||
else
|
else
|
||||||
|
|
@ -53,6 +61,10 @@ module Tracks
|
||||||
return object, new_object_created
|
return object, new_object_created
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def object_name(object_type)
|
||||||
|
send("#{object_type}_name")
|
||||||
|
end
|
||||||
|
|
||||||
def attribute_with_id_of(object_type)
|
def attribute_with_id_of(object_type)
|
||||||
map = { project: 'project_id', context: 'context_id' }
|
map = { project: 'project_id', context: 'context_id' }
|
||||||
get map[object_type]
|
get map[object_type]
|
||||||
|
|
|
||||||
97
test/models/attribute_handler_test.rb
Normal file
97
test/models/attribute_handler_test.rb
Normal 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
|
||||||
|
|
@ -46,7 +46,7 @@ module RecurringTodos
|
||||||
}
|
}
|
||||||
|
|
||||||
builder = RecurringTodosBuilder.new(@admin, attributes)
|
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
|
# given attributes without tag_list
|
||||||
attributes = {
|
attributes = {
|
||||||
|
|
@ -55,7 +55,7 @@ module RecurringTodos
|
||||||
}
|
}
|
||||||
|
|
||||||
builder = RecurringTodosBuilder.new(@admin, attributes)
|
builder = RecurringTodosBuilder.new(@admin, attributes)
|
||||||
assert_equal "", builder.attributes.get(:tag_list)
|
assert_equal "", builder.attributes[:tag_list]
|
||||||
|
|
||||||
# given attributes with nil tag_list
|
# given attributes with nil tag_list
|
||||||
attributes = {
|
attributes = {
|
||||||
|
|
@ -65,7 +65,7 @@ module RecurringTodos
|
||||||
}
|
}
|
||||||
|
|
||||||
builder = RecurringTodosBuilder.new(@admin, attributes)
|
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
|
# given attributes with empty tag_list ==> should be stripped
|
||||||
attributes = {
|
attributes = {
|
||||||
|
|
@ -75,7 +75,7 @@ module RecurringTodos
|
||||||
}
|
}
|
||||||
|
|
||||||
builder = RecurringTodosBuilder.new(@admin, attributes)
|
builder = RecurringTodosBuilder.new(@admin, attributes)
|
||||||
assert_equal "", builder.attributes.get(:tag_list)
|
assert_equal "", builder.attributes[:tag_list]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tags_should_be_saved_on_create_and_update
|
def test_tags_should_be_saved_on_create_and_update
|
||||||
|
|
@ -137,27 +137,27 @@ module RecurringTodos
|
||||||
def test_map_removes_mapped_key
|
def test_map_removes_mapped_key
|
||||||
attributes = Tracks::AttributeHandler.new(@admin, { :source => "value"})
|
attributes = Tracks::AttributeHandler.new(@admin, { :source => "value"})
|
||||||
|
|
||||||
arp = WeeklyRecurringTodosBuilder.new(@admin, attributes)
|
a_builder = WeeklyRecurringTodosBuilder.new(@admin, attributes)
|
||||||
attributes = arp.map(attributes, :target, :source)
|
attributes = a_builder.map(attributes, :target, :source)
|
||||||
|
|
||||||
assert_equal "value", attributes.get(:target)
|
assert_equal "value", attributes[:target]
|
||||||
assert_nil attributes.get(:source)
|
assert_nil attributes[:source]
|
||||||
assert !attributes.key?(:source)
|
assert !attributes.key?(:source)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_get_selector_removes_selector_from_hash
|
def test_get_selector_removes_selector_from_hash
|
||||||
attributes = Tracks::AttributeHandler.new(@admin, { :selector => "weekly" })
|
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 "weekly", a_builder.get_selector(:selector)
|
||||||
assert !arp.attributes.key?(:selector)
|
assert !a_builder.attributes.key?(:selector)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_get_selector_raises_exception_when_missing_selector
|
def test_get_selector_raises_exception_when_missing_selector
|
||||||
attributes = Tracks::AttributeHandler.new(@admin, { })
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,93 @@ module RecurringTodos
|
||||||
@admin = users(:admin_user)
|
@admin = users(:admin_user)
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
@ -10,7 +10,11 @@ module RecurringTodos
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_create_builder_needs_selector
|
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
|
end
|
||||||
|
|
||||||
def test_create_builder_uses_selector
|
def test_create_builder_uses_selector
|
||||||
|
|
@ -35,8 +39,8 @@ module RecurringTodos
|
||||||
'end_date' => '05/05/05'
|
'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[: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[:end_date ].is_a?(ActiveSupport::TimeWithZone), "Dates should be parsed to ActiveSupport::TimeWithZone class"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exisisting_project_is_used
|
def test_exisisting_project_is_used
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue