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