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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2014 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,
  21. :trackers, :issue_statuses,
  22. :projects_trackers,
  23. :journals, :journal_details,
  24. :issue_categories, :enumerations,
  25. :groups_users,
  26. :enabled_modules
  27. def test_hours_format
  28. assertions = { "2" => 2.0,
  29. "21.1" => 21.1,
  30. "2,1" => 2.1,
  31. "1,5h" => 1.5,
  32. "7:12" => 7.2,
  33. "10h" => 10.0,
  34. "10 h" => 10.0,
  35. "45m" => 0.75,
  36. "45 m" => 0.75,
  37. "3h15" => 3.25,
  38. "3h 15" => 3.25,
  39. "3 h 15" => 3.25,
  40. "3 h 15m" => 3.25,
  41. "3 h 15 m" => 3.25,
  42. "3 hours" => 3.0,
  43. "12min" => 0.2,
  44. "12 Min" => 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. def test_spent_on_with_2_digits_year_should_not_be_valid
  101. entry = TimeEntry.new(:project => Project.find(1), :user => User.find(1), :activity => TimeEntryActivity.first, :hours => 1)
  102. entry.spent_on = "09-02-04"
  103. assert !entry.valid?
  104. assert_include I18n.translate('activerecord.errors.messages.not_a_date'), entry.errors[:spent_on]
  105. end
  106. def test_set_project_if_nil
  107. anon = User.anonymous
  108. project = Project.find(1)
  109. issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
  110. :priority => IssuePriority.all.first, :subject => 'test_create',
  111. :description => 'IssueTest#test_create', :estimated_hours => '1:30')
  112. assert issue.save
  113. activity = TimeEntryActivity.find_by_name('Design')
  114. te = TimeEntry.create(:spent_on => '2010-01-01',
  115. :hours => 10,
  116. :issue => issue,
  117. :user => anon,
  118. :activity => activity)
  119. assert_equal project.id, te.project.id
  120. end
  121. end