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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # redMine - project management software
  2. # Copyright (C) 2006-2008 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. def test_hours_format
  21. assertions = { "2" => 2.0,
  22. "21.1" => 21.1,
  23. "2,1" => 2.1,
  24. "1,5h" => 1.5,
  25. "7:12" => 7.2,
  26. "10h" => 10.0,
  27. "10 h" => 10.0,
  28. "45m" => 0.75,
  29. "45 m" => 0.75,
  30. "3h15" => 3.25,
  31. "3h 15" => 3.25,
  32. "3 h 15" => 3.25,
  33. "3 h 15m" => 3.25,
  34. "3 h 15 m" => 3.25,
  35. "3 hours" => 3.0,
  36. "12min" => 0.2,
  37. }
  38. assertions.each do |k, v|
  39. t = TimeEntry.new(:hours => k)
  40. assert_equal v, t.hours, "Converting #{k} failed:"
  41. end
  42. end
  43. def test_hours_should_default_to_nil
  44. assert_nil TimeEntry.new.hours
  45. end
  46. def test_spent_on_with_blank
  47. c = TimeEntry.new
  48. c.spent_on = ''
  49. assert_nil c.spent_on
  50. end
  51. def test_spent_on_with_nil
  52. c = TimeEntry.new
  53. c.spent_on = nil
  54. assert_nil c.spent_on
  55. end
  56. def test_spent_on_with_string
  57. c = TimeEntry.new
  58. c.spent_on = "2011-01-14"
  59. assert_equal Date.parse("2011-01-14"), c.spent_on
  60. end
  61. def test_spent_on_with_invalid_string
  62. c = TimeEntry.new
  63. c.spent_on = "foo"
  64. assert_nil c.spent_on
  65. end
  66. def test_spent_on_with_date
  67. c = TimeEntry.new
  68. c.spent_on = Date.today
  69. assert_equal Date.today, c.spent_on
  70. end
  71. def test_spent_on_with_time
  72. c = TimeEntry.new
  73. c.spent_on = Time.now
  74. assert_equal Date.today, c.spent_on
  75. end
  76. context "#earilest_date_for_project" do
  77. setup do
  78. User.current = nil
  79. @public_project = Project.generate!(:is_public => true)
  80. @issue = Issue.generate_for_project!(@public_project)
  81. TimeEntry.generate!(:spent_on => '2010-01-01',
  82. :issue => @issue,
  83. :project => @public_project)
  84. end
  85. context "without a project" do
  86. should "return the lowest spent_on value that is visible to the current user" do
  87. assert_equal "2007-03-12", TimeEntry.earilest_date_for_project.to_s
  88. end
  89. end
  90. context "with a project" do
  91. should "return the lowest spent_on value that is visible to the current user for that project and it's subprojects only" do
  92. assert_equal "2010-01-01", TimeEntry.earilest_date_for_project(@public_project).to_s
  93. end
  94. end
  95. end
  96. context "#latest_date_for_project" do
  97. setup do
  98. User.current = nil
  99. @public_project = Project.generate!(:is_public => true)
  100. @issue = Issue.generate_for_project!(@public_project)
  101. TimeEntry.generate!(:spent_on => '2010-01-01',
  102. :issue => @issue,
  103. :project => @public_project)
  104. end
  105. context "without a project" do
  106. should "return the highest spent_on value that is visible to the current user" do
  107. assert_equal "2010-01-01", TimeEntry.latest_date_for_project.to_s
  108. end
  109. end
  110. context "with a project" do
  111. should "return the highest spent_on value that is visible to the current user for that project and it's subprojects only" do
  112. project = Project.find(1)
  113. assert_equal "2007-04-22", TimeEntry.latest_date_for_project(project).to_s
  114. end
  115. end
  116. end
  117. end