annotate .svn/pristine/77/77dca881d9fa5f30825d9090833ebab69a043327.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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