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