##// END OF EJS Templates
Replaced a custom test with a shoulda macro....
Eric Davis -
r2821:257c92f8f910
parent child
Show More
@@ -1,422 +1,421
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 should_validate_presence_of :name
32 should_validate_presence_of :identifier
33
34 should_validate_uniqueness_of :name
35 should_validate_uniqueness_of :identifier
36
31 37 def test_truth
32 38 assert_kind_of Project, @ecookbook
33 39 assert_equal "eCookbook", @ecookbook.name
34 40 end
35 41
36 42 def test_update
37 43 assert_equal "eCookbook", @ecookbook.name
38 44 @ecookbook.name = "eCook"
39 45 assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ")
40 46 @ecookbook.reload
41 47 assert_equal "eCook", @ecookbook.name
42 48 end
43 49
44 def test_validate
45 @ecookbook.name = ""
46 assert !@ecookbook.save
47 assert_equal 1, @ecookbook.errors.count
48 assert_equal I18n.translate('activerecord.errors.messages.blank'), @ecookbook.errors.on(:name)
49 end
50
51 50 def test_validate_identifier
52 51 to_test = {"abc" => true,
53 52 "ab12" => true,
54 53 "ab-12" => true,
55 54 "12" => false,
56 55 "new" => false}
57 56
58 57 to_test.each do |identifier, valid|
59 58 p = Project.new
60 59 p.identifier = identifier
61 60 p.valid?
62 61 assert_equal valid, p.errors.on('identifier').nil?
63 62 end
64 63 end
65
64
66 65 def test_members_should_be_active_users
67 66 Project.all.each do |project|
68 67 assert_nil project.members.detect {|m| !(m.user.is_a?(User) && m.user.active?) }
69 68 end
70 69 end
71 70
72 71 def test_users_should_be_active_users
73 72 Project.all.each do |project|
74 73 assert_nil project.users.detect {|u| !(u.is_a?(User) && u.active?) }
75 74 end
76 75 end
77 76
78 77 def test_archive
79 78 user = @ecookbook.members.first.user
80 79 @ecookbook.archive
81 80 @ecookbook.reload
82 81
83 82 assert !@ecookbook.active?
84 83 assert !user.projects.include?(@ecookbook)
85 84 # Subproject are also archived
86 85 assert !@ecookbook.children.empty?
87 86 assert @ecookbook.descendants.active.empty?
88 87 end
89 88
90 89 def test_unarchive
91 90 user = @ecookbook.members.first.user
92 91 @ecookbook.archive
93 92 # A subproject of an archived project can not be unarchived
94 93 assert !@ecookbook_sub1.unarchive
95 94
96 95 # Unarchive project
97 96 assert @ecookbook.unarchive
98 97 @ecookbook.reload
99 98 assert @ecookbook.active?
100 99 assert user.projects.include?(@ecookbook)
101 100 # Subproject can now be unarchived
102 101 @ecookbook_sub1.reload
103 102 assert @ecookbook_sub1.unarchive
104 103 end
105 104
106 105 def test_destroy
107 106 # 2 active members
108 107 assert_equal 2, @ecookbook.members.size
109 108 # and 1 is locked
110 109 assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size
111 110 # some boards
112 111 assert @ecookbook.boards.any?
113 112
114 113 @ecookbook.destroy
115 114 # make sure that the project non longer exists
116 115 assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) }
117 116 # make sure related data was removed
118 117 assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
119 118 assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
120 119 end
121 120
122 121 def test_move_an_orphan_project_to_a_root_project
123 122 sub = Project.find(2)
124 123 sub.set_parent! @ecookbook
125 124 assert_equal @ecookbook.id, sub.parent.id
126 125 @ecookbook.reload
127 126 assert_equal 4, @ecookbook.children.size
128 127 end
129 128
130 129 def test_move_an_orphan_project_to_a_subproject
131 130 sub = Project.find(2)
132 131 assert sub.set_parent!(@ecookbook_sub1)
133 132 end
134 133
135 134 def test_move_a_root_project_to_a_project
136 135 sub = @ecookbook
137 136 assert sub.set_parent!(Project.find(2))
138 137 end
139 138
140 139 def test_should_not_move_a_project_to_its_children
141 140 sub = @ecookbook
142 141 assert !(sub.set_parent!(Project.find(3)))
143 142 end
144 143
145 144 def test_set_parent_should_add_roots_in_alphabetical_order
146 145 ProjectCustomField.delete_all
147 146 Project.delete_all
148 147 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil)
149 148 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil)
150 149 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil)
151 150 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil)
152 151
153 152 assert_equal 4, Project.count
154 153 assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft)
155 154 end
156 155
157 156 def test_set_parent_should_add_children_in_alphabetical_order
158 157 ProjectCustomField.delete_all
159 158 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
160 159 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent)
161 160 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent)
162 161 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent)
163 162 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent)
164 163
165 164 parent.reload
166 165 assert_equal 4, parent.children.size
167 166 assert_equal parent.children.sort_by(&:name), parent.children
168 167 end
169 168
170 169 def test_rebuild_should_sort_children_alphabetically
171 170 ProjectCustomField.delete_all
172 171 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
173 172 Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
174 173 Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
175 174 Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
176 175 Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
177 176
178 177 Project.update_all("lft = NULL, rgt = NULL")
179 178 Project.rebuild!
180 179
181 180 parent.reload
182 181 assert_equal 4, parent.children.size
183 182 assert_equal parent.children.sort_by(&:name), parent.children
184 183 end
185 184
186 185 def test_parent
187 186 p = Project.find(6).parent
188 187 assert p.is_a?(Project)
189 188 assert_equal 5, p.id
190 189 end
191 190
192 191 def test_ancestors
193 192 a = Project.find(6).ancestors
194 193 assert a.first.is_a?(Project)
195 194 assert_equal [1, 5], a.collect(&:id)
196 195 end
197 196
198 197 def test_root
199 198 r = Project.find(6).root
200 199 assert r.is_a?(Project)
201 200 assert_equal 1, r.id
202 201 end
203 202
204 203 def test_children
205 204 c = Project.find(1).children
206 205 assert c.first.is_a?(Project)
207 206 assert_equal [5, 3, 4], c.collect(&:id)
208 207 end
209 208
210 209 def test_descendants
211 210 d = Project.find(1).descendants
212 211 assert d.first.is_a?(Project)
213 212 assert_equal [5, 6, 3, 4], d.collect(&:id)
214 213 end
215 214
216 215 def test_users_by_role
217 216 users_by_role = Project.find(1).users_by_role
218 217 assert_kind_of Hash, users_by_role
219 218 role = Role.find(1)
220 219 assert_kind_of Array, users_by_role[role]
221 220 assert users_by_role[role].include?(User.find(2))
222 221 end
223 222
224 223 def test_rolled_up_trackers
225 224 parent = Project.find(1)
226 225 parent.trackers = Tracker.find([1,2])
227 226 child = parent.children.find(3)
228 227
229 228 assert_equal [1, 2], parent.tracker_ids
230 229 assert_equal [2, 3], child.trackers.collect(&:id)
231 230
232 231 assert_kind_of Tracker, parent.rolled_up_trackers.first
233 232 assert_equal Tracker.find(1), parent.rolled_up_trackers.first
234 233
235 234 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id)
236 235 assert_equal [2, 3], child.rolled_up_trackers.collect(&:id)
237 236 end
238 237
239 238 def test_rolled_up_trackers_should_ignore_archived_subprojects
240 239 parent = Project.find(1)
241 240 parent.trackers = Tracker.find([1,2])
242 241 child = parent.children.find(3)
243 242 child.trackers = Tracker.find([1,3])
244 243 parent.children.each(&:archive)
245 244
246 245 assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
247 246 end
248 247
249 248 def test_next_identifier
250 249 ProjectCustomField.delete_all
251 250 Project.create!(:name => 'last', :identifier => 'p2008040')
252 251 assert_equal 'p2008041', Project.next_identifier
253 252 end
254 253
255 254 def test_next_identifier_first_project
256 255 Project.delete_all
257 256 assert_nil Project.next_identifier
258 257 end
259 258
260 259
261 260 def test_enabled_module_names_should_not_recreate_enabled_modules
262 261 project = Project.find(1)
263 262 # Remove one module
264 263 modules = project.enabled_modules.slice(0..-2)
265 264 assert modules.any?
266 265 assert_difference 'EnabledModule.count', -1 do
267 266 project.enabled_module_names = modules.collect(&:name)
268 267 end
269 268 project.reload
270 269 # Ids should be preserved
271 270 assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort
272 271 end
273 272
274 273 def test_copy_from_existing_project
275 274 source_project = Project.find(1)
276 275 copied_project = Project.copy_from(1)
277 276
278 277 assert copied_project
279 278 # Cleared attributes
280 279 assert copied_project.id.blank?
281 280 assert copied_project.name.blank?
282 281 assert copied_project.identifier.blank?
283 282
284 283 # Duplicated attributes
285 284 assert_equal source_project.description, copied_project.description
286 285 assert_equal source_project.enabled_modules, copied_project.enabled_modules
287 286 assert_equal source_project.trackers, copied_project.trackers
288 287
289 288 # Default attributes
290 289 assert_equal 1, copied_project.status
291 290 end
292 291
293 292 context "Project#copy" do
294 293 setup do
295 294 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
296 295 Project.destroy_all :identifier => "copy-test"
297 296 @source_project = Project.find(2)
298 297 @project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
299 298 @project.trackers = @source_project.trackers
300 299 @project.enabled_modules = @source_project.enabled_modules
301 300 end
302 301
303 302 should "copy issues" do
304 303 assert @project.valid?
305 304 assert @project.issues.empty?
306 305 assert @project.copy(@source_project)
307 306
308 307 assert_equal @source_project.issues.size, @project.issues.size
309 308 @project.issues.each do |issue|
310 309 assert issue.valid?
311 310 assert ! issue.assigned_to.blank?
312 311 assert_equal @project, issue.project
313 312 end
314 313 end
315 314
316 315 should "change the new issues to use the copied version" do
317 316 assigned_version = Version.generate!(:name => "Assigned Issues")
318 317 @source_project.versions << assigned_version
319 318 assert_equal 1, @source_project.versions.size
320 319 @source_project.issues << Issue.generate!(:fixed_version_id => assigned_version.id,
321 320 :subject => "change the new issues to use the copied version",
322 321 :tracker_id => 1,
323 322 :project_id => @source_project.id)
324 323
325 324 assert @project.copy(@source_project)
326 325 @project.reload
327 326 copied_issue = @project.issues.first(:conditions => {:subject => "change the new issues to use the copied version"})
328 327
329 328 assert copied_issue
330 329 assert copied_issue.fixed_version
331 330 assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name
332 331 assert_not_equal assigned_version.id, copied_issue.fixed_version.id # Different record
333 332 end
334 333
335 334 should "copy members" do
336 335 assert @project.valid?
337 336 assert @project.members.empty?
338 337 assert @project.copy(@source_project)
339 338
340 339 assert_equal @source_project.members.size, @project.members.size
341 340 @project.members.each do |member|
342 341 assert member
343 342 assert_equal @project, member.project
344 343 end
345 344 end
346 345
347 346 should "copy project specific queries" do
348 347 assert @project.valid?
349 348 assert @project.queries.empty?
350 349 assert @project.copy(@source_project)
351 350
352 351 assert_equal @source_project.queries.size, @project.queries.size
353 352 @project.queries.each do |query|
354 353 assert query
355 354 assert_equal @project, query.project
356 355 end
357 356 end
358 357
359 358 should "copy versions" do
360 359 @source_project.versions << Version.generate!
361 360 @source_project.versions << Version.generate!
362 361
363 362 assert @project.versions.empty?
364 363 assert @project.copy(@source_project)
365 364
366 365 assert_equal @source_project.versions.size, @project.versions.size
367 366 @project.versions.each do |version|
368 367 assert version
369 368 assert_equal @project, version.project
370 369 end
371 370 end
372 371
373 372 should "copy wiki" do
374 373 assert @project.copy(@source_project)
375 374
376 375 assert @project.wiki
377 376 assert_not_equal @source_project.wiki, @project.wiki
378 377 assert_equal "Start page", @project.wiki.start_page
379 378 end
380 379
381 380 should "copy wiki pages and content" do
382 381 assert @project.copy(@source_project)
383 382
384 383 assert @project.wiki
385 384 assert_equal 1, @project.wiki.pages.length
386 385
387 386 @project.wiki.pages.each do |wiki_page|
388 387 assert wiki_page.content
389 388 assert !@source_project.wiki.pages.include?(wiki_page)
390 389 end
391 390 end
392 391
393 392 should "copy custom fields"
394 393
395 394 should "copy issue categories" do
396 395 assert @project.copy(@source_project)
397 396
398 397 assert_equal 2, @project.issue_categories.size
399 398 @project.issue_categories.each do |issue_category|
400 399 assert !@source_project.issue_categories.include?(issue_category)
401 400 end
402 401 end
403 402
404 403 should "change the new issues to use the copied issue categories" do
405 404 issue = Issue.find(4)
406 405 issue.update_attribute(:category_id, 3)
407 406
408 407 assert @project.copy(@source_project)
409 408
410 409 @project.issues.each do |issue|
411 410 assert issue.category
412 411 assert_equal "Stock management", issue.category.name # Same name
413 412 assert_not_equal IssueCategory.find(3), issue.category # Different record
414 413 end
415 414 end
416 415
417 416 should "copy issue relations"
418 417 should "link issue relations if cross project issue relations are valid"
419 418
420 419 end
421 420
422 421 end
General Comments 0
You need to be logged in to leave comments. Login now