Project

General

Profile

Defect #4682 » wrong_completed_percent_computation_with_0_time_issues.patch

v2 - Etienne Massip, 2011-04-16 18:04

View differences:

app/models/version.rb (working copy)
101 101
  def closed_pourcent
102 102
    if issues_count == 0
103 103
      0
104
    elsif open_issues_count == 0
105
      100
104 106
    else
105 107
      issues_progress(false)
106 108
    end
......
200 202
  # Used to weigth unestimated issues in progress calculation
201 203
  def estimated_average
202 204
    if @estimated_average.nil?
203
      average = fixed_issues.average(:estimated_hours).to_f
205
      average = fixed_issues.average(:estimated_hours,
206
                        :conditions => 'estimated_hours > 0').to_f
204 207
      if average == 0
205 208
        average = 1
206 209
      end
......
208 211
    end
209 212
    @estimated_average
210 213
  end
211
  
214

  
212 215
  # Returns the total progress of open or closed issues.  The returned percentage takes into account
213 216
  # the amount of estimated time set for this version.
214 217
  #
......
220 223
    @issues_progress[open] ||= begin
221 224
      progress = 0
222 225
      if issues_count > 0
226

  
223 227
        ratio = open ? 'done_ratio' : 100
224
        
225
        done = fixed_issues.sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}",
228

  
229
        if estimated_hours == 0
230
          todo = issues_count
231
          done = fixed_issues.sum("#{ratio}",
226 232
                                  :include => :status,
227
                                  :conditions => ["is_closed = ?", !open]).to_f
228
        progress = done / (estimated_average * issues_count)
233
                                  :conditions => ['is_closed = ?', !open]).to_f
234
        else
235
          todo = estimated_hours + estimated_average * fixed_issues.count(:conditions => { :estimated_hours => nil })
236
          done = fixed_issues.sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}",
237
                                    :include => :status,
238
                                    :conditions => ['is_closed = ?', !open]).to_f
239
        end
240
        progress = done / todo
229 241
      end
230 242
      progress
231 243
    end
test/unit/version_test.rb (working copy)
72 72
    assert_progress_equal (0.0 + 20.0 + 70.0)/3, v.completed_pourcent
73 73
    assert_progress_equal 0, v.closed_pourcent
74 74
  end
75
  
75

  
76
  def test_progress_should_consider_100pct_done_0h_issues_as_completed
77
    project = Project.find(1)
78
    closed = IssueStatus.find(:first, :conditions => {:is_closed => true})
79
    v = Version.create!(:project => project, :name => 'Progress')
80
    add_issue(v, :done_ratio => 100, :estimated_hours => 0)
81
    add_issue(v, :done_ratio => 100, :estimated_hours => 0, :status => closed)
82
    assert_progress_equal 100, v.completed_pourcent
83
    assert_progress_equal 50, v.closed_pourcent
84
  end
85

  
76 86
  def test_progress_should_consider_closed_issues_as_completed
77 87
    project = Project.find(1)
78 88
    v = Version.create!(:project => project, :name => 'Progress')
......
82 92
    assert_progress_equal (0.0 + 20.0 + 100.0)/3, v.completed_pourcent
83 93
    assert_progress_equal (100.0)/3, v.closed_pourcent
84 94
  end
85
  
95

  
86 96
  def test_progress_should_consider_estimated_hours_to_weigth_issues
87 97
    project = Project.find(1)
88 98
    v = Version.create!(:project => project, :name => 'Progress')
99
    add_issue(v, :estimated_hours => 0, :done_ratio => 100)
89 100
    add_issue(v, :estimated_hours => 10)
90 101
    add_issue(v, :estimated_hours => 20, :done_ratio => 30)
91 102
    add_issue(v, :estimated_hours => 40, :done_ratio => 10)
92 103
    add_issue(v, :estimated_hours => 25, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
93
    assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_pourcent
104
    assert_progress_equal (0*100.0 + 10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_pourcent
94 105
    assert_progress_equal 25.0/95.0*100, v.closed_pourcent
95 106
  end
96
  
107

  
97 108
  def test_progress_should_consider_average_estimated_hours_to_weigth_unestimated_issues
98 109
    project = Project.find(1)
99 110
    v = Version.create!(:project => project, :name => 'Progress')
......
229 240
  end
230 241
  
231 242
  def assert_progress_equal(expected_float, actual_float, message="")
232
    assert_in_delta(expected_float, actual_float, 0.000001, message="")
243
    assert_in_delta(expected_float, actual_float, 0.000001, message)
233 244
  end
234 245
end
(3-3/3)