##// END OF EJS Templates
Refactored duplicated test code to a setup....
Eric Davis -
r2818:945ea9b01c43
parent child
Show More
@@ -1,358 +1,342
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class ProjectTest < ActiveSupport::TestCase
21 21 fixtures :projects, :enabled_modules,
22 22 :issues, :issue_statuses, :journals, :journal_details,
23 23 :users, :members, :member_roles, :roles, :projects_trackers, :trackers, :boards,
24 24 :queries
25 25
26 26 def setup
27 27 @ecookbook = Project.find(1)
28 28 @ecookbook_sub1 = Project.find(3)
29 29 end
30 30
31 31 def test_truth
32 32 assert_kind_of Project, @ecookbook
33 33 assert_equal "eCookbook", @ecookbook.name
34 34 end
35 35
36 36 def test_update
37 37 assert_equal "eCookbook", @ecookbook.name
38 38 @ecookbook.name = "eCook"
39 39 assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ")
40 40 @ecookbook.reload
41 41 assert_equal "eCook", @ecookbook.name
42 42 end
43 43
44 44 def test_validate
45 45 @ecookbook.name = ""
46 46 assert !@ecookbook.save
47 47 assert_equal 1, @ecookbook.errors.count
48 48 assert_equal I18n.translate('activerecord.errors.messages.blank'), @ecookbook.errors.on(:name)
49 49 end
50 50
51 51 def test_validate_identifier
52 52 to_test = {"abc" => true,
53 53 "ab12" => true,
54 54 "ab-12" => true,
55 55 "12" => false,
56 56 "new" => false}
57 57
58 58 to_test.each do |identifier, valid|
59 59 p = Project.new
60 60 p.identifier = identifier
61 61 p.valid?
62 62 assert_equal valid, p.errors.on('identifier').nil?
63 63 end
64 64 end
65 65
66 66 def test_members_should_be_active_users
67 67 Project.all.each do |project|
68 68 assert_nil project.members.detect {|m| !(m.user.is_a?(User) && m.user.active?) }
69 69 end
70 70 end
71 71
72 72 def test_users_should_be_active_users
73 73 Project.all.each do |project|
74 74 assert_nil project.users.detect {|u| !(u.is_a?(User) && u.active?) }
75 75 end
76 76 end
77 77
78 78 def test_archive
79 79 user = @ecookbook.members.first.user
80 80 @ecookbook.archive
81 81 @ecookbook.reload
82 82
83 83 assert !@ecookbook.active?
84 84 assert !user.projects.include?(@ecookbook)
85 85 # Subproject are also archived
86 86 assert !@ecookbook.children.empty?
87 87 assert @ecookbook.descendants.active.empty?
88 88 end
89 89
90 90 def test_unarchive
91 91 user = @ecookbook.members.first.user
92 92 @ecookbook.archive
93 93 # A subproject of an archived project can not be unarchived
94 94 assert !@ecookbook_sub1.unarchive
95 95
96 96 # Unarchive project
97 97 assert @ecookbook.unarchive
98 98 @ecookbook.reload
99 99 assert @ecookbook.active?
100 100 assert user.projects.include?(@ecookbook)
101 101 # Subproject can now be unarchived
102 102 @ecookbook_sub1.reload
103 103 assert @ecookbook_sub1.unarchive
104 104 end
105 105
106 106 def test_destroy
107 107 # 2 active members
108 108 assert_equal 2, @ecookbook.members.size
109 109 # and 1 is locked
110 110 assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size
111 111 # some boards
112 112 assert @ecookbook.boards.any?
113 113
114 114 @ecookbook.destroy
115 115 # make sure that the project non longer exists
116 116 assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) }
117 117 # make sure related data was removed
118 118 assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
119 119 assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
120 120 end
121 121
122 122 def test_move_an_orphan_project_to_a_root_project
123 123 sub = Project.find(2)
124 124 sub.set_parent! @ecookbook
125 125 assert_equal @ecookbook.id, sub.parent.id
126 126 @ecookbook.reload
127 127 assert_equal 4, @ecookbook.children.size
128 128 end
129 129
130 130 def test_move_an_orphan_project_to_a_subproject
131 131 sub = Project.find(2)
132 132 assert sub.set_parent!(@ecookbook_sub1)
133 133 end
134 134
135 135 def test_move_a_root_project_to_a_project
136 136 sub = @ecookbook
137 137 assert sub.set_parent!(Project.find(2))
138 138 end
139 139
140 140 def test_should_not_move_a_project_to_its_children
141 141 sub = @ecookbook
142 142 assert !(sub.set_parent!(Project.find(3)))
143 143 end
144 144
145 145 def test_set_parent_should_add_roots_in_alphabetical_order
146 146 ProjectCustomField.delete_all
147 147 Project.delete_all
148 148 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil)
149 149 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil)
150 150 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil)
151 151 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil)
152 152
153 153 assert_equal 4, Project.count
154 154 assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft)
155 155 end
156 156
157 157 def test_set_parent_should_add_children_in_alphabetical_order
158 158 ProjectCustomField.delete_all
159 159 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
160 160 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent)
161 161 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent)
162 162 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent)
163 163 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent)
164 164
165 165 parent.reload
166 166 assert_equal 4, parent.children.size
167 167 assert_equal parent.children.sort_by(&:name), parent.children
168 168 end
169 169
170 170 def test_rebuild_should_sort_children_alphabetically
171 171 ProjectCustomField.delete_all
172 172 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
173 173 Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
174 174 Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
175 175 Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
176 176 Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
177 177
178 178 Project.update_all("lft = NULL, rgt = NULL")
179 179 Project.rebuild!
180 180
181 181 parent.reload
182 182 assert_equal 4, parent.children.size
183 183 assert_equal parent.children.sort_by(&:name), parent.children
184 184 end
185 185
186 186 def test_parent
187 187 p = Project.find(6).parent
188 188 assert p.is_a?(Project)
189 189 assert_equal 5, p.id
190 190 end
191 191
192 192 def test_ancestors
193 193 a = Project.find(6).ancestors
194 194 assert a.first.is_a?(Project)
195 195 assert_equal [1, 5], a.collect(&:id)
196 196 end
197 197
198 198 def test_root
199 199 r = Project.find(6).root
200 200 assert r.is_a?(Project)
201 201 assert_equal 1, r.id
202 202 end
203 203
204 204 def test_children
205 205 c = Project.find(1).children
206 206 assert c.first.is_a?(Project)
207 207 assert_equal [5, 3, 4], c.collect(&:id)
208 208 end
209 209
210 210 def test_descendants
211 211 d = Project.find(1).descendants
212 212 assert d.first.is_a?(Project)
213 213 assert_equal [5, 6, 3, 4], d.collect(&:id)
214 214 end
215 215
216 216 def test_users_by_role
217 217 users_by_role = Project.find(1).users_by_role
218 218 assert_kind_of Hash, users_by_role
219 219 role = Role.find(1)
220 220 assert_kind_of Array, users_by_role[role]
221 221 assert users_by_role[role].include?(User.find(2))
222 222 end
223 223
224 224 def test_rolled_up_trackers
225 225 parent = Project.find(1)
226 226 parent.trackers = Tracker.find([1,2])
227 227 child = parent.children.find(3)
228 228
229 229 assert_equal [1, 2], parent.tracker_ids
230 230 assert_equal [2, 3], child.trackers.collect(&:id)
231 231
232 232 assert_kind_of Tracker, parent.rolled_up_trackers.first
233 233 assert_equal Tracker.find(1), parent.rolled_up_trackers.first
234 234
235 235 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id)
236 236 assert_equal [2, 3], child.rolled_up_trackers.collect(&:id)
237 237 end
238 238
239 239 def test_rolled_up_trackers_should_ignore_archived_subprojects
240 240 parent = Project.find(1)
241 241 parent.trackers = Tracker.find([1,2])
242 242 child = parent.children.find(3)
243 243 child.trackers = Tracker.find([1,3])
244 244 parent.children.each(&:archive)
245 245
246 246 assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
247 247 end
248 248
249 249 def test_next_identifier
250 250 ProjectCustomField.delete_all
251 251 Project.create!(:name => 'last', :identifier => 'p2008040')
252 252 assert_equal 'p2008041', Project.next_identifier
253 253 end
254 254
255 255 def test_next_identifier_first_project
256 256 Project.delete_all
257 257 assert_nil Project.next_identifier
258 258 end
259 259
260 260
261 261 def test_enabled_module_names_should_not_recreate_enabled_modules
262 262 project = Project.find(1)
263 263 # Remove one module
264 264 modules = project.enabled_modules.slice(0..-2)
265 265 assert modules.any?
266 266 assert_difference 'EnabledModule.count', -1 do
267 267 project.enabled_module_names = modules.collect(&:name)
268 268 end
269 269 project.reload
270 270 # Ids should be preserved
271 271 assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort
272 272 end
273 273
274 274 def test_copy_from_existing_project
275 275 source_project = Project.find(1)
276 276 copied_project = Project.copy_from(1)
277 277
278 278 assert copied_project
279 279 # Cleared attributes
280 280 assert copied_project.id.blank?
281 281 assert copied_project.name.blank?
282 282 assert copied_project.identifier.blank?
283 283
284 284 # Duplicated attributes
285 285 assert_equal source_project.description, copied_project.description
286 286 assert_equal source_project.enabled_modules, copied_project.enabled_modules
287 287 assert_equal source_project.trackers, copied_project.trackers
288 288
289 289 # Default attributes
290 290 assert_equal 1, copied_project.status
291 291 end
292 292
293 293 context "#copy" do
294
295 should "copy issues" do
296 # Setup
294 setup do
297 295 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
298 source_project = Project.find(2)
299 296 Project.destroy_all :identifier => "copy-test"
300 project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
301 project.trackers = source_project.trackers
302 assert project.valid?
303
304 assert project.issues.empty?
305 assert project.copy(source_project)
297 @source_project = Project.find(2)
298 @project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
299 @project.trackers = @source_project.trackers
300 @project.enabled_modules = @source_project.enabled_modules
301 end
302
303 should "copy issues" do
304 assert @project.valid?
305 assert @project.issues.empty?
306 assert @project.copy(@source_project)
306 307
307 # Tests
308 assert_equal source_project.issues.size, project.issues.size
309 project.issues.each do |issue|
308 assert_equal @source_project.issues.size, @project.issues.size
309 @project.issues.each do |issue|
310 310 assert issue.valid?
311 311 assert ! issue.assigned_to.blank?
312 assert_equal project, issue.project
312 assert_equal @project, issue.project
313 313 end
314 314 end
315 315
316 316 should "copy members" do
317 # Setup
318 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
319 source_project = Project.find(2)
320 project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
321 project.trackers = source_project.trackers
322 project.enabled_modules = source_project.enabled_modules
323 assert project.valid?
324
325 assert project.members.empty?
326 assert project.copy(source_project)
317 assert @project.valid?
318 assert @project.members.empty?
319 assert @project.copy(@source_project)
327 320
328 # Tests
329 assert_equal source_project.members.size, project.members.size
330 project.members.each do |member|
321 assert_equal @source_project.members.size, @project.members.size
322 @project.members.each do |member|
331 323 assert member
332 assert_equal project, member.project
324 assert_equal @project, member.project
333 325 end
334 326 end
335 327
336 328 should "copy project specific queries" do
337 # Setup
338 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
339 source_project = Project.find(2)
340 project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
341 project.trackers = source_project.trackers
342 project.enabled_modules = source_project.enabled_modules
343 assert project.valid?
344
345 assert project.queries.empty?
346 assert project.copy(source_project)
329 assert @project.valid?
330 assert @project.queries.empty?
331 assert @project.copy(@source_project)
347 332
348 # Tests
349 assert_equal source_project.queries.size, project.queries.size
350 project.queries.each do |query|
333 assert_equal @source_project.queries.size, @project.queries.size
334 @project.queries.each do |query|
351 335 assert query
352 assert_equal project, query.project
336 assert_equal @project, query.project
353 337 end
354 338 end
355 339
356 340 end
357 341
358 342 end
General Comments 0
You need to be logged in to leave comments. Login now