##// END OF EJS Templates
scm: code clean up unit model changeset test....
Toshi MARUYAMA -
r5041:8d0d1d50ccba
parent child
Show More
@@ -1,290 +1,294
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2010 Jean-Philippe Lang
4 # Copyright (C) 2006-2010 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 require File.expand_path('../../test_helper', __FILE__)
20 require File.expand_path('../../test_helper', __FILE__)
21
21
22 class ChangesetTest < ActiveSupport::TestCase
22 class ChangesetTest < ActiveSupport::TestCase
23 fixtures :projects, :repositories, :issues, :issue_statuses, :changesets, :changes, :issue_categories, :enumerations, :custom_fields, :custom_values, :users, :members, :member_roles, :trackers
23 fixtures :projects, :repositories, :issues, :issue_statuses, :changesets, :changes, :issue_categories, :enumerations, :custom_fields, :custom_values, :users, :members, :member_roles, :trackers
24
24
25 def setup
25 def setup
26 end
26 end
27
27
28 def test_ref_keywords_any
28 def test_ref_keywords_any
29 ActionMailer::Base.deliveries.clear
29 ActionMailer::Base.deliveries.clear
30 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
30 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
31 Setting.commit_fix_done_ratio = '90'
31 Setting.commit_fix_done_ratio = '90'
32 Setting.commit_ref_keywords = '*'
32 Setting.commit_ref_keywords = '*'
33 Setting.commit_fix_keywords = 'fixes , closes'
33 Setting.commit_fix_keywords = 'fixes , closes'
34
34
35 c = Changeset.new(:repository => Project.find(1).repository,
35 c = Changeset.new(:repository => Project.find(1).repository,
36 :committed_on => Time.now,
36 :committed_on => Time.now,
37 :comments => 'New commit (#2). Fixes #1')
37 :comments => 'New commit (#2). Fixes #1')
38 c.scan_comment_for_issue_ids
38 c.scan_comment_for_issue_ids
39
39
40 assert_equal [1, 2], c.issue_ids.sort
40 assert_equal [1, 2], c.issue_ids.sort
41 fixed = Issue.find(1)
41 fixed = Issue.find(1)
42 assert fixed.closed?
42 assert fixed.closed?
43 assert_equal 90, fixed.done_ratio
43 assert_equal 90, fixed.done_ratio
44 assert_equal 1, ActionMailer::Base.deliveries.size
44 assert_equal 1, ActionMailer::Base.deliveries.size
45 end
45 end
46
46
47 def test_ref_keywords
47 def test_ref_keywords
48 Setting.commit_ref_keywords = 'refs'
48 Setting.commit_ref_keywords = 'refs'
49 Setting.commit_fix_keywords = ''
49 Setting.commit_fix_keywords = ''
50
50
51 c = Changeset.new(:repository => Project.find(1).repository,
51 c = Changeset.new(:repository => Project.find(1).repository,
52 :committed_on => Time.now,
52 :committed_on => Time.now,
53 :comments => 'Ignores #2. Refs #1')
53 :comments => 'Ignores #2. Refs #1')
54 c.scan_comment_for_issue_ids
54 c.scan_comment_for_issue_ids
55
55
56 assert_equal [1], c.issue_ids.sort
56 assert_equal [1], c.issue_ids.sort
57 end
57 end
58
58
59 def test_ref_keywords_any_only
59 def test_ref_keywords_any_only
60 Setting.commit_ref_keywords = '*'
60 Setting.commit_ref_keywords = '*'
61 Setting.commit_fix_keywords = ''
61 Setting.commit_fix_keywords = ''
62
62
63 c = Changeset.new(:repository => Project.find(1).repository,
63 c = Changeset.new(:repository => Project.find(1).repository,
64 :committed_on => Time.now,
64 :committed_on => Time.now,
65 :comments => 'Ignores #2. Refs #1')
65 :comments => 'Ignores #2. Refs #1')
66 c.scan_comment_for_issue_ids
66 c.scan_comment_for_issue_ids
67
67
68 assert_equal [1, 2], c.issue_ids.sort
68 assert_equal [1, 2], c.issue_ids.sort
69 end
69 end
70
70
71 def test_ref_keywords_any_with_timelog
71 def test_ref_keywords_any_with_timelog
72 Setting.commit_ref_keywords = '*'
72 Setting.commit_ref_keywords = '*'
73 Setting.commit_logtime_enabled = '1'
73 Setting.commit_logtime_enabled = '1'
74
74
75 {
75 {
76 '2' => 2.0,
76 '2' => 2.0,
77 '2h' => 2.0,
77 '2h' => 2.0,
78 '2hours' => 2.0,
78 '2hours' => 2.0,
79 '15m' => 0.25,
79 '15m' => 0.25,
80 '15min' => 0.25,
80 '15min' => 0.25,
81 '3h15' => 3.25,
81 '3h15' => 3.25,
82 '3h15m' => 3.25,
82 '3h15m' => 3.25,
83 '3h15min' => 3.25,
83 '3h15min' => 3.25,
84 '3:15' => 3.25,
84 '3:15' => 3.25,
85 '3.25' => 3.25,
85 '3.25' => 3.25,
86 '3.25h' => 3.25,
86 '3.25h' => 3.25,
87 '3,25' => 3.25,
87 '3,25' => 3.25,
88 '3,25h' => 3.25,
88 '3,25h' => 3.25,
89 }.each do |syntax, expected_hours|
89 }.each do |syntax, expected_hours|
90 c = Changeset.new(:repository => Project.find(1).repository,
90 c = Changeset.new(:repository => Project.find(1).repository,
91 :committed_on => 24.hours.ago,
91 :committed_on => 24.hours.ago,
92 :comments => "Worked on this issue #1 @#{syntax}",
92 :comments => "Worked on this issue #1 @#{syntax}",
93 :revision => '520',
93 :revision => '520',
94 :user => User.find(2))
94 :user => User.find(2))
95 assert_difference 'TimeEntry.count' do
95 assert_difference 'TimeEntry.count' do
96 c.scan_comment_for_issue_ids
96 c.scan_comment_for_issue_ids
97 end
97 end
98 assert_equal [1], c.issue_ids.sort
98 assert_equal [1], c.issue_ids.sort
99
99
100 time = TimeEntry.first(:order => 'id desc')
100 time = TimeEntry.first(:order => 'id desc')
101 assert_equal 1, time.issue_id
101 assert_equal 1, time.issue_id
102 assert_equal 1, time.project_id
102 assert_equal 1, time.project_id
103 assert_equal 2, time.user_id
103 assert_equal 2, time.user_id
104 assert_equal expected_hours, time.hours, "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}"
104 assert_equal expected_hours, time.hours, "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}"
105 assert_equal Date.yesterday, time.spent_on
105 assert_equal Date.yesterday, time.spent_on
106 assert time.activity.is_default?
106 assert time.activity.is_default?
107 assert time.comments.include?('r520'), "r520 was expected in time_entry comments: #{time.comments}"
107 assert time.comments.include?('r520'), "r520 was expected in time_entry comments: #{time.comments}"
108 end
108 end
109 end
109 end
110
110
111 def test_ref_keywords_closing_with_timelog
111 def test_ref_keywords_closing_with_timelog
112 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
112 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
113 Setting.commit_ref_keywords = '*'
113 Setting.commit_ref_keywords = '*'
114 Setting.commit_fix_keywords = 'fixes , closes'
114 Setting.commit_fix_keywords = 'fixes , closes'
115 Setting.commit_logtime_enabled = '1'
115 Setting.commit_logtime_enabled = '1'
116
116
117 c = Changeset.new(:repository => Project.find(1).repository,
117 c = Changeset.new(:repository => Project.find(1).repository,
118 :committed_on => Time.now,
118 :committed_on => Time.now,
119 :comments => 'This is a comment. Fixes #1 @4.5, #2 @1',
119 :comments => 'This is a comment. Fixes #1 @4.5, #2 @1',
120 :user => User.find(2))
120 :user => User.find(2))
121 assert_difference 'TimeEntry.count', 2 do
121 assert_difference 'TimeEntry.count', 2 do
122 c.scan_comment_for_issue_ids
122 c.scan_comment_for_issue_ids
123 end
123 end
124
124
125 assert_equal [1, 2], c.issue_ids.sort
125 assert_equal [1, 2], c.issue_ids.sort
126 assert Issue.find(1).closed?
126 assert Issue.find(1).closed?
127 assert Issue.find(2).closed?
127 assert Issue.find(2).closed?
128
128
129 times = TimeEntry.all(:order => 'id desc', :limit => 2)
129 times = TimeEntry.all(:order => 'id desc', :limit => 2)
130 assert_equal [1, 2], times.collect(&:issue_id).sort
130 assert_equal [1, 2], times.collect(&:issue_id).sort
131 end
131 end
132
132
133 def test_ref_keywords_any_line_start
133 def test_ref_keywords_any_line_start
134 Setting.commit_ref_keywords = '*'
134 Setting.commit_ref_keywords = '*'
135
135
136 c = Changeset.new(:repository => Project.find(1).repository,
136 c = Changeset.new(:repository => Project.find(1).repository,
137 :committed_on => Time.now,
137 :committed_on => Time.now,
138 :comments => '#1 is the reason of this commit')
138 :comments => '#1 is the reason of this commit')
139 c.scan_comment_for_issue_ids
139 c.scan_comment_for_issue_ids
140
140
141 assert_equal [1], c.issue_ids.sort
141 assert_equal [1], c.issue_ids.sort
142 end
142 end
143
143
144 def test_ref_keywords_allow_brackets_around_a_issue_number
144 def test_ref_keywords_allow_brackets_around_a_issue_number
145 Setting.commit_ref_keywords = '*'
145 Setting.commit_ref_keywords = '*'
146
146
147 c = Changeset.new(:repository => Project.find(1).repository,
147 c = Changeset.new(:repository => Project.find(1).repository,
148 :committed_on => Time.now,
148 :committed_on => Time.now,
149 :comments => '[#1] Worked on this issue')
149 :comments => '[#1] Worked on this issue')
150 c.scan_comment_for_issue_ids
150 c.scan_comment_for_issue_ids
151
151
152 assert_equal [1], c.issue_ids.sort
152 assert_equal [1], c.issue_ids.sort
153 end
153 end
154
154
155 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
155 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
156 Setting.commit_ref_keywords = '*'
156 Setting.commit_ref_keywords = '*'
157
157
158 c = Changeset.new(:repository => Project.find(1).repository,
158 c = Changeset.new(:repository => Project.find(1).repository,
159 :committed_on => Time.now,
159 :committed_on => Time.now,
160 :comments => '[#1 #2, #3] Worked on these')
160 :comments => '[#1 #2, #3] Worked on these')
161 c.scan_comment_for_issue_ids
161 c.scan_comment_for_issue_ids
162
162
163 assert_equal [1,2,3], c.issue_ids.sort
163 assert_equal [1,2,3], c.issue_ids.sort
164 end
164 end
165
165
166 def test_commit_referencing_a_subproject_issue
166 def test_commit_referencing_a_subproject_issue
167 c = Changeset.new(:repository => Project.find(1).repository,
167 c = Changeset.new(:repository => Project.find(1).repository,
168 :committed_on => Time.now,
168 :committed_on => Time.now,
169 :comments => 'refs #5, a subproject issue')
169 :comments => 'refs #5, a subproject issue')
170 c.scan_comment_for_issue_ids
170 c.scan_comment_for_issue_ids
171
171
172 assert_equal [5], c.issue_ids.sort
172 assert_equal [5], c.issue_ids.sort
173 assert c.issues.first.project != c.project
173 assert c.issues.first.project != c.project
174 end
174 end
175
175
176 def test_commit_referencing_a_parent_project_issue
176 def test_commit_referencing_a_parent_project_issue
177 # repository of child project
177 # repository of child project
178 r = Repository::Subversion.create!(:project => Project.find(3), :url => 'svn://localhost/test')
178 r = Repository::Subversion.create!(
179 :project => Project.find(3),
180 :url => 'svn://localhost/test')
179
181
180 c = Changeset.new(:repository => r,
182 c = Changeset.new(:repository => r,
181 :committed_on => Time.now,
183 :committed_on => Time.now,
182 :comments => 'refs #2, an issue of a parent project')
184 :comments => 'refs #2, an issue of a parent project')
183 c.scan_comment_for_issue_ids
185 c.scan_comment_for_issue_ids
184
186
185 assert_equal [2], c.issue_ids.sort
187 assert_equal [2], c.issue_ids.sort
186 assert c.issues.first.project != c.project
188 assert c.issues.first.project != c.project
187 end
189 end
188
190
189 def test_text_tag_revision
191 def test_text_tag_revision
190 c = Changeset.new(:revision => '520')
192 c = Changeset.new(:revision => '520')
191 assert_equal 'r520', c.text_tag
193 assert_equal 'r520', c.text_tag
192 end
194 end
193
195
194 def test_text_tag_hash
196 def test_text_tag_hash
195 c = Changeset.new(:scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518', :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
197 c = Changeset.new(
198 :scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518',
199 :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
196 assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
200 assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
197 end
201 end
198
202
199 def test_text_tag_hash_all_number
203 def test_text_tag_hash_all_number
200 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
204 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
201 assert_equal 'commit:0123456789', c.text_tag
205 assert_equal 'commit:0123456789', c.text_tag
202 end
206 end
203
207
204 def test_previous
208 def test_previous
205 changeset = Changeset.find_by_revision('3')
209 changeset = Changeset.find_by_revision('3')
206 assert_equal Changeset.find_by_revision('2'), changeset.previous
210 assert_equal Changeset.find_by_revision('2'), changeset.previous
207 end
211 end
208
212
209 def test_previous_nil
213 def test_previous_nil
210 changeset = Changeset.find_by_revision('1')
214 changeset = Changeset.find_by_revision('1')
211 assert_nil changeset.previous
215 assert_nil changeset.previous
212 end
216 end
213
217
214 def test_next
218 def test_next
215 changeset = Changeset.find_by_revision('2')
219 changeset = Changeset.find_by_revision('2')
216 assert_equal Changeset.find_by_revision('3'), changeset.next
220 assert_equal Changeset.find_by_revision('3'), changeset.next
217 end
221 end
218
222
219 def test_next_nil
223 def test_next_nil
220 changeset = Changeset.find_by_revision('10')
224 changeset = Changeset.find_by_revision('10')
221 assert_nil changeset.next
225 assert_nil changeset.next
222 end
226 end
223
227
224 def test_comments_should_be_converted_to_utf8
228 def test_comments_should_be_converted_to_utf8
225 proj = Project.find(3)
229 proj = Project.find(3)
226 str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
230 str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
227 r = Repository::Bazaar.create!(
231 r = Repository::Bazaar.create!(
228 :project => proj, :url => '/tmp/test/bazaar',
232 :project => proj, :url => '/tmp/test/bazaar',
229 :log_encoding => 'ISO-8859-1' )
233 :log_encoding => 'ISO-8859-1' )
230 assert r
234 assert r
231 c = Changeset.new(:repository => r,
235 c = Changeset.new(:repository => r,
232 :committed_on => Time.now,
236 :committed_on => Time.now,
233 :revision => '123',
237 :revision => '123',
234 :scmid => '12345',
238 :scmid => '12345',
235 :comments => str)
239 :comments => str)
236 assert( c.save )
240 assert( c.save )
237 assert_equal "Texte encodΓ© en ISO-8859-1.", c.comments
241 assert_equal "Texte encodΓ© en ISO-8859-1.", c.comments
238 end
242 end
239
243
240 def test_invalid_utf8_sequences_in_comments_should_be_stripped
244 def test_invalid_utf8_sequences_in_comments_should_be_stripped
241 proj = Project.find(3)
245 proj = Project.find(3)
242 str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
246 str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
243 r = Repository::Bazaar.create!(
247 r = Repository::Bazaar.create!(
244 :project => proj, :url => '/tmp/test/bazaar',
248 :project => proj, :url => '/tmp/test/bazaar',
245 :log_encoding => 'UTF-8' )
249 :log_encoding => 'UTF-8' )
246 assert r
250 assert r
247 c = Changeset.new(:repository => r,
251 c = Changeset.new(:repository => r,
248 :committed_on => Time.now,
252 :committed_on => Time.now,
249 :revision => '123',
253 :revision => '123',
250 :scmid => '12345',
254 :scmid => '12345',
251 :comments => str)
255 :comments => str)
252 assert( c.save )
256 assert( c.save )
253 if str.respond_to?(:force_encoding)
257 if str.respond_to?(:force_encoding)
254 assert_equal "Texte encod? en ISO-8859-1.", c.comments
258 assert_equal "Texte encod? en ISO-8859-1.", c.comments
255 else
259 else
256 assert_equal "Texte encod en ISO-8859-1.", c.comments
260 assert_equal "Texte encod en ISO-8859-1.", c.comments
257 end
261 end
258 end
262 end
259
263
260 def test_comments_should_be_converted_all_latin1_to_utf8
264 def test_comments_should_be_converted_all_latin1_to_utf8
261 s1 = "\xC2\x80"
265 s1 = "\xC2\x80"
262 s2 = "\xc3\x82\xc2\x80"
266 s2 = "\xc3\x82\xc2\x80"
263 if s1.respond_to?(:force_encoding)
267 if s1.respond_to?(:force_encoding)
264 s3 = s1.dup
268 s3 = s1.dup
265 s4 = s2.dup
269 s4 = s2.dup
266 s1.force_encoding('ASCII-8BIT')
270 s1.force_encoding('ASCII-8BIT')
267 s2.force_encoding('ASCII-8BIT')
271 s2.force_encoding('ASCII-8BIT')
268 s3.force_encoding('ISO-8859-1')
272 s3.force_encoding('ISO-8859-1')
269 s4.force_encoding('UTF-8')
273 s4.force_encoding('UTF-8')
270 assert_equal s3.encode('UTF-8'), s4
274 assert_equal s3.encode('UTF-8'), s4
271 end
275 end
272 proj = Project.find(3)
276 proj = Project.find(3)
273 r = Repository::Bazaar.create!(
277 r = Repository::Bazaar.create!(
274 :project => proj, :url => '/tmp/test/bazaar',
278 :project => proj, :url => '/tmp/test/bazaar',
275 :log_encoding => 'ISO-8859-1' )
279 :log_encoding => 'ISO-8859-1' )
276 assert r
280 assert r
277 c = Changeset.new(:repository => r,
281 c = Changeset.new(:repository => r,
278 :committed_on => Time.now,
282 :committed_on => Time.now,
279 :revision => '123',
283 :revision => '123',
280 :scmid => '12345',
284 :scmid => '12345',
281 :comments => s1)
285 :comments => s1)
282 assert( c.save )
286 assert( c.save )
283 assert_equal s2, c.comments
287 assert_equal s2, c.comments
284 end
288 end
285
289
286 def test_identifier
290 def test_identifier
287 c = Changeset.find_by_revision('1')
291 c = Changeset.find_by_revision('1')
288 assert_equal c.revision, c.identifier
292 assert_equal c.revision, c.identifier
289 end
293 end
290 end
294 end
General Comments 0
You need to be logged in to leave comments. Login now