##// END OF EJS Templates
add unit test to set project if project is nil at unit time entry test...
Toshi MARUYAMA -
r7332:5778c264349e
parent child
Show More
@@ -1,160 +1,176
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class TimeEntryTest < ActiveSupport::TestCase
21 21 fixtures :issues, :projects, :users, :time_entries,
22 22 :members, :roles, :member_roles, :auth_sources,
23 23 :trackers, :issue_statuses,
24 24 :projects_trackers,
25 25 :journals, :journal_details,
26 26 :issue_categories, :enumerations,
27 27 :groups_users,
28 28 :enabled_modules,
29 29 :workflows
30 30
31 31 def test_hours_format
32 32 assertions = { "2" => 2.0,
33 33 "21.1" => 21.1,
34 34 "2,1" => 2.1,
35 35 "1,5h" => 1.5,
36 36 "7:12" => 7.2,
37 37 "10h" => 10.0,
38 38 "10 h" => 10.0,
39 39 "45m" => 0.75,
40 40 "45 m" => 0.75,
41 41 "3h15" => 3.25,
42 42 "3h 15" => 3.25,
43 43 "3 h 15" => 3.25,
44 44 "3 h 15m" => 3.25,
45 45 "3 h 15 m" => 3.25,
46 46 "3 hours" => 3.0,
47 47 "12min" => 0.2,
48 48 }
49 49
50 50 assertions.each do |k, v|
51 51 t = TimeEntry.new(:hours => k)
52 52 assert_equal v, t.hours, "Converting #{k} failed:"
53 53 end
54 54 end
55 55
56 56 def test_hours_should_default_to_nil
57 57 assert_nil TimeEntry.new.hours
58 58 end
59 59
60 60 def test_spent_on_with_blank
61 61 c = TimeEntry.new
62 62 c.spent_on = ''
63 63 assert_nil c.spent_on
64 64 end
65 65
66 66 def test_spent_on_with_nil
67 67 c = TimeEntry.new
68 68 c.spent_on = nil
69 69 assert_nil c.spent_on
70 70 end
71 71
72 72 def test_spent_on_with_string
73 73 c = TimeEntry.new
74 74 c.spent_on = "2011-01-14"
75 75 assert_equal Date.parse("2011-01-14"), c.spent_on
76 76 end
77 77
78 78 def test_spent_on_with_invalid_string
79 79 c = TimeEntry.new
80 80 c.spent_on = "foo"
81 81 assert_nil c.spent_on
82 82 end
83 83
84 84 def test_spent_on_with_date
85 85 c = TimeEntry.new
86 86 c.spent_on = Date.today
87 87 assert_equal Date.today, c.spent_on
88 88 end
89 89
90 90 def test_spent_on_with_time
91 91 c = TimeEntry.new
92 92 c.spent_on = Time.now
93 93 assert_equal Date.today, c.spent_on
94 94 end
95 95
96 96 def test_validate_time_entry
97 97 anon = User.anonymous
98 98 project = Project.find(1)
99 99 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
100 100 :priority => IssuePriority.all.first, :subject => 'test_create',
101 101 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
102 102 assert issue.save
103 103 activity = TimeEntryActivity.find_by_name('Design')
104 104 te = TimeEntry.create(:spent_on => '2010-01-01',
105 105 :hours => 100000,
106 106 :issue => issue,
107 107 :project => project,
108 108 :user => anon,
109 109 :activity => activity)
110 110 assert_equal 1, te.errors.count
111 111 end
112 112
113 def test_set_project_if_nil
114 anon = User.anonymous
115 project = Project.find(1)
116 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
117 :priority => IssuePriority.all.first, :subject => 'test_create',
118 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
119 assert issue.save
120 activity = TimeEntryActivity.find_by_name('Design')
121 te = TimeEntry.create(:spent_on => '2010-01-01',
122 :hours => 10,
123 :issue => issue,
124 :user => anon,
125 :activity => activity)
126 assert_equal project.id, te.project.id
127 end
128
113 129 context "#earilest_date_for_project" do
114 130 setup do
115 131 User.current = nil
116 132 @public_project = Project.generate!(:is_public => true)
117 133 @issue = Issue.generate_for_project!(@public_project)
118 134 TimeEntry.generate!(:spent_on => '2010-01-01',
119 135 :issue => @issue,
120 136 :project => @public_project)
121 137 end
122 138
123 139 context "without a project" do
124 140 should "return the lowest spent_on value that is visible to the current user" do
125 141 assert_equal "2007-03-12", TimeEntry.earilest_date_for_project.to_s
126 142 end
127 143 end
128 144
129 145 context "with a project" do
130 146 should "return the lowest spent_on value that is visible to the current user for that project and it's subprojects only" do
131 147 assert_equal "2010-01-01", TimeEntry.earilest_date_for_project(@public_project).to_s
132 148 end
133 149 end
134 150
135 151 end
136 152
137 153 context "#latest_date_for_project" do
138 154 setup do
139 155 User.current = nil
140 156 @public_project = Project.generate!(:is_public => true)
141 157 @issue = Issue.generate_for_project!(@public_project)
142 158 TimeEntry.generate!(:spent_on => '2010-01-01',
143 159 :issue => @issue,
144 160 :project => @public_project)
145 161 end
146 162
147 163 context "without a project" do
148 164 should "return the highest spent_on value that is visible to the current user" do
149 165 assert_equal "2010-01-01", TimeEntry.latest_date_for_project.to_s
150 166 end
151 167 end
152 168
153 169 context "with a project" do
154 170 should "return the highest spent_on value that is visible to the current user for that project and it's subprojects only" do
155 171 project = Project.find(1)
156 172 assert_equal "2007-04-22", TimeEntry.latest_date_for_project(project).to_s
157 173 end
158 174 end
159 175 end
160 176 end
General Comments 0
You need to be logged in to leave comments. Login now