default hourly rates specs

git-svn-id: https://dev.finn.de/svn/cockpit/trunk@199 7926756e-e54e-46e6-9721-ed318f58905e
pull/6827/head
rkh 15 years ago
parent aafd46759b
commit 7d4760d9aa
  1. 13
      app/models/default_hourly_rate.rb
  2. 5
      app/models/default_hourly_rate_observer.rb
  3. 24
      spec/fixtures/rates.yml
  4. 17
      spec/fixtures/time_entries.yml
  5. 64
      spec/models/time_entry_spec.rb

@ -6,9 +6,12 @@ class DefaultHourlyRate < Rate
def validate
# Only allow change of user on first creation
return if self.new_record?
errors.add :user_id, :activerecord_error_invalid if user_id_changed?
errors.add :user_id, :activerecord_error_invalid if !self.new_record? and user_id_changed?
begin
valid_from.to_date
rescue Exception
errors.add :valid_from, :activerecord_error_invalid
end
end
def next(reference_date = self.valid_from)
@ -23,4 +26,8 @@ class DefaultHourlyRate < Rate
def previous(reference_date = self.valid_from)
self.user.default_rate_at(reference_date - 1)
end
def before_save
self.valid_from &&= valid_from.to_date
end
end

@ -36,7 +36,7 @@ class DefaultHourlyRateObserver < ActiveRecord::Observer
# we have two dates, query between
conditions = [
"user_id = ? AND (rate_id IN (?) OR rate_id IS NULL) AND spent_on BETWEEN ? AND ?",
@rate.user_id, default_rates, date1, date2
@rate.user_id, default_rates, date1, date2 - 1
]
end
@ -61,9 +61,6 @@ class DefaultHourlyRateObserver < ActiveRecord::Observer
# and entries from all projects that need updating
entries = o.orphaned_child_entries(rate.valid_from, (next_rate.valid_from if next_rate))
puts "UPDATING TIME ENTRIES !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
o.update_entries(entries)
end

@ -42,3 +42,27 @@ hourly_five:
user_id: 1
project_id: 1
type: HourlyRate
default_hourly_one:
id: 7
valid_from: <%= 2.days.ago.to_date.to_s :db %>
rate: 42.0
user_id: 2
project_id: 1
type: DefaultHourlyRate
default_hourly_three:
id: 8
valid_from: <%= 4.days.ago.to_date.to_s :db %>
rate: 42.0
user_id: 2
project_id: 1
type: DefaultHourlyRate
default_hourly_five:
id: 9
valid_from: <%= 6.days.ago.to_date.to_s :db %>
rate: 42.0
user_id: 2
project_id: 1
type: DefaultHourlyRate

@ -15,3 +15,20 @@ example:
tyear: 2007
rate_id: 4
costs: 0.0
default_example:
created_on: 2007-03-23 12:54:18 +01:00
tweek: 12
tmonth: 3
project_id: 1
comments: My hours
updated_on: 2007-03-23 12:54:18 +01:00
activity_id: 9
spent_on: <%= Time.now %>
issue_id: 1
id: 2
hours: 1.0
user_id: 2
tyear: 2007
rate_id: 7
costs: 0.0

@ -5,6 +5,7 @@ describe TimeEntry do
before(:each) do
User.current = users("admin")
@example = time_entries "example"
@default_example = time_entries "default_example"
end
fixtures :users
@ -24,6 +25,8 @@ describe TimeEntry do
@example.save!
end
describe "given rate" do
it "should return the current costs depending on the number of hours" do
(0..100).each do |hours|
@example.hours = hours
@ -82,3 +85,64 @@ describe TimeEntry do
end
end
describe "default rate" do
it "should return the current costs depending on the number of hours" do
(0..100).each do |hours|
@default_example.hours = hours
@default_example.save!
@default_example.costs.should == @default_example.rate.rate * hours
end
end
it "should update cost if a new rate is added at the end" do
@default_example.user = users("john")
@default_example.spent_on = Time.now.to_date
@default_example.hours = 1
@default_example.save!
@default_example.costs.should == rates("default_hourly_one").rate
hourly = DefaultHourlyRate.create! :valid_from => 1.day.ago.to_date, :rate => 1.0, :user => users("john")
@default_example.reload
@default_example.rate.should_not == rates("default_hourly_one")
@default_example.costs.should == hourly.rate
end
it "should update cost if a new rate is added in between" do
@default_example.user = users("john")
@default_example.spent_on = 3.days.ago.to_date
@default_example.hours = 1
@default_example.save!
@default_example.costs.should == rates("default_hourly_three").rate
hourly = DefaultHourlyRate.create! :valid_from => 3.days.ago.to_date, :rate => 1.0, :user => users("john")
@default_example.reload
@default_example.rate.should_not == rates("default_hourly_three")
@default_example.costs.should == hourly.rate
end
it "should update cost if a spent_on changes" do
@default_example.hours = 1
(5.days.ago..Time.now).step(1.day) do |time|
@default_example.spent_on = time.to_date
@default_example.save!
@default_example.costs.should == @default_example.user.rate_at(time, 1).rate
end
end
it "should update cost if a rate is removed" do
default_hourly_one = rates("default_hourly_one")
@default_example.spent_on = default_hourly_one.valid_from
@default_example.hours = 1
@default_example.save!
@default_example.costs.should == default_hourly_one.rate
default_hourly_one.destroy
@default_example.reload
@default_example.costs.should == rates("default_hourly_three").rate
rates("default_hourly_three").destroy
@default_example.reload
@default_example.costs.should == rates("default_hourly_five").rate
end
end
end
Loading…
Cancel
Save