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