|
|
|
@ -2,221 +2,240 @@ 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)]) |
|
|
|
|
let(:project) { Factory.create(:project_with_trackers) } |
|
|
|
|
let(:project2) { Factory.create(:project_with_trackers) } |
|
|
|
|
let(:issue) { Factory.create(:issue, :project => project, |
|
|
|
|
:tracker => project.trackers.first, |
|
|
|
|
:author => user) } |
|
|
|
|
let(:issue2) { Factory.create(:issue, :project => project2, |
|
|
|
|
:tracker => project2.trackers.first, |
|
|
|
|
:author => user) } |
|
|
|
|
let(:user) { Factory.create(:user) } |
|
|
|
|
let(:user2) { Factory.create(:user) } |
|
|
|
|
let(:klass) { CostEntry } |
|
|
|
|
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") |
|
|
|
|
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 |
|
|
|
|
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) } |
|
|
|
|
let(:role) { Factory.create(:role, :permissions => []) } |
|
|
|
|
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 |
|
|
|
|
cost_entry.spent_on = first_rate.valid_from + 1.day |
|
|
|
|
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 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 |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
describe "WHEN a new rate is added at the end" do |
|
|
|
|
before do |
|
|
|
|
cost_entry.save! |
|
|
|
|
fourth_rate.save! |
|
|
|
|
cost_entry.reload |
|
|
|
|
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 { cost_entry.costs.should == fourth_rate.rate * cost_entry.units } |
|
|
|
|
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 |
|
|
|
|
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 "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 |
|
|
|
|
it { cost_entry.costs.should == third_rate.rate * cost_entry.units } |
|
|
|
|
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, |
|
|
|
|
:tracker => project.trackers.first, |
|
|
|
|
:author => user) } |
|
|
|
|
let(:issue2) { Factory.create(:issue, :project => project2, |
|
|
|
|
:tracker => project2.trackers.first, |
|
|
|
|
:author => user) } |
|
|
|
|
let(:user) { Factory.create(:user) } |
|
|
|
|
let(:user2) { Factory.create(:user) } |
|
|
|
|
let(:klass) { CostEntry } |
|
|
|
|
let(:cost_entry) { 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) } |
|
|
|
|
let(:member) { Factory.create(:member, :project => project, |
|
|
|
|
:roles => [role], |
|
|
|
|
:principal => user) } |
|
|
|
|
let(:role) { Factory.create(:role, :permissions => []) } |
|
|
|
|
let(:units) { 5.0 } |
|
|
|
|
let(:date) { Date.today } |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
CostType.delete_all |
|
|
|
|
User.delete_all |
|
|
|
|
Project.delete_all |
|
|
|
|
Issue.delete_all |
|
|
|
|
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 "instance" do |
|
|
|
|
describe "valid" do |
|
|
|
|
describe "WHEN a rate is destroyed" do |
|
|
|
|
before do |
|
|
|
|
member.save! |
|
|
|
|
cost_entry.save! |
|
|
|
|
third_rate.destroy |
|
|
|
|
cost_entry.reload |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it{ cost_entry.should be_valid } |
|
|
|
|
it { cost_entry.costs.should == cost_entry.units * second_rate.rate } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN no cost_type is provided" do |
|
|
|
|
before { cost_entry.cost_type = nil } |
|
|
|
|
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.should_not be_valid } |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
describe "WHEN no project is provided" do |
|
|
|
|
before do |
|
|
|
|
cost_entry.project = nil |
|
|
|
|
# unfortunately the project get's set to the issue's project if no project is provided |
|
|
|
|
# TODO: check if that is necessary |
|
|
|
|
cost_entry.issue = nil |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN no issue is provided" do |
|
|
|
|
before { cost_entry.issue = nil } |
|
|
|
|
describe :overridden_costs do |
|
|
|
|
describe "WHEN overridden costs are seet" do |
|
|
|
|
let(:value) { rand(500) } |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
before do |
|
|
|
|
cost_entry.overridden_costs = value |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN the issue is not in the project" do |
|
|
|
|
before { cost_entry.issue = issue2 } |
|
|
|
|
it { cost_entry.overridden_costs.should == value } |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
describe :real_costs do |
|
|
|
|
describe "WHEN overrridden cost are set" do |
|
|
|
|
let(:value) { rand(500) } |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
cost_entry.overridden_costs = value |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN no units are provided" do |
|
|
|
|
before { cost_entry.units = nil } |
|
|
|
|
it { cost_entry.real_costs.should == value } |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
describe :valid do |
|
|
|
|
before do |
|
|
|
|
cost_entry.save! |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN no spent_on is provided" do |
|
|
|
|
before { cost_entry.spent_on = nil } |
|
|
|
|
it{ cost_entry.should be_valid } |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
describe "WHEN no cost_type is provided" do |
|
|
|
|
before { cost_entry.cost_type = nil } |
|
|
|
|
|
|
|
|
|
describe "WHEN no user is provided" do |
|
|
|
|
before { cost_entry.user = nil } |
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
describe "WHEN no project is provided" do |
|
|
|
|
before do |
|
|
|
|
cost_entry.project = nil |
|
|
|
|
# unfortunately the project get's set to the issue's project if no project is provided |
|
|
|
|
# TODO: check if that is necessary |
|
|
|
|
cost_entry.issue = nil |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN the provided user is no member of the project |
|
|
|
|
WHEN the user is unchanged" do |
|
|
|
|
before { member.destroy } |
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it { cost_entry.should be_valid } |
|
|
|
|
end |
|
|
|
|
describe "WHEN no issue is provided" do |
|
|
|
|
before { cost_entry.issue = nil } |
|
|
|
|
|
|
|
|
|
describe "WHEN the provided user is no member of the project |
|
|
|
|
WHEN the user changes" do |
|
|
|
|
before do |
|
|
|
|
cost_entry.user = user2 |
|
|
|
|
member.destroy |
|
|
|
|
end |
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN the issue is not in the project" do |
|
|
|
|
before { cost_entry.issue = issue2 } |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
describe "WHEN no units are provided" do |
|
|
|
|
before { cost_entry.units = nil } |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN no spent_on is provided" do |
|
|
|
|
before { cost_entry.spent_on = nil } |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN no user is provided" do |
|
|
|
|
before { cost_entry.user = nil } |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN the provided user is no member of the project |
|
|
|
|
WHEN the user is unchanged" do |
|
|
|
|
before { member.destroy } |
|
|
|
|
|
|
|
|
|
it { cost_entry.should be_valid } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN the provided user is no member of the project |
|
|
|
|
WHEN the user changes" do |
|
|
|
|
before do |
|
|
|
|
cost_entry.user = user2 |
|
|
|
|
member.destroy |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN the cost_type is deleted" do |
|
|
|
|
before { cost_type.deleted_at = Date.new } |
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "WHEN the cost_type is deleted" do |
|
|
|
|
before { cost_type.deleted_at = Date.new } |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it { cost_entry.should_not be_valid } |
|
|
|
|
describe :user do |
|
|
|
|
describe "WHEN a non existing user is provided (i.e. the user has been deleted)" do |
|
|
|
|
before do |
|
|
|
|