require File.expand_path('../../test_helper', __FILE__)

class IssueSubtaskingTest < ActiveSupport::TestCase
  fixtures :projects, :users, :roles, :members, :member_roles,
           :trackers, :projects_trackers,
           :issue_statuses, :issue_categories, :enumerations,
           :issues,
           :enabled_modules,
           :workflows

  def test_parent_done_ratio_with_estimated_hours_and_multiple_levels_of_children
    with_settings :parent_issue_done_ratio => 'derived' do
      #
      # [1_0_0]             - (3h)
      #  | |
      #  | +- [1_1_0]       - 1h
      #  |
      #  +--- [1_2_0]       - (2h)
      #        | |
      #        | +- [1_2_1] - 1h
      #        |
      #        +--- [1_2_1] - 1h
      #
      # If 1_1_0 is done (i.e. Status == closed || done_ratio == 100), then 1h
      # out of 3h of work on the sub tasks are completed. So 1_0_0's done_ratio
      # should be (1 / 3) == 33 %.

      issue_1_0_0 = Issue.generate!
      issue_1_1_0 = issue_1_0_0.generate_child!(:estimated_hours => 1)
      issue_1_2_0 = issue_1_0_0.generate_child!
      issue_1_2_1 = issue_1_2_0.generate_child!(:estimated_hours => 1)
      issue_1_2_2 = issue_1_2_0.generate_child!(:estimated_hours => 1)

      issue_1_1_0.update(:done_ratio => 100)

      issue_1_0_0.reload

      assert_equal 33, issue_1_0_0.done_ratio
    end
  end

  def test_parent_done_ratio_with_estimated_hours_and_multiple_levels_of_children_part_two
    with_settings :parent_issue_done_ratio => 'derived' do
      #
      # [1_0_0]             - 1h (Total 5h)
      #  | |
      #  | +- [1_1_0]       - 1h (Total 1h)
      #  |
      #  +--- [1_2_0]       - 1h (Total 3h)
      #        | |
      #        | +- [1_2_1] - 1h (Total 1h)
      #        |
      #        +--- [1_2_1] - 1h (Total 1h)
      #
      # If 1_1_0 is done (i.e. Status == closed || done_ratio == 100), then 1h
      # out of 4h of work on the sub tasks are completed. So 1_0_0's done_ratio
      # should be (1 / 4) == 25 %.

      issue_1_0_0 = Issue.generate!(:estimated_hours => 1)
      issue_1_1_0 = issue_1_0_0.generate_child!(:estimated_hours => 1)
      issue_1_2_0 = issue_1_0_0.generate_child!(:estimated_hours => 1)
      issue_1_2_1 = issue_1_2_0.generate_child!(:estimated_hours => 1)
      issue_1_2_2 = issue_1_2_0.generate_child!(:estimated_hours => 1)

      issue_1_1_0.update(:done_ratio => 100)

      issue_1_0_0.reload

      assert_equal 25, issue_1_0_0.done_ratio
    end
  end
end
