Increase Budget Result spec to check incompatible investments can't be winners

Why:

* A incompatible investment can't be chosen as a winner
* When a winner investment is marked as incompatible, winnersmust be recalculated and it can't be a winner

How:

* Increasing existing scenarios to include a incompatible investment
* Adding a new scenario where a winner investment gets flagged as incompatible
This commit is contained in:
Bertocq
2017-07-03 15:43:37 +02:00
parent e8c6c6ad8c
commit 21864f3fb5

View File

@@ -7,29 +7,48 @@ describe Budget::Result do
let(:group) { create(:budget_group, budget: budget) }
let(:heading) { create(:budget_heading, group: group, price: 1000) }
it "calculates a budget's winner investments" do
investment1 = create(:budget_investment, :selected, heading: heading, price: 200, ballot_lines_count: 900)
investment2 = create(:budget_investment, :selected, heading: heading, price: 300, ballot_lines_count: 800)
investment3 = create(:budget_investment, :selected, heading: heading, price: 500, ballot_lines_count: 700)
investment4 = create(:budget_investment, :selected, heading: heading, price: 100, ballot_lines_count: 600)
context "When there is no winners" do
it "calculates the correct winner set" do
investment1 = create(:budget_investment, :selected, heading: heading, price: 200, ballot_lines_count: 900, winner: false)
investment2 = create(:budget_investment, :selected, heading: heading, price: 300, ballot_lines_count: 800, winner: false)
investment3 = create(:budget_investment, :incompatible, heading: heading, price: 500, ballot_lines_count: 700, winner: false)
investment4 = create(:budget_investment, :selected, heading: heading, price: 500, ballot_lines_count: 600, winner: false)
investment5 = create(:budget_investment, :selected, heading: heading, price: 100, ballot_lines_count: 500, winner: false)
result = Budget::Result.new(budget, heading)
result.calculate_winners
Budget::Result.new(budget, heading).calculate_winners
expect(result.winners).to eq([investment1, investment2, investment3])
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id])
end
end
it "resets winners before recalculating" do
investment1 = create(:budget_investment, :selected, heading: heading, price: 200, ballot_lines_count: 900, winner: true)
investment2 = create(:budget_investment, :selected, heading: heading, price: 300, ballot_lines_count: 800, winner: true)
investment3 = create(:budget_investment, :selected, heading: heading, price: 500, ballot_lines_count: 700, winner: true)
investment4 = create(:budget_investment, :selected, heading: heading, price: 100, ballot_lines_count: 600, winner: true)
context "When there are winners" do
it "removes winners and recalculates" do
investment1 = create(:budget_investment, :winner, heading: heading, price: 200, ballot_lines_count: 900)
investment2 = create(:budget_investment, :winner, heading: heading, price: 300, ballot_lines_count: 800)
investment3 = create(:budget_investment, :incompatible, heading: heading, price: 500, ballot_lines_count: 700, winner: true)
investment4 = create(:budget_investment, :winner, heading: heading, price: 500, ballot_lines_count: 600)
investment5 = create(:budget_investment, :winner, heading: heading, price: 100, ballot_lines_count: 500)
result = Budget::Result.new(budget, heading)
result.calculate_winners
Budget::Result.new(budget, heading).calculate_winners
expect(result.winners).to eq([investment1, investment2, investment3])
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id])
end
end
context "When a winner is flagged as incompatible" do
it "recalculates winners leaving it out" do
investment1 = create(:budget_investment, :winner, heading: heading, price: 200, ballot_lines_count: 900)
investment2 = create(:budget_investment, :winner, heading: heading, price: 300, ballot_lines_count: 800)
investment3 = create(:budget_investment, :winner, heading: heading, price: 500, ballot_lines_count: 700)
investment4 = create(:budget_investment, :winner, heading: heading, price: 500, ballot_lines_count: 600)
investment5 = create(:budget_investment, :winner, heading: heading, price: 100, ballot_lines_count: 500)
investment3.incompatible = true
investment3.save
expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id])
end
end
end
end
end