annotate test/unit/version_test.rb @ 1516:b450a9d58aed redmine-2.4

Update to Redmine SVN revision 13356 on 2.4-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:28:31 +0100
parents e248c7af89ec
children c86dacc2ef0a
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@119 18 require File.expand_path('../../test_helper', __FILE__)
Chris@0 19
Chris@0 20 class VersionTest < ActiveSupport::TestCase
Chris@1464 21 fixtures :projects, :users, :issues, :issue_statuses, :trackers,
Chris@1464 22 :enumerations, :versions, :projects_trackers
Chris@909 23
Chris@0 24 def test_create
Chris@1464 25 v = Version.new(:project => Project.find(1), :name => '1.1',
Chris@1464 26 :effective_date => '2011-03-25')
Chris@0 27 assert v.save
Chris@0 28 assert_equal 'open', v.status
Chris@909 29 assert_equal 'none', v.sharing
Chris@0 30 end
Chris@909 31
Chris@0 32 def test_invalid_effective_date_validation
Chris@1464 33 v = Version.new(:project => Project.find(1), :name => '1.1',
Chris@1464 34 :effective_date => '99999-01-01')
Chris@1115 35 assert !v.valid?
Chris@1115 36 v.effective_date = '2012-11-33'
Chris@1115 37 assert !v.valid?
Chris@1115 38 v.effective_date = '2012-31-11'
Chris@1115 39 assert !v.valid?
Chris@1464 40 v.effective_date = '-2012-31-11'
Chris@1464 41 assert !v.valid?
Chris@1115 42 v.effective_date = 'ABC'
Chris@1115 43 assert !v.valid?
Chris@1115 44 assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
Chris@1115 45 v.errors[:effective_date]
Chris@0 46 end
Chris@909 47
Chris@0 48 def test_progress_should_be_0_with_no_assigned_issues
Chris@0 49 project = Project.find(1)
Chris@0 50 v = Version.create!(:project => project, :name => 'Progress')
Chris@1464 51 assert_equal 0, v.completed_percent
Chris@1464 52 assert_equal 0, v.closed_percent
Chris@0 53 end
Chris@909 54
Chris@0 55 def test_progress_should_be_0_with_unbegun_assigned_issues
Chris@0 56 project = Project.find(1)
Chris@0 57 v = Version.create!(:project => project, :name => 'Progress')
Chris@0 58 add_issue(v)
Chris@0 59 add_issue(v, :done_ratio => 0)
Chris@1464 60 assert_progress_equal 0, v.completed_percent
Chris@1464 61 assert_progress_equal 0, v.closed_percent
Chris@0 62 end
Chris@909 63
Chris@0 64 def test_progress_should_be_100_with_closed_assigned_issues
Chris@0 65 project = Project.find(1)
Chris@1464 66 status = IssueStatus.where(:is_closed => true).first
Chris@0 67 v = Version.create!(:project => project, :name => 'Progress')
Chris@0 68 add_issue(v, :status => status)
Chris@0 69 add_issue(v, :status => status, :done_ratio => 20)
Chris@0 70 add_issue(v, :status => status, :done_ratio => 70, :estimated_hours => 25)
Chris@0 71 add_issue(v, :status => status, :estimated_hours => 15)
Chris@1464 72 assert_progress_equal 100.0, v.completed_percent
Chris@1464 73 assert_progress_equal 100.0, v.closed_percent
Chris@0 74 end
Chris@909 75
Chris@0 76 def test_progress_should_consider_done_ratio_of_open_assigned_issues
Chris@0 77 project = Project.find(1)
Chris@0 78 v = Version.create!(:project => project, :name => 'Progress')
Chris@0 79 add_issue(v)
Chris@0 80 add_issue(v, :done_ratio => 20)
Chris@0 81 add_issue(v, :done_ratio => 70)
Chris@1464 82 assert_progress_equal (0.0 + 20.0 + 70.0)/3, v.completed_percent
Chris@1464 83 assert_progress_equal 0, v.closed_percent
Chris@0 84 end
Chris@909 85
Chris@0 86 def test_progress_should_consider_closed_issues_as_completed
Chris@0 87 project = Project.find(1)
Chris@0 88 v = Version.create!(:project => project, :name => 'Progress')
Chris@0 89 add_issue(v)
Chris@0 90 add_issue(v, :done_ratio => 20)
Chris@1464 91 add_issue(v, :status => IssueStatus.where(:is_closed => true).first)
Chris@1464 92 assert_progress_equal (0.0 + 20.0 + 100.0)/3, v.completed_percent
Chris@1464 93 assert_progress_equal (100.0)/3, v.closed_percent
Chris@0 94 end
Chris@909 95
Chris@0 96 def test_progress_should_consider_estimated_hours_to_weigth_issues
Chris@0 97 project = Project.find(1)
Chris@0 98 v = Version.create!(:project => project, :name => 'Progress')
Chris@0 99 add_issue(v, :estimated_hours => 10)
Chris@0 100 add_issue(v, :estimated_hours => 20, :done_ratio => 30)
Chris@0 101 add_issue(v, :estimated_hours => 40, :done_ratio => 10)
Chris@1464 102 add_issue(v, :estimated_hours => 25, :status => IssueStatus.where(:is_closed => true).first)
Chris@1464 103 assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_percent
Chris@1464 104 assert_progress_equal 25.0/95.0*100, v.closed_percent
Chris@0 105 end
Chris@909 106
Chris@0 107 def test_progress_should_consider_average_estimated_hours_to_weigth_unestimated_issues
Chris@0 108 project = Project.find(1)
Chris@0 109 v = Version.create!(:project => project, :name => 'Progress')
Chris@0 110 add_issue(v, :done_ratio => 20)
Chris@1464 111 add_issue(v, :status => IssueStatus.where(:is_closed => true).first)
Chris@0 112 add_issue(v, :estimated_hours => 10, :done_ratio => 30)
Chris@0 113 add_issue(v, :estimated_hours => 40, :done_ratio => 10)
Chris@1464 114 assert_progress_equal (25.0*0.2 + 25.0*1 + 10.0*0.3 + 40.0*0.1)/100.0*100, v.completed_percent
Chris@1464 115 assert_progress_equal 25.0/100.0*100, v.closed_percent
Chris@0 116 end
chris@22 117
Chris@1115 118 def test_should_sort_scheduled_then_unscheduled_versions
Chris@1115 119 Version.delete_all
Chris@1115 120 v4 = Version.create!(:project_id => 1, :name => 'v4')
Chris@1115 121 v3 = Version.create!(:project_id => 1, :name => 'v2', :effective_date => '2012-07-14')
Chris@1115 122 v2 = Version.create!(:project_id => 1, :name => 'v1')
Chris@1115 123 v1 = Version.create!(:project_id => 1, :name => 'v3', :effective_date => '2012-08-02')
Chris@1115 124 v5 = Version.create!(:project_id => 1, :name => 'v5', :effective_date => '2012-07-02')
Chris@1115 125
Chris@1115 126 assert_equal [v5, v3, v1, v2, v4], [v1, v2, v3, v4, v5].sort
Chris@1115 127 assert_equal [v5, v3, v1, v2, v4], Version.sorted.all
Chris@1115 128 end
Chris@1115 129
Chris@1115 130 def test_completed_should_be_false_when_due_today
Chris@1115 131 version = Version.create!(:project_id => 1, :effective_date => Date.today, :name => 'Due today')
Chris@1115 132 assert_equal false, version.completed?
Chris@1115 133 end
Chris@1115 134
Chris@1464 135 test "#behind_schedule? should be false if there are no issues assigned" do
Chris@1464 136 version = Version.generate!(:effective_date => Date.yesterday)
Chris@1464 137 assert_equal false, version.behind_schedule?
chris@22 138 end
chris@22 139
Chris@1464 140 test "#behind_schedule? should be false if there is no effective_date" do
Chris@1464 141 version = Version.generate!(:effective_date => nil)
Chris@1464 142 assert_equal false, version.behind_schedule?
Chris@1464 143 end
Chris@909 144
Chris@1464 145 test "#behind_schedule? should be false if all of the issues are ahead of schedule" do
Chris@1464 146 version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
Chris@1464 147 add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
Chris@1464 148 add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
Chris@1464 149 assert_equal 60, version.completed_percent
Chris@1464 150 assert_equal false, version.behind_schedule?
Chris@1464 151 end
Chris@909 152
Chris@1464 153 test "#behind_schedule? should be true if any of the issues are behind schedule" do
Chris@1464 154 version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
Chris@1464 155 add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
Chris@1464 156 add_issue(version, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left
Chris@1464 157 assert_equal 40, version.completed_percent
Chris@1464 158 assert_equal true, version.behind_schedule?
Chris@1464 159 end
Chris@909 160
Chris@1464 161 test "#behind_schedule? should be false if all of the issues are complete" do
Chris@1464 162 version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
Chris@1464 163 add_issue(version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
Chris@1464 164 add_issue(version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
Chris@1464 165 assert_equal 100, version.completed_percent
Chris@1464 166 assert_equal false, version.behind_schedule?
Chris@1464 167 end
Chris@909 168
Chris@1464 169 test "#estimated_hours should return 0 with no assigned issues" do
Chris@1464 170 version = Version.generate!
Chris@1464 171 assert_equal 0, version.estimated_hours
Chris@1464 172 end
Chris@1464 173
Chris@1464 174 test "#estimated_hours should return 0 with no estimated hours" do
Chris@1464 175 version = Version.create!(:project_id => 1, :name => 'test')
Chris@1464 176 add_issue(version)
Chris@1464 177 assert_equal 0, version.estimated_hours
Chris@1464 178 end
Chris@1464 179
Chris@1464 180 test "#estimated_hours should return return the sum of estimated hours" do
Chris@1464 181 version = Version.create!(:project_id => 1, :name => 'test')
Chris@1464 182 add_issue(version, :estimated_hours => 2.5)
Chris@1464 183 add_issue(version, :estimated_hours => 5)
Chris@1464 184 assert_equal 7.5, version.estimated_hours
Chris@1464 185 end
Chris@1464 186
Chris@1464 187 test "#estimated_hours should return the sum of leaves estimated hours" do
Chris@1464 188 version = Version.create!(:project_id => 1, :name => 'test')
Chris@1464 189 parent = add_issue(version)
Chris@1464 190 add_issue(version, :estimated_hours => 2.5, :parent_issue_id => parent.id)
Chris@1464 191 add_issue(version, :estimated_hours => 5, :parent_issue_id => parent.id)
Chris@1464 192 assert_equal 7.5, version.estimated_hours
Chris@0 193 end
Chris@0 194
Chris@0 195 test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do
Chris@0 196 User.current = User.find(1) # Need the admin's permissions
Chris@909 197
Chris@0 198 @version = Version.find(7)
Chris@0 199 # Separate hierarchy
Chris@0 200 project_1_issue = Issue.find(1)
Chris@0 201 project_1_issue.fixed_version = @version
Chris@119 202 assert project_1_issue.save, project_1_issue.errors.full_messages.to_s
Chris@909 203
Chris@0 204 project_5_issue = Issue.find(6)
Chris@0 205 project_5_issue.fixed_version = @version
Chris@0 206 assert project_5_issue.save
Chris@909 207
Chris@0 208 # Project
Chris@0 209 project_2_issue = Issue.find(4)
Chris@0 210 project_2_issue.fixed_version = @version
Chris@0 211 assert project_2_issue.save
Chris@0 212
Chris@0 213 # Update the sharing
Chris@0 214 @version.sharing = 'none'
Chris@0 215 assert @version.save
Chris@0 216
Chris@0 217 # Project 1 now out of the shared scope
Chris@0 218 project_1_issue.reload
Chris@1464 219 assert_equal nil, project_1_issue.fixed_version,
Chris@1464 220 "Fixed version is still set after changing the Version's sharing"
Chris@909 221
Chris@0 222 # Project 5 now out of the shared scope
Chris@0 223 project_5_issue.reload
Chris@1464 224 assert_equal nil, project_5_issue.fixed_version,
Chris@1464 225 "Fixed version is still set after changing the Version's sharing"
Chris@0 226
Chris@0 227 # Project 2 issue remains
Chris@0 228 project_2_issue.reload
Chris@0 229 assert_equal @version, project_2_issue.fixed_version
Chris@0 230 end
Chris@909 231
Chris@0 232 private
Chris@909 233
Chris@0 234 def add_issue(version, attributes={})
Chris@0 235 Issue.create!({:project => version.project,
Chris@0 236 :fixed_version => version,
Chris@0 237 :subject => 'Test',
Chris@1464 238 :author => User.first,
Chris@1464 239 :tracker => version.project.trackers.first}.merge(attributes))
Chris@0 240 end
Chris@909 241
Chris@0 242 def assert_progress_equal(expected_float, actual_float, message="")
Chris@0 243 assert_in_delta(expected_float, actual_float, 0.000001, message="")
Chris@0 244 end
Chris@0 245 end