##// END OF EJS Templates
Insert children issues to respect same order as ids....
Jean-Philippe Lang -
r13460:daef6a8037ab
parent child
Show More
@@ -1,195 +1,195
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module NestedSet
20 20 module IssueNestedSet
21 21 def self.included(base)
22 22 base.class_eval do
23 23 belongs_to :parent, :class_name => self.name
24 24
25 25 before_create :add_to_nested_set, :if => lambda {|issue| issue.parent.present?}
26 26 after_create :add_as_root, :if => lambda {|issue| issue.parent.blank?}
27 27 before_update :handle_parent_change, :if => lambda {|issue| issue.parent_id_changed?}
28 28 before_destroy :destroy_children
29 29 end
30 30 base.extend ClassMethods
31 31 base.send :include, Redmine::NestedSet::Traversing
32 32 end
33 33
34 34 private
35 35
36 36 def target_lft
37 37 scope_for_max_rgt = self.class.where(:root_id => root_id).where(:parent_id => parent_id)
38 38 if id
39 #scope_for_max_rgt = scope_for_max_rgt.where("id < ?", id)
39 scope_for_max_rgt = scope_for_max_rgt.where("id < ?", id)
40 40 end
41 41 max_rgt = scope_for_max_rgt.maximum(:rgt)
42 42 if max_rgt
43 43 max_rgt + 1
44 44 elsif parent
45 45 parent.lft + 1
46 46 else
47 47 1
48 48 end
49 49 end
50 50
51 51 def add_to_nested_set(lock=true)
52 52 lock_nested_set if lock
53 53 parent.send :reload_nested_set_values
54 54 self.root_id = parent.root_id
55 55 self.lft = target_lft
56 56 self.rgt = lft + 1
57 57 self.class.where(:root_id => root_id).where("lft >= ? OR rgt >= ?", lft, lft).update_all([
58 58 "lft = CASE WHEN lft >= :lft THEN lft + 2 ELSE lft END, " +
59 59 "rgt = CASE WHEN rgt >= :lft THEN rgt + 2 ELSE rgt END",
60 60 {:lft => lft}
61 61 ])
62 62 end
63 63
64 64 def add_as_root
65 65 self.root_id = id
66 66 self.lft = 1
67 67 self.rgt = 2
68 68 self.class.where(:id => id).update_all(:root_id => root_id, :lft => lft, :rgt => rgt)
69 69 end
70 70
71 71 def handle_parent_change
72 72 lock_nested_set
73 73 reload_nested_set_values
74 74 if parent_id_was
75 75 remove_from_nested_set
76 76 end
77 77 if parent
78 78 move_to_nested_set
79 79 end
80 80 reload_nested_set_values
81 81 end
82 82
83 83 def move_to_nested_set
84 84 if parent
85 85 previous_root_id = root_id
86 86 self.root_id = parent.root_id
87 87
88 88 lft_after_move = target_lft
89 89 self.class.where(:root_id => parent.root_id).update_all([
90 90 "lft = CASE WHEN lft >= :lft THEN lft + :shift ELSE lft END, " +
91 91 "rgt = CASE WHEN rgt >= :lft THEN rgt + :shift ELSE rgt END",
92 92 {:lft => lft_after_move, :shift => (rgt - lft + 1)}
93 93 ])
94 94
95 95 self.class.where(:root_id => previous_root_id).update_all([
96 96 "root_id = :root_id, lft = lft + :shift, rgt = rgt + :shift",
97 97 {:root_id => parent.root_id, :shift => lft_after_move - lft}
98 98 ])
99 99
100 100 self.lft, self.rgt = lft_after_move, (rgt - lft + lft_after_move)
101 101 parent.send :reload_nested_set_values
102 102 end
103 103 end
104 104
105 105 def remove_from_nested_set
106 106 self.class.where(:root_id => root_id).where("lft >= ? AND rgt <= ?", lft, rgt).
107 107 update_all(["root_id = :id, lft = lft - :shift, rgt = rgt - :shift", {:id => id, :shift => lft - 1}])
108 108
109 109 self.class.where(:root_id => root_id).update_all([
110 110 "lft = CASE WHEN lft >= :lft THEN lft - :shift ELSE lft END, " +
111 111 "rgt = CASE WHEN rgt >= :lft THEN rgt - :shift ELSE rgt END",
112 112 {:lft => lft, :shift => rgt - lft + 1}
113 113 ])
114 114 self.root_id = id
115 115 self.lft, self.rgt = 1, (rgt - lft + 1)
116 116 end
117 117
118 118 def destroy_children
119 119 unless @without_nested_set_update
120 120 lock_nested_set
121 121 reload_nested_set_values
122 122 end
123 123 children.each {|c| c.send :destroy_without_nested_set_update}
124 124 reload
125 125 unless @without_nested_set_update
126 126 self.class.where(:root_id => root_id).where("lft > ? OR rgt > ?", lft, lft).update_all([
127 127 "lft = CASE WHEN lft > :lft THEN lft - :shift ELSE lft END, " +
128 128 "rgt = CASE WHEN rgt > :lft THEN rgt - :shift ELSE rgt END",
129 129 {:lft => lft, :shift => rgt - lft + 1}
130 130 ])
131 131 end
132 132 end
133 133
134 134 def destroy_without_nested_set_update
135 135 @without_nested_set_update = true
136 136 destroy
137 137 end
138 138
139 139 def reload_nested_set_values
140 140 self.root_id, self.lft, self.rgt = self.class.where(:id => id).pluck(:root_id, :lft, :rgt).first
141 141 end
142 142
143 143 def save_nested_set_values
144 144 self.class.where(:id => id).update_all(:root_id => root_id, :lft => lft, :rgt => rgt)
145 145 end
146 146
147 147 def move_possible?(issue)
148 148 !is_or_is_ancestor_of?(issue)
149 149 end
150 150
151 151 def lock_nested_set
152 152 lock = true
153 153 if self.class.connection.adapter_name =~ /sqlserver/i
154 154 lock = "WITH (ROWLOCK HOLDLOCK UPDLOCK)"
155 155 end
156 156 sets_to_lock = [id, parent_id].compact
157 157 self.class.reorder(:id).where("root_id IN (SELECT root_id FROM #{self.class.table_name} WHERE id IN (?))", sets_to_lock).lock(lock).ids
158 158 end
159 159
160 160 def nested_set_scope
161 161 self.class.order(:lft).where(:root_id => root_id)
162 162 end
163 163
164 164 def same_nested_set_scope?(issue)
165 165 root_id == issue.root_id
166 166 end
167 167
168 168 module ClassMethods
169 169 def rebuild_tree!
170 170 transaction do
171 171 reorder(:id).lock.ids
172 172 update_all(:root_id => nil, :lft => nil, :rgt => nil)
173 173 where(:parent_id => nil).update_all(["root_id = id, lft = ?, rgt = ?", 1, 2])
174 174 roots_with_children = joins("JOIN #{table_name} parent ON parent.id = #{table_name}.parent_id AND parent.id = parent.root_id").uniq.pluck("parent.id")
175 175 roots_with_children.each do |root_id|
176 176 rebuild_nodes(root_id)
177 177 end
178 178 end
179 179 end
180 180
181 181 private
182 182
183 183 def rebuild_nodes(parent_id = nil)
184 184 nodes = where(:parent_id => parent_id, :rgt => nil, :lft => nil).order(:id).to_a
185 185
186 186 nodes.each do |node|
187 187 node.send :add_to_nested_set, false
188 188 node.send :save_nested_set_values
189 189 rebuild_nodes node.id
190 190 end
191 191 end
192 192 end
193 193 end
194 194 end
195 195 end
@@ -1,426 +1,426
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class IssueNestedSetTest < ActiveSupport::TestCase
21 21 fixtures :projects, :users, :roles,
22 22 :trackers, :projects_trackers,
23 23 :issue_statuses, :issue_categories, :issue_relations,
24 24 :enumerations,
25 25 :issues
26 26
27 27 def test_new_record_is_leaf
28 28 i = Issue.new
29 29 assert i.leaf?
30 30 end
31 31
32 32 def test_create_root_issue
33 33 lft1 = new_issue_lft
34 34 issue1 = Issue.generate!
35 35 lft2 = new_issue_lft
36 36 issue2 = Issue.generate!
37 37 issue1.reload
38 38 issue2.reload
39 39 assert_equal [issue1.id, nil, lft1, lft1 + 1], [issue1.root_id, issue1.parent_id, issue1.lft, issue1.rgt]
40 40 assert_equal [issue2.id, nil, lft2, lft2 + 1], [issue2.root_id, issue2.parent_id, issue2.lft, issue2.rgt]
41 41 end
42 42
43 43 def test_create_child_issue
44 44 lft = new_issue_lft
45 45 parent = Issue.generate!
46 46 child = parent.generate_child!
47 47 parent.reload
48 48 child.reload
49 49 assert_equal [parent.id, nil, lft, lft + 3], [parent.root_id, parent.parent_id, parent.lft, parent.rgt]
50 50 assert_equal [parent.id, parent.id, lft + 1, lft + 2], [child.root_id, child.parent_id, child.lft, child.rgt]
51 51 end
52 52
53 53 def test_creating_a_child_in_a_subproject_should_validate
54 54 issue = Issue.generate!
55 55 child = Issue.new(:project_id => 3, :tracker_id => 2, :author_id => 1,
56 56 :subject => 'child', :parent_issue_id => issue.id)
57 57 assert_save child
58 58 assert_equal issue, child.reload.parent
59 59 end
60 60
61 61 def test_creating_a_child_in_an_invalid_project_should_not_validate
62 62 issue = Issue.generate!
63 63 child = Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1,
64 64 :subject => 'child', :parent_issue_id => issue.id)
65 65 assert !child.save
66 66 assert_not_equal [], child.errors[:parent_issue_id]
67 67 end
68 68
69 69 def test_move_a_root_to_child
70 70 lft = new_issue_lft
71 71 parent1 = Issue.generate!
72 72 parent2 = Issue.generate!
73 73 child = parent1.generate_child!
74 74 parent2.parent_issue_id = parent1.id
75 75 parent2.save!
76 76 child.reload
77 77 parent1.reload
78 78 parent2.reload
79 79 assert_equal [parent1.id, lft, lft + 5], [parent1.root_id, parent1.lft, parent1.rgt]
80 assert_equal [parent1.id, lft + 3, lft + 4], [parent2.root_id, parent2.lft, parent2.rgt]
81 assert_equal [parent1.id, lft + 1, lft + 2], [child.root_id, child.lft, child.rgt]
80 assert_equal [parent1.id, lft + 1, lft + 2], [parent2.root_id, parent2.lft, parent2.rgt]
81 assert_equal [parent1.id, lft + 3, lft + 4], [child.root_id, child.lft, child.rgt]
82 82 end
83 83
84 84 def test_move_a_child_to_root
85 85 lft1 = new_issue_lft
86 86 parent1 = Issue.generate!
87 87 lft2 = new_issue_lft
88 88 parent2 = Issue.generate!
89 89 lft3 = new_issue_lft
90 90 child = parent1.generate_child!
91 91 child.parent_issue_id = nil
92 92 child.save!
93 93 child.reload
94 94 parent1.reload
95 95 parent2.reload
96 96 assert_equal [parent1.id, lft1, lft1 + 1], [parent1.root_id, parent1.lft, parent1.rgt]
97 97 assert_equal [parent2.id, lft2, lft2 + 1], [parent2.root_id, parent2.lft, parent2.rgt]
98 98 assert_equal [child.id, lft3, lft3 + 1], [child.root_id, child.lft, child.rgt]
99 99 end
100 100
101 101 def test_move_a_child_to_another_issue
102 102 lft1 = new_issue_lft
103 103 parent1 = Issue.generate!
104 104 lft2 = new_issue_lft
105 105 parent2 = Issue.generate!
106 106 child = parent1.generate_child!
107 107 child.parent_issue_id = parent2.id
108 108 child.save!
109 109 child.reload
110 110 parent1.reload
111 111 parent2.reload
112 112 assert_equal [parent1.id, lft1, lft1 + 1], [parent1.root_id, parent1.lft, parent1.rgt]
113 113 assert_equal [parent2.id, lft2, lft2 + 3], [parent2.root_id, parent2.lft, parent2.rgt]
114 114 assert_equal [parent2.id, lft2 + 1, lft2 + 2], [child.root_id, child.lft, child.rgt]
115 115 end
116 116
117 117 def test_move_a_child_with_descendants_to_another_issue
118 118 lft1 = new_issue_lft
119 119 parent1 = Issue.generate!
120 120 lft2 = new_issue_lft
121 121 parent2 = Issue.generate!
122 122 child = parent1.generate_child!
123 123 grandchild = child.generate_child!
124 124 parent1.reload
125 125 parent2.reload
126 126 child.reload
127 127 grandchild.reload
128 128 assert_equal [parent1.id, lft1, lft1 + 5], [parent1.root_id, parent1.lft, parent1.rgt]
129 129 assert_equal [parent2.id, lft2, lft2 + 1], [parent2.root_id, parent2.lft, parent2.rgt]
130 130 assert_equal [parent1.id, lft1 + 1, lft1 + 4], [child.root_id, child.lft, child.rgt]
131 131 assert_equal [parent1.id, lft1 + 2, lft1 + 3], [grandchild.root_id, grandchild.lft, grandchild.rgt]
132 132 child.reload.parent_issue_id = parent2.id
133 133 child.save!
134 134 child.reload
135 135 grandchild.reload
136 136 parent1.reload
137 137 parent2.reload
138 138 assert_equal [parent1.id, lft1, lft1 + 1], [parent1.root_id, parent1.lft, parent1.rgt]
139 139 assert_equal [parent2.id, lft2, lft2 + 5], [parent2.root_id, parent2.lft, parent2.rgt]
140 140 assert_equal [parent2.id, lft2 + 1, lft2 + 4], [child.root_id, child.lft, child.rgt]
141 141 assert_equal [parent2.id, lft2 + 2, lft2 + 3], [grandchild.root_id, grandchild.lft, grandchild.rgt]
142 142 end
143 143
144 144 def test_move_a_child_with_descendants_to_another_project
145 145 lft1 = new_issue_lft
146 146 parent1 = Issue.generate!
147 147 child = parent1.generate_child!
148 148 grandchild = child.generate_child!
149 149 lft4 = new_issue_lft
150 150 child.reload
151 151 child.project = Project.find(2)
152 152 assert child.save
153 153 child.reload
154 154 grandchild.reload
155 155 parent1.reload
156 156 assert_equal [1, parent1.id, lft1, lft1 + 1], [parent1.project_id, parent1.root_id, parent1.lft, parent1.rgt]
157 157 assert_equal [2, child.id, lft4, lft4 + 3],
158 158 [child.project_id, child.root_id, child.lft, child.rgt]
159 159 assert_equal [2, child.id, lft4 + 1, lft4 + 2],
160 160 [grandchild.project_id, grandchild.root_id, grandchild.lft, grandchild.rgt]
161 161 end
162 162
163 163 def test_moving_an_issue_to_a_descendant_should_not_validate
164 164 parent1 = Issue.generate!
165 165 parent2 = Issue.generate!
166 166 child = parent1.generate_child!
167 167 grandchild = child.generate_child!
168 168
169 169 child.reload
170 170 child.parent_issue_id = grandchild.id
171 171 assert !child.save
172 172 assert_not_equal [], child.errors[:parent_issue_id]
173 173 end
174 174
175 175 def test_updating_a_root_issue_should_not_trigger_update_nested_set_attributes_on_parent_change
176 176 issue = Issue.find(Issue.generate!.id)
177 177 issue.parent_issue_id = ""
178 178 issue.expects(:update_nested_set_attributes_on_parent_change).never
179 179 issue.save!
180 180 end
181 181
182 182 def test_updating_a_child_issue_should_not_trigger_update_nested_set_attributes_on_parent_change
183 183 issue = Issue.find(Issue.generate!(:parent_issue_id => 1).id)
184 184 issue.parent_issue_id = "1"
185 185 issue.expects(:update_nested_set_attributes_on_parent_change).never
186 186 issue.save!
187 187 end
188 188
189 189 def test_moving_a_root_issue_should_trigger_update_nested_set_attributes_on_parent_change
190 190 issue = Issue.find(Issue.generate!.id)
191 191 issue.parent_issue_id = "1"
192 192 issue.expects(:update_nested_set_attributes_on_parent_change).once
193 193 issue.save!
194 194 end
195 195
196 196 def test_moving_a_child_issue_to_another_parent_should_trigger_update_nested_set_attributes_on_parent_change
197 197 issue = Issue.find(Issue.generate!(:parent_issue_id => 1).id)
198 198 issue.parent_issue_id = "2"
199 199 issue.expects(:update_nested_set_attributes_on_parent_change).once
200 200 issue.save!
201 201 end
202 202
203 203 def test_moving_a_child_issue_to_root_should_trigger_update_nested_set_attributes_on_parent_change
204 204 issue = Issue.find(Issue.generate!(:parent_issue_id => 1).id)
205 205 issue.parent_issue_id = ""
206 206 issue.expects(:update_nested_set_attributes_on_parent_change).once
207 207 issue.save!
208 208 end
209 209
210 210 def test_destroy_should_destroy_children
211 211 lft1 = new_issue_lft
212 212 issue1 = Issue.generate!
213 213 issue2 = Issue.generate!
214 214 issue3 = issue2.generate_child!
215 215 issue4 = issue1.generate_child!
216 216 issue3.init_journal(User.find(2))
217 217 issue3.subject = 'child with journal'
218 218 issue3.save!
219 219 assert_difference 'Issue.count', -2 do
220 220 assert_difference 'Journal.count', -1 do
221 221 assert_difference 'JournalDetail.count', -1 do
222 222 Issue.find(issue2.id).destroy
223 223 end
224 224 end
225 225 end
226 226 issue1.reload
227 227 issue4.reload
228 228 assert !Issue.exists?(issue2.id)
229 229 assert !Issue.exists?(issue3.id)
230 230 assert_equal [issue1.id, lft1, lft1 + 3], [issue1.root_id, issue1.lft, issue1.rgt]
231 231 assert_equal [issue1.id, lft1 + 1, lft1 + 2], [issue4.root_id, issue4.lft, issue4.rgt]
232 232 end
233 233
234 234 def test_destroy_child_should_update_parent
235 235 lft1 = new_issue_lft
236 236 issue = Issue.generate!
237 237 child1 = issue.generate_child!
238 238 child2 = issue.generate_child!
239 239 issue.reload
240 240 assert_equal [issue.id, lft1, lft1 + 5], [issue.root_id, issue.lft, issue.rgt]
241 241 child2.reload.destroy
242 242 issue.reload
243 243 assert_equal [issue.id, lft1, lft1 + 3], [issue.root_id, issue.lft, issue.rgt]
244 244 end
245 245
246 246 def test_destroy_parent_issue_updated_during_children_destroy
247 247 parent = Issue.generate!
248 248 parent.generate_child!(:start_date => Date.today)
249 249 parent.generate_child!(:start_date => 2.days.from_now)
250 250
251 251 assert_difference 'Issue.count', -3 do
252 252 Issue.find(parent.id).destroy
253 253 end
254 254 end
255 255
256 256 def test_destroy_child_issue_with_children
257 257 root = Issue.generate!
258 258 child = root.generate_child!
259 259 leaf = child.generate_child!
260 260 leaf.init_journal(User.find(2))
261 261 leaf.subject = 'leaf with journal'
262 262 leaf.save!
263 263
264 264 assert_difference 'Issue.count', -2 do
265 265 assert_difference 'Journal.count', -1 do
266 266 assert_difference 'JournalDetail.count', -1 do
267 267 Issue.find(child.id).destroy
268 268 end
269 269 end
270 270 end
271 271
272 272 root = Issue.find(root.id)
273 273 assert root.leaf?, "Root issue is not a leaf (lft: #{root.lft}, rgt: #{root.rgt})"
274 274 end
275 275
276 276 def test_destroy_issue_with_grand_child
277 277 lft1 = new_issue_lft
278 278 parent = Issue.generate!
279 279 issue = parent.generate_child!
280 280 child = issue.generate_child!
281 281 grandchild1 = child.generate_child!
282 282 grandchild2 = child.generate_child!
283 283 assert_difference 'Issue.count', -4 do
284 284 Issue.find(issue.id).destroy
285 285 parent.reload
286 286 assert_equal [lft1, lft1 + 1], [parent.lft, parent.rgt]
287 287 end
288 288 end
289 289
290 290 def test_parent_priority_should_be_the_highest_child_priority
291 291 parent = Issue.generate!(:priority => IssuePriority.find_by_name('Normal'))
292 292 # Create children
293 293 child1 = parent.generate_child!(:priority => IssuePriority.find_by_name('High'))
294 294 assert_equal 'High', parent.reload.priority.name
295 295 child2 = child1.generate_child!(:priority => IssuePriority.find_by_name('Immediate'))
296 296 assert_equal 'Immediate', child1.reload.priority.name
297 297 assert_equal 'Immediate', parent.reload.priority.name
298 298 child3 = parent.generate_child!(:priority => IssuePriority.find_by_name('Low'))
299 299 assert_equal 'Immediate', parent.reload.priority.name
300 300 # Destroy a child
301 301 child1.destroy
302 302 assert_equal 'Low', parent.reload.priority.name
303 303 # Update a child
304 304 child3.reload.priority = IssuePriority.find_by_name('Normal')
305 305 child3.save!
306 306 assert_equal 'Normal', parent.reload.priority.name
307 307 end
308 308
309 309 def test_parent_dates_should_be_lowest_start_and_highest_due_dates
310 310 parent = Issue.generate!
311 311 parent.generate_child!(:start_date => '2010-01-25', :due_date => '2010-02-15')
312 312 parent.generate_child!( :due_date => '2010-02-13')
313 313 parent.generate_child!(:start_date => '2010-02-01', :due_date => '2010-02-22')
314 314 parent.reload
315 315 assert_equal Date.parse('2010-01-25'), parent.start_date
316 316 assert_equal Date.parse('2010-02-22'), parent.due_date
317 317 end
318 318
319 319 def test_parent_done_ratio_should_be_average_done_ratio_of_leaves
320 320 parent = Issue.generate!
321 321 parent.generate_child!(:done_ratio => 20)
322 322 assert_equal 20, parent.reload.done_ratio
323 323 parent.generate_child!(:done_ratio => 70)
324 324 assert_equal 45, parent.reload.done_ratio
325 325
326 326 child = parent.generate_child!(:done_ratio => 0)
327 327 assert_equal 30, parent.reload.done_ratio
328 328
329 329 child.generate_child!(:done_ratio => 30)
330 330 assert_equal 30, child.reload.done_ratio
331 331 assert_equal 40, parent.reload.done_ratio
332 332 end
333 333
334 334 def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any
335 335 parent = Issue.generate!
336 336 parent.generate_child!(:estimated_hours => 10, :done_ratio => 20)
337 337 assert_equal 20, parent.reload.done_ratio
338 338 parent.generate_child!(:estimated_hours => 20, :done_ratio => 50)
339 339 assert_equal (50 * 20 + 20 * 10) / 30, parent.reload.done_ratio
340 340 end
341 341
342 342 def test_parent_done_ratio_with_child_estimate_to_0_should_reach_100
343 343 parent = Issue.generate!
344 344 issue1 = parent.generate_child!
345 345 issue2 = parent.generate_child!(:estimated_hours => 0)
346 346 assert_equal 0, parent.reload.done_ratio
347 347 issue1.reload.close!
348 348 assert_equal 50, parent.reload.done_ratio
349 349 issue2.reload.close!
350 350 assert_equal 100, parent.reload.done_ratio
351 351 end
352 352
353 353 def test_parent_estimate_should_be_sum_of_leaves
354 354 parent = Issue.generate!
355 355 parent.generate_child!(:estimated_hours => nil)
356 356 assert_equal nil, parent.reload.estimated_hours
357 357 parent.generate_child!(:estimated_hours => 5)
358 358 assert_equal 5, parent.reload.estimated_hours
359 359 parent.generate_child!(:estimated_hours => 7)
360 360 assert_equal 12, parent.reload.estimated_hours
361 361 end
362 362
363 363 def test_done_ratio_of_parent_with_a_child_without_estimated_time_should_not_exceed_100
364 364 parent = Issue.generate!
365 365 parent.generate_child!(:estimated_hours => 40)
366 366 parent.generate_child!(:estimated_hours => 40)
367 367 parent.generate_child!(:estimated_hours => 20)
368 368 parent.generate_child!
369 369 parent.reload.children.each(&:close!)
370 370 assert_equal 100, parent.reload.done_ratio
371 371 end
372 372
373 373 def test_done_ratio_of_parent_with_a_child_with_estimated_time_at_0_should_not_exceed_100
374 374 parent = Issue.generate!
375 375 parent.generate_child!(:estimated_hours => 40)
376 376 parent.generate_child!(:estimated_hours => 40)
377 377 parent.generate_child!(:estimated_hours => 20)
378 378 parent.generate_child!(:estimated_hours => 0)
379 379 parent.reload.children.each(&:close!)
380 380 assert_equal 100, parent.reload.done_ratio
381 381 end
382 382
383 383 def test_move_parent_updates_old_parent_attributes
384 384 first_parent = Issue.generate!
385 385 second_parent = Issue.generate!
386 386 child = first_parent.generate_child!(:estimated_hours => 5)
387 387 assert_equal 5, first_parent.reload.estimated_hours
388 388 child.update_attributes(:estimated_hours => 7, :parent_issue_id => second_parent.id)
389 389 assert_equal 7, second_parent.reload.estimated_hours
390 390 assert_nil first_parent.reload.estimated_hours
391 391 end
392 392
393 393 def test_reschuling_a_parent_should_reschedule_subtasks
394 394 parent = Issue.generate!
395 395 c1 = parent.generate_child!(:start_date => '2010-05-12', :due_date => '2010-05-18')
396 396 c2 = parent.generate_child!(:start_date => '2010-06-03', :due_date => '2010-06-10')
397 397 parent.reload
398 398 parent.reschedule_on!(Date.parse('2010-06-02'))
399 399 c1.reload
400 400 assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-08')], [c1.start_date, c1.due_date]
401 401 c2.reload
402 402 assert_equal [Date.parse('2010-06-03'), Date.parse('2010-06-10')], [c2.start_date, c2.due_date] # no change
403 403 parent.reload
404 404 assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-10')], [parent.start_date, parent.due_date]
405 405 end
406 406
407 407 def test_project_copy_should_copy_issue_tree
408 408 p = Project.create!(:name => 'Tree copy', :identifier => 'tree-copy', :tracker_ids => [1, 2])
409 409 i1 = Issue.generate!(:project => p, :subject => 'i1')
410 410 i2 = i1.generate_child!(:project => p, :subject => 'i2')
411 411 i3 = i1.generate_child!(:project => p, :subject => 'i3')
412 412 i4 = i2.generate_child!(:project => p, :subject => 'i4')
413 413 i5 = Issue.generate!(:project => p, :subject => 'i5')
414 414 c = Project.new(:name => 'Copy', :identifier => 'copy', :tracker_ids => [1, 2])
415 415 c.copy(p, :only => 'issues')
416 416 c.reload
417 417
418 418 assert_equal 5, c.issues.count
419 419 ic1, ic2, ic3, ic4, ic5 = c.issues.order('subject').to_a
420 420 assert ic1.root?
421 421 assert_equal ic1, ic2.parent
422 422 assert_equal ic1, ic3.parent
423 423 assert_equal ic2, ic4.parent
424 424 assert ic5.root?
425 425 end
426 426 end
General Comments 0
You need to be logged in to leave comments. Login now