Adds some test helpers to cleanup tests.

git-svn-id: http://svn.redmine.org/redmine/trunk@12746 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2014-01-31 18:13:42 +00:00
parent 888823a449
commit d651b93eff
2 changed files with 71 additions and 62 deletions

View File

@ -179,3 +179,16 @@ module ObjectHelpers
changeset changeset
end end
end end
module IssueObjectHelpers
def close!
self.status = IssueStatus.where(:is_closed => true).first
save!
end
def generate_child!(attributes={})
Issue.generate!(attributes.merge(:parent_issue_id => self.id))
end
end
Issue.send :include, IssueObjectHelpers

View File

@ -43,7 +43,7 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_create_child_issue def test_create_child_issue
lft = new_issue_lft lft = new_issue_lft
parent = Issue.generate! parent = Issue.generate!
child = Issue.generate!(:parent_issue_id => parent.id) child = parent.generate_child!
parent.reload parent.reload
child.reload child.reload
assert_equal [parent.id, nil, lft, lft + 3], [parent.root_id, parent.parent_id, parent.lft, parent.rgt] assert_equal [parent.id, nil, lft, lft + 3], [parent.root_id, parent.parent_id, parent.lft, parent.rgt]
@ -70,7 +70,7 @@ class IssueNestedSetTest < ActiveSupport::TestCase
lft = new_issue_lft lft = new_issue_lft
parent1 = Issue.generate! parent1 = Issue.generate!
parent2 = Issue.generate! parent2 = Issue.generate!
child = Issue.generate!(:parent_issue_id => parent1.id) child = parent1.generate_child!
parent2.parent_issue_id = parent1.id parent2.parent_issue_id = parent1.id
parent2.save! parent2.save!
child.reload child.reload
@ -86,7 +86,7 @@ class IssueNestedSetTest < ActiveSupport::TestCase
parent1 = Issue.generate! parent1 = Issue.generate!
lft2 = new_issue_lft lft2 = new_issue_lft
parent2 = Issue.generate! parent2 = Issue.generate!
child = Issue.generate!(:parent_issue_id => parent1.id) child = parent1.generate_child!
child.parent_issue_id = nil child.parent_issue_id = nil
child.save! child.save!
child.reload child.reload
@ -102,7 +102,7 @@ class IssueNestedSetTest < ActiveSupport::TestCase
parent1 = Issue.generate! parent1 = Issue.generate!
lft2 = new_issue_lft lft2 = new_issue_lft
parent2 = Issue.generate! parent2 = Issue.generate!
child = Issue.generate!(:parent_issue_id => parent1.id) child = parent1.generate_child!
child.parent_issue_id = parent2.id child.parent_issue_id = parent2.id
child.save! child.save!
child.reload child.reload
@ -118,8 +118,8 @@ class IssueNestedSetTest < ActiveSupport::TestCase
parent1 = Issue.generate! parent1 = Issue.generate!
lft2 = new_issue_lft lft2 = new_issue_lft
parent2 = Issue.generate! parent2 = Issue.generate!
child = Issue.generate!(:parent_issue_id => parent1.id) child = parent1.generate_child!
grandchild = Issue.generate!(:parent_issue_id => child.id) grandchild = child.generate_child!
parent1.reload parent1.reload
parent2.reload parent2.reload
child.reload child.reload
@ -143,8 +143,8 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_move_a_child_with_descendants_to_another_project def test_move_a_child_with_descendants_to_another_project
lft1 = new_issue_lft lft1 = new_issue_lft
parent1 = Issue.generate! parent1 = Issue.generate!
child = Issue.generate!(:parent_issue_id => parent1.id) child = parent1.generate_child!
grandchild = Issue.generate!(:parent_issue_id => child.id) grandchild = child.generate_child!
child.reload child.reload
child.project = Project.find(2) child.project = Project.find(2)
assert child.save assert child.save
@ -159,8 +159,8 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_moving_an_issue_to_a_descendant_should_not_validate def test_moving_an_issue_to_a_descendant_should_not_validate
parent1 = Issue.generate! parent1 = Issue.generate!
parent2 = Issue.generate! parent2 = Issue.generate!
child = Issue.generate!(:parent_issue_id => parent1.id) child = parent1.generate_child!
grandchild = Issue.generate!(:parent_issue_id => child.id) grandchild = child.generate_child!
child.reload child.reload
child.parent_issue_id = grandchild.id child.parent_issue_id = grandchild.id
@ -207,8 +207,8 @@ class IssueNestedSetTest < ActiveSupport::TestCase
lft1 = new_issue_lft lft1 = new_issue_lft
issue1 = Issue.generate! issue1 = Issue.generate!
issue2 = Issue.generate! issue2 = Issue.generate!
issue3 = Issue.generate!(:parent_issue_id => issue2.id) issue3 = issue2.generate_child!
issue4 = Issue.generate!(:parent_issue_id => issue1.id) issue4 = issue1.generate_child!
issue3.init_journal(User.find(2)) issue3.init_journal(User.find(2))
issue3.subject = 'child with journal' issue3.subject = 'child with journal'
issue3.save! issue3.save!
@ -230,8 +230,8 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_destroy_child_should_update_parent def test_destroy_child_should_update_parent
lft1 = new_issue_lft lft1 = new_issue_lft
issue = Issue.generate! issue = Issue.generate!
child1 = Issue.generate!(:parent_issue_id => issue.id) child1 = issue.generate_child!
child2 = Issue.generate!(:parent_issue_id => issue.id) child2 = issue.generate_child!
issue.reload issue.reload
assert_equal [issue.id, lft1, lft1 + 5], [issue.root_id, issue.lft, issue.rgt] assert_equal [issue.id, lft1, lft1 + 5], [issue.root_id, issue.lft, issue.rgt]
child2.reload.destroy child2.reload.destroy
@ -241,8 +241,8 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_destroy_parent_issue_updated_during_children_destroy def test_destroy_parent_issue_updated_during_children_destroy
parent = Issue.generate! parent = Issue.generate!
Issue.generate!(:start_date => Date.today, :parent_issue_id => parent.id) parent.generate_child!(:start_date => Date.today)
Issue.generate!(:start_date => 2.days.from_now, :parent_issue_id => parent.id) parent.generate_child!(:start_date => 2.days.from_now)
assert_difference 'Issue.count', -3 do assert_difference 'Issue.count', -3 do
Issue.find(parent.id).destroy Issue.find(parent.id).destroy
@ -250,9 +250,9 @@ class IssueNestedSetTest < ActiveSupport::TestCase
end end
def test_destroy_child_issue_with_children def test_destroy_child_issue_with_children
root = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'root') root = Issue.generate!
child = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => root.id) child = root.generate_child!
leaf = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'leaf', :parent_issue_id => child.id) leaf = child.generate_child!
leaf.init_journal(User.find(2)) leaf.init_journal(User.find(2))
leaf.subject = 'leaf with journal' leaf.subject = 'leaf with journal'
leaf.save! leaf.save!
@ -272,10 +272,10 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_destroy_issue_with_grand_child def test_destroy_issue_with_grand_child
lft1 = new_issue_lft lft1 = new_issue_lft
parent = Issue.generate! parent = Issue.generate!
issue = Issue.generate!(:parent_issue_id => parent.id) issue = parent.generate_child!
child = Issue.generate!(:parent_issue_id => issue.id) child = issue.generate_child!
grandchild1 = Issue.generate!(:parent_issue_id => child.id) grandchild1 = child.generate_child!
grandchild2 = Issue.generate!(:parent_issue_id => child.id) grandchild2 = child.generate_child!
assert_difference 'Issue.count', -4 do assert_difference 'Issue.count', -4 do
Issue.find(issue.id).destroy Issue.find(issue.id).destroy
parent.reload parent.reload
@ -286,12 +286,12 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_parent_priority_should_be_the_highest_child_priority def test_parent_priority_should_be_the_highest_child_priority
parent = Issue.generate!(:priority => IssuePriority.find_by_name('Normal')) parent = Issue.generate!(:priority => IssuePriority.find_by_name('Normal'))
# Create children # Create children
child1 = Issue.generate!(:priority => IssuePriority.find_by_name('High'), :parent_issue_id => parent.id) child1 = parent.generate_child!(:priority => IssuePriority.find_by_name('High'))
assert_equal 'High', parent.reload.priority.name assert_equal 'High', parent.reload.priority.name
child2 = Issue.generate!(:priority => IssuePriority.find_by_name('Immediate'), :parent_issue_id => child1.id) child2 = child1.generate_child!(:priority => IssuePriority.find_by_name('Immediate'))
assert_equal 'Immediate', child1.reload.priority.name assert_equal 'Immediate', child1.reload.priority.name
assert_equal 'Immediate', parent.reload.priority.name assert_equal 'Immediate', parent.reload.priority.name
child3 = Issue.generate!(:priority => IssuePriority.find_by_name('Low'), :parent_issue_id => parent.id) child3 = parent.generate_child!(:priority => IssuePriority.find_by_name('Low'))
assert_equal 'Immediate', parent.reload.priority.name assert_equal 'Immediate', parent.reload.priority.name
# Destroy a child # Destroy a child
child1.destroy child1.destroy
@ -304,9 +304,9 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_parent_dates_should_be_lowest_start_and_highest_due_dates def test_parent_dates_should_be_lowest_start_and_highest_due_dates
parent = Issue.generate! parent = Issue.generate!
Issue.generate!(:start_date => '2010-01-25', :due_date => '2010-02-15', :parent_issue_id => parent.id) parent.generate_child!(:start_date => '2010-01-25', :due_date => '2010-02-15')
Issue.generate!( :due_date => '2010-02-13', :parent_issue_id => parent.id) parent.generate_child!( :due_date => '2010-02-13')
Issue.generate!(:start_date => '2010-02-01', :due_date => '2010-02-22', :parent_issue_id => parent.id) parent.generate_child!(:start_date => '2010-02-01', :due_date => '2010-02-22')
parent.reload parent.reload
assert_equal Date.parse('2010-01-25'), parent.start_date assert_equal Date.parse('2010-01-25'), parent.start_date
assert_equal Date.parse('2010-02-22'), parent.due_date assert_equal Date.parse('2010-02-22'), parent.due_date
@ -314,76 +314,72 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_parent_done_ratio_should_be_average_done_ratio_of_leaves def test_parent_done_ratio_should_be_average_done_ratio_of_leaves
parent = Issue.generate! parent = Issue.generate!
Issue.generate!(:done_ratio => 20, :parent_issue_id => parent.id) parent.generate_child!(:done_ratio => 20)
assert_equal 20, parent.reload.done_ratio assert_equal 20, parent.reload.done_ratio
Issue.generate!(:done_ratio => 70, :parent_issue_id => parent.id) parent.generate_child!(:done_ratio => 70)
assert_equal 45, parent.reload.done_ratio assert_equal 45, parent.reload.done_ratio
child = Issue.generate!(:done_ratio => 0, :parent_issue_id => parent.id) child = parent.generate_child!(:done_ratio => 0)
assert_equal 30, parent.reload.done_ratio assert_equal 30, parent.reload.done_ratio
Issue.generate!(:done_ratio => 30, :parent_issue_id => child.id) child.generate_child!(:done_ratio => 30)
assert_equal 30, child.reload.done_ratio assert_equal 30, child.reload.done_ratio
assert_equal 40, parent.reload.done_ratio assert_equal 40, parent.reload.done_ratio
end end
def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any
parent = Issue.generate! parent = Issue.generate!
Issue.generate!(:estimated_hours => 10, :done_ratio => 20, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 10, :done_ratio => 20)
assert_equal 20, parent.reload.done_ratio assert_equal 20, parent.reload.done_ratio
Issue.generate!(:estimated_hours => 20, :done_ratio => 50, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 20, :done_ratio => 50)
assert_equal (50 * 20 + 20 * 10) / 30, parent.reload.done_ratio assert_equal (50 * 20 + 20 * 10) / 30, parent.reload.done_ratio
end end
def test_parent_done_ratio_with_child_estimate_to_0_should_reach_100 def test_parent_done_ratio_with_child_estimate_to_0_should_reach_100
parent = Issue.generate! parent = Issue.generate!
issue1 = Issue.generate!(:parent_issue_id => parent.id) issue1 = parent.generate_child!
issue2 = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 0) issue2 = parent.generate_child!(:estimated_hours => 0)
assert_equal 0, parent.reload.done_ratio assert_equal 0, parent.reload.done_ratio
issue1.reload.update_attribute :status_id, 5 issue1.reload.close!
assert_equal 50, parent.reload.done_ratio assert_equal 50, parent.reload.done_ratio
issue2.reload.update_attribute :status_id, 5 issue2.reload.close!
assert_equal 100, parent.reload.done_ratio assert_equal 100, parent.reload.done_ratio
end end
def test_parent_estimate_should_be_sum_of_leaves def test_parent_estimate_should_be_sum_of_leaves
parent = Issue.generate! parent = Issue.generate!
Issue.generate!(:estimated_hours => nil, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => nil)
assert_equal nil, parent.reload.estimated_hours assert_equal nil, parent.reload.estimated_hours
Issue.generate!(:estimated_hours => 5, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 5)
assert_equal 5, parent.reload.estimated_hours assert_equal 5, parent.reload.estimated_hours
Issue.generate!(:estimated_hours => 7, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 7)
assert_equal 12, parent.reload.estimated_hours assert_equal 12, parent.reload.estimated_hours
end end
def test_done_ratio_of_parent_with_a_child_without_estimated_time_should_not_exceed_100 def test_done_ratio_of_parent_with_a_child_without_estimated_time_should_not_exceed_100
parent = Issue.generate! parent = Issue.generate!
Issue.generate!(:estimated_hours => 40, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 40)
Issue.generate!(:estimated_hours => 40, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 40)
Issue.generate!(:estimated_hours => 20, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 20)
Issue.generate!(:parent_issue_id => parent.id) parent.generate_child!
parent.reload.children.each do |child| parent.reload.children.each(&:close!)
child.update_attribute :status_id, 5
end
assert_equal 100, parent.reload.done_ratio assert_equal 100, parent.reload.done_ratio
end end
def test_done_ratio_of_parent_with_a_child_with_estimated_time_at_0_should_not_exceed_100 def test_done_ratio_of_parent_with_a_child_with_estimated_time_at_0_should_not_exceed_100
parent = Issue.generate! parent = Issue.generate!
Issue.generate!(:estimated_hours => 40, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 40)
Issue.generate!(:estimated_hours => 40, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 40)
Issue.generate!(:estimated_hours => 20, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 20)
Issue.generate!(:estimated_hours => 0, :parent_issue_id => parent.id) parent.generate_child!(:estimated_hours => 0)
parent.reload.children.each do |child| parent.reload.children.each(&:close!)
child.update_attribute :status_id, 5
end
assert_equal 100, parent.reload.done_ratio assert_equal 100, parent.reload.done_ratio
end end
def test_move_parent_updates_old_parent_attributes def test_move_parent_updates_old_parent_attributes
first_parent = Issue.generate! first_parent = Issue.generate!
second_parent = Issue.generate! second_parent = Issue.generate!
child = Issue.generate!(:estimated_hours => 5, :parent_issue_id => first_parent.id) child = first_parent.generate_child!(:estimated_hours => 5)
assert_equal 5, first_parent.reload.estimated_hours assert_equal 5, first_parent.reload.estimated_hours
child.update_attributes(:estimated_hours => 7, :parent_issue_id => second_parent.id) child.update_attributes(:estimated_hours => 7, :parent_issue_id => second_parent.id)
assert_equal 7, second_parent.reload.estimated_hours assert_equal 7, second_parent.reload.estimated_hours
@ -392,8 +388,8 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_reschuling_a_parent_should_reschedule_subtasks def test_reschuling_a_parent_should_reschedule_subtasks
parent = Issue.generate! parent = Issue.generate!
c1 = Issue.generate!(:start_date => '2010-05-12', :due_date => '2010-05-18', :parent_issue_id => parent.id) c1 = parent.generate_child!(:start_date => '2010-05-12', :due_date => '2010-05-18')
c2 = Issue.generate!(:start_date => '2010-06-03', :due_date => '2010-06-10', :parent_issue_id => parent.id) c2 = parent.generate_child!(:start_date => '2010-06-03', :due_date => '2010-06-10')
parent.reload parent.reload
parent.reschedule_on!(Date.parse('2010-06-02')) parent.reschedule_on!(Date.parse('2010-06-02'))
c1.reload c1.reload
@ -407,9 +403,9 @@ class IssueNestedSetTest < ActiveSupport::TestCase
def test_project_copy_should_copy_issue_tree def test_project_copy_should_copy_issue_tree
p = Project.create!(:name => 'Tree copy', :identifier => 'tree-copy', :tracker_ids => [1, 2]) p = Project.create!(:name => 'Tree copy', :identifier => 'tree-copy', :tracker_ids => [1, 2])
i1 = Issue.generate!(:project => p, :subject => 'i1') i1 = Issue.generate!(:project => p, :subject => 'i1')
i2 = Issue.generate!(:project => p, :subject => 'i2', :parent_issue_id => i1.id) i2 = i1.generate_child!(:project => p, :subject => 'i2')
i3 = Issue.generate!(:project => p, :subject => 'i3', :parent_issue_id => i1.id) i3 = i1.generate_child!(:project => p, :subject => 'i3')
i4 = Issue.generate!(:project => p, :subject => 'i4', :parent_issue_id => i2.id) i4 = i2.generate_child!(:project => p, :subject => 'i4')
i5 = Issue.generate!(:project => p, :subject => 'i5') i5 = Issue.generate!(:project => p, :subject => 'i5')
c = Project.new(:name => 'Copy', :identifier => 'copy', :tracker_ids => [1, 2]) c = Project.new(:name => 'Copy', :identifier => 'copy', :tracker_ids => [1, 2])
c.copy(p, :only => 'issues') c.copy(p, :only => 'issues')