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.

time_entry_test.rb 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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 TimeEntryTest < ActiveSupport::TestCase
  19. fixtures :issues, :projects, :users, :time_entries,
  20. :members, :roles, :member_roles, :auth_sources,
  21. :trackers, :issue_statuses,
  22. :projects_trackers,
  23. :journals, :journal_details,
  24. :issue_categories, :enumerations,
  25. :groups_users,
  26. :enabled_modules,
  27. :workflows
  28. def test_hours_format
  29. assertions = { "2" => 2.0,
  30. "21.1" => 21.1,
  31. "2,1" => 2.1,
  32. "1,5h" => 1.5,
  33. "7:12" => 7.2,
  34. "10h" => 10.0,
  35. "10 h" => 10.0,
  36. "45m" => 0.75,
  37. "45 m" => 0.75,
  38. "3h15" => 3.25,
  39. "3h 15" => 3.25,
  40. "3 h 15" => 3.25,
  41. "3 h 15m" => 3.25,
  42. "3 h 15 m" => 3.25,
  43. "3 hours" => 3.0,
  44. "12min" => 0.2,
  45. }
  46. assertions.each do |k, v|
  47. t = TimeEntry.new(:hours => k)
  48. assert_equal v, t.hours, "Converting #{k} failed:"
  49. end
  50. end
  51. def test_hours_should_default_to_nil
  52. assert_nil TimeEntry.new.hours
  53. end
  54. def test_spent_on_with_blank
  55. c = TimeEntry.new
  56. c.spent_on = ''
  57. assert_nil c.spent_on
  58. end
  59. def test_spent_on_with_nil
  60. c = TimeEntry.new
  61. c.spent_on = nil
  62. assert_nil c.spent_on
  63. end
  64. def test_spent_on_with_string
  65. c = TimeEntry.new
  66. c.spent_on = "2011-01-14"
  67. assert_equal Date.parse("2011-01-14"), c.spent_on
  68. end
  69. def test_spent_on_with_invalid_string
  70. c = TimeEntry.new
  71. c.spent_on = "foo"
  72. assert_nil c.spent_on
  73. end
  74. def test_spent_on_with_date
  75. c = TimeEntry.new
  76. c.spent_on = Date.today
  77. assert_equal Date.today, c.spent_on
  78. end
  79. def test_spent_on_with_time
  80. c = TimeEntry.new
  81. c.spent_on = Time.now
  82. assert_equal Date.today, c.spent_on
  83. end
  84. def test_validate_time_entry
  85. anon = User.anonymous
  86. project = Project.find(1)
  87. issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
  88. :priority => IssuePriority.all.first, :subject => 'test_create',
  89. :description => 'IssueTest#test_create', :estimated_hours => '1:30')
  90. assert issue.save
  91. activity = TimeEntryActivity.find_by_name('Design')
  92. te = TimeEntry.create(:spent_on => '2010-01-01',
  93. :hours => 100000,
  94. :issue => issue,
  95. :project => project,
  96. :user => anon,
  97. :activity => activity)
  98. assert_equal 1, te.errors.count
  99. end
  100. context "#earilest_date_for_project" do
  101. setup do
  102. User.current = nil
  103. @public_project = Project.generate!(:is_public => true)
  104. @issue = Issue.generate_for_project!(@public_project)
  105. TimeEntry.generate!(:spent_on => '2010-01-01',
  106. :issue => @issue,
  107. :project => @public_project)
  108. end
  109. context "without a project" do
  110. should "return the lowest spent_on value that is visible to the current user" do
  111. assert_equal "2007-03-12", TimeEntry.earilest_date_for_project.to_s
  112. end
  113. end
  114. context "with a project" do
  115. should "return the lowest spent_on value that is visible to the current user for that project and it's subprojects only" do
  116. assert_equal "2010-01-01", TimeEntry.earilest_date_for_project(@public_project).to_s
  117. end
  118. end
  119. end
  120. context "#latest_date_for_project" do
  121. setup do
  122. User.current = nil
  123. @public_project = Project.generate!(:is_public => true)
  124. @issue = Issue.generate_for_project!(@public_project)
  125. TimeEntry.generate!(:spent_on => '2010-01-01',
  126. :issue => @issue,
  127. :project => @public_project)
  128. end
  129. context "without a project" do
  130. should "return the highest spent_on value that is visible to the current user" do
  131. assert_equal "2010-01-01", TimeEntry.latest_date_for_project.to_s
  132. end
  133. end
  134. context "with a project" do
  135. should "return the highest spent_on value that is visible to the current user for that project and it's subprojects only" do
  136. project = Project.find(1)
  137. assert_equal "2007-04-22", TimeEntry.latest_date_for_project(project).to_s
  138. end
  139. end
  140. end
  141. end