You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

version_test.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class VersionTest < ActiveSupport::TestCase
  19. fixtures :projects, :users, :issues, :issue_statuses, :trackers,
  20. :enumerations, :versions, :projects_trackers,
  21. :custom_fields, :custom_fields_trackers, :custom_fields_projects
  22. def setup
  23. User.current = nil
  24. end
  25. def test_create
  26. v = Version.new(:project => Project.find(1), :name => '1.1',
  27. :effective_date => '2011-03-25')
  28. assert v.save
  29. assert_equal 'open', v.status
  30. assert_equal 'none', v.sharing
  31. end
  32. def test_create_as_default_project_version
  33. project = Project.find(1)
  34. v = Version.new(:project => project, :name => '1.1',
  35. :default_project_version => '1')
  36. assert v.save
  37. assert_equal v, project.reload.default_version
  38. end
  39. def test_create_not_as_default_project_version
  40. project = Project.find(1)
  41. v = Version.new(:project => project, :name => '1.1',
  42. :default_project_version => '0')
  43. assert v.save
  44. assert_nil project.reload.default_version
  45. end
  46. def test_invalid_effective_date_validation
  47. v = Version.new(:project => Project.find(1), :name => '1.1',
  48. :effective_date => '99999-01-01')
  49. assert !v.valid?
  50. v.effective_date = '2012-11-33'
  51. assert !v.valid?
  52. v.effective_date = '2012-31-11'
  53. assert !v.valid?
  54. v.effective_date = '-2012-31-11'
  55. assert !v.valid?
  56. v.effective_date = 'ABC'
  57. assert !v.valid?
  58. assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
  59. v.errors[:effective_date]
  60. end
  61. def test_progress_should_be_0_with_no_assigned_issues
  62. project = Project.find(1)
  63. v = Version.create!(:project => project, :name => 'Progress')
  64. assert_equal 0, v.completed_percent
  65. assert_equal 0, v.closed_percent
  66. end
  67. def test_progress_should_be_0_with_unbegun_assigned_issues
  68. project = Project.find(1)
  69. v = Version.create!(:project => project, :name => 'Progress')
  70. add_issue(v)
  71. add_issue(v, :done_ratio => 0)
  72. assert_progress_equal 0, v.completed_percent
  73. assert_progress_equal 0, v.closed_percent
  74. end
  75. def test_progress_should_be_100_with_closed_assigned_issues
  76. project = Project.find(1)
  77. status = IssueStatus.where(:is_closed => true).first
  78. v = Version.create!(:project => project, :name => 'Progress')
  79. add_issue(v, :status => status)
  80. add_issue(v, :status => status, :done_ratio => 20)
  81. add_issue(v, :status => status, :done_ratio => 70, :estimated_hours => 25)
  82. add_issue(v, :status => status, :estimated_hours => 15)
  83. assert_progress_equal 100.0, v.completed_percent
  84. assert_progress_equal 100.0, v.closed_percent
  85. end
  86. def test_progress_should_consider_done_ratio_of_open_assigned_issues
  87. project = Project.find(1)
  88. v = Version.create!(:project => project, :name => 'Progress')
  89. add_issue(v)
  90. add_issue(v, :done_ratio => 20)
  91. add_issue(v, :done_ratio => 70)
  92. assert_progress_equal (0.0 + 20.0 + 70.0)/3, v.completed_percent
  93. assert_progress_equal 0, v.closed_percent
  94. end
  95. def test_progress_should_consider_closed_issues_as_completed
  96. project = Project.find(1)
  97. v = Version.create!(:project => project, :name => 'Progress')
  98. add_issue(v)
  99. add_issue(v, :done_ratio => 20)
  100. add_issue(v, :status => IssueStatus.where(:is_closed => true).first)
  101. assert_progress_equal (0.0 + 20.0 + 100.0)/3, v.completed_percent
  102. assert_progress_equal (100.0)/3, v.closed_percent
  103. end
  104. def test_progress_should_consider_estimated_hours_to_weight_issues
  105. project = Project.find(1)
  106. v = Version.create!(:project => project, :name => 'Progress')
  107. add_issue(v, :estimated_hours => 10)
  108. add_issue(v, :estimated_hours => 20, :done_ratio => 30)
  109. add_issue(v, :estimated_hours => 40, :done_ratio => 10)
  110. add_issue(v, :estimated_hours => 25, :status => IssueStatus.where(:is_closed => true).first)
  111. assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_percent
  112. assert_progress_equal 25.0/95.0*100, v.closed_percent
  113. end
  114. def test_progress_should_consider_average_estimated_hours_to_weight_unestimated_issues
  115. project = Project.find(1)
  116. v = Version.create!(:project => project, :name => 'Progress')
  117. add_issue(v, :done_ratio => 20)
  118. add_issue(v, :status => IssueStatus.where(:is_closed => true).first)
  119. add_issue(v, :estimated_hours => 10, :done_ratio => 30)
  120. add_issue(v, :estimated_hours => 40, :done_ratio => 10)
  121. assert_progress_equal (25.0*0.2 + 25.0*1 + 10.0*0.3 + 40.0*0.1)/100.0*100, v.completed_percent
  122. assert_progress_equal 25.0/100.0*100, v.closed_percent
  123. end
  124. def test_should_sort_scheduled_then_unscheduled_versions
  125. Version.delete_all
  126. v4 = Version.create!(:project_id => 1, :name => 'v4')
  127. v3 = Version.create!(:project_id => 1, :name => 'v2', :effective_date => '2012-07-14')
  128. v2 = Version.create!(:project_id => 1, :name => 'v1')
  129. v1 = Version.create!(:project_id => 1, :name => 'v3', :effective_date => '2012-08-02')
  130. v5 = Version.create!(:project_id => 1, :name => 'v5', :effective_date => '2012-07-02')
  131. assert_equal [v5, v3, v1, v2, v4], [v1, v2, v3, v4, v5].sort
  132. assert_equal [v5, v3, v1, v2, v4], Version.sorted.to_a
  133. end
  134. def test_should_sort_versions_with_same_date_by_name
  135. v1 = Version.new(:effective_date => '2014-12-03', :name => 'v2')
  136. v2 = Version.new(:effective_date => '2014-12-03', :name => 'v1')
  137. assert_equal [v2, v1], [v1, v2].sort
  138. end
  139. def test_completed_should_be_false_when_due_today
  140. version = Version.create!(:project_id => 1, :effective_date => Date.today, :name => 'Due today')
  141. assert_equal false, version.completed?
  142. end
  143. def test_completed_should_be_true_when_closed
  144. version = Version.create!(:project_id => 1, :status => 'closed', :name => 'Closed')
  145. assert_equal true, version.completed?
  146. end
  147. test "#behind_schedule? should be false if there are no issues assigned" do
  148. version = Version.generate!(:effective_date => Date.yesterday)
  149. assert_equal false, version.behind_schedule?
  150. end
  151. test "#behind_schedule? should be false if there is no effective_date" do
  152. version = Version.generate!(:effective_date => nil)
  153. assert_equal false, version.behind_schedule?
  154. end
  155. test "#behind_schedule? should be false if all of the issues are ahead of schedule" do
  156. version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
  157. add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  158. add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  159. assert_equal 60, version.completed_percent
  160. assert_equal false, version.behind_schedule?
  161. end
  162. test "#behind_schedule? should be true if any of the issues are behind schedule" do
  163. version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
  164. add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  165. add_issue(version, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left
  166. assert_equal 40, version.completed_percent
  167. assert_equal true, version.behind_schedule?
  168. end
  169. test "#behind_schedule? should be false if all of the issues are complete" do
  170. version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
  171. add_issue(version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
  172. add_issue(version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
  173. assert_equal 100, version.completed_percent
  174. assert_equal false, version.behind_schedule?
  175. end
  176. test "#estimated_hours should return 0 with no assigned issues" do
  177. version = Version.generate!
  178. assert_equal 0, version.estimated_hours
  179. end
  180. test "#estimated_hours should return 0 with no estimated hours" do
  181. version = Version.create!(:project_id => 1, :name => 'test')
  182. add_issue(version)
  183. assert_equal 0, version.estimated_hours
  184. end
  185. test "#estimated_hours should return return the sum of estimated hours" do
  186. version = Version.create!(:project_id => 1, :name => 'test')
  187. add_issue(version, :estimated_hours => 2.5)
  188. add_issue(version, :estimated_hours => 5)
  189. assert_equal 7.5, version.estimated_hours
  190. end
  191. test "#estimated_hours should return the sum of leaves estimated hours" do
  192. version = Version.create!(:project_id => 1, :name => 'test')
  193. parent = add_issue(version)
  194. add_issue(version, :estimated_hours => 2.5, :parent_issue_id => parent.id)
  195. add_issue(version, :estimated_hours => 5, :parent_issue_id => parent.id)
  196. assert_equal 7.5, version.estimated_hours
  197. end
  198. test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do
  199. User.current = User.find(1) # Need the admin's permissions
  200. @version = Version.find(7)
  201. # Separate hierarchy
  202. project_1_issue = Issue.find(1)
  203. project_1_issue.fixed_version = @version
  204. assert project_1_issue.save, project_1_issue.errors.full_messages.to_s
  205. project_5_issue = Issue.find(6)
  206. project_5_issue.fixed_version = @version
  207. assert project_5_issue.save
  208. # Project
  209. project_2_issue = Issue.find(4)
  210. project_2_issue.fixed_version = @version
  211. assert project_2_issue.save
  212. # Update the sharing
  213. @version.sharing = 'none'
  214. assert @version.save
  215. # Project 1 now out of the shared scope
  216. project_1_issue.reload
  217. assert_nil project_1_issue.fixed_version,
  218. "Fixed version is still set after changing the Version's sharing"
  219. # Project 5 now out of the shared scope
  220. project_5_issue.reload
  221. assert_nil project_5_issue.fixed_version,
  222. "Fixed version is still set after changing the Version's sharing"
  223. # Project 2 issue remains
  224. project_2_issue.reload
  225. assert_equal @version, project_2_issue.fixed_version
  226. end
  227. def test_deletable_should_return_true_when_not_referenced
  228. version = Version.generate!
  229. assert_equal true, version.deletable?
  230. end
  231. def test_deletable_should_return_false_when_referenced_by_an_issue
  232. version = Version.generate!
  233. Issue.generate!(:fixed_version => version)
  234. assert_equal false, version.deletable?
  235. end
  236. def test_deletable_should_return_false_when_referenced_by_a_custom_field
  237. version = Version.generate!
  238. field = IssueCustomField.generate!(:field_format => 'version')
  239. value = CustomValue.create!(:custom_field => field, :customized => Issue.first, :value => version.id)
  240. assert_equal false, version.deletable?
  241. end
  242. def test_like_scope
  243. version = Version.create!(:project => Project.find(1), :name => 'Version for like scope test')
  244. assert_includes Version.like('VERSION FOR LIKE SCOPE TEST'), version
  245. assert_includes Version.like('version for like scope test'), version
  246. assert_includes Version.like('like scope'), version
  247. end
  248. private
  249. def add_issue(version, attributes={})
  250. Issue.create!({:project => version.project,
  251. :fixed_version => version,
  252. :subject => 'Test',
  253. :author => User.first,
  254. :tracker => version.project.trackers.first}.merge(attributes))
  255. end
  256. def assert_progress_equal(expected_float, actual_float, message="")
  257. assert_in_delta(expected_float, actual_float, 0.000001, message="")
  258. end
  259. end