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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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, :enumerations, :versions, :projects_trackers
  20. def setup
  21. end
  22. def test_create
  23. v = Version.new(:project => Project.find(1), :name => '1.1', :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_invalid_effective_date_validation
  29. v = Version.new(:project => Project.find(1), :name => '1.1', :effective_date => '99999-01-01')
  30. assert !v.valid?
  31. v.effective_date = '2012-11-33'
  32. assert !v.valid?
  33. v.effective_date = '2012-31-11'
  34. assert !v.valid?
  35. v.effective_date = '-2012-31-11'
  36. assert !v.valid?
  37. v.effective_date = 'ABC'
  38. assert !v.valid?
  39. assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
  40. v.errors[:effective_date]
  41. end
  42. def test_progress_should_be_0_with_no_assigned_issues
  43. project = Project.find(1)
  44. v = Version.create!(:project => project, :name => 'Progress')
  45. assert_equal 0, v.completed_percent
  46. assert_equal 0, v.closed_percent
  47. end
  48. def test_progress_should_be_0_with_unbegun_assigned_issues
  49. project = Project.find(1)
  50. v = Version.create!(:project => project, :name => 'Progress')
  51. add_issue(v)
  52. add_issue(v, :done_ratio => 0)
  53. assert_progress_equal 0, v.completed_percent
  54. assert_progress_equal 0, v.closed_percent
  55. end
  56. def test_progress_should_be_100_with_closed_assigned_issues
  57. project = Project.find(1)
  58. status = IssueStatus.where(:is_closed => true).first
  59. v = Version.create!(:project => project, :name => 'Progress')
  60. add_issue(v, :status => status)
  61. add_issue(v, :status => status, :done_ratio => 20)
  62. add_issue(v, :status => status, :done_ratio => 70, :estimated_hours => 25)
  63. add_issue(v, :status => status, :estimated_hours => 15)
  64. assert_progress_equal 100.0, v.completed_percent
  65. assert_progress_equal 100.0, v.closed_percent
  66. end
  67. def test_progress_should_consider_done_ratio_of_open_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 => 20)
  72. add_issue(v, :done_ratio => 70)
  73. assert_progress_equal (0.0 + 20.0 + 70.0)/3, v.completed_percent
  74. assert_progress_equal 0, v.closed_percent
  75. end
  76. def test_progress_should_consider_closed_issues_as_completed
  77. project = Project.find(1)
  78. v = Version.create!(:project => project, :name => 'Progress')
  79. add_issue(v)
  80. add_issue(v, :done_ratio => 20)
  81. add_issue(v, :status => IssueStatus.where(:is_closed => true).first)
  82. assert_progress_equal (0.0 + 20.0 + 100.0)/3, v.completed_percent
  83. assert_progress_equal (100.0)/3, v.closed_percent
  84. end
  85. def test_progress_should_consider_estimated_hours_to_weigth_issues
  86. project = Project.find(1)
  87. v = Version.create!(:project => project, :name => 'Progress')
  88. add_issue(v, :estimated_hours => 10)
  89. add_issue(v, :estimated_hours => 20, :done_ratio => 30)
  90. add_issue(v, :estimated_hours => 40, :done_ratio => 10)
  91. add_issue(v, :estimated_hours => 25, :status => IssueStatus.where(:is_closed => true).first)
  92. assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_percent
  93. assert_progress_equal 25.0/95.0*100, v.closed_percent
  94. end
  95. def test_progress_should_consider_average_estimated_hours_to_weigth_unestimated_issues
  96. project = Project.find(1)
  97. v = Version.create!(:project => project, :name => 'Progress')
  98. add_issue(v, :done_ratio => 20)
  99. add_issue(v, :status => IssueStatus.where(:is_closed => true).first)
  100. add_issue(v, :estimated_hours => 10, :done_ratio => 30)
  101. add_issue(v, :estimated_hours => 40, :done_ratio => 10)
  102. assert_progress_equal (25.0*0.2 + 25.0*1 + 10.0*0.3 + 40.0*0.1)/100.0*100, v.completed_percent
  103. assert_progress_equal 25.0/100.0*100, v.closed_percent
  104. end
  105. def test_should_sort_scheduled_then_unscheduled_versions
  106. Version.delete_all
  107. v4 = Version.create!(:project_id => 1, :name => 'v4')
  108. v3 = Version.create!(:project_id => 1, :name => 'v2', :effective_date => '2012-07-14')
  109. v2 = Version.create!(:project_id => 1, :name => 'v1')
  110. v1 = Version.create!(:project_id => 1, :name => 'v3', :effective_date => '2012-08-02')
  111. v5 = Version.create!(:project_id => 1, :name => 'v5', :effective_date => '2012-07-02')
  112. assert_equal [v5, v3, v1, v2, v4], [v1, v2, v3, v4, v5].sort
  113. assert_equal [v5, v3, v1, v2, v4], Version.sorted.all
  114. end
  115. def test_completed_should_be_false_when_due_today
  116. version = Version.create!(:project_id => 1, :effective_date => Date.today, :name => 'Due today')
  117. assert_equal false, version.completed?
  118. end
  119. context "#behind_schedule?" do
  120. setup do
  121. ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
  122. @project = Project.create!(:name => 'test0', :identifier => 'test0')
  123. @project.trackers << Tracker.create!(:name => 'track')
  124. @version = Version.create!(:project => @project, :effective_date => nil, :name => 'version')
  125. end
  126. should "be false if there are no issues assigned" do
  127. @version.update_attribute(:effective_date, Date.yesterday)
  128. assert_equal false, @version.behind_schedule?
  129. end
  130. should "be false if there is no effective_date" do
  131. assert_equal false, @version.behind_schedule?
  132. end
  133. should "be false if all of the issues are ahead of schedule" do
  134. @version.update_attribute(:effective_date, 7.days.from_now.to_date)
  135. add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  136. add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  137. assert_equal 60, @version.completed_percent
  138. assert_equal false, @version.behind_schedule?
  139. end
  140. should "be true if any of the issues are behind schedule" do
  141. @version.update_attribute(:effective_date, 7.days.from_now.to_date)
  142. add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  143. add_issue(@version, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left
  144. assert_equal 40, @version.completed_percent
  145. assert_equal true, @version.behind_schedule?
  146. end
  147. should "be false if all of the issues are complete" do
  148. @version.update_attribute(:effective_date, 7.days.from_now.to_date)
  149. add_issue(@version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
  150. add_issue(@version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
  151. assert_equal 100, @version.completed_percent
  152. assert_equal false, @version.behind_schedule?
  153. end
  154. end
  155. context "#estimated_hours" do
  156. setup do
  157. @version = Version.create!(:project_id => 1, :name => '#estimated_hours')
  158. end
  159. should "return 0 with no assigned issues" do
  160. assert_equal 0, @version.estimated_hours
  161. end
  162. should "return 0 with no estimated hours" do
  163. add_issue(@version)
  164. assert_equal 0, @version.estimated_hours
  165. end
  166. should "return the sum of estimated hours" do
  167. add_issue(@version, :estimated_hours => 2.5)
  168. add_issue(@version, :estimated_hours => 5)
  169. assert_equal 7.5, @version.estimated_hours
  170. end
  171. should "return the sum of leaves estimated hours" do
  172. parent = add_issue(@version)
  173. add_issue(@version, :estimated_hours => 2.5, :parent_issue_id => parent.id)
  174. add_issue(@version, :estimated_hours => 5, :parent_issue_id => parent.id)
  175. assert_equal 7.5, @version.estimated_hours
  176. end
  177. end
  178. test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do
  179. User.current = User.find(1) # Need the admin's permissions
  180. @version = Version.find(7)
  181. # Separate hierarchy
  182. project_1_issue = Issue.find(1)
  183. project_1_issue.fixed_version = @version
  184. assert project_1_issue.save, project_1_issue.errors.full_messages.to_s
  185. project_5_issue = Issue.find(6)
  186. project_5_issue.fixed_version = @version
  187. assert project_5_issue.save
  188. # Project
  189. project_2_issue = Issue.find(4)
  190. project_2_issue.fixed_version = @version
  191. assert project_2_issue.save
  192. # Update the sharing
  193. @version.sharing = 'none'
  194. assert @version.save
  195. # Project 1 now out of the shared scope
  196. project_1_issue.reload
  197. assert_equal nil, project_1_issue.fixed_version, "Fixed version is still set after changing the Version's sharing"
  198. # Project 5 now out of the shared scope
  199. project_5_issue.reload
  200. assert_equal nil, project_5_issue.fixed_version, "Fixed version is still set after changing the Version's sharing"
  201. # Project 2 issue remains
  202. project_2_issue.reload
  203. assert_equal @version, project_2_issue.fixed_version
  204. end
  205. private
  206. def add_issue(version, attributes={})
  207. Issue.create!({:project => version.project,
  208. :fixed_version => version,
  209. :subject => 'Test',
  210. :author => User.first,
  211. :tracker => version.project.trackers.first}.merge(attributes))
  212. end
  213. def assert_progress_equal(expected_float, actual_float, message="")
  214. assert_in_delta(expected_float, actual_float, 0.000001, message="")
  215. end
  216. end