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

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