##// END OF EJS Templates
Rails4: replace deprecated Relation#update_all at RepositoryTest...
Toshi MARUYAMA -
r12282:95bc7bf189ca
parent child
Show More
@@ -1,393 +1,391
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 RepositoryTest < ActiveSupport::TestCase
21 21 fixtures :projects,
22 22 :trackers,
23 23 :projects_trackers,
24 24 :enabled_modules,
25 25 :repositories,
26 26 :issues,
27 27 :issue_statuses,
28 28 :issue_categories,
29 29 :changesets,
30 30 :changes,
31 31 :users,
32 32 :members,
33 33 :member_roles,
34 34 :roles,
35 35 :enumerations
36 36
37 37 include Redmine::I18n
38 38
39 39 def setup
40 40 @repository = Project.find(1).repository
41 41 end
42 42
43 43 def test_blank_log_encoding_error_message
44 44 set_language_if_valid 'en'
45 45 repo = Repository::Bazaar.new(
46 46 :project => Project.find(3),
47 47 :url => "/test",
48 48 :log_encoding => ''
49 49 )
50 50 assert !repo.save
51 51 assert_include "Commit messages encoding can't be blank",
52 52 repo.errors.full_messages
53 53 end
54 54
55 55 def test_blank_log_encoding_error_message_fr
56 56 set_language_if_valid 'fr'
57 57 str = "Encodage des messages de commit doit \xc3\xaatre renseign\xc3\xa9(e)"
58 58 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
59 59 repo = Repository::Bazaar.new(
60 60 :project => Project.find(3),
61 61 :url => "/test"
62 62 )
63 63 assert !repo.save
64 64 assert_include str, repo.errors.full_messages
65 65 end
66 66
67 67 def test_create
68 68 repository = Repository::Subversion.new(:project => Project.find(3))
69 69 assert !repository.save
70 70
71 71 repository.url = "svn://localhost"
72 72 assert repository.save
73 73 repository.reload
74 74
75 75 project = Project.find(3)
76 76 assert_equal repository, project.repository
77 77 end
78 78
79 79 def test_first_repository_should_be_set_as_default
80 80 repository1 = Repository::Subversion.new(
81 81 :project => Project.find(3),
82 82 :identifier => 'svn1',
83 83 :url => 'file:///svn1'
84 84 )
85 85 assert repository1.save
86 86 assert repository1.is_default?
87 87
88 88 repository2 = Repository::Subversion.new(
89 89 :project => Project.find(3),
90 90 :identifier => 'svn2',
91 91 :url => 'file:///svn2'
92 92 )
93 93 assert repository2.save
94 94 assert !repository2.is_default?
95 95
96 96 assert_equal repository1, Project.find(3).repository
97 97 assert_equal [repository1, repository2], Project.find(3).repositories.sort
98 98 end
99 99
100 100 def test_default_repository_should_be_one
101 101 assert_equal 0, Project.find(3).repositories.count
102 102 repository1 = Repository::Subversion.new(
103 103 :project => Project.find(3),
104 104 :identifier => 'svn1',
105 105 :url => 'file:///svn1'
106 106 )
107 107 assert repository1.save
108 108 assert repository1.is_default?
109 109
110 110 repository2 = Repository::Subversion.new(
111 111 :project => Project.find(3),
112 112 :identifier => 'svn2',
113 113 :url => 'file:///svn2',
114 114 :is_default => true
115 115 )
116 116 assert repository2.save
117 117 assert repository2.is_default?
118 118 repository1.reload
119 119 assert !repository1.is_default?
120 120
121 121 assert_equal repository2, Project.find(3).repository
122 122 assert_equal [repository2, repository1], Project.find(3).repositories.sort
123 123 end
124 124
125 125 def test_identifier_should_accept_letters_digits_dashes_and_underscores
126 126 r = Repository::Subversion.new(
127 127 :project_id => 3,
128 128 :identifier => 'svn-123_45',
129 129 :url => 'file:///svn'
130 130 )
131 131 assert r.save
132 132 end
133 133
134 134 def test_identifier_should_not_be_frozen_for_a_new_repository
135 135 assert_equal false, Repository.new.identifier_frozen?
136 136 end
137 137
138 138 def test_identifier_should_not_be_frozen_for_a_saved_repository_with_blank_identifier
139 Repository.update_all(["identifier = ''"], "id = 10")
140
139 Repository.where(:id => 10).update_all(["identifier = ''"])
141 140 assert_equal false, Repository.find(10).identifier_frozen?
142 141 end
143 142
144 143 def test_identifier_should_be_frozen_for_a_saved_repository_with_valid_identifier
145 Repository.update_all(["identifier = 'abc123'"], "id = 10")
146
144 Repository.where(:id => 10).update_all(["identifier = 'abc123'"])
147 145 assert_equal true, Repository.find(10).identifier_frozen?
148 146 end
149 147
150 148 def test_identifier_should_not_accept_change_if_frozen
151 149 r = Repository.new(:identifier => 'foo')
152 150 r.stubs(:identifier_frozen?).returns(true)
153 151
154 152 r.identifier = 'bar'
155 153 assert_equal 'foo', r.identifier
156 154 end
157 155
158 156 def test_identifier_should_accept_change_if_not_frozen
159 157 r = Repository.new(:identifier => 'foo')
160 158 r.stubs(:identifier_frozen?).returns(false)
161 159
162 160 r.identifier = 'bar'
163 161 assert_equal 'bar', r.identifier
164 162 end
165 163
166 164 def test_destroy
167 165 repository = Repository.find(10)
168 166 changesets = repository.changesets.count
169 167 changes = repository.filechanges.count
170 168
171 169 assert_difference 'Changeset.count', -changesets do
172 170 assert_difference 'Change.count', -changes do
173 171 Repository.find(10).destroy
174 172 end
175 173 end
176 174 end
177 175
178 176 def test_destroy_should_delete_parents_associations
179 177 changeset = Changeset.find(102)
180 178 changeset.parents = Changeset.find_all_by_id([100, 101])
181 179 assert_difference 'Changeset.connection.select_all("select * from changeset_parents").count', -2 do
182 180 Repository.find(10).destroy
183 181 end
184 182 end
185 183
186 184 def test_destroy_should_delete_issues_associations
187 185 changeset = Changeset.find(102)
188 186 changeset.issues = Issue.find_all_by_id([1, 2])
189 187 assert_difference 'Changeset.connection.select_all("select * from changesets_issues").count', -2 do
190 188 Repository.find(10).destroy
191 189 end
192 190 end
193 191
194 192 def test_should_not_create_with_disabled_scm
195 193 # disable Subversion
196 194 with_settings :enabled_scm => ['Darcs', 'Git'] do
197 195 repository = Repository::Subversion.new(
198 196 :project => Project.find(3), :url => "svn://localhost")
199 197 assert !repository.save
200 198 assert_include I18n.translate('activerecord.errors.messages.invalid'),
201 199 repository.errors[:type]
202 200 end
203 201 end
204 202
205 203 def test_scan_changesets_for_issue_ids
206 204 Setting.default_language = 'en'
207 205
208 206 Setting.commit_ref_keywords = 'refs , references, IssueID'
209 207 Setting.commit_update_keywords = [
210 208 {'keywords' => 'fixes , closes', 'status_id' => IssueStatus.where(:is_closed => true).first.id, 'done_ratio' => '90'}
211 209 ]
212 210 Setting.default_language = 'en'
213 211 ActionMailer::Base.deliveries.clear
214 212
215 213 # make sure issue 1 is not already closed
216 214 fixed_issue = Issue.find(1)
217 215 assert !fixed_issue.status.is_closed?
218 216 old_status = fixed_issue.status
219 217
220 218 with_settings :notified_events => %w(issue_added issue_updated) do
221 219 Repository.scan_changesets_for_issue_ids
222 220 end
223 221 assert_equal [101, 102], Issue.find(3).changeset_ids
224 222
225 223 # fixed issues
226 224 fixed_issue.reload
227 225 assert fixed_issue.status.is_closed?
228 226 assert_equal 90, fixed_issue.done_ratio
229 227 assert_equal [101], fixed_issue.changeset_ids
230 228
231 229 # issue change
232 230 journal = fixed_issue.journals.reorder('created_on desc').first
233 231 assert_equal User.find_by_login('dlopper'), journal.user
234 232 assert_equal 'Applied in changeset r2.', journal.notes
235 233
236 234 # 2 email notifications
237 235 assert_equal 2, ActionMailer::Base.deliveries.size
238 236 mail = ActionMailer::Base.deliveries.first
239 237 assert_not_nil mail
240 238 assert mail.subject.starts_with?(
241 239 "[#{fixed_issue.project.name} - #{fixed_issue.tracker.name} ##{fixed_issue.id}]")
242 240 assert_mail_body_match(
243 241 "Status changed from #{old_status} to #{fixed_issue.status}", mail)
244 242
245 243 # ignoring commits referencing an issue of another project
246 244 assert_equal [], Issue.find(4).changesets
247 245 end
248 246
249 247 def test_for_changeset_comments_strip
250 248 repository = Repository::Mercurial.create(
251 249 :project => Project.find( 4 ),
252 250 :url => '/foo/bar/baz' )
253 251 comment = <<-COMMENT
254 252 This is a loooooooooooooooooooooooooooong comment
255 253
256 254
257 255 COMMENT
258 256 changeset = Changeset.new(
259 257 :comments => comment, :commit_date => Time.now,
260 258 :revision => 0, :scmid => 'f39b7922fb3c',
261 259 :committer => 'foo <foo@example.com>',
262 260 :committed_on => Time.now, :repository => repository )
263 261 assert( changeset.save )
264 262 assert_not_equal( comment, changeset.comments )
265 263 assert_equal( 'This is a loooooooooooooooooooooooooooong comment',
266 264 changeset.comments )
267 265 end
268 266
269 267 def test_for_urls_strip_cvs
270 268 repository = Repository::Cvs.create(
271 269 :project => Project.find(4),
272 270 :url => ' :pserver:login:password@host:/path/to/the/repository',
273 271 :root_url => 'foo ',
274 272 :log_encoding => 'UTF-8')
275 273 assert repository.save
276 274 repository.reload
277 275 assert_equal ':pserver:login:password@host:/path/to/the/repository',
278 276 repository.url
279 277 assert_equal 'foo', repository.root_url
280 278 end
281 279
282 280 def test_for_urls_strip_subversion
283 281 repository = Repository::Subversion.create(
284 282 :project => Project.find(4),
285 283 :url => ' file:///dummy ')
286 284 assert repository.save
287 285 repository.reload
288 286 assert_equal 'file:///dummy', repository.url
289 287 end
290 288
291 289 def test_for_urls_strip_git
292 290 repository = Repository::Git.create(
293 291 :project => Project.find(4),
294 292 :url => ' c:\dummy ')
295 293 assert repository.save
296 294 repository.reload
297 295 assert_equal 'c:\dummy', repository.url
298 296 end
299 297
300 298 def test_manual_user_mapping
301 299 assert_no_difference "Changeset.where('user_id <> 2').count" do
302 300 c = Changeset.create!(
303 301 :repository => @repository,
304 302 :committer => 'foo',
305 303 :committed_on => Time.now,
306 304 :revision => 100,
307 305 :comments => 'Committed by foo.'
308 306 )
309 307 assert_nil c.user
310 308 @repository.committer_ids = {'foo' => '2'}
311 309 assert_equal User.find(2), c.reload.user
312 310 # committer is now mapped
313 311 c = Changeset.create!(
314 312 :repository => @repository,
315 313 :committer => 'foo',
316 314 :committed_on => Time.now,
317 315 :revision => 101,
318 316 :comments => 'Another commit by foo.'
319 317 )
320 318 assert_equal User.find(2), c.user
321 319 end
322 320 end
323 321
324 322 def test_auto_user_mapping_by_username
325 323 c = Changeset.create!(
326 324 :repository => @repository,
327 325 :committer => 'jsmith',
328 326 :committed_on => Time.now,
329 327 :revision => 100,
330 328 :comments => 'Committed by john.'
331 329 )
332 330 assert_equal User.find(2), c.user
333 331 end
334 332
335 333 def test_auto_user_mapping_by_email
336 334 c = Changeset.create!(
337 335 :repository => @repository,
338 336 :committer => 'john <jsmith@somenet.foo>',
339 337 :committed_on => Time.now,
340 338 :revision => 100,
341 339 :comments => 'Committed by john.'
342 340 )
343 341 assert_equal User.find(2), c.user
344 342 end
345 343
346 344 def test_filesystem_avaialbe
347 345 klass = Repository::Filesystem
348 346 assert klass.scm_adapter_class
349 347 assert_equal true, klass.scm_available
350 348 end
351 349
352 350 def test_merge_extra_info
353 351 repo = Repository::Subversion.new(:project => Project.find(3))
354 352 assert !repo.save
355 353 repo.url = "svn://localhost"
356 354 assert repo.save
357 355 repo.reload
358 356 project = Project.find(3)
359 357 assert_equal repo, project.repository
360 358 assert_nil repo.extra_info
361 359 h1 = {"test_1" => {"test_11" => "test_value_11"}}
362 360 repo.merge_extra_info(h1)
363 361 assert_equal h1, repo.extra_info
364 362 h2 = {"test_2" => {
365 363 "test_21" => "test_value_21",
366 364 "test_22" => "test_value_22",
367 365 }}
368 366 repo.merge_extra_info(h2)
369 367 assert_equal (h = {"test_11" => "test_value_11"}),
370 368 repo.extra_info["test_1"]
371 369 assert_equal "test_value_21",
372 370 repo.extra_info["test_2"]["test_21"]
373 371 h3 = {"test_2" => {
374 372 "test_23" => "test_value_23",
375 373 "test_24" => "test_value_24",
376 374 }}
377 375 repo.merge_extra_info(h3)
378 376 assert_equal (h = {"test_11" => "test_value_11"}),
379 377 repo.extra_info["test_1"]
380 378 assert_nil repo.extra_info["test_2"]["test_21"]
381 379 assert_equal "test_value_23",
382 380 repo.extra_info["test_2"]["test_23"]
383 381 end
384 382
385 383 def test_sort_should_not_raise_an_error_with_nil_identifiers
386 384 r1 = Repository.new
387 385 r2 = Repository.new
388 386
389 387 assert_nothing_raised do
390 388 [r1, r2].sort
391 389 end
392 390 end
393 391 end
General Comments 0
You need to be logged in to leave comments. Login now