##// END OF EJS Templates
Rails3: test: replace deprecated Errors#on to Errors#[] and join with to_s at test/unit/version_test.rb...
Toshi MARUYAMA -
r8012:c8556330c15b
parent child
Show More
@@ -1,235 +1,236
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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
21 fixtures :projects, :users, :issues, :issue_statuses, :trackers, :enumerations, :versions
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.save
35 assert !v.save
36 assert_equal I18n.translate('activerecord.errors.messages.not_a_date'), v.errors.on(:effective_date)
36 assert_equal I18n.translate('activerecord.errors.messages.not_a_date'),
37 v.errors[:effective_date].to_s
37 end
38 end
38
39
39 def test_progress_should_be_0_with_no_assigned_issues
40 def test_progress_should_be_0_with_no_assigned_issues
40 project = Project.find(1)
41 project = Project.find(1)
41 v = Version.create!(:project => project, :name => 'Progress')
42 v = Version.create!(:project => project, :name => 'Progress')
42 assert_equal 0, v.completed_pourcent
43 assert_equal 0, v.completed_pourcent
43 assert_equal 0, v.closed_pourcent
44 assert_equal 0, v.closed_pourcent
44 end
45 end
45
46
46 def test_progress_should_be_0_with_unbegun_assigned_issues
47 def test_progress_should_be_0_with_unbegun_assigned_issues
47 project = Project.find(1)
48 project = Project.find(1)
48 v = Version.create!(:project => project, :name => 'Progress')
49 v = Version.create!(:project => project, :name => 'Progress')
49 add_issue(v)
50 add_issue(v)
50 add_issue(v, :done_ratio => 0)
51 add_issue(v, :done_ratio => 0)
51 assert_progress_equal 0, v.completed_pourcent
52 assert_progress_equal 0, v.completed_pourcent
52 assert_progress_equal 0, v.closed_pourcent
53 assert_progress_equal 0, v.closed_pourcent
53 end
54 end
54
55
55 def test_progress_should_be_100_with_closed_assigned_issues
56 def test_progress_should_be_100_with_closed_assigned_issues
56 project = Project.find(1)
57 project = Project.find(1)
57 status = IssueStatus.find(:first, :conditions => {:is_closed => true})
58 status = IssueStatus.find(:first, :conditions => {:is_closed => true})
58 v = Version.create!(:project => project, :name => 'Progress')
59 v = Version.create!(:project => project, :name => 'Progress')
59 add_issue(v, :status => status)
60 add_issue(v, :status => status)
60 add_issue(v, :status => status, :done_ratio => 20)
61 add_issue(v, :status => status, :done_ratio => 20)
61 add_issue(v, :status => status, :done_ratio => 70, :estimated_hours => 25)
62 add_issue(v, :status => status, :done_ratio => 70, :estimated_hours => 25)
62 add_issue(v, :status => status, :estimated_hours => 15)
63 add_issue(v, :status => status, :estimated_hours => 15)
63 assert_progress_equal 100.0, v.completed_pourcent
64 assert_progress_equal 100.0, v.completed_pourcent
64 assert_progress_equal 100.0, v.closed_pourcent
65 assert_progress_equal 100.0, v.closed_pourcent
65 end
66 end
66
67
67 def test_progress_should_consider_done_ratio_of_open_assigned_issues
68 def test_progress_should_consider_done_ratio_of_open_assigned_issues
68 project = Project.find(1)
69 project = Project.find(1)
69 v = Version.create!(:project => project, :name => 'Progress')
70 v = Version.create!(:project => project, :name => 'Progress')
70 add_issue(v)
71 add_issue(v)
71 add_issue(v, :done_ratio => 20)
72 add_issue(v, :done_ratio => 20)
72 add_issue(v, :done_ratio => 70)
73 add_issue(v, :done_ratio => 70)
73 assert_progress_equal (0.0 + 20.0 + 70.0)/3, v.completed_pourcent
74 assert_progress_equal (0.0 + 20.0 + 70.0)/3, v.completed_pourcent
74 assert_progress_equal 0, v.closed_pourcent
75 assert_progress_equal 0, v.closed_pourcent
75 end
76 end
76
77
77 def test_progress_should_consider_closed_issues_as_completed
78 def test_progress_should_consider_closed_issues_as_completed
78 project = Project.find(1)
79 project = Project.find(1)
79 v = Version.create!(:project => project, :name => 'Progress')
80 v = Version.create!(:project => project, :name => 'Progress')
80 add_issue(v)
81 add_issue(v)
81 add_issue(v, :done_ratio => 20)
82 add_issue(v, :done_ratio => 20)
82 add_issue(v, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
83 add_issue(v, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
83 assert_progress_equal (0.0 + 20.0 + 100.0)/3, v.completed_pourcent
84 assert_progress_equal (0.0 + 20.0 + 100.0)/3, v.completed_pourcent
84 assert_progress_equal (100.0)/3, v.closed_pourcent
85 assert_progress_equal (100.0)/3, v.closed_pourcent
85 end
86 end
86
87
87 def test_progress_should_consider_estimated_hours_to_weigth_issues
88 def test_progress_should_consider_estimated_hours_to_weigth_issues
88 project = Project.find(1)
89 project = Project.find(1)
89 v = Version.create!(:project => project, :name => 'Progress')
90 v = Version.create!(:project => project, :name => 'Progress')
90 add_issue(v, :estimated_hours => 10)
91 add_issue(v, :estimated_hours => 10)
91 add_issue(v, :estimated_hours => 20, :done_ratio => 30)
92 add_issue(v, :estimated_hours => 20, :done_ratio => 30)
92 add_issue(v, :estimated_hours => 40, :done_ratio => 10)
93 add_issue(v, :estimated_hours => 40, :done_ratio => 10)
93 add_issue(v, :estimated_hours => 25, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
94 add_issue(v, :estimated_hours => 25, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
94 assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_pourcent
95 assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_pourcent
95 assert_progress_equal 25.0/95.0*100, v.closed_pourcent
96 assert_progress_equal 25.0/95.0*100, v.closed_pourcent
96 end
97 end
97
98
98 def test_progress_should_consider_average_estimated_hours_to_weigth_unestimated_issues
99 def test_progress_should_consider_average_estimated_hours_to_weigth_unestimated_issues
99 project = Project.find(1)
100 project = Project.find(1)
100 v = Version.create!(:project => project, :name => 'Progress')
101 v = Version.create!(:project => project, :name => 'Progress')
101 add_issue(v, :done_ratio => 20)
102 add_issue(v, :done_ratio => 20)
102 add_issue(v, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
103 add_issue(v, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
103 add_issue(v, :estimated_hours => 10, :done_ratio => 30)
104 add_issue(v, :estimated_hours => 10, :done_ratio => 30)
104 add_issue(v, :estimated_hours => 40, :done_ratio => 10)
105 add_issue(v, :estimated_hours => 40, :done_ratio => 10)
105 assert_progress_equal (25.0*0.2 + 25.0*1 + 10.0*0.3 + 40.0*0.1)/100.0*100, v.completed_pourcent
106 assert_progress_equal (25.0*0.2 + 25.0*1 + 10.0*0.3 + 40.0*0.1)/100.0*100, v.completed_pourcent
106 assert_progress_equal 25.0/100.0*100, v.closed_pourcent
107 assert_progress_equal 25.0/100.0*100, v.closed_pourcent
107 end
108 end
108
109
109 context "#behind_schedule?" do
110 context "#behind_schedule?" do
110 setup do
111 setup do
111 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
112 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
112 @project = Project.generate!(:identifier => 'test0')
113 @project = Project.generate!(:identifier => 'test0')
113 @project.trackers << Tracker.generate!
114 @project.trackers << Tracker.generate!
114
115
115 @version = Version.generate!(:project => @project, :effective_date => nil)
116 @version = Version.generate!(:project => @project, :effective_date => nil)
116 end
117 end
117
118
118 should "be false if there are no issues assigned" do
119 should "be false if there are no issues assigned" do
119 @version.update_attribute(:effective_date, Date.yesterday)
120 @version.update_attribute(:effective_date, Date.yesterday)
120 assert_equal false, @version.behind_schedule?
121 assert_equal false, @version.behind_schedule?
121 end
122 end
122
123
123 should "be false if there is no effective_date" do
124 should "be false if there is no effective_date" do
124 assert_equal false, @version.behind_schedule?
125 assert_equal false, @version.behind_schedule?
125 end
126 end
126
127
127 should "be false if all of the issues are ahead of schedule" do
128 should "be false if all of the issues are ahead of schedule" do
128 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
129 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
129 @version.fixed_issues = [
130 @version.fixed_issues = [
130 Issue.generate_for_project!(@project, :start_date => 7.days.ago, :done_ratio => 60), # 14 day span, 60% done, 50% time left
131 Issue.generate_for_project!(@project, :start_date => 7.days.ago, :done_ratio => 60), # 14 day span, 60% done, 50% time left
131 Issue.generate_for_project!(@project, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
132 Issue.generate_for_project!(@project, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
132 ]
133 ]
133 assert_equal 60, @version.completed_pourcent
134 assert_equal 60, @version.completed_pourcent
134 assert_equal false, @version.behind_schedule?
135 assert_equal false, @version.behind_schedule?
135 end
136 end
136
137
137 should "be true if any of the issues are behind schedule" do
138 should "be true if any of the issues are behind schedule" do
138 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
139 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
139 @version.fixed_issues = [
140 @version.fixed_issues = [
140 Issue.generate_for_project!(@project, :start_date => 7.days.ago, :done_ratio => 60), # 14 day span, 60% done, 50% time left
141 Issue.generate_for_project!(@project, :start_date => 7.days.ago, :done_ratio => 60), # 14 day span, 60% done, 50% time left
141 Issue.generate_for_project!(@project, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left
142 Issue.generate_for_project!(@project, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left
142 ]
143 ]
143 assert_equal 40, @version.completed_pourcent
144 assert_equal 40, @version.completed_pourcent
144 assert_equal true, @version.behind_schedule?
145 assert_equal true, @version.behind_schedule?
145 end
146 end
146
147
147 should "be false if all of the issues are complete" do
148 should "be false if all of the issues are complete" do
148 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
149 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
149 @version.fixed_issues = [
150 @version.fixed_issues = [
150 Issue.generate_for_project!(@project, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)), # 7 day span
151 Issue.generate_for_project!(@project, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)), # 7 day span
151 Issue.generate_for_project!(@project, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
152 Issue.generate_for_project!(@project, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
152 ]
153 ]
153 assert_equal 100, @version.completed_pourcent
154 assert_equal 100, @version.completed_pourcent
154 assert_equal false, @version.behind_schedule?
155 assert_equal false, @version.behind_schedule?
155
156
156 end
157 end
157 end
158 end
158
159
159 context "#estimated_hours" do
160 context "#estimated_hours" do
160 setup do
161 setup do
161 @version = Version.create!(:project_id => 1, :name => '#estimated_hours')
162 @version = Version.create!(:project_id => 1, :name => '#estimated_hours')
162 end
163 end
163
164
164 should "return 0 with no assigned issues" do
165 should "return 0 with no assigned issues" do
165 assert_equal 0, @version.estimated_hours
166 assert_equal 0, @version.estimated_hours
166 end
167 end
167
168
168 should "return 0 with no estimated hours" do
169 should "return 0 with no estimated hours" do
169 add_issue(@version)
170 add_issue(@version)
170 assert_equal 0, @version.estimated_hours
171 assert_equal 0, @version.estimated_hours
171 end
172 end
172
173
173 should "return the sum of estimated hours" do
174 should "return the sum of estimated hours" do
174 add_issue(@version, :estimated_hours => 2.5)
175 add_issue(@version, :estimated_hours => 2.5)
175 add_issue(@version, :estimated_hours => 5)
176 add_issue(@version, :estimated_hours => 5)
176 assert_equal 7.5, @version.estimated_hours
177 assert_equal 7.5, @version.estimated_hours
177 end
178 end
178
179
179 should "return the sum of leaves estimated hours" do
180 should "return the sum of leaves estimated hours" do
180 parent = add_issue(@version)
181 parent = add_issue(@version)
181 add_issue(@version, :estimated_hours => 2.5, :parent_issue_id => parent.id)
182 add_issue(@version, :estimated_hours => 2.5, :parent_issue_id => parent.id)
182 add_issue(@version, :estimated_hours => 5, :parent_issue_id => parent.id)
183 add_issue(@version, :estimated_hours => 5, :parent_issue_id => parent.id)
183 assert_equal 7.5, @version.estimated_hours
184 assert_equal 7.5, @version.estimated_hours
184 end
185 end
185 end
186 end
186
187
187 test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do
188 test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do
188 User.current = User.find(1) # Need the admin's permissions
189 User.current = User.find(1) # Need the admin's permissions
189
190
190 @version = Version.find(7)
191 @version = Version.find(7)
191 # Separate hierarchy
192 # Separate hierarchy
192 project_1_issue = Issue.find(1)
193 project_1_issue = Issue.find(1)
193 project_1_issue.fixed_version = @version
194 project_1_issue.fixed_version = @version
194 assert project_1_issue.save, project_1_issue.errors.full_messages.to_s
195 assert project_1_issue.save, project_1_issue.errors.full_messages.to_s
195
196
196 project_5_issue = Issue.find(6)
197 project_5_issue = Issue.find(6)
197 project_5_issue.fixed_version = @version
198 project_5_issue.fixed_version = @version
198 assert project_5_issue.save
199 assert project_5_issue.save
199
200
200 # Project
201 # Project
201 project_2_issue = Issue.find(4)
202 project_2_issue = Issue.find(4)
202 project_2_issue.fixed_version = @version
203 project_2_issue.fixed_version = @version
203 assert project_2_issue.save
204 assert project_2_issue.save
204
205
205 # Update the sharing
206 # Update the sharing
206 @version.sharing = 'none'
207 @version.sharing = 'none'
207 assert @version.save
208 assert @version.save
208
209
209 # Project 1 now out of the shared scope
210 # Project 1 now out of the shared scope
210 project_1_issue.reload
211 project_1_issue.reload
211 assert_equal nil, project_1_issue.fixed_version, "Fixed version is still set after changing the Version's sharing"
212 assert_equal nil, project_1_issue.fixed_version, "Fixed version is still set after changing the Version's sharing"
212
213
213 # Project 5 now out of the shared scope
214 # Project 5 now out of the shared scope
214 project_5_issue.reload
215 project_5_issue.reload
215 assert_equal nil, project_5_issue.fixed_version, "Fixed version is still set after changing the Version's sharing"
216 assert_equal nil, project_5_issue.fixed_version, "Fixed version is still set after changing the Version's sharing"
216
217
217 # Project 2 issue remains
218 # Project 2 issue remains
218 project_2_issue.reload
219 project_2_issue.reload
219 assert_equal @version, project_2_issue.fixed_version
220 assert_equal @version, project_2_issue.fixed_version
220 end
221 end
221
222
222 private
223 private
223
224
224 def add_issue(version, attributes={})
225 def add_issue(version, attributes={})
225 Issue.create!({:project => version.project,
226 Issue.create!({:project => version.project,
226 :fixed_version => version,
227 :fixed_version => version,
227 :subject => 'Test',
228 :subject => 'Test',
228 :author => User.find(:first),
229 :author => User.find(:first),
229 :tracker => version.project.trackers.find(:first)}.merge(attributes))
230 :tracker => version.project.trackers.find(:first)}.merge(attributes))
230 end
231 end
231
232
232 def assert_progress_equal(expected_float, actual_float, message="")
233 def assert_progress_equal(expected_float, actual_float, message="")
233 assert_in_delta(expected_float, actual_float, 0.000001, message="")
234 assert_in_delta(expected_float, actual_float, 0.000001, message="")
234 end
235 end
235 end
236 end
General Comments 0
You need to be logged in to leave comments. Login now