rewrite cost_entry_spec to no longer use fixtures

pull/6827/head
Jens Ulferts 13 years ago
parent b69d6dca86
commit 6797b78099
  1. 249
      spec/models/cost_entry_spec.rb

@ -2,111 +2,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe CostEntry do
before(:each) do
User.current = users("admin")
@example = cost_entries "example"
Factory.create(:member, :project => @example.project,
:principal => @example.user,
:roles => [Factory.create(:role)])
end
fixtures :users
fixtures :cost_types
fixtures :cost_entries
fixtures :rates
fixtures :projects
fixtures :issues
fixtures :trackers
fixtures :enumerations
fixtures :enabled_modules
fixtures :issue_statuses
it "should always prefer overridden_costs" do
value = rand(500)
@example.overridden_costs = value
@example.overridden_costs.should == value
@example.real_costs.should == value
@example.save!
end
it "should return the current costs depending on the number of units" do
(0..100).each do |units|
@example.units = units
@example.save!
@example.costs.should == @example.cost_type.rate_at(@example.spent_on).rate * units
end
end
it "should update cost if a new rate is added at the end" do
@example.cost_type = cost_types("umbrella")
@example.spent_on = Time.now
@example.units = 1
@example.save!
@example.costs.should == rates("cheap_one").rate
(cheap = CostRate.new.tap do |cr|
cr.valid_from = 1.day.ago
cr.rate = 1.0
cr.cost_type = cost_types("umbrella")
end).save!
@example.reload
@example.rate.should_not == rates("cheap_one")
@example.costs.should == cheap.rate
end
it "should update cost if a new rate is added in between" do
@example.cost_type = cost_types("umbrella")
@example.spent_on = 3.days.ago
@example.units = 1
@example.save!
@example.costs.should == rates("cheap_three").rate
(cheap = CostRate.new.tap do |cr|
cr.valid_from = 3.days.ago.to_date
cr.rate = 1.0
cr.cost_type = cost_types("umbrella")
end).save!
@example.reload
@example.rate.should_not == rates("cheap_three")
@example.costs.should == cheap.rate
end
it "should update cost if a spent_on changes" do
@example.units = 1
(5.days.ago..Time.now).step(1.day) do |time|
@example.spent_on = time
@example.save!
@example.costs.should == @example.cost_type.rate_at(time).rate
end
end
it "should update cost if a rate is removed" do
cheap_one = rates("cheap_one")
@example.spent_on = rates("cheap_one").valid_from
@example.units = 1
@example.save!
@example.costs.should == cheap_one.rate
cheap_one.destroy
@example.reload
@example.costs.should == rates("cheap_three").rate
rates("cheap_three").destroy
@example.reload
@example.costs.should == rates("cheap_five").rate
end
it "should be able to change order of rates (sorted by valid_from)" do
cheap_one = rates("cheap_one")
cheap_three = rates("cheap_three")
@example.spent_on = cheap_one.valid_from
@example.save!
@example.rate.should == cheap_one
cheap_one.valid_from = cheap_three.valid_from - 1.day
cheap_one.save!
@example.reload
@example.rate.should == cheap_three
end
describe "fixtures free" do
# TODO: rewrite fixture dependent tests towards using factories
let(:project) { Factory.create(:project_with_trackers) }
let(:project2) { Factory.create(:project_with_trackers) }
let(:issue) { Factory.create(:issue, :project => project,
@ -118,14 +13,32 @@ describe CostEntry do
let(:user) { Factory.create(:user) }
let(:user2) { Factory.create(:user) }
let(:klass) { CostEntry }
let(:cost_entry) { Factory.build(:cost_entry, :cost_type => cost_type,
let(:cost_entry) do
member
Factory.build(:cost_entry, :cost_type => cost_type,
:project => project,
:issue => issue,
:spent_on => date,
:units => units,
:user => user,
:comments => "lorem") }
let(:cost_type) { Factory.create(:cost_type) }
:comments => "lorem")
end
let(:cost_type) do
cost_type = Factory.create(:cost_type)
[first_rate, second_rate, third_rate].each do |rate|
rate.cost_type = cost_type
rate.save!
end
cost_type.reload
cost_type
end
let(:first_rate) { Factory.build(:cost_rate, :valid_from => 6.days.ago,
:rate => 10.0) }
let(:second_rate) { Factory.build(:cost_rate, :valid_from => 4.days.ago,
:rate => 100.0) }
let(:third_rate) { Factory.build(:cost_rate, :valid_from => 2.days.ago,
:rate => 1000.0) }
let(:member) { Factory.create(:member, :project => project,
:roles => [role],
:principal => user) }
@ -133,17 +46,120 @@ describe CostEntry do
let(:units) { 5.0 }
let(:date) { Date.today }
describe "instance" do
describe :costs do
let(:fourth_rate) { Factory.build(:cost_rate, :valid_from => 1.days.ago,
:rate => 10000.0,
:cost_type => cost_type) }
describe "WHEN updating the number of units" do
before do
CostType.delete_all
User.delete_all
Project.delete_all
Issue.delete_all
cost_entry.spent_on = first_rate.valid_from + 1.day
end
describe "instance" do
describe "valid" do
it "should update costs" do
(0..5).each do |units|
cost_entry.units = units
cost_entry.save!
cost_entry.costs.should == first_rate.rate * units
end
end
end
describe "WHEN a new rate is added at the end" do
before do
cost_entry.save!
fourth_rate.save!
cost_entry.reload
end
it { cost_entry.costs.should == fourth_rate.rate * cost_entry.units }
end
describe "WHEN a new rate is added for the future" do
before do
cost_entry.save!
fourth_rate.valid_from = 1.day.from_now
fourth_rate.save!
cost_entry.reload
end
it { cost_entry.costs.should == third_rate.rate * cost_entry.units }
end
describe "WHEN a new rate is added in between" do
before do
cost_entry.save!
fourth_rate.valid_from = 3.days.ago
fourth_rate.save!
cost_entry.reload
end
it { cost_entry.costs.should == third_rate.rate * cost_entry.units }
end
describe "WHEN a rate is destroyed" do
before do
cost_entry.save!
third_rate.destroy
cost_entry.reload
end
it { cost_entry.costs.should == cost_entry.units * second_rate.rate }
end
describe "WHEN a rate's valid from is updated" do
before do
cost_entry.save!
first_rate.update_attribute(:valid_from, 1.days.ago)
cost_entry.reload
end
it { cost_entry.costs.should == cost_entry.units * first_rate.rate }
end
describe "WHEN spent on is changed" do
before do
cost_type.save!
cost_entry.save!
end
it "should take the then active rate to calculate" do
(5.days.ago..Time.now).step(1.day) do |time|
cost_entry.spent_on = time
cost_entry.save!
cost_entry.costs.should == cost_entry.units * CostRate.first(:conditions => ["cost_type_id = ? AND valid_from <= ?", cost_entry.cost_type.id, cost_entry.spent_on], :order => "valid_from DESC").rate
end
end
end
end
describe :overridden_costs do
describe "WHEN overridden costs are seet" do
let(:value) { rand(500) }
before do
cost_entry.overridden_costs = value
end
it { cost_entry.overridden_costs.should == value }
end
end
describe :real_costs do
describe "WHEN overrridden cost are set" do
let(:value) { rand(500) }
before do
cost_entry.overridden_costs = value
end
it { cost_entry.real_costs.should == value }
end
end
describe :valid do
before do
member.save!
cost_entry.save!
end
@ -217,6 +233,9 @@ describe CostEntry do
before { cost_type.deleted_at = Date.new }
it { cost_entry.should_not be_valid }
end
end
describe :user do
describe "WHEN a non existing user is provided (i.e. the user has been deleted)" do
before do

Loading…
Cancel
Save